From: Joel Rosdahl Date: Fri, 14 Jul 2023 06:14:09 +0000 (+0200) Subject: refactor: Move Util::parse_duration to util X-Git-Tag: v4.9~129 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f04ad2ea247bbefcdb4758016b66ffe2293ecab7;p=thirdparty%2Fccache.git refactor: Move Util::parse_duration to util --- diff --git a/src/Util.cpp b/src/Util.cpp index 5ad6b7f00..c69f42e38 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -687,33 +687,6 @@ normalize_concrete_absolute_path(const std::string& path) : path; } -uint64_t -parse_duration(std::string_view duration) -{ - uint64_t factor = 0; - char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1]; - - switch (last_ch) { - case 'd': - factor = 24 * 60 * 60; - break; - case 's': - factor = 1; - break; - default: - throw core::Error(FMT( - "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration)); - } - - const auto value = - util::parse_unsigned(duration.substr(0, duration.length() - 1)); - if (value) { - return factor * *value; - } else { - throw core::Error(value.error()); - } -} - std::string_view remove_extension(std::string_view path) { diff --git a/src/Util.hpp b/src/Util.hpp index bf7a0ec9b..e8f3f0c27 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -175,10 +175,6 @@ std::string normalize_abstract_absolute_path(std::string_view path); // normalized result doesn't resolve to the same file system entry as `path`. std::string normalize_concrete_absolute_path(const std::string& path); -// Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix, -// into seconds. Throws `core::Error` on error. -uint64_t parse_duration(std::string_view duration); - // Return a view into `path` containing the given path without the filename // extension as determined by `get_extension()`. std::string_view remove_extension(std::string_view path); diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index 685b34f47..01cf78102 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -597,7 +597,7 @@ process_main_options(int argc, const char* const* argv) } case EVICT_OLDER_THAN: { - evict_max_age = Util::parse_duration(arg); + evict_max_age = util::value_or_throw(util::parse_duration(arg)); break; } diff --git a/src/util/string.cpp b/src/util/string.cpp index d1c98ea01..7f17826d7 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -121,6 +121,31 @@ parse_double(const std::string& value) } } +nonstd::expected +parse_duration(std::string_view duration) +{ + uint64_t factor = 0; + char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1]; + + switch (last_ch) { + case 'd': + factor = 24 * 60 * 60; + break; + case 's': + factor = 1; + break; + default: + return nonstd::make_unexpected(FMT( + "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration)); + } + + auto value = util::parse_unsigned(duration.substr(0, duration.length() - 1)); + if (!value) { + return value; + }; + return factor * *value; +} + nonstd::expected parse_signed(std::string_view value, const std::optional min_value, diff --git a/src/util/string.hpp b/src/util/string.hpp index 300faab85..1739e9a6c 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -83,6 +83,11 @@ join(const T& begin, const T& end, const std::string_view delimiter); // Returns an error string if `value` cannot be parsed as a double. nonstd::expected parse_double(const std::string& value); +// Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix, +// into seconds. +nonstd::expected +parse_duration(std::string_view duration); + // Parse a string into a signed integer. // // Returns an error string if `value` cannot be parsed as an int64_t or if the diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 321b69ed3..01f89076a 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -421,23 +421,6 @@ TEST_CASE("Util::normalize_concrete_absolute_path") #endif } -TEST_CASE("Util::parse_duration") -{ - CHECK(Util::parse_duration("0s") == 0); - CHECK(Util::parse_duration("2s") == 2); - CHECK(Util::parse_duration("1d") == 3600 * 24); - CHECK(Util::parse_duration("2d") == 2 * 3600 * 24); - CHECK_THROWS_WITH( - Util::parse_duration("-2"), - "invalid suffix (supported: d (day) and s (second)): \"-2\""); - CHECK_THROWS_WITH( - Util::parse_duration("2x"), - "invalid suffix (supported: d (day) and s (second)): \"2x\""); - CHECK_THROWS_WITH( - Util::parse_duration("2"), - "invalid suffix (supported: d (day) and s (second)): \"2\""); -} - TEST_CASE("Util::remove_extension") { CHECK(Util::remove_extension("") == ""); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 12e4822fa..70a291c93 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -250,6 +250,20 @@ TEST_CASE("util::parse_double") CHECK(util::parse_double("x").error() == "invalid floating point: \"x\""); } +TEST_CASE("util::parse_duration") +{ + CHECK(*util::parse_duration("0s") == 0); + CHECK(*util::parse_duration("2s") == 2); + CHECK(*util::parse_duration("1d") == 3600 * 24); + CHECK(*util::parse_duration("2d") == 2 * 3600 * 24); + CHECK(util::parse_duration("-2").error() + == "invalid suffix (supported: d (day) and s (second)): \"-2\""); + CHECK(util::parse_duration("2x").error() + == "invalid suffix (supported: d (day) and s (second)): \"2x\""); + CHECK(util::parse_duration("2").error() + == "invalid suffix (supported: d (day) and s (second)): \"2\""); +} + TEST_CASE("util::parse_signed") { CHECK(*util::parse_signed("0") == 0);