SlHelpers
Misc.h
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #pragma once
4 
5 #include <chrono>
6 #include <cstddef>
7 #include <iomanip>
8 #include <optional>
9 #include <sstream>
10 #include <string>
11 
12 #include "String.h"
13 
14 namespace SlHelpers {
15 
19 struct Version {
20  Version() = delete;
21 
23  static auto versionSplit(std::string_view version) noexcept {
24  return String::splitSV(version, ".-");
25  }
26 
28  static unsigned versionPart(std::string_view version, bool rc = false) noexcept {
29  if (rc && version.starts_with("rc"))
30  version.remove_prefix(2);
31  if (const auto verPart = String::toNum(version))
32  return *verPart;
33  return 0;
34  }
35 
41  static unsigned versionSum(std::string_view version) noexcept {
42  const auto arr = Version::versionSplit(version);
43  unsigned ret = 0;
44  for (auto i = 0U; i < 3; ++i) {
45  ret <<= 8;
46  if (i < arr.size())
47  ret += Version::versionPart(arr[i], i == 2);
48  }
49  return ret;
50  }
51 
52 };
53 
57 struct CmpVersions {
64  bool operator()(std::string_view ver1, std::string_view ver2) const noexcept
65  {
66  const auto arr1 = Version::versionSplit(ver1);
67  const auto arr2 = Version::versionSplit(ver2);
68  const auto at = [](const auto &arr, size_t i) noexcept {
69  return i < arr.size() ? arr[i] : std::string_view{};
70  };
71  for (auto i = 0U; i < 2U; ++i) {
72  auto ver1 = Version::versionPart(at(arr1, i));
73  auto ver2 = Version::versionPart(at(arr2, i));
74  if (ver1 != ver2)
75  return ver1 < ver2;
76  const auto arr1Last = arr1.size() == i + 1;
77  const auto arr2Last = arr2.size() == i + 1;
78  if (arr1Last && arr2Last)
79  return false;
80  if (arr1Last || arr2Last)
81  return arr1Last;
82  }
83 
84  return Version::versionPart(at(arr1, 2), true) < Version::versionPart(at(arr2, 2), true);
85  }
86 };
87 
91 struct Env {
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()))
100  return env;
101  return std::nullopt;
102  }
103 };
104 
108 template <typename Rep = double, typename Period = std::milli>
109 class Measure {
110  using Clock = std::chrono::steady_clock;
111  using TimePoint = Clock::time_point;
112  using Dur = std::chrono::duration<Rep, Period>;
113 public:
114  Measure() noexcept : m_start(Clock::now()) {}
115 
117  void reset(TimePoint point = Clock::now()) noexcept { m_start = point; }
118 
120  Dur elapsed() const noexcept {
121  return std::chrono::duration_cast<Dur>(Clock::now() - m_start);
122  }
123 
125  Dur lap() noexcept {
126  auto now = Clock::now();
127  auto ret = std::chrono::duration_cast<Dur>(now - m_start);
128  reset(now);
129  return ret;
130  }
131 
133  template <typename Func, typename... Args>
134  static Dur profile(Func &&func, Args&&... args) {
135  Measure m;
136  std::forward<Func>(func)(std::forward<Args>(args)...);
137  return m.elapsed();
138  }
139 private:
140  TimePoint m_start;
141 };
142 
146 struct Unit {
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"
158  };
159 
160  auto unit = 0U;
161  auto bytesD = static_cast<double>(bytes);
162  while (bytesD >= 1024.) {
163  bytesD /= 1024.;
164  unit++;
165  }
166 
167  std::ostringstream s;
168  if (fixed)
169  s << std::fixed << std::setprecision(precision);
170  s << bytesD << ' ' << units[unit];
171  return s.str();
172  }
173 };
174 
175 }
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
requires static std::is_integral_v< I > std::optional< I > toNum(std::string_view str, int base=10) noexcept
Convert str to a number.
Definition: String.h:68
A helper for environment variables.
Definition: Misc.h:91
void reset(TimePoint point=Clock::now()) noexcept
Reset to count from point (or now)
Definition: Misc.h:117
Dur elapsed() const noexcept
Returns the duration it took from the construction (or last reset())
Definition: Misc.h:120
Parse a version string into numbers.
Definition: Misc.h:19
static unsigned versionSum(std::string_view version) noexcept
Sum up version parts as parsed from version.
Definition: Misc.h:41
bool operator()(std::string_view ver1, std::string_view ver2) const noexcept
Comparator for versions ver1 and ver2.
Definition: Misc.h:64
Compare versions, to be used as Compare in containers.
Definition: Misc.h:57
static auto versionSplit(std::string_view version) noexcept
Split version into a string array.
Definition: Misc.h:23
static Dur profile(Func &&func, Args &&... args)
Run func with args and return how long it took.
Definition: Misc.h:134
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
Measure elapsed times.
Definition: Misc.h:109
A helper to convert units.
Definition: Misc.h:146
Dur lap() noexcept
Returns the duration it took from the construction (or last reset()) and reset.
Definition: Misc.h:125
static unsigned versionPart(std::string_view version, bool rc=false) noexcept
Convert version into a number.
Definition: Misc.h:28
Definition: Color.h:8