apio
814672c771
All checks were successful
continuous-integration/drone/push Build is passing
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.
28 lines
697 B
C++
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};
|
|
} |