]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
C++-ify format_{human_readable_size,parsable_size_with_suffix}
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Jul 2020 14:27:53 +0000 (16:27 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 26 Jul 2020 18:26:13 +0000 (20:26 +0200)
src/Config.cpp
src/Util.cpp
src/Util.hpp
src/ccache.cpp
src/compress.cpp
src/legacy_util.cpp
src/legacy_util.hpp
src/stats.cpp
unittest/test_Util.cpp
unittest/test_legacy_util.cpp

index a8fdfc805fabbb1a089df10a5d98a681f9860e6a..4b4ceec7ede6b8947c29fc2b961fa9bd1de9c516 100644 (file)
@@ -240,10 +240,7 @@ parse_cache_size(const std::string& value)
 std::string
 format_cache_size(uint64_t value)
 {
-  char* string = format_parsable_size_with_suffix(value);
-  std::string result = string;
-  free(string);
-  return result;
+  return Util::format_parsable_size_with_suffix(value);
 }
 
 uint32_t
index b20c549a1c42473d079bd521ab9dfed54506addd..05403aa460426acbe4f294c02e96f956d1c08f02 100644 (file)
@@ -310,6 +310,28 @@ format_hex(const uint8_t* data, size_t size)
   return result;
 }
 
+std::string
+format_human_readable_size(uint64_t size)
+{
+  if (size >= 1000 * 1000 * 1000) {
+    return fmt::format("{:.1f} GB", size / ((double)(1000 * 1000 * 1000)));
+  } else {
+    return fmt::format("{:.1f} MB", size / ((double)(1000 * 1000)));
+  }
+}
+
+std::string
+format_parsable_size_with_suffix(uint64_t size)
+{
+  if (size >= 1000 * 1000 * 1000) {
+    return fmt::format("{:.1f}G", size / ((double)(1000 * 1000 * 1000)));
+  } else if (size >= 1000 * 1000) {
+    return fmt::format("{:.1f}M", size / ((double)(1000 * 1000)));
+  } else {
+    return fmt::format("{}", (unsigned)size);
+  }
+}
+
 std::string
 get_actual_cwd()
 {
index 10f5fdb8a0d8a1bc29065c4de90104bceecf1245..f3c23f0435d910dd046dfb29a2f79c206d988a01 100644 (file)
@@ -131,6 +131,12 @@ void for_each_level_1_subdir(const std::string& cache_dir,
 // string will be `2 * size` long.
 std::string format_hex(const uint8_t* data, size_t size);
 
+// 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 c12c6a854b562760b5c260d18486cad140a1f102..884b1830d5b8e80fec76d622cecb9000715ed042 100644 (file)
@@ -1793,9 +1793,8 @@ create_initial_config_file(Config& config)
     config.set_max_files(max_files);
   }
   if (max_size != 0) {
-    char* size = format_parsable_size_with_suffix(max_size);
-    fprintf(f, "max_size = %s\n", size);
-    free(size);
+    std::string size = Util::format_parsable_size_with_suffix(max_size);
+    fprintf(f, "max_size = %s\n", size.c_str());
     config.set_max_size(max_size);
   }
   fclose(f);
@@ -2341,9 +2340,8 @@ handle_main_options(int argc, const char* const* argv)
       if (size == 0) {
         printf("Unset cache size limit\n");
       } else {
-        char* s = format_human_readable_size(size);
-        printf("Set cache size limit to %s\n", s);
-        free(s);
+        fmt::print("Set cache size limit to {}\n",
+                   Util::format_human_readable_size(size));
       }
       break;
     }
