diff options
author | Joel Rosdahl <joel@rosdahl.net> | 2023-02-20 21:12:03 +0100 |
---|---|---|
committer | Joel Rosdahl <joel@rosdahl.net> | 2023-03-04 10:10:20 +0100 |
commit | d62b9df5546bee65795188b4e4cbd8dc872887d7 (patch) | |
tree | 238a1d6068fc5ad5561b4f63319a17f60510fc1a | |
parent | f8565ec6a72972c2fb8e6676d3dba3e85961b356 (diff) | |
download | ccache-d62b9df5546bee65795188b4e4cbd8dc872887d7.tar.gz |
refactor: Move Util::format_{human,parsable}_* to util
-rw-r--r-- | src/Config.cpp | 2 | ||||
-rw-r--r-- | src/Util.cpp | 35 | ||||
-rw-r--r-- | src/Util.hpp | 11 | ||||
-rw-r--r-- | src/core/mainoptions.cpp | 16 | ||||
-rw-r--r-- | src/storage/local/LocalStorage.cpp | 14 | ||||
-rw-r--r-- | src/util/string.cpp | 37 | ||||
-rw-r--r-- | src/util/string.hpp | 11 | ||||
-rw-r--r-- | unittest/test_Util.cpp | 57 | ||||
-rw-r--r-- | unittest/test_util_string.cpp | 59 |
9 files changed, 121 insertions, 121 deletions
diff --git a/src/Config.cpp b/src/Config.cpp index 42b254c1..1e554773 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -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 diff --git a/src/Util.cpp b/src/Util.cpp index e14d1b1e..c9cf3cbb 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -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) { diff --git a/src/Util.hpp b/src/Util.hpp index 1bd57b0d..7f7be73e 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -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(); diff --git a/src/core/mainoptions.cpp b/src/core/mainoptions.cpp index e8b46437..40db41dc 100644 --- a/src/core/mainoptions.cpp +++ b/src/core/mainoptions.cpp @@ -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; } diff --git a/src/storage/local/LocalStorage.cpp b/src/storage/local/LocalStorage.cpp index 3e30fe8c..706d1eae 100644 --- a/src/storage/local/LocalStorage.cpp +++ b/src/storage/local/LocalStorage.cpp @@ -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); diff --git a/src/util/string.cpp b/src/util/string.cpp index 3a03a0be..914f1f74 100644 --- a/src/util/string.cpp +++ b/src/util/string.cpp @@ -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. // @@ -27,6 +27,41 @@ 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) { diff --git a/src/util/string.hpp b/src/util/string.hpp index 0e48bc4e..5154915d 100644 --- a/src/util/string.hpp +++ b/src/util/string.hpp @@ -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> diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index e5bca40c..9c54e0b4 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -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("") == ""); diff --git a/unittest/test_util_string.cpp b/unittest/test_util_string.cpp index 62696635..26fcab24 100644 --- a/unittest/test_util_string.cpp +++ b/unittest/test_util_string.cpp @@ -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") { { |