]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
refactor: Move Util::format_{human,parsable}_* to util
authorJoel Rosdahl <joel@rosdahl.net>
Mon, 20 Feb 2023 20:12:03 +0000 (21:12 +0100)
committerJoel Rosdahl <joel@rosdahl.net>
Sat, 4 Mar 2023 09:10:20 +0000 (10:10 +0100)
src/Config.cpp
src/Util.cpp
src/Util.hpp
src/core/mainoptions.cpp
src/storage/local/LocalStorage.cpp
src/util/string.cpp
src/util/string.hpp
unittest/test_Util.cpp
unittest/test_util_string.cpp

index 42b254c1bb0089a285f22b209a827dc31b87a239..1e55477372e6604469c24511642f4a530dfbdeb2 100644 (file)
@@ -249,7 +249,7 @@ format_bool(bool value)
 std::string
 format_cache_size(uint64_t value)
 {
-  return Util::format_parsable_size_with_suffix(value);
+  return util::format_parsable_size_with_suffix(value);
 }
 
 CompilerType
index e14d1b1e947689ed96aa4372405667a9bcc032f9..c9cf3cbb6624cf8f53ce944e344962b8662903a8 100644 (file)
@@ -557,41 +557,6 @@ format_base32hex(const uint8_t* data, size_t size)
   return result;
 }
 
