20[[nodiscard]]
inline std::string formatted_bytes(
size_t const bytes)
25 size_t integer{bytes};
27 while (integer >> 10u && iterations < 6u)
34 auto first_decimal_position = [&]()
36 assert(iterations > 0u);
37 size_t decimal{bytes};
38 decimal -= integer << (iterations * 10u);
39 decimal >>= (iterations - 1u) * 10u;
40 decimal = decimal * 1000u / 1024u;
41 size_t const diff{decimal - (decimal / 100u) * 100u};
42 uint32_t
const round_up{diff >= 50u};
43 decimal += round_up * 100u - diff;
48 auto formatted_string = [&]()
50 static constexpr int8_t int_to_char_offset{
'0'};
51 size_t const decimal = iterations ? first_decimal_position() : 0u;
52 assert(decimal <= 10u);
56 else if (decimal < 10u)
57 return std::to_string(integer) +
'.' +
static_cast<char>(decimal + int_to_char_offset);
75 return "[Bytes]: " + formatted;
77 return "[KiB]: " + formatted;
79 return "[MiB]: " + formatted;
81 return "[GiB]: " + formatted;
83 return "[TiB]: " + formatted;
85 return "[PiB]: " + formatted;
87 return "[EiB]: " + formatted;