roar
Loading...
Searching...
No Matches
bytes_to_human_readable.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <cstdint>
5#include <cstddef>
6#include <cmath>
7
8namespace Roar::Detail
9{
10 template <unsigned Base>
11 std::string bytesToHumanReadable(std::uint64_t bytes)
12 {
13 const auto log = std::log(bytes) / std::log(Base);
14 unsigned floored = static_cast<unsigned>(log);
15 for (unsigned i = 0; i < floored; ++i)
16 bytes /= Base;
17
18 if constexpr (Base == 1024)
19 {
20 constexpr const char units[8][4]{"B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB"};
21 return std::to_string(bytes) + " " + units[floored];
22 }
23 else
24 {
25 constexpr const char units[8][3]{"b", "kb", "mb", "gb", "tb", "pb", "eb", "zb"};
26 return std::to_string(bytes) + " " + units[floored];
27 }
28 }
29}
Definition range_file_body.hpp:18
std::string bytesToHumanReadable(std::uint64_t bytes)
Definition bytes_to_human_readable.hpp:11