/root/bitcoin/src/util/strencodings.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <util/strencodings.h> |
7 | | |
8 | | #include <crypto/hex_base.h> |
9 | | #include <span.h> |
10 | | #include <util/check.h> |
11 | | #include <util/overflow.h> |
12 | | |
13 | | #include <limits> |
14 | | #include <optional> |
15 | | #include <sstream> |
16 | | #include <string> |
17 | | #include <vector> |
18 | | |
19 | | static const std::string CHARS_ALPHA_NUM = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; |
20 | | |
21 | | static const std::string SAFE_CHARS[] = |
22 | | { |
23 | | CHARS_ALPHA_NUM + " .,;-_/:?@()", // SAFE_CHARS_DEFAULT |
24 | | CHARS_ALPHA_NUM + " .,;-_?@", // SAFE_CHARS_UA_COMMENT |
25 | | CHARS_ALPHA_NUM + ".-_", // SAFE_CHARS_FILENAME |
26 | | CHARS_ALPHA_NUM + "!*'();:@&=+$,/?#[]-_.~%", // SAFE_CHARS_URI |
27 | | }; |
28 | | |
29 | | std::string SanitizeString(std::string_view str, int rule) |
30 | 0 | { |
31 | 0 | std::string result; |
32 | 0 | for (char c : str) { Branch (32:17): [True: 0, False: 0]
|
33 | 0 | if (SAFE_CHARS[rule].find(c) != std::string::npos) { Branch (33:13): [True: 0, False: 0]
|
34 | 0 | result.push_back(c); |
35 | 0 | } |
36 | 0 | } |
37 | 0 | return result; |
38 | 0 | } |
39 | | |
40 | | bool IsHex(std::string_view str) |
41 | 0 | { |
42 | 0 | for (char c : str) { Branch (42:17): [True: 0, False: 0]
|
43 | 0 | if (HexDigit(c) < 0) return false; Branch (43:13): [True: 0, False: 0]
|
44 | 0 | } |
45 | 0 | return (str.size() > 0) && (str.size()%2 == 0); Branch (45:12): [True: 0, False: 0]
Branch (45:32): [True: 0, False: 0]
|
46 | 0 | } |
47 | | |
48 | | template <typename Byte> |
49 | | std::optional<std::vector<Byte>> TryParseHex(std::string_view str) |
50 | 0 | { |
51 | 0 | std::vector<Byte> vch; |
52 | 0 | vch.reserve(str.size() / 2); // two hex characters form a single byte |
53 | |
|
54 | 0 | auto it = str.begin(); |
55 | 0 | while (it != str.end()) { Branch (55:12): [True: 0, False: 0]
Branch (55:12): [True: 0, False: 0]
|
56 | 0 | if (IsSpace(*it)) { Branch (56:13): [True: 0, False: 0]
Branch (56:13): [True: 0, False: 0]
|
57 | 0 | ++it; |
58 | 0 | continue; |
59 | 0 | } |
60 | 0 | auto c1 = HexDigit(*(it++)); |
61 | 0 | if (it == str.end()) return std::nullopt; Branch (61:13): [True: 0, False: 0]
Branch (61:13): [True: 0, False: 0]
|
62 | 0 | auto c2 = HexDigit(*(it++)); |
63 | 0 | if (c1 < 0 || c2 < 0) return std::nullopt; Branch (63:13): [True: 0, False: 0]
Branch (63:23): [True: 0, False: 0]
Branch (63:13): [True: 0, False: 0]
Branch (63:23): [True: 0, False: 0]
|
64 | 0 | vch.push_back(Byte(c1 << 4) | Byte(c2)); |
65 | 0 | } |
66 | 0 | return vch; |
67 | 0 | } Unexecuted instantiation: _Z11TryParseHexISt4byteESt8optionalISt6vectorIT_SaIS3_EEESt17basic_string_viewIcSt11char_traitsIcEE Unexecuted instantiation: _Z11TryParseHexIhESt8optionalISt6vectorIT_SaIS2_EEESt17basic_string_viewIcSt11char_traitsIcEE |
68 | | template std::optional<std::vector<std::byte>> TryParseHex(std::string_view); |
69 | | template std::optional<std::vector<uint8_t>> TryParseHex(std::string_view); |
70 | | |
71 | | bool SplitHostPort(std::string_view in, uint16_t& portOut, std::string& hostOut) |
72 | 0 | { |
73 | 0 | bool valid = false; |
74 | 0 | size_t colon = in.find_last_of(':'); |
75 | | // if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator |
76 | 0 | bool fHaveColon = colon != in.npos; |
77 | 0 | bool fBracketed = fHaveColon && (in[0] == '[' && in[colon - 1] == ']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe Branch (77:23): [True: 0, False: 0]
Branch (77:38): [True: 0, False: 0]
Branch (77:54): [True: 0, False: 0]
|
78 | 0 | bool fMultiColon{fHaveColon && colon != 0 && (in.find_last_of(':', colon - 1) != in.npos)}; Branch (78:22): [True: 0, False: 0]
Branch (78:36): [True: 0, False: 0]
Branch (78:50): [True: 0, False: 0]
|
79 | 0 | if (fHaveColon && (colon == 0 || fBracketed || !fMultiColon)) { Branch (79:9): [True: 0, False: 0]
Branch (79:24): [True: 0, False: 0]
Branch (79:38): [True: 0, False: 0]
Branch (79:52): [True: 0, False: 0]
|
80 | 0 | if (const auto n{ToIntegral<uint16_t>(in.substr(colon + 1))}) { Branch (80:24): [True: 0, False: 0]
|
81 | 0 | in = in.substr(0, colon); |
82 | 0 | portOut = *n; |
83 | 0 | valid = (portOut != 0); |
84 | 0 | } |
85 | 0 | } else { |
86 | 0 | valid = true; |
87 | 0 | } |
88 | 0 | if (in.size() > 0 && in[0] == '[' && in[in.size() - 1] == ']') { Branch (88:9): [True: 0, False: 0]
Branch (88:26): [True: 0, False: 0]
Branch (88:42): [True: 0, False: 0]
|
89 | 0 | hostOut = in.substr(1, in.size() - 2); |
90 | 0 | } else { |
91 | 0 | hostOut = in; |
92 | 0 | } |
93 | |
|
94 | 0 | return valid; |
95 | 0 | } |
96 | | |
97 | | std::string EncodeBase64(std::span<const unsigned char> input) |
98 | 0 | { |
99 | 0 | static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
100 | |
|
101 | 0 | std::string str; |
102 | 0 | str.reserve(CeilDiv(input.size(), 3u) * 4); |
103 | 0 | ConvertBits<8, 6, true>([&](int v) { str += pbase64[v]; }, input.begin(), input.end()); |
104 | 0 | while (str.size() % 4) str += '='; Branch (104:12): [True: 0, False: 0]
|
105 | 0 | return str; |
106 | 0 | } |
107 | | |
108 | | std::optional<std::vector<unsigned char>> DecodeBase64(std::string_view str) |
109 | 0 | { |
110 | 0 | static const int8_t decode64_table[256]{ |
111 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
112 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
113 | 0 | -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, |
114 | 0 | -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
115 | 0 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, |
116 | 0 | 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, |
117 | 0 | 49, 50, 51, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
118 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
119 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
120 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
121 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
122 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
123 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
124 | 0 | }; |
125 | |
|
126 | 0 | if (str.size() % 4 != 0) return {}; Branch (126:9): [True: 0, False: 0]
|
127 | | /* One or two = characters at the end are permitted. */ |
128 | 0 | if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1); Branch (128:9): [True: 0, False: 0]
Branch (128:28): [True: 0, False: 0]
|
129 | 0 | if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1); Branch (129:9): [True: 0, False: 0]
Branch (129:28): [True: 0, False: 0]
|
130 | |
|
131 | 0 | std::vector<unsigned char> ret; |
132 | 0 | ret.reserve((str.size() * 3) / 4); |
133 | 0 | bool valid = ConvertBits<6, 8, false>( |
134 | 0 | [&](unsigned char c) { ret.push_back(c); }, |
135 | 0 | str.begin(), str.end(), |
136 | 0 | [](char c) { return decode64_table[uint8_t(c)]; } |
137 | 0 | ); |
138 | 0 | if (!valid) return {}; Branch (138:9): [True: 0, False: 0]
|
139 | | |
140 | 0 | return ret; |
141 | 0 | } |
142 | | |
143 | | std::string EncodeBase32(std::span<const unsigned char> input, bool pad) |
144 | 0 | { |
145 | 0 | static const char *pbase32 = "abcdefghijklmnopqrstuvwxyz234567"; |
146 | |
|
147 | 0 | std::string str; |
148 | 0 | str.reserve(CeilDiv(input.size(), 5u) * 8); |
149 | 0 | ConvertBits<8, 5, true>([&](int v) { str += pbase32[v]; }, input.begin(), input.end()); |
150 | 0 | if (pad) { Branch (150:9): [True: 0, False: 0]
|
151 | 0 | while (str.size() % 8) { Branch (151:16): [True: 0, False: 0]
|
152 | 0 | str += '='; |
153 | 0 | } |
154 | 0 | } |
155 | 0 | return str; |
156 | 0 | } |
157 | | |
158 | | std::string EncodeBase32(std::string_view str, bool pad) |
159 | 0 | { |
160 | 0 | return EncodeBase32(MakeUCharSpan(str), pad); |
161 | 0 | } |
162 | | |
163 | | std::optional<std::vector<unsigned char>> DecodeBase32(std::string_view str) |
164 | 0 | { |
165 | 0 | static const int8_t decode32_table[256]{ |
166 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
167 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
168 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, -1, -1, -1, -1, |
169 | 0 | -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
170 | 0 | 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 0, 1, 2, |
171 | 0 | 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, |
172 | 0 | 23, 24, 25, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
173 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
174 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
175 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
176 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
177 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, |
178 | 0 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 |
179 | 0 | }; |
180 | |
|
181 | 0 | if (str.size() % 8 != 0) return {}; Branch (181:9): [True: 0, False: 0]
|
182 | | /* 1, 3, 4, or 6 padding '=' suffix characters are permitted. */ |
183 | 0 | if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1); Branch (183:9): [True: 0, False: 0]
Branch (183:28): [True: 0, False: 0]
|
184 | 0 | if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2); Branch (184:9): [True: 0, False: 0]
Branch (184:28): [True: 0, False: 0]
|
185 | 0 | if (str.size() >= 1 && str.back() == '=') str.remove_suffix(1); Branch (185:9): [True: 0, False: 0]
Branch (185:28): [True: 0, False: 0]
|
186 | 0 | if (str.size() >= 2 && str.substr(str.size() - 2) == "==") str.remove_suffix(2); Branch (186:9): [True: 0, False: 0]
Branch (186:28): [True: 0, False: 0]
|
187 | |
|
188 | 0 | std::vector<unsigned char> ret; |
189 | 0 | ret.reserve((str.size() * 5) / 8); |
190 | 0 | bool valid = ConvertBits<5, 8, false>( |
191 | 0 | [&](unsigned char c) { ret.push_back(c); }, |
192 | 0 | str.begin(), str.end(), |
193 | 0 | [](char c) { return decode32_table[uint8_t(c)]; } |
194 | 0 | ); |
195 | |
|
196 | 0 | if (!valid) return {}; Branch (196:9): [True: 0, False: 0]
|
197 | | |
198 | 0 | return ret; |
199 | 0 | } |
200 | | |
201 | | std::string FormatParagraph(std::string_view in, size_t width, size_t indent) |
202 | 0 | { |
203 | 0 | assert(width >= indent); Branch (203:5): [True: 0, False: 0]
|
204 | 0 | std::stringstream out; |
205 | 0 | size_t ptr = 0; |
206 | 0 | size_t indented = 0; |
207 | 0 | while (ptr < in.size()) Branch (207:12): [True: 0, False: 0]
|
208 | 0 | { |
209 | 0 | size_t lineend = in.find_first_of('\n', ptr); |
210 | 0 | if (lineend == std::string::npos) { Branch (210:13): [True: 0, False: 0]
|
211 | 0 | lineend = in.size(); |
212 | 0 | } |
213 | 0 | const size_t linelen = lineend - ptr; |
214 | 0 | const size_t rem_width = width - indented; |
215 | 0 | if (linelen <= rem_width) { Branch (215:13): [True: 0, False: 0]
|
216 | 0 | out << in.substr(ptr, linelen + 1); |
217 | 0 | ptr = lineend + 1; |
218 | 0 | indented = 0; |
219 | 0 | } else { |
220 | 0 | size_t finalspace = in.find_last_of(" \n", ptr + rem_width); |
221 | 0 | if (finalspace == std::string::npos || finalspace < ptr) { Branch (221:17): [True: 0, False: 0]
Branch (221:52): [True: 0, False: 0]
|
222 | | // No place to break; just include the entire word and move on |
223 | 0 | finalspace = in.find_first_of("\n ", ptr); |
224 | 0 | if (finalspace == std::string::npos) { Branch (224:21): [True: 0, False: 0]
|
225 | | // End of the string, just add it and break |
226 | 0 | out << in.substr(ptr); |
227 | 0 | break; |
228 | 0 | } |
229 | 0 | } |
230 | 0 | out << in.substr(ptr, finalspace - ptr) << "\n"; |
231 | 0 | if (in[finalspace] == '\n') { Branch (231:17): [True: 0, False: 0]
|
232 | 0 | indented = 0; |
233 | 0 | } else if (indent) { Branch (233:24): [True: 0, False: 0]
|
234 | 0 | out << std::string(indent, ' '); |
235 | 0 | indented = indent; |
236 | 0 | } |
237 | 0 | ptr = finalspace + 1; |
238 | 0 | } |
239 | 0 | } |
240 | 0 | return out.str(); |
241 | 0 | } |
242 | | |
243 | | /** Upper bound for mantissa. |
244 | | * 10^18-1 is the largest arbitrary decimal that will fit in a signed 64-bit integer. |
245 | | * Larger integers cannot consist of arbitrary combinations of 0-9: |
246 | | * |
247 | | * 999999999999999999 1^18-1 |
248 | | * 9223372036854775807 (1<<63)-1 (max int64_t) |
249 | | * 9999999999999999999 1^19-1 (would overflow) |
250 | | */ |
251 | | static const int64_t UPPER_BOUND = 1000000000000000000LL - 1LL; |
252 | | |
253 | | /** Helper function for ParseFixedPoint */ |
254 | | static inline bool ProcessMantissaDigit(char ch, int64_t &mantissa, int &mantissa_tzeros) |
255 | 0 | { |
256 | 0 | if(ch == '0') Branch (256:8): [True: 0, False: 0]
|
257 | 0 | ++mantissa_tzeros; |
258 | 0 | else { |
259 | 0 | for (int i=0; i<=mantissa_tzeros; ++i) { Branch (259:23): [True: 0, False: 0]
|
260 | 0 | if (mantissa > (UPPER_BOUND / 10LL)) Branch (260:17): [True: 0, False: 0]
|
261 | 0 | return false; /* overflow */ |
262 | 0 | mantissa *= 10; |
263 | 0 | } |
264 | 0 | mantissa += ch - '0'; |
265 | 0 | mantissa_tzeros = 0; |
266 | 0 | } |
267 | 0 | return true; |
268 | 0 | } |
269 | | |
270 | | bool ParseFixedPoint(std::string_view val, int decimals, int64_t *amount_out) |
271 | 0 | { |
272 | 0 | int64_t mantissa = 0; |
273 | 0 | int64_t exponent = 0; |
274 | 0 | int mantissa_tzeros = 0; |
275 | 0 | bool mantissa_sign = false; |
276 | 0 | bool exponent_sign = false; |
277 | 0 | int ptr = 0; |
278 | 0 | int end = val.size(); |
279 | 0 | int point_ofs = 0; |
280 | |
|
281 | 0 | if (ptr < end && val[ptr] == '-') { Branch (281:9): [True: 0, False: 0]
Branch (281:22): [True: 0, False: 0]
|
282 | 0 | mantissa_sign = true; |
283 | 0 | ++ptr; |
284 | 0 | } |
285 | 0 | if (ptr < end) Branch (285:9): [True: 0, False: 0]
|
286 | 0 | { |
287 | 0 | if (val[ptr] == '0') { Branch (287:13): [True: 0, False: 0]
|
288 | | /* pass single 0 */ |
289 | 0 | ++ptr; |
290 | 0 | } else if (val[ptr] >= '1' && val[ptr] <= '9') { Branch (290:20): [True: 0, False: 0]
Branch (290:39): [True: 0, False: 0]
|
291 | 0 | while (ptr < end && IsDigit(val[ptr])) { Branch (291:20): [True: 0, False: 0]
Branch (291:33): [True: 0, False: 0]
|
292 | 0 | if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) Branch (292:21): [True: 0, False: 0]
|
293 | 0 | return false; /* overflow */ |
294 | 0 | ++ptr; |
295 | 0 | } |
296 | 0 | } else return false; /* missing expected digit */ |
297 | 0 | } else return false; /* empty string or loose '-' */ |
298 | 0 | if (ptr < end && val[ptr] == '.') Branch (298:9): [True: 0, False: 0]
Branch (298:22): [True: 0, False: 0]
|
299 | 0 | { |
300 | 0 | ++ptr; |
301 | 0 | if (ptr < end && IsDigit(val[ptr])) Branch (301:13): [True: 0, False: 0]
Branch (301:26): [True: 0, False: 0]
|
302 | 0 | { |
303 | 0 | while (ptr < end && IsDigit(val[ptr])) { Branch (303:20): [True: 0, False: 0]
Branch (303:33): [True: 0, False: 0]
|
304 | 0 | if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) Branch (304:21): [True: 0, False: 0]
|
305 | 0 | return false; /* overflow */ |
306 | 0 | ++ptr; |
307 | 0 | ++point_ofs; |
308 | 0 | } |
309 | 0 | } else return false; /* missing expected digit */ |
310 | 0 | } |
311 | 0 | if (ptr < end && (val[ptr] == 'e' || val[ptr] == 'E')) Branch (311:9): [True: 0, False: 0]
Branch (311:23): [True: 0, False: 0]
Branch (311:42): [True: 0, False: 0]
|
312 | 0 | { |
313 | 0 | ++ptr; |
314 | 0 | if (ptr < end && val[ptr] == '+') Branch (314:13): [True: 0, False: 0]
Branch (314:26): [True: 0, False: 0]
|
315 | 0 | ++ptr; |
316 | 0 | else if (ptr < end && val[ptr] == '-') { Branch (316:18): [True: 0, False: 0]
Branch (316:31): [True: 0, False: 0]
|
317 | 0 | exponent_sign = true; |
318 | 0 | ++ptr; |
319 | 0 | } |
320 | 0 | if (ptr < end && IsDigit(val[ptr])) { Branch (320:13): [True: 0, False: 0]
Branch (320:26): [True: 0, False: 0]
|
321 | 0 | while (ptr < end && IsDigit(val[ptr])) { Branch (321:20): [True: 0, False: 0]
Branch (321:33): [True: 0, False: 0]
|
322 | 0 | if (exponent > (UPPER_BOUND / 10LL)) Branch (322:21): [True: 0, False: 0]
|
323 | 0 | return false; /* overflow */ |
324 | 0 | exponent = exponent * 10 + val[ptr] - '0'; |
325 | 0 | ++ptr; |
326 | 0 | } |
327 | 0 | } else return false; /* missing expected digit */ |
328 | 0 | } |
329 | 0 | if (ptr != end) Branch (329:9): [True: 0, False: 0]
|
330 | 0 | return false; /* trailing garbage */ |
331 | | |
332 | | /* finalize exponent */ |
333 | 0 | if (exponent_sign) Branch (333:9): [True: 0, False: 0]
|
334 | 0 | exponent = -exponent; |
335 | 0 | exponent = exponent - point_ofs + mantissa_tzeros; |
336 | | |
337 | | /* finalize mantissa */ |
338 | 0 | if (mantissa_sign) Branch (338:9): [True: 0, False: 0]
|
339 | 0 | mantissa = -mantissa; |
340 | | |
341 | | /* convert to one 64-bit fixed-point value */ |
342 | 0 | exponent += decimals; |
343 | 0 | if (exponent < 0) Branch (343:9): [True: 0, False: 0]
|
344 | 0 | return false; /* cannot represent values smaller than 10^-decimals */ |
345 | 0 | if (exponent >= 18) Branch (345:9): [True: 0, False: 0]
|
346 | 0 | return false; /* cannot represent values larger than or equal to 10^(18-decimals) */ |
347 | | |
348 | 0 | for (int i=0; i < exponent; ++i) { Branch (348:19): [True: 0, False: 0]
|
349 | 0 | if (mantissa > (UPPER_BOUND / 10LL) || mantissa < -(UPPER_BOUND / 10LL)) Branch (349:13): [True: 0, False: 0]
Branch (349:48): [True: 0, False: 0]
|
350 | 0 | return false; /* overflow */ |
351 | 0 | mantissa *= 10; |
352 | 0 | } |
353 | 0 | if (mantissa > UPPER_BOUND || mantissa < -UPPER_BOUND) Branch (353:9): [True: 0, False: 0]
Branch (353:35): [True: 0, False: 0]
|
354 | 0 | return false; /* overflow */ |
355 | | |
356 | 0 | if (amount_out) Branch (356:9): [True: 0, False: 0]
|
357 | 0 | *amount_out = mantissa; |
358 | |
|
359 | 0 | return true; |
360 | 0 | } |
361 | | |
362 | | std::string ToLower(std::string_view str) |
363 | 0 | { |
364 | 0 | std::string r; |
365 | 0 | r.reserve(str.size()); |
366 | 0 | for (auto ch : str) r += ToLower(ch); Branch (366:18): [True: 0, False: 0]
|
367 | 0 | return r; |
368 | 0 | } |
369 | | |
370 | | std::string ToUpper(std::string_view str) |
371 | 0 | { |
372 | 0 | std::string r; |
373 | 0 | r.reserve(str.size()); |
374 | 0 | for (auto ch : str) r += ToUpper(ch); Branch (374:18): [True: 0, False: 0]
|
375 | 0 | return r; |
376 | 0 | } |
377 | | |
378 | | std::string Capitalize(std::string str) |
379 | 0 | { |
380 | 0 | if (str.empty()) return str; Branch (380:9): [True: 0, False: 0]
|
381 | 0 | str[0] = ToUpper(str.front()); |
382 | 0 | return str; |
383 | 0 | } |
384 | | |
385 | | std::optional<uint64_t> ParseByteUnits(std::string_view str, ByteUnit default_multiplier) |
386 | 0 | { |
387 | 0 | if (str.empty()) { Branch (387:9): [True: 0, False: 0]
|
388 | 0 | return std::nullopt; |
389 | 0 | } |
390 | 0 | auto multiplier = default_multiplier; |
391 | 0 | char unit = str.back(); |
392 | 0 | switch (unit) { |
393 | 0 | case 'k': Branch (393:5): [True: 0, False: 0]
|
394 | 0 | multiplier = ByteUnit::k; |
395 | 0 | break; |
396 | 0 | case 'K': Branch (396:5): [True: 0, False: 0]
|
397 | 0 | multiplier = ByteUnit::K; |
398 | 0 | break; |
399 | 0 | case 'm': Branch (399:5): [True: 0, False: 0]
|
400 | 0 | multiplier = ByteUnit::m; |
401 | 0 | break; |
402 | 0 | case 'M': Branch (402:5): [True: 0, False: 0]
|
403 | 0 | multiplier = ByteUnit::M; |
404 | 0 | break; |
405 | 0 | case 'g': Branch (405:5): [True: 0, False: 0]
|
406 | 0 | multiplier = ByteUnit::g; |
407 | 0 | break; |
408 | 0 | case 'G': Branch (408:5): [True: 0, False: 0]
|
409 | 0 | multiplier = ByteUnit::G; |
410 | 0 | break; |
411 | 0 | case 't': Branch (411:5): [True: 0, False: 0]
|
412 | 0 | multiplier = ByteUnit::t; |
413 | 0 | break; |
414 | 0 | case 'T': Branch (414:5): [True: 0, False: 0]
|
415 | 0 | multiplier = ByteUnit::T; |
416 | 0 | break; |
417 | 0 | default: Branch (417:5): [True: 0, False: 0]
|
418 | 0 | unit = 0; |
419 | 0 | break; |
420 | 0 | } |
421 | | |
422 | 0 | uint64_t unit_amount = static_cast<uint64_t>(multiplier); |
423 | 0 | auto parsed_num = ToIntegral<uint64_t>(unit ? str.substr(0, str.size() - 1) : str); Branch (423:44): [True: 0, False: 0]
|
424 | 0 | if (!parsed_num || parsed_num > std::numeric_limits<uint64_t>::max() / unit_amount) { // check overflow Branch (424:9): [True: 0, False: 0]
Branch (424:9): [True: 0, False: 0]
Branch (424:24): [True: 0, False: 0]
|
425 | 0 | return std::nullopt; |
426 | 0 | } |
427 | 0 | return *parsed_num * unit_amount; |
428 | 0 | } |
429 | | |
430 | | bool CaseInsensitiveEqual(std::string_view s1, std::string_view s2) |
431 | 0 | { |
432 | 0 | if (s1.size() != s2.size()) return false; Branch (432:9): [True: 0, False: 0]
|
433 | 0 | for (size_t i = 0; i < s1.size(); ++i) { Branch (433:24): [True: 0, False: 0]
|
434 | 0 | char c1 = s1[i]; |
435 | 0 | if (c1 >= 'A' && c1 <= 'Z') c1 -= ('A' - 'a'); Branch (435:13): [True: 0, False: 0]
Branch (435:26): [True: 0, False: 0]
|
436 | 0 | char c2 = s2[i]; |
437 | 0 | if (c2 >= 'A' && c2 <= 'Z') c2 -= ('A' - 'a'); Branch (437:13): [True: 0, False: 0]
Branch (437:26): [True: 0, False: 0]
|
438 | 0 | if (c1 != c2) return false; Branch (438:13): [True: 0, False: 0]
|
439 | 0 | } |
440 | 0 | return true; |
441 | 0 | } |