]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::parse_duration to util
authorJoel Rosdahl <joel@rosdahl.net>
Fri, 14 Jul 2023 06:14:09 +0000 (08:14 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Fri, 14 Jul 2023 18:33:17 +0000 (20:33 +0200)
src/Util.cpp
src/Util.hpp
src/core/mainoptions.cpp
src/util/string.cpp
src/util/string.hpp
unittest/test_Util.cpp
unittest/test_util_string.cpp

index 5ad6b7f00f3b16d3a2bd99fb5a14d092c6d18871..c69f42e38a957f15719d7af361926605989f8a62 100644 (file)
@@ -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)
 {
index bf7a0ec9b469a3e11958abce80b9192646f4b698..e8f3f0c27814d629188b50ea93be71b0db9e53a8 100644 (file)
@@ -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);
index 685b34f473cf469fbfbf25b27b7ad5f5fb95d95d..01cf78102705d17f17052b15ae575e2406c40e13 100644 (file)
@@ -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<Error>(util::parse_duration(arg));
       break;
     }
 
index d1c98ea01c6a1f553b452e50ea39e5395083d94a..7f17826d78c1b8226da48868295f5100d28d3545 100644 (file)
@@ -121,6 +121,31 @@ parse_double(const std::string& value)
   }
 }
 
+nonstd::expected<uint64_t, std::string>
+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<int64_t, std::string>
 parse_signed(std::string_view value,
              const std::optional<int64_t> min_value,
index 300faab852acb81b98a5be5cf008ddf9abfa84bb..1739e9a6cd61745b3a0379c49fab89c25cb91bdd 100644 (file)
@@ -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<double, std::string> parse_double(const std::string& value);
 
+// Parse `duration`, an unsigned integer with d (days) or s (seconds) suffix,
+// into seconds.
+nonstd::expected<uint64_t, std::string>
+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
index 321b69ed31613962965b4ea5d68f7ee10c3668c2..01f89076a0abc0f42e6a7205f2ed99d9cb11484d 100644 (file)
@@ -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("") == "");
index 12e4822facbc9f43242a49605284b87aaad10b40..70a291c933b48e9b75450161dec5d2962c5230e1 100644 (file)
@@ -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);