Luna/luna/src/Units.cpp
apio 814672c771
All checks were successful
continuous-integration/drone/push Build is passing
Remove some redundant error propagation
Why can printing to the serial port or format onto a string fail?
Even if cstyle_format returns Result<usize>, we shouldn't always follow suit.
2022-12-16 18:32:29 +01:00

28 lines
697 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, "%u bytes", value); }
const char* unit_prefixes = "KMGTPE";
while (value > (1024 * 1024))
{
value /= 1024;
unit_prefixes++;
}
return string_format(buffer, max, "%u.%u %ciB", value / 1024, (value % 1024) / 103, *unit_prefixes);
}
Result<OwnedStringView> to_dynamic_unit(usize value)
{
char* buf = TRY(make_array<char>(64));
to_dynamic_unit_cstr(value, buf, 64);
return OwnedStringView{buf};
}