Create utility function for formatting bytes

Byte count to prettified representation as string.
This commit is contained in:
Kawe Mazidjatari 2023-07-31 11:43:25 +02:00
parent de26e5735f
commit eb14469178
2 changed files with 13 additions and 0 deletions

View File

@ -82,6 +82,7 @@ void PrintM128i64(__m128i in);
void AppendPrintf(char* pBuffer, size_t nBufSize, char const* pFormat, ...);
string PrintPercentageEscape(const string& svInput);
string FormatBytes(size_t nBytes);
string FormatV(const char* szFormat, va_list args);
string Format(const char* szFormat, ...);

View File

@ -987,6 +987,18 @@ string PrintPercentageEscape(const string& svInput)
return result;
}
///////////////////////////////////////////////////////////////////////////////
// For formatting a STL string to a prettified representation of input bytes.
string FormatBytes(size_t nBytes)
{
char szBuf[128] = "";
const char* szPrefix[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB" };
const int iBase = 1024;
size_t c = (std::min)((size_t)(log((double)nBytes) / log((double)iBase)), (size_t)sizeof(szPrefix) - 1);
sprintf(szBuf, "%1.2lf %s", nBytes / pow((double)iBase, c), szPrefix[c]);
return string(szBuf);
}
///////////////////////////////////////////////////////////////////////////////
// For formatting a STL string using C-style format specifiers (va_list version).
string FormatV(const char* szFormat, va_list args)