diff --git a/include/uzuki2/Version.hpp b/include/uzuki2/Version.hpp index 841185a..55ab9dc 100644 --- a/include/uzuki2/Version.hpp +++ b/include/uzuki2/Version.hpp @@ -1,9 +1,6 @@ #ifndef UZUKI2_VERSIONED_BASE_HPP #define UZUKI2_VERSIONED_BASE_HPP -#include -#include - /** * @file Version.hpp * @brief Version-related definitions. @@ -63,54 +60,6 @@ struct Version { } }; -/** - * @cond - */ -inline Version parse_version_string(const std::string& version_string) { - int major = 0, minor = 0; - size_t i = 0, end = version_string.size(); - - if (version_string.empty()) { - throw std::runtime_error("version string is empty"); - } - if (version_string[i] == '0') { - throw std::runtime_error("invalid version string '" + version_string + "' has leading zeros in its major version"); - } - while (i < end && version_string[i] != '.') { - if (!std::isdigit(version_string[i])) { - throw std::runtime_error("invalid version string '" + version_string + "' contains non-digit characters"); - } - major *= 10; - major += version_string[i] - '0'; - ++i; - } - - if (i == end) { - throw std::runtime_error("version string '" + version_string + "' is missing a minor version"); - } - ++i; // get past the period and check again. - if (i == end) { - throw std::runtime_error("version string '" + version_string + "' is missing a minor version"); - } - - if (version_string[i] == '0' && i + 1 < end) { - throw std::runtime_error("invalid version string '" + version_string + "' has leading zeros in its minor version"); - } - while (i < end) { - if (!std::isdigit(version_string[i])) { - throw std::runtime_error("invalid version string '" + version_string + "' contains non-digit characters"); - } - minor *= 10; - minor += version_string[i] - '0'; - ++i; - } - - return Version(major, minor); -} -/** - * @cond - */ - } #endif diff --git a/include/uzuki2/parse_hdf5.hpp b/include/uzuki2/parse_hdf5.hpp index 200c7b7..6f0e5ff 100644 --- a/include/uzuki2/parse_hdf5.hpp +++ b/include/uzuki2/parse_hdf5.hpp @@ -460,7 +460,9 @@ ParsedList parse(const H5::Group& handle, Externals ext) { Version version; if (handle.attrExists("uzuki_version")) { auto ver_str = ritsuko::hdf5::load_scalar_string_attribute(handle, "uzuki_version"); - version = parse_version_string(ver_str); + auto vraw = ritsuko::parse_version_string(ver_str.c_str(), ver_str.size(), /* skip_patch = */ true); + version.major = vraw.major; + version.minor = vraw.minor; } ExternalTracker etrack(std::move(ext)); diff --git a/include/uzuki2/parse_json.hpp b/include/uzuki2/parse_json.hpp index b380c92..99303ca 100644 --- a/include/uzuki2/parse_json.hpp +++ b/include/uzuki2/parse_json.hpp @@ -434,8 +434,10 @@ ParsedList parse(byteme::Reader& reader, Externals ext, Options options = Option if (vIt->second->type() != millijson::STRING) { throw std::runtime_error("expected a string in 'version'"); } - auto vptr = static_cast(vIt->second.get()); - version = parse_version_string(vptr->value); + const auto& vstr = static_cast(vIt->second.get())->value; + auto vraw = ritsuko::parse_version_string(vstr.c_str(), vstr.size(), /* skip_patch = */ true); + version.major = vraw.major; + version.minor = vraw.minor; } } diff --git a/tests/src/misc.cpp b/tests/src/misc.cpp index 90ee28e..ebdf5d1 100644 --- a/tests/src/misc.cpp +++ b/tests/src/misc.cpp @@ -148,44 +148,3 @@ TEST(JsonFileTest, CheckMethods) { TEST(JsonFileTest, CheckVersion) { expect_json_error("{ version: true }", "expected a string"); } - -TEST(VersionParsing, Basic) { - { - auto v = uzuki2::parse_version_string("1.0"); - EXPECT_EQ(v.major, 1); - EXPECT_EQ(v.minor, 0); - } - - { - auto v = uzuki2::parse_version_string("1.12"); - EXPECT_EQ(v.major, 1); - EXPECT_EQ(v.minor, 12); - } - - { - auto v = uzuki2::parse_version_string("123.456"); - EXPECT_EQ(v.major, 123); - EXPECT_EQ(v.minor, 456); - } -} - -static void expect_version_error(std::string version, std::string msg) { - EXPECT_ANY_THROW({ - try { - uzuki2::parse_version_string(version); - } catch (std::exception& e) { - EXPECT_THAT(e.what(), ::testing::HasSubstr(msg)); - throw; - } - }); -} - -TEST(VersionParsing, Errors) { - expect_version_error("", "empty"); - expect_version_error("0.1", "leading zeros"); - expect_version_error("a.1", "non-digit"); - expect_version_error("1", "minor version"); - expect_version_error("1.", "minor version"); - expect_version_error("1.01", "leading zeros"); - expect_version_error("1.a", "non-digit"); -}