0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-18 16:00:57 +01:00

ircd: Add pretty IEC unit printing utils.

This commit is contained in:
Jason Volk 2018-09-20 16:39:27 -07:00
parent db9d1dcb99
commit 5e158e37a5
2 changed files with 39 additions and 0 deletions

View file

@ -36,6 +36,9 @@ namespace ircd
const_buffer a2u(const mutable_buffer &out, const const_buffer &in);
string_view u2a(const mutable_buffer &out, const const_buffer &in);
std::string u2a(const const_buffer &in);
string_view pretty_iec_unit(const mutable_buffer &out, const uint64_t &value);
std::string pretty_iec_unit(const uint64_t &value);
}
namespace ircd

View file

@ -22,6 +22,42 @@
// misc util
//
std::string
ircd::pretty_iec_unit(const uint64_t &value)
{
return util::string(32, [&value]
(const mutable_buffer &out)
{
return pretty_iec_unit(out, value);
});
}
ircd::string_view
ircd::pretty_iec_unit(const mutable_buffer &out,
const uint64_t &value)
try
{
auto pos(0);
auto v(value);
for(; v > 1024; v /= 1024, ++pos);
static const std::array<string_view, 7> unit
{
"B", " KiB", "MiB", "GiB", "TiB", "PiB", "EiB"
};
return fmt::sprintf
{
out, "%lu %s", v, unit.at(pos)
};
}
catch(const std::out_of_range &e)
{
return fmt::sprintf
{
out, "%lu B", value
};
}
std::string
ircd::u2a(const const_buffer &in)
{