29 lines
692 B
C++
29 lines
692 B
C++
#include <luna/Alloc.h>
|
|
#include <luna/Format.h>
|
|
#include <luna/Result.h>
|
|
#include <luna/ScopeGuard.h>
|
|
#include <luna/Units.h>
|
|
|
|
usize to_dynamic_unit_cstr(usize value, char* buffer, usize max)
|
|
{
|
|
if (value < 1024) { return string_format(buffer, max, "%zu bytes", value); }
|
|
|
|
const char* unit_prefixes = "KMGTPE";
|
|
while (value > (1024 * 1024))
|
|
{
|
|
value /= 1024;
|
|
unit_prefixes++;
|
|
}
|
|
|
|
return string_format(buffer, max, "%zu.%zu %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes);
|
|
}
|
|
|
|
Result<String> to_dynamic_unit(usize value)
|
|
{
|
|
char* const buf = TRY(make_array<char>(64));
|
|
|
|
to_dynamic_unit_cstr(value, buf, 64);
|
|
|
|
return String { buf };
|
|
}
|