From d0e2be1755746da004e0256b0e52584823ac44ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Thu, 22 Feb 2018 15:45:42 -0800 Subject: [PATCH] Cleanup. --- include/bx/string.h | 11 ++++++----- src/string.cpp | 10 +++++----- tests/string_test.cpp | 10 ++++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/include/bx/string.h b/include/bx/string.h index 3a253ef..356d6dc 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -10,12 +10,13 @@ namespace bx { + /// Units struct Units { - enum Enum + enum Enum //!< Units: { - Kilo, - Kibi, + Kilo, //!< SI units + Kibi, //!< IEC prefix }; }; @@ -257,11 +258,11 @@ namespace bx /// enough space had been available. int32_t snprintf(char* _out, int32_t _max, const char* _format, ...); - /// Templatized snprintf + /// Templatized snprintf. template void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList); - /// + /// Templatized snprintf. template void stringPrintf(Ty& _out, const char* _format, ...); diff --git a/src/string.cpp b/src/string.cpp index 4b0d00f..9a0c783 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -1112,9 +1112,9 @@ namespace bx return total; } - static const char s_units[] = { 'B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; + static const char s_units[] = { 'B', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y' }; - template + template inline int32_t prettify(char* _out, int32_t _count, uint64_t _value) { uint8_t idx = 0; @@ -1128,7 +1128,7 @@ namespace bx } return snprintf(_out, _count, "%0.2f %c%c%c", value - , s_units[idx] + , fn(s_units[idx]) , idx > 0 ? KiloCh0 : '\0' , KiloCh1 ); @@ -1138,10 +1138,10 @@ namespace bx { if (Units::Kilo == _units) { - return prettify<1000, 'B', '\0'>(_out, _count, _value); + return prettify<1000, 'B', '\0', toNoop>(_out, _count, _value); } - return prettify<1024, 'i', 'B'>(_out, _count, _value); + return prettify<1024, 'i', 'B', toUpper>(_out, _count, _value); } } // namespace bx diff --git a/tests/string_test.cpp b/tests/string_test.cpp index db5e3e2..da01ae3 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -19,6 +19,16 @@ TEST_CASE("stringPrintfTy", "") REQUIRE(0 == bx::strCmp(bx::StringView(test), "printf into std::string.") ); } +TEST_CASE("prettify", "") +{ + char tmp[1024]; + prettify(tmp, BX_COUNTOF(tmp), 4000, bx::Units::Kilo); + REQUIRE(0 == bx::strCmp(tmp, "4.00 kB") ); + + prettify(tmp, BX_COUNTOF(tmp), 4096, bx::Units::Kibi); + REQUIRE(0 == bx::strCmp(tmp, "4.00 KiB") ); +} + TEST_CASE("chars", "") { for (char ch = 'A'; ch <= 'Z'; ++ch)