-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility for splitting version string into integer parts
- Loading branch information
Showing
2 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "doctest.h" | ||
#include "util/version_tools.hh" | ||
|
||
TEST_CASE("parse") { | ||
{ | ||
auto vers = VersionUtil::parse_version("1.2.16"); | ||
CHECK(vers.major == 1); | ||
CHECK(vers.minor == 2); | ||
CHECK(vers.revision == 16); | ||
} | ||
|
||
{ | ||
auto vers = VersionUtil::parse_version("9.1"); | ||
CHECK(vers.major == 9); | ||
CHECK(vers.minor == 1); | ||
CHECK(vers.revision == 0); | ||
} | ||
|
||
{ | ||
auto vers = VersionUtil::parse_version("2.0.99"); | ||
CHECK(vers.major == 2); | ||
CHECK(vers.minor == 0); | ||
CHECK(vers.revision == 99); | ||
} | ||
|
||
{ | ||
auto vers = VersionUtil::parse_version("2099"); | ||
CHECK(vers.major == 2099); | ||
CHECK(vers.minor == 0); | ||
CHECK(vers.revision == 0); | ||
} | ||
|
||
{ | ||
auto vers = VersionUtil::parse_version("garbage"); | ||
CHECK(vers.major == 0); | ||
CHECK(vers.minor == 0); | ||
CHECK(vers.revision == 0); | ||
// try { | ||
// auto vers = VersionUtil::parse_version("garbage"); | ||
// (void)vers; | ||
// CHECK(false); | ||
// } catch (std::exception) { | ||
// CHECK(true); | ||
// } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#pragma once | ||
#include <ranges> | ||
#include <string_view> | ||
|
||
namespace VersionUtil | ||
{ | ||
|
||
struct Version { | ||
unsigned major; | ||
unsigned minor; | ||
unsigned revision; | ||
|
||
void set_field(unsigned index, unsigned val) { | ||
if (index == 0) | ||
major = val; | ||
if (index == 1) | ||
minor = val; | ||
if (index == 2) | ||
revision = val; | ||
} | ||
}; | ||
|
||
auto split(std::string_view str, std::string_view delim) { | ||
auto tokens = str | std::ranges::views::split(delim) | std::ranges::views::transform([](auto &&str) { | ||
return std::string_view(&*str.begin(), std::ranges::distance(str)); | ||
}); | ||
return tokens; | ||
} | ||
|
||
Version parse_version(std::string_view vers) { | ||
auto tokens = split(vers, "."); | ||
Version version{}; | ||
|
||
for (unsigned i = 0; auto &&token : tokens) { | ||
auto num = strtoul(token.data(), nullptr, 10); | ||
|
||
version.set_field(i, num); | ||
|
||
if (++i >= 3) | ||
break; | ||
} | ||
return version; | ||
} | ||
|
||
} // namespace VersionUtil |