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
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()
{
// 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();
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);
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;
}
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
}
}
-// 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.
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);
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*
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("") == "");
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;