index f0100c8ef8c25d0d8892564e2a57e2c4aef186c2..78354c3bf4171055219de84241200e8d74e28b76 100644 (file)
@@ -184,28 +184,25 @@ compress_stats(const Config& config,
   double ratio = compr_size > 0 ? ((double)compr_orig_size) / compr_size : 0.0;
   double savings = ratio > 0.0 ? 100.0 - (100.0 / ratio) : 0.0;
 
-  char* on_disk_size_str = format_human_readable_size(on_disk_size);
-  char* cache_size_str = format_human_readable_size(compr_size + incompr_size);
-  char* compr_size_str = format_human_readable_size(compr_size);
-  char* compr_orig_size_str = format_human_readable_size(compr_orig_size);
-  char* incompr_size_str = format_human_readable_size(incompr_size);
-
-  printf("Total data:            %8s (%s disk blocks)\n",
-         cache_size_str,
-         on_disk_size_str);
-  printf("Compressed data:       %8s (%.1f%% of original size)\n",
-         compr_size_str,
-         100.0 - savings);
-  printf("  - Original size:     %8s\n", compr_orig_size_str);
-  printf(
-    "  - Compression ratio: %5.3f x  (%.1f%% space savings)\n", ratio, savings);
-  printf("Incompressible data:   %8s\n", incompr_size_str);
-
-  free(incompr_size_str);
-  free(compr_orig_size_str);
-  free(compr_size_str);
-  free(cache_size_str);
-  free(on_disk_size_str);
+  std::string on_disk_size_str = Util::format_human_readable_size(on_disk_size);
+  std::string cache_size_str =
+    Util::format_human_readable_size(compr_size + incompr_size);
+  std::string compr_size_str = Util::format_human_readable_size(compr_size);
+  std::string compr_orig_size_str =
+    Util::format_human_readable_size(compr_orig_size);
+  std::string incompr_size_str = Util::format_human_readable_size(incompr_size);
+
+  fmt::print("Total data:            {:8s} ({} disk blocks)\n",
+             cache_size_str,
+             on_disk_size_str);
+  fmt::print("Compressed data:       {:8s} ({:.1f}% of original size)\n",
+             compr_size_str,
+             100.0 - savings);
+  fmt::print("  - Original size:     {:8s}\n", compr_orig_size_str);
+  fmt::print("  - Compression ratio: %5.3f x  ({:.1f}% space savings)\n",
+             ratio,
+             savings);
+  fmt::print("Incompressible data:   {:8s}\n", incompr_size_str);
 }
 
 void
index 90ddf55438ca14a9b0c13a19b0c5d43a318bdba6..2dafad98c24a48cf0e375230095f3b3f56303d50 100644 (file)
@@ -342,34 +342,6 @@ reformat(char** ptr, const char* format, ...)
   }
 }
 
-// Format a size as a human-readable string. Caller frees.
-char*
-format_human_readable_size(uint64_t size)
-{
-  char* s;
-  if (size >= 1000 * 1000 * 1000) {
-    s = format("%.1f GB", size / ((double)(1000 * 1000 * 1000)));
-  } else {
-    s = format("%.1f MB", size / ((double)(1000 * 1000)));
-  }
-  return s;
-}
-
-// Format a size as a parsable string. Caller frees.
-char*
-format_parsable_size_with_suffix(uint64_t size)
-{
-  char* s;
-  if (size >= 1000 * 1000 * 1000) {
-    s = format("%.1fG", size / ((double)(1000 * 1000 * 1000)));
-  } else if (size >= 1000 * 1000) {
-    s = format("%.1fM", size / ((double)(1000 * 1000)));
-  } else {
-    s = format("%u", (unsigned)size);
-  }
-  return s;
-}
-
 // Parse a "size value", i.e. a string that can end in k, M, G, T (10-based
 // suffixes) or Ki, Mi, Gi, Ti (2-based suffixes). For backward compatibility,
 // K is also recognized as a synonym of k.
index 752e2c807554c8f5131e1de3f001dc1701a7e1c1..ded137fb9b779d6d90d00c4716a247fa2b840e20 100644 (file)
@@ -36,8 +36,6 @@ char* x_strdup(const char* s);
 char* x_strndup(const char* s, size_t n);
 void x_setenv(const char* name, const char* value);
 void x_unsetenv(const char* name);
