From 2afd1f1acb7b864fc0e5f2791c5250c1fbc82396 Mon Sep 17 00:00:00 2001 From: Tudor Bosman Date: Sat, 21 Jul 2012 19:13:21 -0700 Subject: [PATCH] Add IEC binary unit prefixes Summary: KiB, MiB, GiB, TiB, etc Test Plan: by hand in a program using them Reviewed By: philipp@fb.com FB internal diff: D527001 --- folly/String.cpp | 36 ++++++++++++++++++++++++++++-------- folly/String.h | 14 +++++++++++--- folly/test/StringTest.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 11 deletions(-) diff --git a/folly/String.cpp b/folly/String.cpp index e58432b4..b413054d 100644 --- a/folly/String.cpp +++ b/folly/String.cpp @@ -132,7 +132,16 @@ const PrettySuffix kPrettyTimeSuffixes[] = { { 0, 0 }, }; -const PrettySuffix kPrettyBytesSuffixes[] = { +const PrettySuffix kPrettyBytesMetricSuffixes[] = { + { "TB", 1e12L }, + { "GB", 1e9L }, + { "MB", 1e6L }, + { "kB", 1e3L }, + { "B ", 0L }, + { 0, 0 }, +}; + +const PrettySuffix kPrettyBytesBinarySuffixes[] = { { "TB", int64_t(1) << 40 }, { "GB", int64_t(1) << 30 }, { "MB", int64_t(1) << 20 }, @@ -141,12 +150,12 @@ const PrettySuffix kPrettyBytesSuffixes[] = { { 0, 0 }, }; -const PrettySuffix kPrettyBytesMetricSuffixes[] = { - { "TB", 1e12L }, - { "GB", 1e9L }, - { "MB", 1e6L }, - { "kB", 1e3L }, - { "B ", 0L }, +const PrettySuffix kPrettyBytesBinaryIECSuffixes[] = { + { "TiB", int64_t(1) << 40 }, + { "GiB", int64_t(1) << 30 }, + { "MiB", int64_t(1) << 20 }, + { "KiB", int64_t(1) << 10 }, + { "B ", 0L }, { 0, 0 }, }; @@ -168,12 +177,23 @@ const PrettySuffix kPrettyUnitsBinarySuffixes[] = { { 0, 0 }, }; +const PrettySuffix kPrettyUnitsBinaryIECSuffixes[] = { + { "Ti", int64_t(1) << 40 }, + { "Gi", int64_t(1) << 30 }, + { "Mi", int64_t(1) << 20 }, + { "Ki", int64_t(1) << 10 }, + { " ", 0 }, + { 0, 0 }, +}; + const PrettySuffix* const kPrettySuffixes[PRETTY_NUM_TYPES] = { kPrettyTimeSuffixes, - kPrettyBytesSuffixes, kPrettyBytesMetricSuffixes, + kPrettyBytesBinarySuffixes, + kPrettyBytesBinaryIECSuffixes, kPrettyUnitsMetricSuffixes, kPrettyUnitsBinarySuffixes, + kPrettyUnitsBinaryIECSuffixes, }; } // namespace diff --git a/folly/String.h b/folly/String.h index 47d79de3..604c0883 100644 --- a/folly/String.h +++ b/folly/String.h @@ -206,19 +206,27 @@ bool unhexlify(const InputString& input, OutputString& output); * * Current types are: * PRETTY_TIME - s, ms, us, ns, etc. - * PRETTY_BYTES - kb, MB, GB, etc (goes up by 2^10 = 1024 each time) - * PRETTY_BYTES_METRIC - kb, MB, GB, etc (goes up by 10^3 = 1000 each time) + * PRETTY_BYTES_METRIC - kB, MB, GB, etc (goes up by 10^3 = 1000 each time) + * PRETTY_BYTES - kB, MB, GB, etc (goes up by 2^10 = 1024 each time) + * PRETTY_BYTES_IEC - KiB, MiB, GiB, etc * PRETTY_UNITS_METRIC - k, M, G, etc (goes up by 10^3 = 1000 each time) * PRETTY_UNITS_BINARY - k, M, G, etc (goes up by 2^10 = 1024 each time) + * PRETTY_UNITS_BINARY_IEC - Ki, Mi, Gi, etc * * @author Mark Rabkin */ enum PrettyType { PRETTY_TIME, - PRETTY_BYTES, + PRETTY_BYTES_METRIC, + PRETTY_BYTES_BINARY, + PRETTY_BYTES = PRETTY_BYTES_BINARY, + PRETTY_BYTES_BINARY_IEC, + PRETTY_BYTES_IEC = PRETTY_BYTES_BINARY_IEC, + PRETTY_UNITS_METRIC, PRETTY_UNITS_BINARY, + PRETTY_UNITS_BINARY_IEC, PRETTY_NUM_TYPES }; diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index 32de3194..ec3b310b 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -221,6 +221,20 @@ TEST(PrettyPrint, Basic) { EXPECT_EQ(string("1 GB"), prettyPrint(pow2(30), PRETTY_BYTES)); EXPECT_EQ(string("1 TB"), prettyPrint(pow2(40), PRETTY_BYTES)); + EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_IEC)); + EXPECT_EQ(string("833 KiB"), prettyPrint(853.e3, PRETTY_BYTES_IEC)); + EXPECT_EQ(string("813.5 MiB"), prettyPrint(853.e6, PRETTY_BYTES_IEC)); + EXPECT_EQ(string("7.944 GiB"), prettyPrint(8.53e9, PRETTY_BYTES_IEC)); + EXPECT_EQ(string("794.4 GiB"), prettyPrint(853.e9, PRETTY_BYTES_IEC)); + EXPECT_EQ(string("775.8 TiB"), prettyPrint(853.e12, PRETTY_BYTES_IEC)); + + EXPECT_EQ(string("0 B "), prettyPrint(0, PRETTY_BYTES_IEC)); + EXPECT_EQ(string("1 B "), prettyPrint(pow2(0), PRETTY_BYTES_IEC)); + EXPECT_EQ(string("1 KiB"), prettyPrint(pow2(10), PRETTY_BYTES_IEC)); + EXPECT_EQ(string("1 MiB"), prettyPrint(pow2(20), PRETTY_BYTES_IEC)); + EXPECT_EQ(string("1 GiB"), prettyPrint(pow2(30), PRETTY_BYTES_IEC)); + EXPECT_EQ(string("1 TiB"), prettyPrint(pow2(40), PRETTY_BYTES_IEC)); + // check bytes metric printing EXPECT_EQ(string("853 B "), prettyPrint(853., PRETTY_BYTES_METRIC)); EXPECT_EQ(string("853 kB"), prettyPrint(853.e3, PRETTY_BYTES_METRIC)); @@ -262,6 +276,22 @@ TEST(PrettyPrint, Basic) { EXPECT_EQ(string("1024 G"), prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY)); + EXPECT_EQ(string("0 "), prettyPrint(0, PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1 "), prettyPrint(pow2(0), PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1 Ki"), prettyPrint(pow2(10), PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1 Mi"), prettyPrint(pow2(20), PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1 Gi"), prettyPrint(pow2(30), PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1 Ti"), prettyPrint(pow2(40), PRETTY_UNITS_BINARY_IEC)); + + EXPECT_EQ(string("1023 "), + prettyPrint(pow2(10) - 1, PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1024 Ki"), + prettyPrint(pow2(20) - 1, PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1024 Mi"), + prettyPrint(pow2(30) - 1, PRETTY_UNITS_BINARY_IEC)); + EXPECT_EQ(string("1024 Gi"), + prettyPrint(pow2(40) - 1, PRETTY_UNITS_BINARY_IEC)); + // check that negative values work EXPECT_EQ(string("-85.3 s "), prettyPrint(-85.3, PRETTY_TIME)); EXPECT_EQ(string("-85.3 ms"), prettyPrint(-85.3e-3, PRETTY_TIME)); -- 2.34.1