Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Commit

Permalink
use parse by regex
Browse files Browse the repository at this point in the history
Signed-off-by: Mamoru Sobue <[email protected]>
  • Loading branch information
soblin committed Jun 12, 2024
1 parent 4661d4f commit 6d2f8a9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <lanelet2_io/io_handlers/OsmHandler.h>

#include <memory>
#include <optional>
#include <string>

namespace lanelet::io_handlers
Expand Down Expand Up @@ -58,6 +59,12 @@ class AutowareOsmParser : public OsmParser
static constexpr const char * name() { return "autoware_osm_handler"; }
};

std::optional<uint64_t> parseMajorVersion(const std::string & format_version);

std::optional<uint64_t> parseMinorVersion(const std::string & format_version);

std::optional<uint64_t> parsePatchVersion(const std::string & format_version);

} // namespace lanelet::io_handlers

// NOLINTEND(readability-identifier-naming)
Expand Down
4 changes: 4 additions & 0 deletions tmp/lanelet2_extension/include/lanelet2_extension/version.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,15 @@

namespace lanelet::autoware
{
/*
* @brief denotes the major format_version
*/
enum class Version : int {
none = 0,
v1,
};

// current format_version
static constexpr Version version = Version::v1;
} // namespace lanelet::autoware

Expand Down
58 changes: 58 additions & 0 deletions tmp/lanelet2_extension/lib/autoware_osm_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <lanelet2_io/io_handlers/OsmHandler.h>

#include <memory>
#include <regex>
#include <string>

namespace lanelet::io_handlers
Expand Down Expand Up @@ -75,6 +76,63 @@ void AutowareOsmParser::parseVersions(
}
}

std::optional<uint64_t> parseMajorVersion(const std::string & format_version)
{
std::regex re(R"(^(\d+\.)?(\d+\.)?(\d+)$)");
// NOTE(Mamoru Sobue): matches `1`, `1.10`, `1.10.100`
// `1` ==> [`1`, ``, ``]
// `1.10` ==> [`1.`, ``, `10`]
// `1.10.100` ==> [`1.`, `10.`, `100`]
std::smatch match;
if (!std::regex_match(format_version, match, re)) {
return std::nullopt;
}
std::string major = match[1].str();
major.erase(std::remove(major.begin(), major.end(), '.'), major.end());
return std::stoi(major);
}

std::optional<uint64_t> parseMinorVersion(const std::string & format_version)
{
std::regex re(R"(^(\d+\.)?(\d+\.)?(\d+)$)");
// NOTE(Mamoru Sobue): matches `1`, `1.10`, `1.10.100`
// `1` ==> [`1`, ``, ``]
// `1.10` ==> [`1.`, ``, `10`]
// `1.10.100` ==> [`1.`, `10.`, `100`]
std::smatch match;
if (!std::regex_match(format_version, match, re)) {
return std::nullopt;
}
std::string minor = match[2].str();
if (minor == "") {
if (match[3].str() == "") {
return std::nullopt;
}
minor = match[3].str();
}
minor.erase(std::remove(minor.begin(), minor.end(), '.'), minor.end());
return std::stoi(minor);
}

std::optional<uint64_t> parsePatchVersion(const std::string & format_version)
{
std::regex re(R"(^(\d+\.)?(\d+\.)?(\d+)$)");
// NOTE(Mamoru Sobue): matches `1`, `1.10`, `1.10.100`
// `1` ==> [`1`, ``, ``]
// `1.10` ==> [`1.`, ``, `10`]
// `1.10.100` ==> [`1.`, `10.`, `100`]
std::smatch match;
if (!std::regex_match(format_version, match, re)) {
return std::nullopt;
}
std::string patch = match[3].str();
if (patch == "") {
return std::nullopt;
}
patch.erase(std::remove(patch.begin(), patch.end(), '.'), patch.end());
return std::stoi(patch);
}

} // namespace lanelet::io_handlers

// NOLINTEND(readability-identifier-naming)

0 comments on commit 6d2f8a9

Please sign in to comment.