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
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)
{
-// 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.
//
// 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();
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({
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;
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");
} else {
PRINT(stdout,
"Set cache size limit to {}\n",
- Util::format_human_readable_size(size));
+ util::format_human_readable_size(size));
}
break;
}
- 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);
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);
-// 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)
{
-// 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.
//
// 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>
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("") == "");
-// 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.
//
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")
{
{