28 static unsigned versionPart(std::string_view version,
bool rc =
false) noexcept {
29 if (rc && version.starts_with(
"rc"))
30 version.remove_prefix(2);
41 static unsigned versionSum(std::string_view version)
noexcept {
44 for (
auto i = 0U; i < 3; ++i) {
64 bool operator()(std::string_view ver1, std::string_view ver2)
const noexcept
68 const auto at = [](
const auto &arr,
size_t i)
noexcept {
69 return i < arr.size() ? arr[i] : std::string_view{};
71 for (
auto i = 0U; i < 2U; ++i) {
76 const auto arr1Last = arr1.size() == i + 1;
77 const auto arr2Last = arr2.size() == i + 1;
78 if (arr1Last && arr2Last)
80 if (arr1Last || arr2Last)
97 template <
typename T = std::
string>
98 static std::optional<T>
get(
const std::string &name)
noexcept {
99 if (
const auto env = std::getenv(name.c_str()))
108template <
typename Rep =
double,
typename Period = std::milli>
110 using Clock = std::chrono::steady_clock;
111 using TimePoint = Clock::time_point;
112 using Dur = std::chrono::duration<Rep, Period>;
114 Measure() noexcept : m_start(Clock::now()) {}
117 void reset(TimePoint point = Clock::now()) noexcept { m_start = point; }
121 return std::chrono::duration_cast<Dur>(Clock::now() - m_start);
126 auto now = Clock::now();
127 auto ret = std::chrono::duration_cast<Dur>(now - m_start);
133 template <
typename Func,
typename... Args>
134 static Dur
profile(Func &&func, Args&&... args) {
136 std::forward<Func>(func)(std::forward<Args>(args)...);
154 static std::string
human(
const size_t bytes,
const unsigned precision = 2,
155 const bool fixed =
true) noexcept {
156 static constexpr const std::string_view units[] {
157 "B",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB"
161 auto bytesD =
static_cast<double>(bytes);
162 while (bytesD >= 1024.) {
167 std::ostringstream s;
169 s << std::fixed << std::setprecision(precision);
170 s << bytesD <<
' ' << units[unit];
Dur elapsed() const noexcept
Returns the duration it took from the construction (or last reset()).
Definition Misc.h:120
void reset(TimePoint point=Clock::now()) noexcept
Reset to count from point (or now).
Definition Misc.h:117
static Dur profile(Func &&func, Args &&... args)
Run func with args and return how long it took.
Definition Misc.h:134
Dur lap() noexcept
Returns the duration it took from the construction (or last reset()) and reset.
Definition Misc.h:125
static std::vector< std::string_view > splitSV(std::string_view str, std::string_view delim, std::optional< char > comment=std::nullopt) noexcept
Split str by delim into a vector, ignoring everything after comment.
Definition String.h:162
static std::optional< I > toNum(std::string_view str, int base=10) noexcept
Convert str to a number.
Definition String.h:68
Compare versions, to be used as Compare in containers.
Definition Misc.h:57
bool operator()(std::string_view ver1, std::string_view ver2) const noexcept
Comparator for versions ver1 and ver2.
Definition Misc.h:64
A helper for environment variables.
Definition Misc.h:91
static std::optional< T > get(const std::string &name) noexcept
Get value of name in environment.
Definition Misc.h:98
A helper to convert units.
Definition Misc.h:146
static std::string human(const size_t bytes, const unsigned precision=2, const bool fixed=true) noexcept
Convert bytes into human readable form (1 Kib, 20.5 MiB, ...).
Definition Misc.h:154
static auto versionSplit(std::string_view version) noexcept
Split version into a string array.
Definition Misc.h:23
static unsigned versionSum(std::string_view version) noexcept
Sum up version parts as parsed from version.
Definition Misc.h:41
static unsigned versionPart(std::string_view version, bool rc=false) noexcept
Convert version into a number.
Definition Misc.h:28