Improve unit representation code

This commit is contained in:
apio 2022-11-30 14:41:35 +01:00
parent 9f5fb547f7
commit f8ed74fda5

View File

@ -1,15 +1,14 @@
#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)
while (value > (1024 * 1024))
{
value >>= 10;
value /= 1024;
unit_prefixes++;
}