diff --git a/include/ircd/lex_cast.h b/include/ircd/lex_cast.h index 581ece324..ebc3dc1fa 100644 --- a/include/ircd/lex_cast.h +++ b/include/ircd/lex_cast.h @@ -31,25 +31,6 @@ namespace ircd // Circular static thread_local buffer const size_t LEX_CAST_BUFS {256}; // plenty template string_view lex_cast(const T &t); - - // Binary <-> Hex conversion suite - 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); - - // Human readable space suite - using human_readable_size = std::tuple; - human_readable_size iec(const uint64_t &value); - human_readable_size si(const uint64_t &value); - string_view pretty(const mutable_buffer &out, const human_readable_size &); - std::string pretty(const human_readable_size &); - 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) - string_view pretty_nanoseconds(const mutable_buffer &out, const long double &); - template string_view pretty(const mutable_buffer &out, const duration &); - template std::string pretty(const duration &); } namespace ircd @@ -271,25 +252,3 @@ ircd::try_lex_cast(const string_view &s) { return true; } - -template -std::string -ircd::pretty(const duration &d) -{ - return util::string(32, [&d] - (const mutable_buffer &out) - { - return pretty(out, d); - }); -} - -template -ircd::string_view -ircd::pretty(const mutable_buffer &out, - const duration &d) -{ - const auto &ns(duration_cast(d)); - return pretty_nanoseconds(out, ns.count()); -} diff --git a/include/ircd/util/pretty.h b/include/ircd/util/pretty.h new file mode 100644 index 000000000..e8690c53c --- /dev/null +++ b/include/ircd/util/pretty.h @@ -0,0 +1,51 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_UTIL_PRETTY_H + +namespace ircd::util +{ + // Human readable space suite + using human_readable_size = std::tuple; + human_readable_size iec(const uint64_t &value); + human_readable_size si(const uint64_t &value); + string_view pretty(const mutable_buffer &out, const human_readable_size &); + std::string pretty(const human_readable_size &); + 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) + string_view pretty_nanoseconds(const mutable_buffer &out, const long double &); + template string_view pretty(const mutable_buffer &out, const duration &); + template std::string pretty(const duration &); +} + +template +std::string +ircd::util::pretty(const duration &d) +{ + return util::string(32, [&d] + (const mutable_buffer &out) + { + return pretty(out, d); + }); +} + +template +ircd::string_view +ircd::util::pretty(const mutable_buffer &out, + const duration &d) +{ + const auto &ns(duration_cast(d)); + return pretty_nanoseconds(out, ns.count()); +} diff --git a/include/ircd/util/u2a.h b/include/ircd/util/u2a.h new file mode 100644 index 000000000..504c33e2c --- /dev/null +++ b/include/ircd/util/u2a.h @@ -0,0 +1,20 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +#pragma once +#define HAVE_IRCD_UTIL_U2A_H + +namespace ircd::util +{ + // Binary <-> Hex conversion suite + 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); +} diff --git a/include/ircd/util/util.h b/include/ircd/util/util.h index 932f930dd..adb99dbab 100644 --- a/include/ircd/util/util.h +++ b/include/ircd/util/util.h @@ -52,6 +52,8 @@ namespace ircd #include "iterator.h" #include "nothrow.h" #include "what.h" +#include "u2a.h" +#include "pretty.h" // Unsorted section namespace ircd { diff --git a/ircd/Makefile.am b/ircd/Makefile.am index ed900cdc3..298b0f764 100644 --- a/ircd/Makefile.am +++ b/ircd/Makefile.am @@ -90,6 +90,7 @@ libircd_la_LIBADD = \ libircd_la_SOURCES = \ allocator.cc \ exception.cc \ + util.cc \ ios.cc \ lexical.cc \ tokens.cc \ diff --git a/ircd/lexical.cc b/ircd/lexical.cc index 5c17d330a..192b449d6 100644 --- a/ircd/lexical.cc +++ b/ircd/lexical.cc @@ -18,244 +18,6 @@ #include -// -// misc util -// - -// -// Human readable time suite -// - -ircd::string_view -ircd::pretty_nanoseconds(const mutable_buffer &out, - const long double &ns) -{ - static const std::array unit - { - "nanoseconds", - "microseconds", - "milliseconds", - "seconds", - "minutes", - "hours", - "days", - }; - - auto pos(0); - long double val(ns); - - // 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 > 12.0) - { - val /= 12; - ++pos; - } - else goto done; - - done: - return fmt::sprintf - { - out, "%.2lf %s", - val, - unit.at(pos) - }; -} - -// -// Human readable space suite -// - -std::string -ircd::pretty_only(const human_readable_size &value) -{ - return util::string(32, [&value] - (const mutable_buffer &out) - { - return pretty_only(out, value); - }); -} - -ircd::string_view -ircd::pretty_only(const mutable_buffer &out, - const human_readable_size &value) -try -{ - return fmt::sprintf - { - out, "%.2lf %s", - std::get(value), - std::get(value) - }; -} -catch(const std::out_of_range &e) -{ - return fmt::sprintf - { - out, "%lu B", - std::get(value) - }; -} - -std::string -ircd::pretty(const human_readable_size &value) -{ - return util::string(64, [&value] - (const mutable_buffer &out) - { - return pretty(out, value); - }); -} - -ircd::string_view -ircd::pretty(const mutable_buffer &out, - const human_readable_size &value) -try -{ - return fmt::sprintf - { - out, "%.2lf %s (%lu)", - std::get(value), - std::get(value), - std::get(value) - }; -} -catch(const std::out_of_range &e) -{ - return fmt::sprintf - { - out, "%lu B", - std::get(value) - }; -} - -ircd::human_readable_size -ircd::iec(const uint64_t &value) -{ - static const std::array unit - { - "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" - }; - - auto pos(0); - long double v(value); - for(; v > 1024.0; v /= 1024.0, ++pos); - return - { - value, v, unit.at(pos) - }; -} - -ircd::human_readable_size -ircd::si(const uint64_t &value) -{ - static const std::array unit - { - "B", "KB", "MB", "GB", "TB", "PB", "EB" - }; - - auto pos(0); - long double v(value); - for(; v > 1000.0; v /= 1000.0, ++pos); - return - { - value, v, unit.at(pos) - }; -} - -// -// binary <-> hex suite -// - -std::string -ircd::u2a(const const_buffer &in) -{ - return string(size(in) * 2, [&in] - (const mutable_buffer &out) - { - return u2a(out, in); - }); -} - -ircd::string_view -ircd::u2a(const mutable_buffer &out, - const const_buffer &in) -{ - char *p(data(out)); - for(size_t i(0); i < size(in) && p + 2 <= end(out); ++i) - { - char tmp[3]; - ::snprintf(tmp, sizeof(tmp), "%02x", in[i]); - *p++ = tmp[0]; - *p++ = tmp[1]; - } - - return { data(out), p }; -} - -ircd::const_buffer -ircd::a2u(const mutable_buffer &out, - const const_buffer &in) -{ - const size_t len{size(in) / 2}; - for(size_t i(0); i < len; ++i) - { - const char gl[3] - { - in[i * 2], - in[i * 2 + 1], - '\0' - }; - - out[i] = strtol(gl, nullptr, 16); - } - - return { data(out), len }; -} - -// -// lex_cast -// - namespace ircd { /// The static lex_cast ring buffers are each LEX_CAST_BUFSIZE bytes; diff --git a/ircd/util.cc b/ircd/util.cc new file mode 100644 index 000000000..5d29b1d49 --- /dev/null +++ b/ircd/util.cc @@ -0,0 +1,239 @@ +// Matrix Construct +// +// Copyright (C) Matrix Construct Developers, Authors & Contributors +// Copyright (C) 2016-2018 Jason Volk +// +// Permission to use, copy, modify, and/or distribute this software for any +// purpose with or without fee is hereby granted, provided that the above +// copyright notice and this permission notice is present in all copies. The +// full license for this software is available in the LICENSE file. + +// +// Human readable time suite +// + +ircd::string_view +ircd::util::pretty_nanoseconds(const mutable_buffer &out, + const long double &ns) +{ + static const std::array unit + { + "nanoseconds", + "microseconds", + "milliseconds", + "seconds", + "minutes", + "hours", + "days", + }; + + auto pos(0); + long double val(ns); + + // 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 > 12.0) + { + val /= 12; + ++pos; + } + else goto done; + + done: + return fmt::sprintf + { + out, "%.2lf %s", + val, + unit.at(pos) + }; +} + +// +// Human readable space suite +// + +std::string +ircd::util::pretty_only(const human_readable_size &value) +{ + return util::string(32, [&value] + (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, "%.2lf %s", + std::get(value), + std::get(value) + }; +} +catch(const std::out_of_range &e) +{ + return fmt::sprintf + { + out, "%lu B", + std::get(value) + }; +} + +std::string +ircd::util::pretty(const human_readable_size &value) +{ + return util::string(64, [&value] + (const mutable_buffer &out) + { + return pretty(out, value); + }); +} + +ircd::string_view +ircd::util::pretty(const mutable_buffer &out, + const human_readable_size &value) +try +{ + return fmt::sprintf + { + out, "%.2lf %s (%lu)", + std::get(value), + std::get(value), + std::get(value) + }; +} +catch(const std::out_of_range &e) +{ + return fmt::sprintf + { + out, "%lu B", + std::get(value) + }; +} + +ircd::human_readable_size +ircd::util::iec(const uint64_t &value) +{ + static const std::array unit + { + "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" + }; + + auto pos(0); + long double v(value); + for(; v > 1024.0; v /= 1024.0, ++pos); + return + { + value, v, unit.at(pos) + }; +} + +ircd::human_readable_size +ircd::util::si(const uint64_t &value) +{ + static const std::array unit + { + "B", "KB", "MB", "GB", "TB", "PB", "EB" + }; + + auto pos(0); + long double v(value); + for(; v > 1000.0; v /= 1000.0, ++pos); + return + { + value, v, unit.at(pos) + }; +} + +// +// binary <-> hex suite +// + +std::string +ircd::util::u2a(const const_buffer &in) +{ + return string(size(in) * 2, [&in] + (const mutable_buffer &out) + { + return u2a(out, in); + }); +} + +ircd::string_view +ircd::util::u2a(const mutable_buffer &out, + const const_buffer &in) +{ + char *p(data(out)); + for(size_t i(0); i < size(in) && p + 2 <= end(out); ++i) + { + char tmp[3]; + ::snprintf(tmp, sizeof(tmp), "%02x", in[i]); + *p++ = tmp[0]; + *p++ = tmp[1]; + } + + return { data(out), p }; +} + +ircd::const_buffer +ircd::util::a2u(const mutable_buffer &out, + const const_buffer &in) +{ + const size_t len{size(in) / 2}; + for(size_t i(0); i < len; ++i) + { + const char gl[3] + { + in[i * 2], + in[i * 2 + 1], + '\0' + }; + + out[i] = strtol(gl, nullptr, 16); + } + + return { data(out), len }; +}