]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Simplify and clean up parse_duration_with_suffix_to_seconds
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Jul 2020 19:07:09 +0000 (21:07 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Jul 2020 19:45:03 +0000 (21:45 +0200)
src/Util.cpp
src/Util.hpp
src/ccache.cpp
test/suites/cleanup.bash
unittest/test_Util.cpp

index 8af128cc07d49ae0c1ddabf147ce411ec86e7039..8e36d916d8367eec3cebc87fe874d6b4e11747e2 100644 (file)
@@ -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
index 725a7ee8baacb3aec0745e45bad829ba72ac69be..f4844bee67592888b61da5bf398ca8da63157213 100644 (file)
@@ -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.
 //
index 7584d1e335dd20331a901d15d4015882fb7b7b78..dfdc5ecd5042c11033aca0ab93b5642e658170a7 100644 (file)
@@ -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)) {
index a85088c79d13185474a70f4d8486c5bff5f0ac64..05d86fed8cbcf420b945bd1fa9468b70012e5091 100644 (file)
@@ -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
index 979728a1ea75be187f5e955035424c6004430073..7a864a134ec7a0ae4fb516be560cade000801bc0 100644 (file)
@@ -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")