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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
/*
* InspIRCd -- Internet Relay Chat Daemon
*
* Copyright (C) 2026 Sadie Powell <sadie@sadiepowell.dev>
*
* This file is part of InspIRCd. InspIRCd is free software: you can
* redistribute it and/or modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation, version 2.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <utility>
namespace insp
{
/** Converts a number into a string with a binary suffix (e.g. 3,292,549.12 -> 3.14 mebibytes).
* @param value The value to convert.
* @param bits Whether the value is bits or bytes.
*/
inline std::string binary_suffix(uintmax_t value, bool bits = false)
{
static const std::map<uintmax_t, const char*, std::greater<>> suffixes = {
{ 1ULL<<40, "tebi" },
{ 1ULL<<30, "gibi" },
{ 1ULL<<20, "mebi" },
{ 1ULL<<10, "kibi" },
};
const auto* unit = bits ? "bits" : "bytes";
for (const auto& [threshold, suffix] : suffixes)
{
if (value >= threshold)
return FMT::format("{:.5} {}{}", double(value) / threshold, suffix, unit);
}
return FMT::format("{} {}", value, unit);
}
/** Calculates the percentage of \p total that \p part makes up. This can be called without zero
* checks.
* @param part The partial value.
* @param total The total value.
*/
inline double percentage(double part, double total)
{
if (total == 0)
return 0.0;
return (part / total) * 100.0;
}
/** Adds two integers saturating the return type on overflow.
* @param lhs The first integer.
* @param rhs The second integer.
* @return The largest amount of the two integers added together that is representable by the
* return type on overflow.
*/
template <std::integral R, std::integral T1, std::integral T2>
constexpr R saturating_add(T1 lhs, T2 rhs) noexcept
{
using CommonType = std::common_type_t<R, T1, T2>;
if (std::cmp_greater(rhs, 0) && std::cmp_greater(lhs, std::numeric_limits<CommonType>::max() - rhs))
return std::numeric_limits<R>::max(); // Too big for R.
if (std::cmp_less(rhs, 0) && std::cmp_less(lhs, std::numeric_limits<CommonType>::min() - rhs))
return std::numeric_limits<R>::min(); // Too small for R.
const auto result = static_cast<CommonType>(lhs) + static_cast<CommonType>(rhs);
if constexpr (std::is_signed_v<T1> != std::is_signed_v<T2> || std::is_signed_v<R> != std::is_signed_v<CommonType>)
{
if (!std::in_range<R>(result))
{
return (std::cmp_less(lhs, 0) || std::cmp_less(rhs, 0))
? std::numeric_limits<R>::min()
: std::numeric_limits<R>::max();
}
}
return static_cast<R>(result);
}
/** Subtracts two integers saturating the return type on overflow.
* @param lhs The first integer.
* @param rhs The second integer.
* @return The largest amount of the two integers added together that is representable by the
* return type on underflow.
*/
template <std::integral R, std::integral T1, std::integral T2>
constexpr R saturating_sub(T1 lhs, T2 rhs) noexcept
{
using CommonType = std::common_type_t<R, T1, T2>;
if (std::cmp_greater(rhs, 0) && std::cmp_less(lhs, std::numeric_limits<CommonType>::min() + rhs))
return std::numeric_limits<R>::min(); // Too small for R.
if (std::cmp_less(rhs, 0) && std::cmp_greater(lhs, std::numeric_limits<CommonType>::max() + rhs))
return std::numeric_limits<R>::max(); // Too big for R.
const auto result = static_cast<CommonType>(lhs) - static_cast<CommonType>(rhs);
if constexpr (std::is_signed_v<T1> != std::is_signed_v<T2> || std::is_signed_v<R> != std::is_signed_v<CommonType>)
{
if (!std::in_range<R>(result))
{
return (std::cmp_less(lhs, 0) || std::cmp_greater(rhs, 0))
? std::numeric_limits<R>::min()
: std::numeric_limits<R>::max();
}
}
return static_cast<R>(result);
}
}
|