diff --git a/src/string_util.cc b/src/string_util.cc index 2dc7b186..c69e40a8 100644 --- a/src/string_util.cc +++ b/src/string_util.cc @@ -15,13 +15,13 @@ namespace benchmark { namespace { - // kilo, Mega, Giga, Tera, Peta, Exa, Zetta, Yotta. -const char kBigSIUnits[] = "kMGTPEZY"; +const char* const kBigSIUnits[] = {"k", "M", "G", "T", "P", "E", "Z", "Y"}; // Kibi, Mebi, Gibi, Tebi, Pebi, Exbi, Zebi, Yobi. -const char kBigIECUnits[] = "KMGTPEZY"; +const char* const kBigIECUnits[] = {"Ki", "Mi", "Gi", "Ti", + "Pi", "Ei", "Zi", "Yi"}; // milli, micro, nano, pico, femto, atto, zepto, yocto. -const char kSmallSIUnits[] = "munpfazy"; +const char* const kSmallSIUnits[] = {"m", "u", "n", "p", "f", "a", "z", "y"}; // We require that all three arrays have the same size. static_assert(arraysize(kBigSIUnits) == arraysize(kBigIECUnits), @@ -92,16 +92,14 @@ std::string ExponentToPrefix(int64_t exponent, bool iec) { const int64_t index = (exponent > 0 ? exponent - 1 : -exponent - 1); if (index >= kUnitsSize) return ""; - const char* array = + const char* const* array = (exponent > 0 ? (iec ? kBigIECUnits : kBigSIUnits) : kSmallSIUnits); - if (iec) { - return array[index] + std::string("i"); - } - return std::string(1, array[index]); + + return std::string(array[index]); } -std::string ToBinaryStringFullySpecified( - double value, int precision, Counter::OneK one_k = Counter::kIs1024) { +std::string ToBinaryStringFullySpecified(double value, int precision, + Counter::OneK one_k) { std::string mantissa; int64_t exponent; ToExponentAndMantissa(value, precision, @@ -142,13 +140,6 @@ std::string StrFormatImp(const char* msg, va_list args) { } // end namespace -void AppendHumanReadable(int n, std::string* str) { - std::stringstream ss; - // Round down to the nearest SI prefix. - ss << ToBinaryStringFullySpecified(n, 0); - *str += ss.str(); -} - std::string HumanReadableNumber(double n, Counter::OneK one_k) { return ToBinaryStringFullySpecified(n, 1, one_k); } diff --git a/src/string_util.h b/src/string_util.h index b05281b9..731aa2c0 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -13,9 +13,6 @@ namespace benchmark { -BENCHMARK_EXPORT -void AppendHumanReadable(int n, std::string* str); - BENCHMARK_EXPORT std::string HumanReadableNumber(double n, Counter::OneK one_k); diff --git a/test/string_util_gtest.cc b/test/string_util_gtest.cc index 7cf57498..67b4bc0c 100644 --- a/test/string_util_gtest.cc +++ b/test/string_util_gtest.cc @@ -1,5 +1,5 @@ //===---------------------------------------------------------------------===// -// statistics_test - Unit tests for src/statistics.cc +// string_util_test - Unit tests for src/string_util.cc //===---------------------------------------------------------------------===// #include @@ -161,25 +161,6 @@ TEST(StringUtilTest, StrSplit) { std::vector({"hello", "there", "is", "more"})); } -using AppendHumanReadableFixture = - ::testing::TestWithParam>; - -INSTANTIATE_TEST_SUITE_P( - AppendHumanReadableTests, AppendHumanReadableFixture, - ::testing::Values(std::make_tuple(0, "0"), std::make_tuple(999, "999"), - std::make_tuple(1000, "1000"), - std::make_tuple(1024, "1Ki"), - std::make_tuple(1000 * 1000, "976\\.56.Ki"), - std::make_tuple(1024 * 1024, "1Mi"), - std::make_tuple(1000 * 1000 * 1000, "953\\.674Mi"), - std::make_tuple(1024 * 1024 * 1024, "1Gi"))); - -TEST_P(AppendHumanReadableFixture, AppendHumanReadable) { - std::string str; - benchmark::AppendHumanReadable(std::get<0>(GetParam()), &str); - ASSERT_THAT(str, ::testing::MatchesRegex(std::get<1>(GetParam()))); -} - using HumanReadableFixture = ::testing::TestWithParam< std::tuple>;