From 27d394ebfaf5b03504f50d0626c1271fc7849a2c Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Tue, 20 Aug 2019 20:10:25 -0700 Subject: [PATCH] ircd::util: Simplify tool. --- ircd/util.cc | 95 ++++++++++------------------------------------------ 1 file changed, 17 insertions(+), 78 deletions(-) diff --git a/ircd/util.cc b/ircd/util.cc index ef1a36269..7b566ebb3 100644 --- a/ircd/util.cc +++ b/ircd/util.cc @@ -194,18 +194,20 @@ ircd::util::pretty_nanoseconds(const mutable_buffer &out, const long double &ns, const uint &fmt) { - static const std::array, 9> unit + using formats = std::array; + using element = std::tuple; + static const std::array unit {{ - // fmt=0 fmt=1 - { "nanoseconds", "ns" }, - { "microseconds", "us" }, - { "milliseconds", "ms" }, - { "seconds", "s" }, - { "minutes", "m" }, - { "hours", "h" }, - { "days", "d" }, - { "weeks", "w" }, - { "months", "M" }, + // fmt=0 fmt=1 + { { "nanoseconds", "ns" }, 1000.0 }, + { { "microseconds", "us" }, 1000.0 }, + { { "milliseconds", "ms" }, 1000.0 }, + { { "seconds", "s" }, 60.0 }, + { { "minutes", "m" }, 60.0 }, + { { "hours", "h" }, 24.0 }, + { { "days", "d" }, 7.0 }, + { { "weeks", "w" }, 30.0 }, + { { "months", "M" }, 12.0 }, }}; const string_view &fmtstr @@ -215,77 +217,14 @@ ircd::util::pretty_nanoseconds(const mutable_buffer &out, "%.2lf %s"_sv }; - auto pos(0); + size_t i(0), pos(0); long double val(ns); + for(; val > std::get<1>(unit.at(pos)); ++pos) + val /= std::get<1>(unit.at(pos)); - // nanoseconds -> microseconds - if(val > 1000.0) - { - val /= 1000; - ++pos; - } - else goto done; - - // microseconds -> milliseconds - if(val > 1000.0) - { - val /= 1000; - ++pos; - } - else goto done; - - // milliseconds -> seconds - if(val > 1000.0) - { - val /= 1000; - ++pos; - } - else goto done; - - // seconds -> minutes - if(val > 60.0) - { - val /= 60; - ++pos; - } - else goto done; - - // minutes -> hours - if(val > 60.0) - { - val /= 60; - ++pos; - } - else goto done; - - // hours -> days - if(val > 24.0) - { - val /= 24; - ++pos; - } - else goto done; - - // days -> weeks - if(val > 7.0) - { - val /= 7; - ++pos; - } - else goto done; - - // weeks -> months - if(val > 30.0) - { - val /= 30; - ++pos; - } - else goto done; - - done: return fmt::sprintf { - out, fmtstr, val, unit.at(pos).at(fmt) + out, fmtstr, val, std::get<0>(unit.at(pos)).at(fmt) }; }