This commit is contained in:
Branimir Karadžić
2018-02-22 15:45:42 -08:00
parent e0b537c5af
commit d0e2be1755
3 changed files with 21 additions and 10 deletions

View File

@@ -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 <typename Ty>
void stringPrintfVargs(Ty& _out, const char* _format, va_list _argList);
///
/// Templatized snprintf.
template <typename Ty>
void stringPrintf(Ty& _out, const char* _format, ...);

View File

@@ -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<uint32_t Kilo, char KiloCh0, char KiloCh1>
template<uint32_t Kilo, char KiloCh0, char KiloCh1, CharFn fn>
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

View File

@@ -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)