Skip to content

Commit

Permalink
Switch to ritsuko's version parsing utility.
Browse files Browse the repository at this point in the history
  • Loading branch information
LTLA committed Nov 2, 2023
1 parent cc5fae9 commit fe5e6d7
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 95 deletions.
51 changes: 0 additions & 51 deletions include/uzuki2/Version.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#ifndef UZUKI2_VERSIONED_BASE_HPP
#define UZUKI2_VERSIONED_BASE_HPP

#include <string>
#include <cstring>

/**
* @file Version.hpp
* @brief Version-related definitions.
Expand Down Expand Up @@ -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
4 changes: 3 additions & 1 deletion include/uzuki2/parse_hdf5.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
6 changes: 4 additions & 2 deletions include/uzuki2/parse_json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<const millijson::String*>(vIt->second.get());
version = parse_version_string(vptr->value);
const auto& vstr = static_cast<const millijson::String*>(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;
}
}

Expand Down
41 changes: 0 additions & 41 deletions tests/src/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit fe5e6d7

Please sign in to comment.