17 lines
496 B
C++
17 lines
496 B
C++
#include <Format.h>
|
|
#include <Units.h>
|
|
#include <limits.h>
|
|
|
|
Result<usize> to_dynamic_unit(usize value, char* buffer, size_t max)
|
|
{
|
|
if (value < 1024) { return string_format(buffer, max, "%u bytes", value); }
|
|
|
|
const char* unit_prefixes = "KMGTPE";
|
|
for (int i = 40; i >= 0 && value > (0xfffccccccccccccUL >> i); i -= 10)
|
|
{
|
|
value >>= 10;
|
|
unit_prefixes++;
|
|
}
|
|
|
|
return string_format(buffer, max, "%u.%u %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes);
|
|
} |