19 lines
459 B
C++
19 lines
459 B
C++
#include <luna/Alloc.h>
|
|
#include <luna/Result.h>
|
|
#include <luna/ScopeGuard.h>
|
|
#include <luna/Units.h>
|
|
|
|
Result<String> to_dynamic_unit(usize value)
|
|
{
|
|
if (value < 1024) { return String::format("%zu bytes"_sv, value); }
|
|
|
|
const char* unit_prefixes = "KMGTPE";
|
|
while (value > (1024 * 1024))
|
|
{
|
|
value /= 1024;
|
|
unit_prefixes++;
|
|
}
|
|
|
|
return String::format("%zu.%zu %ciB"_sv, value / 1024, (value % 1024) / 103, *unit_prefixes);
|
|
}
|