0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-12-25 23:14:13 +01:00

ircd::util: Refactor pretty(duration) interface; internal template.

This commit is contained in:
Jason Volk 2020-09-27 17:53:18 -07:00
parent 4b590ecc41
commit 512b12caf1
2 changed files with 80 additions and 38 deletions

View file

@ -33,14 +33,16 @@ inline namespace util
std::string pretty(const human_readable_size &, const uint &fmt = 0);
// 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);
template<class r, class p> string_view pretty(const mutable_buffer &out, const duration<r, p> &, const uint &fmt = 0);
string_view pretty(const mutable_buffer &out, const nanoseconds &, const uint &fmt = 0);
string_view pretty(const mutable_buffer &out, const microseconds &, const uint &fmt = 0);
string_view pretty(const mutable_buffer &out, const milliseconds &, const uint &fmt = 0);
string_view pretty(const mutable_buffer &out, const seconds &, const uint &fmt = 0);
template<class r, class p> std::string pretty(const duration<r, p> &, const uint &fmt = 0);
}}
template<class rep,
class period>
std::string
inline std::string
ircd::util::pretty(const duration<rep, period> &d,
const uint &fmt)
{
@ -50,16 +52,3 @@ ircd::util::pretty(const duration<rep, period> &d,
return pretty(out, d, fmt);
});
}
template<class rep,
class period>
ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const duration<rep, period> &d,
const uint &fmt)
{
using nanoseconds_ld = duration<long double, nanoseconds::period>;
const auto &ns(duration_cast<nanoseconds_ld>(d));
return pretty_nanoseconds(out, ns.count(), fmt);
}

View file

@ -61,26 +61,45 @@ ircd::util::getenv(const string_view &key)
// Human readable time suite
//
ircd::string_view
ircd::util::pretty_nanoseconds(const mutable_buffer &out,
const long double &ns,
const uint &fmt)
namespace ircd { inline namespace util
{
using formats = std::array<string_view, 2>;
using element = std::tuple<formats, double>;
static const std::array<element, 9> unit
{{
// fmt=0 fmt=1
{ { "nanoseconds", "ns" }, 1000.0L },
{ { "microseconds", "us" }, 1000.0L },
{ { "milliseconds", "ms" }, 1000.0L },
{ { "seconds", "s" }, 60.0L },
{ { "minutes", "m" }, 60.0L },
{ { "hours", "h" }, 24.0L },
{ { "days", "d" }, 7.0L },
{ { "weeks", "w" }, 4.0L },
{ { "months", "M" }, 12.0L },
}};
using pretty_time_formats = std::array<string_view, 2>;
using pretty_time_element = std::tuple<pretty_time_formats, long double>;
template<size_t i,
class T>
static string_view pretty(const mutable_buffer &, const T &, const uint &fmt);
[[gnu::visibility("internal")]]
extern const std::array<pretty_time_element, 9> pretty_time_unit;
}}
decltype(ircd::util::pretty_time_unit)
ircd::util::pretty_time_unit
{{
// fmt=0 fmt=1
{ { "nanoseconds", "ns" }, 1000.0L },
{ { "microseconds", "us" }, 1000.0L },
{ { "milliseconds", "ms" }, 1000.0L },
{ { "seconds", "s" }, 60.0L },
{ { "minutes", "m" }, 60.0L },
{ { "hours", "h" }, 24.0L },
{ { "days", "d" }, 7.0L },
{ { "weeks", "w" }, 4.0L },
{ { "months", "M" }, 12.0L },
}};
template<size_t i,
class T>
inline ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const T &dur,
const uint &fmt)
{
static const auto &unit
{
pretty_time_unit
};
const string_view &fmtstr
{
@ -89,17 +108,51 @@ ircd::util::pretty_nanoseconds(const mutable_buffer &out,
"%.2lf %s"_sv
};
size_t i(0), pos(0);
long double val(ns);
size_t pos(i);
long double val(dur.count());
for(; val > std::get<1>(unit.at(pos)) && pos < unit.size() - 1; ++pos)
val /= std::get<1>(unit.at(pos));
return fmt::sprintf
{
out, fmtstr, val, std::get<0>(unit.at(pos)).at(fmt)
out, fmtstr,
val,
std::get<0>(unit.at(pos)).at(fmt)
};
}
ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const nanoseconds &val,
const uint &fmt)
{
return pretty<0>(out, val, fmt);
}
ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const microseconds &val,
const uint &fmt)
{
return pretty<1>(out, val, fmt);
}
ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const milliseconds &val,
const uint &fmt)
{
return pretty<2>(out, val, fmt);
}
ircd::string_view
ircd::util::pretty(const mutable_buffer &out,
const seconds &val,
const uint &fmt)
{
return pretty<3>(out, val, fmt);
}
//
// Human readable space suite
//