-char* format_human_readable_size(uint64_t size);
-char* format_parsable_size_with_suffix(uint64_t size);
 bool parse_size_with_suffix(const char* str, uint64_t* size);
 #ifndef HAVE_LOCALTIME_R
 struct tm* localtime_r(const time_t* timep, struct tm* result);
index 4fe1966f4f47732f19f38d120980598f2d5da588..7fad4f164ef9292956a9873018e82e3137f24bed 100644 (file)
@@ -179,9 +179,9 @@ static struct
 static char*
 format_size(uint64_t size)
 {
-  char* s = format_human_readable_size(size);
-  reformat(&s, "%11s", s);
-  return s;
+  std::string result =
+    fmt::format("{:>11}", Util::format_human_readable_size(size));
+  return x_strdup(result.c_str());
 }
 
 static char*
index 71586e07fc4faaa0d81ac87c8f1aa874e5c439ab..a1644e01c94cbbeb0b0a525354ef759d516fae79 100644 (file)
@@ -222,6 +222,32 @@ TEST_CASE("format_hex")
   CHECK(Util::format_hex(data, sizeof(data)) == "00010203");
 }
 
+TEST_CASE("format_human_readable_size")
+{
+  CHECK(Util::format_human_readable_size(0) == "0.0 MB");
+  CHECK(Util::format_human_readable_size(49) == "0.0 MB");
+  CHECK(Util::format_human_readable_size(420 * 1000) == "0.4 MB");
+  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("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 f20b0668873138d5f515d03f12d51dc1a43cf2ba..c03d6d8d3ec087d49d3bbddc9c3ab016375baa30 100644 (file)
@@ -58,34 +58,6 @@ TEST_CASE("subst_env_in_string")
   CHECK_STR_EQ_FREE2("syntax error: missing '}' after \"FOO\"", errmsg);
 }
 
-TEST_CASE("format_human_readable_size")
-{
-  CHECK_STR_EQ_FREE2("0.0 MB", format_human_readable_size(0));
-  CHECK_STR_EQ_FREE2("0.0 MB", format_human_readable_size(49));
-  CHECK_STR_EQ_FREE2("0.4 MB", format_human_readable_size(420 * 1000));
-  CHECK_STR_EQ_FREE2("1.0 MB", format_human_readable_size(1000 * 1000));
-  CHECK_STR_EQ_FREE2("1.2 MB", format_human_readable_size(1234 * 1000));
-  CHECK_STR_EQ_FREE2("438.5 MB",
-                     format_human_readable_size(438.5 * 1000 * 1000));
-  CHECK_STR_EQ_FREE2("1.0 GB", format_human_readable_size(1000 * 1000 * 1000));
-  CHECK_STR_EQ_FREE2("17.1 GB",
-                     format_human_readable_size(17.11 * 1000 * 1000 * 1000));
-}
-
-TEST_CASE("format_parsable_size_with_suffix")
-{
-  CHECK_STR_EQ_FREE2("0", format_parsable_size_with_suffix(0));
-  CHECK_STR_EQ_FREE2("42000", format_parsable_size_with_suffix(42 * 1000));
-  CHECK_STR_EQ_FREE2("1.0M", format_parsable_size_with_suffix(1000 * 1000));
-  CHECK_STR_EQ_FREE2("1.2M", format_parsable_size_with_suffix(1234 * 1000));
-  CHECK_STR_EQ_FREE2("438.5M",
-                     format_parsable_size_with_suffix(438.5 * 1000 * 1000));
-  CHECK_STR_EQ_FREE2("1.0G",
-                     format_parsable_size_with_suffix(1000 * 1000 * 1000));
-  CHECK_STR_EQ_FREE2(
-    "17.1G", format_parsable_size_with_suffix(17.11 * 1000 * 1000 * 1000));
-}
-
 TEST_CASE("parse_size_with_suffix")
 {
   uint64_t size;