-std::string
-format_human_readable_diff(int64_t diff)
-{
-  const char* sign = diff == 0 ? "" : (diff > 0 ? "+" : "-");
-  return FMT("{}{}", sign, format_human_readable_size(std::abs(diff)));
-}
-
-std::string
-format_human_readable_size(uint64_t size)
-{
-  if (size >= 1000 * 1000 * 1000) {
-    return FMT("{:.1f} GB", size / ((double)(1000 * 1000 * 1000)));
-  } else if (size >= 1000 * 1000) {
-    return FMT("{:.1f} MB", size / ((double)(1000 * 1000)));
-  } else if (size >= 1000) {
-    return FMT("{:.1f} kB", size / 1000.0);
-  } else if (size == 1) {
-    return "1 byte";
-  } else {
-    return FMT("{} bytes", size);
-  }
-}
-
-std::string
-format_parsable_size_with_suffix(uint64_t size)
-{
-  if (size >= 1000 * 1000 * 1000) {
-    return FMT("{:.1f}G", size / ((double)(1000 * 1000 * 1000)));
-  } else if (size >= 1000 * 1000) {
-    return FMT("{:.1f}M", size / ((double)(1000 * 1000)));
-  } else {
-    return FMT("{}", size);
-  }
-}
-
 void
 ensure_dir_exists(std::string_view dir)
 {
index 1bd57b0df640d92640ec93697bd54d7c524ac1c0..7f7be73e2a57976ecd05fbbdeebfc464ec566d3c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2019-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2019-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -143,15 +143,6 @@ std::string format_base16(const uint8_t* data, size_t size);
 // padding characters will be added.
 std::string format_base32hex(const uint8_t* data, size_t size);
 
-// Format `diff` as a human-readable string.
-std::string format_human_readable_diff(int64_t diff);
-
-// Format `size` as a human-readable string.
-std::string format_human_readable_size(uint64_t size);
-
-// Format `size` as a parsable string.
-std::string format_parsable_size_with_suffix(uint64_t size);
-
 // Return current working directory (CWD) as returned from getcwd(3) (i.e.,
 // normalized path without symlink parts). Returns the empty string on error.
 std::string get_actual_cwd();
index e8b464378549e0ab90a3b233dffdc7ac316bb8e7..40db41dccd69e8ea0be0dcd22785cf73ca354060 100644 (file)
@@ -241,7 +241,7 @@ print_compression_statistics(const storage::local::CompressionStatistics& cs)
   const double savings = ratio > 0.0 ? 100.0 - (100.0 / ratio) : 0.0;
 
   using C = util::TextTable::Cell;
-  auto human_readable = Util::format_human_readable_size;
+  auto human_readable = util::format_human_readable_size;
   util::TextTable table;
 
   table.add_row({
@@ -329,11 +329,11 @@ trim_dir(const std::string& dir,
     recompression_diff = recompressor.new_size() - recompressor.old_size();
     PRINT(stdout,
           "Recompressed {} to {} ({})\n",
-          Util::format_human_readable_size(incompressible_size
+          util::format_human_readable_size(incompressible_size
                                            + recompressor.old_size()),
-          Util::format_human_readable_size(incompressible_size
+          util::format_human_readable_size(incompressible_size
                                            + recompressor.new_size()),
-          Util::format_human_readable_diff(recompression_diff));
+          util::format_human_readable_diff(recompression_diff));
   }
 
   uint64_t size_after_recompression = initial_size + recompression_diff;
@@ -352,9 +352,9 @@ trim_dir(const std::string& dir,
 
   PRINT(stdout,
         "Trimmed {} to {} ({}, {}{} file{})\n",
-        Util::format_human_readable_size(size_after_recompression),
-        Util::format_human_readable_size(final_size),
-        Util::format_human_readable_diff(final_size - size_after_recompression),
+        util::format_human_readable_size(size_after_recompression),
+        util::format_human_readable_size(final_size),
+        util::format_human_readable_diff(final_size - size_after_recompression),
         removed_files == 0 ? "" : "-",
         removed_files,
         removed_files == 1 ? "" : "s");
@@ -664,7 +664,7 @@ process_main_options(int argc, const char* const* argv)
       } else {
         PRINT(stdout,
               "Set cache size limit to {}\n",
-              Util::format_human_readable_size(size));
+              util::format_human_readable_size(size));
       }
       break;
     }
index 3e30fe8ce29a40ee5f7e27c424d14a10d6e59080..706d1eaebaba0b908a0c89b5465e153ebf429d2c 100644 (file)
@@ -780,17 +780,17 @@ LocalStorage::recompress(const std::optional<int8_t> level,
     - static_cast<int64_t>(recompressor.old_size());
 
   const std::string old_compr_size_str =
-    Util::format_human_readable_size(recompressor.old_size());
+    util::format_human_readable_size(recompressor.old_size());
   const std::string new_compr_size_str =
-    Util::format_human_readable_size(recompressor.new_size());
+    util::format_human_readable_size(recompressor.new_size());
   const std::string content_size_str =
-    Util::format_human_readable_size(recompressor.content_size());
+    util::format_human_readable_size(recompressor.content_size());
   const std::string incompr_size_str =
-    Util::format_human_readable_size(incompressible_size);
+    util::format_human_readable_size(incompressible_size);
   const std::string size_difference_str =
     FMT("{}{}",
         size_difference < 0 ? "-" : (size_difference > 0 ? "+" : " "),
-        Util::format_human_readable_size(
+        util::format_human_readable_size(
           size_difference < 0 ? -size_difference : size_difference));
 
   PRINT(stdout, "Original data:         {:>8s}\n", content_size_str);
@@ -1174,12 +1174,12 @@ LocalStorage::evaluate_cleanup()
 
   std::string max_size_str =
     m_config.max_size() > 0 ? FMT(
-      ", max size {}", Util::format_human_readable_size(m_config.max_size()))
+      ", max size {}", util::format_human_readable_size(m_config.max_size()))
                             : "";
   std::string max_files_str =
     m_config.max_files() > 0 ? FMT(", max files {}", m_config.max_files()) : "";
   std::string info_str = FMT("size {}, files {}{}{}",
-                             Util::format_human_readable_size(total_size),
+                             util::format_human_readable_size(total_size),
                              total_files,
                              max_size_str,
                              max_files_str);
index 3a03a0becfc1ef0a3bb89eca4b6548e351605f9f..914f1f7406ba50be03b19df015405d2a426bdad7 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
 
 namespace util {
 
+std::string
+format_human_readable_diff(int64_t diff)
+{
+  const char* sign = diff == 0 ? "" : (diff > 0 ? "+" : "-");
+  return FMT("{}{}", sign, format_human_readable_size(std::abs(diff)));
+}
+
+std::string
+format_human_readable_size(uint64_t size)
+{
+  if (size >= 1000 * 1000 * 1000) {
+    return FMT("{:.1f} GB", size / ((double)(1000 * 1000 * 1000)));
+  } else if (size >= 1000 * 1000) {
+    return FMT("{:.1f} MB", size / ((double)(1000 * 1000)));
+  } else if (size >= 1000) {
+    return FMT("{:.1f} kB", size / 1000.0);
+  } else if (size == 1) {
+    return "1 byte";
+  } else {
+    return FMT("{} bytes", size);
+  }
+}
+
+std::string
+format_parsable_size_with_suffix(uint64_t size)
+{
+  if (size >= 1000 * 1000 * 1000) {
+    return FMT("{:.1f}G", size / ((double)(1000 * 1000 * 1000)));
+  } else if (size >= 1000 * 1000) {
+    return FMT("{:.1f}M", size / ((double)(1000 * 1000)));
+  } else {
+    return FMT("{}", size);
+  }
+}
+
 nonstd::expected<double, std::string>
 parse_double(const std::string& value)
 {
index 0e48bc4e31aaebddc27d92271e5b4881ec4de9f2..5154915dfd4361783acbfc4471468cc81468d746 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -39,6 +39,15 @@ namespace util {
 // Return true if `suffix` is a suffix of `string`.
 bool ends_with(std::string_view string, std::string_view suffix);
 
+// Format `diff` as a human-readable string.
+std::string format_human_readable_diff(int64_t diff);
+
+// Format `size` as a human-readable string.
+std::string format_human_readable_size(uint64_t size);
+
+// Format `size` as a parsable string.
+std::string format_parsable_size_with_suffix(uint64_t size);
+
 // Join stringified elements of `container` delimited by `delimiter` into a
 // string. There must exist an `std::string to_string(T::value_type)` function.
 template<typename T>
index e5bca40c29634562556bbb6268b542eea69a4956..9c54e0b4c6295dc72554d60efe9666b6ff025bbd 100644 (file)
@@ -256,63 +256,6 @@ TEST_CASE("Util::format_base32hex")
   CHECK(Util::format_base32hex(input, 6) == "cpnmuoj1e8");
 }
 
-TEST_CASE("Util::format_human_readable_diff")
-{
-  CHECK(Util::format_human_readable_diff(0) == "0 bytes");
-  CHECK(Util::format_human_readable_diff(1) == "+1 byte");
-  CHECK(Util::format_human_readable_diff(42) == "+42 bytes");
-  CHECK(Util::format_human_readable_diff(1949) == "+1.9 kB");
-  CHECK(Util::format_human_readable_diff(1951) == "+2.0 kB");
-  CHECK(Util::format_human_readable_diff(499.7 * 1000) == "+499.7 kB");
-  CHECK(Util::format_human_readable_diff(1000 * 1000) == "+1.0 MB");
-  CHECK(Util::format_human_readable_diff(1234 * 1000) == "+1.2 MB");
-  CHECK(Util::format_human_readable_diff(438.5 * 1000 * 1000) == "+438.5 MB");
-  CHECK(Util::format_human_readable_diff(1000 * 1000 * 1000) == "+1.0 GB");
-  CHECK(Util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000)
-        == "+17.1 GB");
-
-  CHECK(Util::format_human_readable_diff(-1) == "-1 byte");
-  CHECK(Util::format_human_readable_diff(-42) == "-42 bytes");
-  CHECK(Util::format_human_readable_diff(-1949) == "-1.9 kB");
-  CHECK(Util::format_human_readable_diff(-1951) == "-2.0 kB");
-  CHECK(Util::format_human_readable_diff(-499.7 * 1000) == "-499.7 kB");
-  CHECK(Util::format_human_readable_diff(-1000 * 1000) == "-1.0 MB");
-  CHECK(Util::format_human_readable_diff(-1234 * 1000) == "-1.2 MB");
-  CHECK(Util::format_human_readable_diff(-438.5 * 1000 * 1000) == "-438.5 MB");
-  CHECK(Util::format_human_readable_diff(-1000 * 1000 * 1000) == "-1.0 GB");
-  CHECK(Util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000)
-        == "-17.1 GB");
-}
-
-TEST_CASE("Util::format_human_readable_size")
-{
-  CHECK(Util::format_human_readable_size(0) == "0 bytes");
-  CHECK(Util::format_human_readable_size(1) == "1 byte");
-  CHECK(Util::format_human_readable_size(42) == "42 bytes");
-  CHECK(Util::format_human_readable_size(1949) == "1.9 kB");
-  CHECK(Util::format_human_readable_size(1951) == "2.0 kB");
-  CHECK(Util::format_human_readable_size(499.7 * 1000) == "499.7 kB");
-  CHECK(Util::format_human_readable_size(1000 * 1000) == "1.0 MB");
-  CHECK(Util::format_human_readable_size(1234 * 1000) == "1.2 MB");
-  CHECK(Util::format_human_readable_size(438.5 * 1000 * 1000) == "438.5 MB");
-  CHECK(Util::format_human_readable_size(1000 * 1000 * 1000) == "1.0 GB");
-  CHECK(Util::format_human_readable_size(17.11 * 1000 * 1000 * 1000)
-        == "17.1 GB");
-}
-
-TEST_CASE("Util::format_parsable_size_with_suffix")
-{
-  CHECK(Util::format_parsable_size_with_suffix(0) == "0");
-  CHECK(Util::format_parsable_size_with_suffix(42 * 1000) == "42000");
-  CHECK(Util::format_parsable_size_with_suffix(1000 * 1000) == "1.0M");
-  CHECK(Util::format_parsable_size_with_suffix(1234 * 1000) == "1.2M");
-  CHECK(Util::format_parsable_size_with_suffix(438.5 * 1000 * 1000)
-        == "438.5M");
-  CHECK(Util::format_parsable_size_with_suffix(1000 * 1000 * 1000) == "1.0G");
-  CHECK(Util::format_parsable_size_with_suffix(17.11 * 1000 * 1000 * 1000)
-        == "17.1G");
-}
-
 TEST_CASE("Util::get_extension")
 {
   CHECK(Util::get_extension("") == "");
index 626966359f26162cd96235dbaff8f739e72f610b..26fcab246ea6125add616c138b706ad30f8689e0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2021-2022 Joel Rosdahl and other contributors
+// Copyright (C) 2021-2023 Joel Rosdahl and other contributors
 //
 // See doc/AUTHORS.adoc for a complete list of contributors.
 //
@@ -50,6 +50,63 @@ TEST_CASE("util::ends_with")
   CHECK_FALSE(util::ends_with("x", "xy"));
 }
 
+TEST_CASE("util::format_human_readable_diff")
+{
+  CHECK(util::format_human_readable_diff(0) == "0 bytes");
+  CHECK(util::format_human_readable_diff(1) == "+1 byte");
+  CHECK(util::format_human_readable_diff(42) == "+42 bytes");
+  CHECK(util::format_human_readable_diff(1949) == "+1.9 kB");
+  CHECK(util::format_human_readable_diff(1951) == "+2.0 kB");
+  CHECK(util::format_human_readable_diff(499.7 * 1000) == "+499.7 kB");
+  CHECK(util::format_human_readable_diff(1000 * 1000) == "+1.0 MB");
+  CHECK(util::format_human_readable_diff(1234 * 1000) == "+1.2 MB");
+  CHECK(util::format_human_readable_diff(438.5 * 1000 * 1000) == "+438.5 MB");
+  CHECK(util::format_human_readable_diff(1000 * 1000 * 1000) == "+1.0 GB");
+  CHECK(util::format_human_readable_diff(17.11 * 1000 * 1000 * 1000)
+        == "+17.1 GB");
+
+  CHECK(util::format_human_readable_diff(-1) == "-1 byte");
+  CHECK(util::format_human_readable_diff(-42) == "-42 bytes");
+  CHECK(util::format_human_readable_diff(-1949) == "-1.9 kB");
+  CHECK(util::format_human_readable_diff(-1951) == "-2.0 kB");
+  CHECK(util::format_human_readable_diff(-499.7 * 1000) == "-499.7 kB");
+  CHECK(util::format_human_readable_diff(-1000 * 1000) == "-1.0 MB");
+  CHECK(util::format_human_readable_diff(-1234 * 1000) == "-1.2 MB");
+  CHECK(util::format_human_readable_diff(-438.5 * 1000 * 1000) == "-438.5 MB");
+  CHECK(util::format_human_readable_diff(-1000 * 1000 * 1000) == "-1.0 GB");
+  CHECK(util::format_human_readable_diff(-17.11 * 1000 * 1000 * 1000)
+        == "-17.1 GB");
+}
+
+TEST_CASE("util::format_human_readable_size")
+{
+  CHECK(util::format_human_readable_size(0) == "0 bytes");
+  CHECK(util::format_human_readable_size(1) == "1 byte");
+  CHECK(util::format_human_readable_size(42) == "42 bytes");
+  CHECK(util::format_human_readable_size(1949) == "1.9 kB");
+  CHECK(util::format_human_readable_size(1951) == "2.0 kB");
+  CHECK(util::format_human_readable_size(499.7 * 1000) == "499.7 kB");
+  CHECK(util::format_human_readable_size(1000 * 1000) == "1.0 MB");
+  CHECK(util::format_human_readable_size(1234 * 1000) == "1.2 MB");
+  CHECK(util::format_human_readable_size(438.5 * 1000 * 1000) == "438.5 MB");
+  CHECK(util::format_human_readable_size(1000 * 1000 * 1000) == "1.0 GB");
+  CHECK(util::format_human_readable_size(17.11 * 1000 * 1000 * 1000)
+        == "17.1 GB");
+}
+
+TEST_CASE("util::format_parsable_size_with_suffix")
+{
+  CHECK(util::format_parsable_size_with_suffix(0) == "0");
+  CHECK(util::format_parsable_size_with_suffix(42 * 1000) == "42000");
+  CHECK(util::format_parsable_size_with_suffix(1000 * 1000) == "1.0M");
+  CHECK(util::format_parsable_size_with_suffix(1234 * 1000) == "1.2M");
+  CHECK(util::format_parsable_size_with_suffix(438.5 * 1000 * 1000)
+        == "438.5M");
+  CHECK(util::format_parsable_size_with_suffix(1000 * 1000 * 1000) == "1.0G");
+  CHECK(util::format_parsable_size_with_suffix(17.11 * 1000 * 1000 * 1000)
+        == "17.1G");
+}
+
 TEST_CASE("util::join")
 {
   {