6 #include <fmt/format.h>
17 if(memory <
static_cast<uint64_t
>(1<<10))
19 else if(memory <
static_cast<uint64_t
>(1<<20))
20 return fmt::format(
"{:.1f} KiB",
static_cast<double>(memory) /
static_cast<uint64_t
>(1<<10));
21 else if(memory <
static_cast<uint64_t
>(1<<30))
22 return fmt::format(
"{:.1f} MiB",
static_cast<double>(memory) /
static_cast<uint64_t
>(1<<20));
23 else if(memory <
static_cast<uint64_t
>(1ull<<40))
24 return fmt::format(
"{:.1f} GiB",
static_cast<double>(memory) /
static_cast<uint64_t
>(1ull<<30));
26 return fmt::format(
"{:.1f} TiB",
static_cast<double>(memory) /
static_cast<uint64_t
>(1ull<<40));
32 static std::regex memoryRegex{R
"EOS((\d+)\s*(K|M|G|T|E|)i?B?)EOS"};
35 if(!std::regex_match(humanSize, match, memoryRegex))
37 fmt::print(stderr,
"Could not parse memory string '{}' - it should be something like '120B' or '10GB'\n",
43 std::string number = match[1].str();
44 std::string unit = match[2].str();
46 std::uint64_t multiplier = [&]() -> std::uint64_t {
52 case 'K':
return 1ULL << 10;
53 case 'M':
return 1ULL << 20;
54 case 'G':
return 1ULL << 30;
55 case 'T':
return 1ULL << 40;
56 case 'E':
return 1ULL << 50;
59 throw std::logic_error{
"I got regexes wrong :("};
62 return std::stoull(humanSize) * multiplier;