forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_readable.hh
42 lines (34 loc) · 1.09 KB
/
human_readable.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
* SPDX-License-Identifier: LicenseRef-ScyllaDB-Source-Available-1.0
*/
/*
* Copyright (C) 2020-present ScyllaDB
*/
#pragma once
#include <cstdint>
#include <fmt/core.h>
namespace utils {
struct human_readable_value {
uint16_t value; // [0, 1024)
char suffix; // 0 -> no suffix
};
/// Convert a size to a human readable representation.
///
/// The human-readable representation has at most 4 digits
/// and a letter appropriate to the power of two the number has to be multiplied
/// with to arrive to the original number (with some loss of precision).
/// The different powers of two are the conventional 2 ** (N * 10) variants:
/// * N=0: (B)ytes
/// * N=1: (K)bytes
/// * N=2: (M)bytes
/// * N=3: (G)bytes
/// * N=4: (T)bytes
///
/// Examples:
/// * 87665 will be converted to 87K
/// * 1024 will be converted to 1K
human_readable_value to_hr_size(uint64_t size);
} // namespace utils
template <> struct fmt::formatter<utils::human_readable_value> : fmt::formatter<string_view> {
auto format(const utils::human_readable_value&, fmt::format_context& ctx) const -> decltype(ctx.out());
};