0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-12 04:51:08 +01:00

ircd::util: Allow integer index on all overloads for precooked format strings.

This commit is contained in:
Jason Volk 2019-09-22 15:38:51 -07:00
parent 62d91f2c94
commit 2be2d7ca8c
2 changed files with 16 additions and 53 deletions

View file

@ -18,8 +18,7 @@ inline namespace util
using human_readable_size = std::tuple<uint64_t, long double, const string_view &>; using human_readable_size = std::tuple<uint64_t, long double, const string_view &>;
// Default format strings used for pretty(size). // Default format strings used for pretty(size).
extern const string_view pretty_size_fmt; extern const string_view pretty_fmt[];
extern const string_view pretty_only_size_fmt;
// Process the value to be made pretty first with one of the following: // Process the value to be made pretty first with one of the following:
human_readable_size iec(const uint64_t &value); human_readable_size iec(const uint64_t &value);
@ -30,12 +29,8 @@ inline namespace util
std::string pretty(const human_readable_size &, const string_view &fmt); std::string pretty(const human_readable_size &, const string_view &fmt);
// Generate a pretty string with the pretty_fmt_default implied. // Generate a pretty string with the pretty_fmt_default implied.
string_view pretty(const mutable_buffer &out, const human_readable_size &); string_view pretty(const mutable_buffer &out, const human_readable_size &, const uint &fmt = 0);
std::string pretty(const human_readable_size &); std::string pretty(const human_readable_size &, const uint &fmt = 0);
// Generate a pretty string with another (simpler) default fmt string.
string_view pretty_only(const mutable_buffer &out, const human_readable_size &);
std::string pretty_only(const human_readable_size &);
// Human readable time suite (for timers and counts; otherwise see date.h) // Human readable time suite (for timers and counts; otherwise see date.h)
string_view pretty_nanoseconds(const mutable_buffer &out, const long double &, const uint &fmt = 0); string_view pretty_nanoseconds(const mutable_buffer &out, const long double &, const uint &fmt = 0);

View file

@ -232,60 +232,28 @@ ircd::util::pretty_nanoseconds(const mutable_buffer &out,
// Human readable space suite // Human readable space suite
// //
decltype(ircd::util::pretty_size_fmt) decltype(ircd::util::pretty_fmt)
ircd::util::pretty_size_fmt ircd::util::pretty_fmt
{ {
"%.2lf %s (%lu)" "%.2lf %s (%lu)"_sv,
}; "%.2lf %s"_sv,
"%.2lf%s"_sv,
decltype(ircd::util::pretty_only_size_fmt) string_view{},
ircd::util::pretty_only_size_fmt
{
"%.2lf %s",
}; };
std::string std::string
ircd::util::pretty_only(const human_readable_size &value) ircd::util::pretty(const human_readable_size &value,
const uint &fmt)
{ {
return util::string(32, [&value] return pretty(value, pretty_fmt[fmt]);
(const mutable_buffer &out)
{
return pretty_only(out, value);
});
}
ircd::string_view
ircd::util::pretty_only(const mutable_buffer &out,
const human_readable_size &value)
try
{
return fmt::sprintf
{
out, pretty_only_size_fmt,
std::get<long double>(value),
std::get<const string_view &>(value)
};
}
catch(const std::out_of_range &e)
{
return fmt::sprintf
{
out, "%lu B",
std::get<uint64_t>(value)
};
}
std::string
ircd::util::pretty(const human_readable_size &value)
{
return pretty(value, pretty_size_fmt);
} }
ircd::string_view ircd::string_view
ircd::util::pretty(const mutable_buffer &out, ircd::util::pretty(const mutable_buffer &out,
const human_readable_size &value) const human_readable_size &value,
const uint &fmt)
{ {
return pretty(out, pretty_size_fmt, value); return pretty(out, pretty_fmt[fmt], value);
} }
std::string std::string
@ -317,7 +285,7 @@ catch(const std::out_of_range &e)
{ {
return fmt::sprintf return fmt::sprintf
{ {
out, "%lu B", out, "%lu",
std::get<uint64_t>(value) std::get<uint64_t>(value)
}; };
} }