From: Joel Rosdahl Date: Sun, 26 Jul 2020 19:07:09 +0000 (+0200) Subject: Simplify and clean up parse_duration_with_suffix_to_seconds X-Git-Tag: v4.0~286 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=f93c55832ee3934510ca104cbf1f380dc79585fd;p=thirdparty%2Fccache.git Simplify and clean up parse_duration_with_suffix_to_seconds --- diff --git a/src/Util.cpp b/src/Util.cpp index 8af128cc0..8e36d916d 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -654,38 +654,26 @@ normalize_absolute_path(string_view path) #endif } -unsigned -parse_duration_with_suffix_to_seconds(const std::string& value) +uint32_t +parse_duration(const std::string& duration) { - size_t end; - long result; - bool failed = false; - - try { - result = std::stol(value, &end, 10); - } catch (std::exception&) { - failed = true; - } + unsigned factor = 0; + char last_ch = duration.empty() ? '\0' : duration[duration.length() - 1]; - if (failed || result < 0) { - throw Error(fmt::format("invalid unsigned integer: \"{}\"", value)); - } - - if (end + 1 != value.size()) { - throw Error( - fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); - } - - switch (value[end]) { + switch (last_ch) { case 'd': - result *= 24 * 3600; + factor = 24 * 60 * 60; + break; case 's': + factor = 1; break; default: - throw Error( - fmt::format("Invalid suffix, Supported: d(ay)/s(econd): \"{}\"", value)); + throw Error(fmt::format( + "invalid suffix (supported: d (day) and s (second)): \"{}\"", duration)); } - return result; + + const size_t end = factor == 0 ? duration.length() : duration.length() - 1; + return factor * parse_uint32(duration.substr(0, end)); } int diff --git a/src/Util.hpp b/src/Util.hpp index 725a7ee8b..f4844bee6 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -260,12 +260,9 @@ bool matches_dir_prefix_or_file(nonstd::string_view dir_prefix_or_file, // On Windows: Backslashes are replaced with forward slashes. std::string normalize_absolute_path(nonstd::string_view path); -// Parse the given string into an unsigned integer. Then based on suffix -// provided convert the number to seconds possible suffixes = d(ays)/s(econds) -// -// Throws `Error` for any other suffix -// Throws `Error` if parse value is <0 -unsigned parse_duration_with_suffix_to_seconds(const std::string& value); +// Parse `duration`, an unsigned 32-bit integer with d (days) or s (seconds) +// suffix, into seconds. Throws `Error` on error. +uint32_t parse_duration(const std::string& duration); // Parse a string into a signed integer. // diff --git a/src/ccache.cpp b/src/ccache.cpp index 7584d1e33..dfdc5ecd5 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -2252,8 +2252,8 @@ handle_main_options(int argc, const char* const* argv) } case EVICT_OLDER_THAN: { - unsigned seconds = Util::parse_duration_with_suffix_to_seconds(optarg); - ProgressBar progress_bar("Clearing ..."); + auto seconds = Util::parse_duration(optarg); + ProgressBar progress_bar("Evicting ..."); clean_old( ctx, [&](double progress) { progress_bar.update(progress); }, seconds); if (isatty(STDOUT_FILENO)) { diff --git a/test/suites/cleanup.bash b/test/suites/cleanup.bash index a85088c79..05d86fed8 100644 --- a/test/suites/cleanup.bash +++ b/test/suites/cleanup.bash @@ -179,8 +179,9 @@ SUITE_cleanup() { $CCACHE -c >/dev/null expect_file_count 1 '.nfs*' $CCACHE_DIR expect_stat 'files in cache' 10 + # ------------------------------------------------------------------------- - TEST "cleanup of old files by age" + TEST "Cleanup of old files by age" prepare_cleanup_test_dir $CCACHE_DIR/a touch $CCACHE_DIR/a/now.result diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index 979728a1e..7a864a134 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -498,18 +498,21 @@ TEST_CASE("Util::normalize_absolute_path") #endif } -TEST_CASE("Util::parse_duration_with_suffix_to_seconds") -{ - CHECK(Util::parse_duration_with_suffix_to_seconds("0s") == 0); - CHECK(Util::parse_duration_with_suffix_to_seconds("2s") == 2); - CHECK(Util::parse_duration_with_suffix_to_seconds("1d") == 3600 * 24); - CHECK(Util::parse_duration_with_suffix_to_seconds("2d") == 2 * 3600 * 24); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("-2"), - "invalid unsigned integer: \"-2\""); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2x"), - "Invalid suffix, Supported: d(ay)/s(econd): \"2x\""); - CHECK_THROWS_WITH(Util::parse_duration_with_suffix_to_seconds("2"), - "Invalid suffix, Supported: d(ay)/s(econd): \"2\""); +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::parse_int")