/root/doris/be/src/gutil/strings/numbers.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2010 Google Inc. All Rights Reserved. |
2 | | // Maintainer: mec@google.com (Michael Chastain) |
3 | | // |
4 | | // Convert strings to numbers or numbers to strings. |
5 | | |
6 | | #pragma once |
7 | | |
8 | | #include <stddef.h> |
9 | | #include <time.h> |
10 | | #include <stdint.h> |
11 | | #include <functional> |
12 | | |
13 | | using std::less; |
14 | | #include <limits> |
15 | | |
16 | | using std::numeric_limits; |
17 | | #include <string> |
18 | | |
19 | | using std::string; |
20 | | #include <vector> |
21 | | |
22 | | using std::vector; |
23 | | |
24 | | #include "gutil/integral_types.h" |
25 | | // IWYU pragma: no_include <butil/macros.h> |
26 | | #include "gutil/macros.h" // IWYU pragma: keep |
27 | | #include "gutil/port.h" |
28 | | #include "gutil/stringprintf.h" |
29 | | |
30 | | // START DOXYGEN NumbersFunctions grouping |
31 | | /* @defgroup NumbersFunctions |
32 | | * @{ */ |
33 | | |
34 | | // Convert a fingerprint to 16 hex digits. |
35 | | string Uint64ToString(uint64 fp); |
36 | | |
37 | | // Convert strings to numeric values, with strict error checking. |
38 | | // Leading and trailing spaces are allowed. |
39 | | // Negative inputs are not allowed for unsigned ints (unlike strtoul). |
40 | | // Numbers must be in base 10; see the _base variants below for other bases. |
41 | | // Returns false on errors (including overflow/underflow). |
42 | | bool safe_strto32(const char* str, int32* value); |
43 | | bool safe_strto64(const char* str, int64* value); |
44 | | bool safe_strtou32(const char* str, uint32* value); |
45 | | bool safe_strtou64(const char* str, uint64* value); |
46 | | // Convert strings to floating point values. |
47 | | // Leading and trailing spaces are allowed. |
48 | | // Values may be rounded on over- and underflow. |
49 | | bool safe_strtof(const char* str, float* value); |
50 | | bool safe_strtod(const char* str, double* value); |
51 | | |
52 | | bool safe_strto32(const string& str, int32* value); |
53 | | bool safe_strto64(const string& str, int64* value); |
54 | | bool safe_strtou32(const string& str, uint32* value); |
55 | | bool safe_strtou64(const string& str, uint64* value); |
56 | | bool safe_strtof(const string& str, float* value); |
57 | | bool safe_strtod(const string& str, double* value); |
58 | | |
59 | | // Parses buffer_size many characters from startptr into value. |
60 | | bool safe_strto32(const char* startptr, int buffer_size, int32* value); |
61 | | bool safe_strto64(const char* startptr, int buffer_size, int64* value); |
62 | | |
63 | | // Parses with a fixed base between 2 and 36. For base 16, leading "0x" is ok. |
64 | | // If base is set to 0, its value is inferred from the beginning of str: |
65 | | // "0x" means base 16, "0" means base 8, otherwise base 10 is used. |
66 | | bool safe_strto32_base(const char* str, int32* value, int base); |
67 | | bool safe_strto64_base(const char* str, int64* value, int base); |
68 | | bool safe_strtou32_base(const char* str, uint32* value, int base); |
69 | | bool safe_strtou64_base(const char* str, uint64* value, int base); |
70 | | |
71 | | bool safe_strto32_base(const string& str, int32* value, int base); |
72 | | bool safe_strto64_base(const string& str, int64* value, int base); |
73 | | bool safe_strtou32_base(const string& str, uint32* value, int base); |
74 | | bool safe_strtou64_base(const string& str, uint64* value, int base); |
75 | | |
76 | | bool safe_strto32_base(const char* startptr, int buffer_size, int32* value, int base); |
77 | | bool safe_strto64_base(const char* startptr, int buffer_size, int64* value, int base); |
78 | | |
79 | | // u64tostr_base36() |
80 | | // The inverse of safe_strtou64_base, converts the number agument to |
81 | | // a string representation in base-36. |
82 | | // Conversion fails if buffer is too small to to hold the string and |
83 | | // terminating NUL. |
84 | | // Returns number of bytes written, not including terminating NUL. |
85 | | // Return value 0 indicates error. |
86 | | size_t u64tostr_base36(uint64 number, size_t buf_size, char* buffer); |
87 | | |
88 | | // Similar to atoi(s), except s could be like "16k", "32M", "2G", "4t". |
89 | | uint64 atoi_kmgt(const char* s); |
90 | 0 | inline uint64 atoi_kmgt(const string& s) { |
91 | 0 | return atoi_kmgt(s.c_str()); |
92 | 0 | } |
93 | | |
94 | | // ---------------------------------------------------------------------- |
95 | | // FastIntToBuffer() |
96 | | // FastHexToBuffer() |
97 | | // FastHex64ToBuffer() |
98 | | // FastHex32ToBuffer() |
99 | | // FastTimeToBuffer() |
100 | | // These are intended for speed. FastIntToBuffer() assumes the |
101 | | // integer is non-negative. FastHexToBuffer() puts output in |
102 | | // hex rather than decimal. FastTimeToBuffer() puts the output |
103 | | // into RFC822 format. |
104 | | // |
105 | | // FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format, |
106 | | // padded to exactly 16 bytes (plus one byte for '\0') |
107 | | // |
108 | | // FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format, |
109 | | // padded to exactly 8 bytes (plus one byte for '\0') |
110 | | // |
111 | | // All functions take the output buffer as an arg. FastInt() uses |
112 | | // at most 22 bytes, FastTime() uses exactly 30 bytes. They all |
113 | | // return a pointer to the beginning of the output, which for |
114 | | // FastHex() may not be the beginning of the input buffer. (For |
115 | | // all others, we guarantee that it is.) |
116 | | // |
117 | | // NOTE: In 64-bit land, sizeof(time_t) is 8, so it is possible |
118 | | // to pass to FastTimeToBuffer() a time whose year cannot be |
119 | | // represented in 4 digits. In this case, the output buffer |
120 | | // will contain the string "Invalid:<value>" |
121 | | // ---------------------------------------------------------------------- |
122 | | |
123 | | // Previously documented minimums -- the buffers provided must be at least this |
124 | | // long, though these numbers are subject to change: |
125 | | // Int32, UInt32: 12 bytes |
126 | | // Int64, UInt64, Hex: 22 bytes |
127 | | // Time: 30 bytes |
128 | | // Hex32: 9 bytes |
129 | | // Hex64: 17 bytes |
130 | | // Use kFastToBufferSize rather than hardcoding constants. |
131 | | static const int kFastToBufferSize = 32; |
132 | | |
133 | | char* FastInt32ToBuffer(int32 i, char* buffer); |
134 | | char* FastInt64ToBuffer(int64 i, char* buffer); |
135 | | char* FastUInt32ToBuffer(uint32 i, char* buffer); |
136 | | char* FastUInt64ToBuffer(uint64 i, char* buffer); |
137 | | char* FastHexToBuffer(int i, char* buffer) MUST_USE_RESULT; |
138 | | char* FastTimeToBuffer(time_t t, char* buffer); |
139 | | char* FastHex64ToBuffer(uint64 i, char* buffer); |
140 | | char* FastHex32ToBuffer(uint32 i, char* buffer); |
141 | | |
142 | | // at least 22 bytes long |
143 | 0 | inline char* FastIntToBuffer(int i, char* buffer) { |
144 | 0 | return (sizeof(i) == 4 ? FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer)); |
145 | 0 | } |
146 | 0 | inline char* FastUIntToBuffer(unsigned int i, char* buffer) { |
147 | 0 | return (sizeof(i) == 4 ? FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer)); |
148 | 0 | } |
149 | | |
150 | | // ---------------------------------------------------------------------- |
151 | | // FastInt32ToBufferLeft() |
152 | | // FastUInt32ToBufferLeft() |
153 | | // FastInt64ToBufferLeft() |
154 | | // FastUInt64ToBufferLeft() |
155 | | // |
156 | | // Like the Fast*ToBuffer() functions above, these are intended for speed. |
157 | | // Unlike the Fast*ToBuffer() functions, however, these functions write |
158 | | // their output to the beginning of the buffer (hence the name, as the |
159 | | // output is left-aligned). The caller is responsible for ensuring that |
160 | | // the buffer has enough space to hold the output. |
161 | | // |
162 | | // Returns a pointer to the end of the string (i.e. the null character |
163 | | // terminating the string). |
164 | | // ---------------------------------------------------------------------- |
165 | | |
166 | | char* FastInt32ToBufferLeft(int32 i, char* buffer); // at least 12 bytes |
167 | | char* FastUInt32ToBufferLeft(uint32 i, char* buffer); // at least 12 bytes |
168 | | char* FastInt64ToBufferLeft(int64 i, char* buffer); // at least 22 bytes |
169 | | char* FastUInt64ToBufferLeft(uint64 i, char* buffer); // at least 22 bytes |
170 | | |
171 | | // Just define these in terms of the above. |
172 | 0 | inline char* FastUInt32ToBuffer(uint32 i, char* buffer) { |
173 | 0 | FastUInt32ToBufferLeft(i, buffer); |
174 | 0 | return buffer; |
175 | 0 | } |
176 | 0 | inline char* FastUInt64ToBuffer(uint64 i, char* buffer) { |
177 | 0 | FastUInt64ToBufferLeft(i, buffer); |
178 | 0 | return buffer; |
179 | 0 | } |
180 | | |
181 | | // ---------------------------------------------------------------------- |
182 | | // HexDigitsPrefix() |
183 | | // returns 1 if buf is prefixed by "num_digits" of hex digits |
184 | | // returns 0 otherwise. |
185 | | // The function checks for '\0' for string termination. |
186 | | // ---------------------------------------------------------------------- |
187 | | int HexDigitsPrefix(const char* buf, int num_digits); |
188 | | |
189 | | // ---------------------------------------------------------------------- |
190 | | // ConsumeStrayLeadingZeroes |
191 | | // Eliminates all leading zeroes (unless the string itself is composed |
192 | | // of nothing but zeroes, in which case one is kept: 0...0 becomes 0). |
193 | | void ConsumeStrayLeadingZeroes(string* str); |
194 | | |
195 | | // ---------------------------------------------------------------------- |
196 | | // ParseLeadingInt32Value |
197 | | // A simple parser for int32 values. Returns the parsed value |
198 | | // if a valid integer is found; else returns deflt. It does not |
199 | | // check if str is entirely consumed. |
200 | | // This cannot handle decimal numbers with leading 0s, since they will be |
201 | | // treated as octal. If you know it's decimal, use ParseLeadingDec32Value. |
202 | | // -------------------------------------------------------------------- |
203 | | int32 ParseLeadingInt32Value(const char* str, int32 deflt); |
204 | 0 | inline int32 ParseLeadingInt32Value(const string& str, int32 deflt) { |
205 | 0 | return ParseLeadingInt32Value(str.c_str(), deflt); |
206 | 0 | } |
207 | | |
208 | | // ParseLeadingUInt32Value |
209 | | // A simple parser for uint32 values. Returns the parsed value |
210 | | // if a valid integer is found; else returns deflt. It does not |
211 | | // check if str is entirely consumed. |
212 | | // This cannot handle decimal numbers with leading 0s, since they will be |
213 | | // treated as octal. If you know it's decimal, use ParseLeadingUDec32Value. |
214 | | // -------------------------------------------------------------------- |
215 | | uint32 ParseLeadingUInt32Value(const char* str, uint32 deflt); |
216 | 0 | inline uint32 ParseLeadingUInt32Value(const string& str, uint32 deflt) { |
217 | 0 | return ParseLeadingUInt32Value(str.c_str(), deflt); |
218 | 0 | } |
219 | | |
220 | | // ---------------------------------------------------------------------- |
221 | | // ParseLeadingDec32Value |
222 | | // A simple parser for decimal int32 values. Returns the parsed value |
223 | | // if a valid integer is found; else returns deflt. It does not |
224 | | // check if str is entirely consumed. |
225 | | // The string passed in is treated as *10 based*. |
226 | | // This can handle strings with leading 0s. |
227 | | // See also: ParseLeadingDec64Value |
228 | | // -------------------------------------------------------------------- |
229 | | int32 ParseLeadingDec32Value(const char* str, int32 deflt); |
230 | 0 | inline int32 ParseLeadingDec32Value(const string& str, int32 deflt) { |
231 | 0 | return ParseLeadingDec32Value(str.c_str(), deflt); |
232 | 0 | } |
233 | | |
234 | | // ParseLeadingUDec32Value |
235 | | // A simple parser for decimal uint32 values. Returns the parsed value |
236 | | // if a valid integer is found; else returns deflt. It does not |
237 | | // check if str is entirely consumed. |
238 | | // The string passed in is treated as *10 based*. |
239 | | // This can handle strings with leading 0s. |
240 | | // See also: ParseLeadingUDec64Value |
241 | | // -------------------------------------------------------------------- |
242 | | uint32 ParseLeadingUDec32Value(const char* str, uint32 deflt); |
243 | 0 | inline uint32 ParseLeadingUDec32Value(const string& str, uint32 deflt) { |
244 | 0 | return ParseLeadingUDec32Value(str.c_str(), deflt); |
245 | 0 | } |
246 | | |
247 | | // ---------------------------------------------------------------------- |
248 | | // ParseLeadingUInt64Value |
249 | | // ParseLeadingInt64Value |
250 | | // ParseLeadingHex64Value |
251 | | // ParseLeadingDec64Value |
252 | | // ParseLeadingUDec64Value |
253 | | // A simple parser for long long values. |
254 | | // Returns the parsed value if a |
255 | | // valid integer is found; else returns deflt |
256 | | // -------------------------------------------------------------------- |
257 | | uint64 ParseLeadingUInt64Value(const char* str, uint64 deflt); |
258 | 0 | inline uint64 ParseLeadingUInt64Value(const string& str, uint64 deflt) { |
259 | 0 | return ParseLeadingUInt64Value(str.c_str(), deflt); |
260 | 0 | } |
261 | | int64 ParseLeadingInt64Value(const char* str, int64 deflt); |
262 | 0 | inline int64 ParseLeadingInt64Value(const string& str, int64 deflt) { |
263 | 0 | return ParseLeadingInt64Value(str.c_str(), deflt); |
264 | 0 | } |
265 | | uint64 ParseLeadingHex64Value(const char* str, uint64 deflt); |
266 | 0 | inline uint64 ParseLeadingHex64Value(const string& str, uint64 deflt) { |
267 | 0 | return ParseLeadingHex64Value(str.c_str(), deflt); |
268 | 0 | } |
269 | | int64 ParseLeadingDec64Value(const char* str, int64 deflt); |
270 | 0 | inline int64 ParseLeadingDec64Value(const string& str, int64 deflt) { |
271 | 0 | return ParseLeadingDec64Value(str.c_str(), deflt); |
272 | 0 | } |
273 | | uint64 ParseLeadingUDec64Value(const char* str, uint64 deflt); |
274 | 0 | inline uint64 ParseLeadingUDec64Value(const string& str, uint64 deflt) { |
275 | 0 | return ParseLeadingUDec64Value(str.c_str(), deflt); |
276 | 0 | } |
277 | | |
278 | | // ---------------------------------------------------------------------- |
279 | | // ParseLeadingDoubleValue |
280 | | // A simple parser for double values. Returns the parsed value |
281 | | // if a valid double is found; else returns deflt. It does not |
282 | | // check if str is entirely consumed. |
283 | | // -------------------------------------------------------------------- |
284 | | double ParseLeadingDoubleValue(const char* str, double deflt); |
285 | 0 | inline double ParseLeadingDoubleValue(const string& str, double deflt) { |
286 | 0 | return ParseLeadingDoubleValue(str.c_str(), deflt); |
287 | 0 | } |
288 | | |
289 | | // ---------------------------------------------------------------------- |
290 | | // ParseLeadingBoolValue() |
291 | | // A recognizer of boolean string values. Returns the parsed value |
292 | | // if a valid value is found; else returns deflt. This skips leading |
293 | | // whitespace, is case insensitive, and recognizes these forms: |
294 | | // 0/1, false/true, no/yes, n/y |
295 | | // -------------------------------------------------------------------- |
296 | | bool ParseLeadingBoolValue(const char* str, bool deflt); |
297 | 0 | inline bool ParseLeadingBoolValue(const string& str, bool deflt) { |
298 | 0 | return ParseLeadingBoolValue(str.c_str(), deflt); |
299 | 0 | } |
300 | | |
301 | | // ---------------------------------------------------------------------- |
302 | | // AutoDigitStrCmp |
303 | | // AutoDigitLessThan |
304 | | // StrictAutoDigitLessThan |
305 | | // autodigit_less |
306 | | // autodigit_greater |
307 | | // strict_autodigit_less |
308 | | // strict_autodigit_greater |
309 | | // These are like less<string> and greater<string>, except when a |
310 | | // run of digits is encountered at corresponding points in the two |
311 | | // arguments. Such digit strings are compared numerically instead |
312 | | // of lexicographically. Therefore if you sort by |
313 | | // "autodigit_less", some machine names might get sorted as: |
314 | | // exaf1 |
315 | | // exaf2 |
316 | | // exaf10 |
317 | | // When using "strict" comparison (AutoDigitStrCmp with the strict flag |
318 | | // set to true, or the strict version of the other functions), |
319 | | // strings that represent equal numbers will not be considered equal if |
320 | | // the string representations are not identical. That is, "01" < "1" in |
321 | | // strict mode, but "01" == "1" otherwise. |
322 | | // ---------------------------------------------------------------------- |
323 | | |
324 | | int AutoDigitStrCmp(const char* a, int alen, const char* b, int blen, bool strict); |
325 | | |
326 | | bool AutoDigitLessThan(const char* a, int alen, const char* b, int blen); |
327 | | |
328 | | bool StrictAutoDigitLessThan(const char* a, int alen, const char* b, int blen); |
329 | | |
330 | | struct autodigit_less { |
331 | 0 | bool operator()(const string& a, const string& b) const { |
332 | 0 | return AutoDigitLessThan(a.data(), a.size(), b.data(), b.size()); |
333 | 0 | } |
334 | | }; |
335 | | |
336 | | struct autodigit_greater { |
337 | 0 | bool operator()(const string& a, const string& b) const { |
338 | 0 | return AutoDigitLessThan(b.data(), b.size(), a.data(), a.size()); |
339 | 0 | } |
340 | | }; |
341 | | |
342 | | struct strict_autodigit_less { |
343 | 0 | bool operator()(const string& a, const string& b) const { |
344 | 0 | return StrictAutoDigitLessThan(a.data(), a.size(), b.data(), b.size()); |
345 | 0 | } |
346 | | }; |
347 | | |
348 | | struct strict_autodigit_greater { |
349 | 0 | bool operator()(const string& a, const string& b) const { |
350 | 0 | return StrictAutoDigitLessThan(b.data(), b.size(), a.data(), a.size()); |
351 | 0 | } |
352 | | }; |
353 | | |
354 | | // ---------------------------------------------------------------------- |
355 | | // SimpleItoa() |
356 | | // Description: converts an integer to a string. |
357 | | // Faster than printf("%d"). |
358 | | // |
359 | | // Return value: string |
360 | | // ---------------------------------------------------------------------- |
361 | 0 | inline string SimpleItoa(int32 i) { |
362 | 0 | char buf[16]; // Longest is -2147483648 |
363 | 0 | return string(buf, FastInt32ToBufferLeft(i, buf)); |
364 | 0 | } |
365 | | |
366 | | // We need this overload because otherwise SimpleItoa(5U) wouldn't compile. |
367 | 0 | inline string SimpleItoa(uint32 i) { |
368 | 0 | char buf[16]; // Longest is 4294967295 |
369 | 0 | return string(buf, FastUInt32ToBufferLeft(i, buf)); |
370 | 0 | } |
371 | | |
372 | 0 | inline string SimpleItoa(int64 i) { |
373 | 0 | char buf[32]; // Longest is -9223372036854775808 |
374 | 0 | return string(buf, FastInt64ToBufferLeft(i, buf)); |
375 | 0 | } |
376 | | |
377 | | // We need this overload because otherwise SimpleItoa(5ULL) wouldn't compile. |
378 | 0 | inline string SimpleItoa(uint64 i) { |
379 | 0 | char buf[32]; // Longest is 18446744073709551615 |
380 | 0 | return string(buf, FastUInt64ToBufferLeft(i, buf)); |
381 | 0 | } |
382 | | |
383 | | // SimpleAtoi converts a string to an integer. |
384 | | // Uses safe_strto?() for actual parsing, so strict checking is |
385 | | // applied, which is to say, the string must be a base-10 integer, optionally |
386 | | // followed or preceded by whitespace, and value has to be in the range of |
387 | | // the corresponding integer type. |
388 | | // |
389 | | // Returns true if parsing was successful. |
390 | | template <typename int_type> |
391 | | bool MUST_USE_RESULT SimpleAtoi(const char* s, int_type* out) { |
392 | | // Must be of integer type (not pointer type), with more than 16-bitwidth. |
393 | | COMPILE_ASSERT(sizeof(*out) == 4 || sizeof(*out) == 8, SimpleAtoiWorksWith32Or64BitInts); |
394 | | if (std::numeric_limits<int_type>::is_signed) { // Signed |
395 | | if (sizeof(*out) == 64 / 8) { // 64-bit |
396 | | return safe_strto64(s, reinterpret_cast<int64*>(out)); |
397 | | } else { // 32-bit |
398 | | return safe_strto32(s, reinterpret_cast<int32*>(out)); |
399 | | } |
400 | | } else { // Unsigned |
401 | | if (sizeof(*out) == 64 / 8) { // 64-bit |
402 | | return safe_strtou64(s, reinterpret_cast<uint64*>(out)); |
403 | | } else { // 32-bit |
404 | | return safe_strtou32(s, reinterpret_cast<uint32*>(out)); |
405 | | } |
406 | | } |
407 | | } |
408 | | |
409 | | template <typename int_type> |
410 | | bool MUST_USE_RESULT SimpleAtoi(const string& s, int_type* out) { |
411 | | return SimpleAtoi(s.c_str(), out); |
412 | | } |
413 | | |
414 | | // ---------------------------------------------------------------------- |
415 | | // SimpleDtoa() |
416 | | // SimpleFtoa() |
417 | | // DoubleToBuffer() |
418 | | // FloatToBuffer() |
419 | | // Description: converts a double or float to a string which, if |
420 | | // passed to strtod(), will produce the exact same original double |
421 | | // (except in case of NaN; all NaNs are considered the same value). |
422 | | // We try to keep the string short but it's not guaranteed to be as |
423 | | // short as possible. |
424 | | // |
425 | | // DoubleToBuffer() and FloatToBuffer() write the text to the given |
426 | | // buffer and return it. The buffer must be at least |
427 | | // kDoubleToBufferSize bytes for doubles and kFloatToBufferSize |
428 | | // bytes for floats. kFastToBufferSize is also guaranteed to be large |
429 | | // enough to hold either. |
430 | | // |
431 | | // Return value: string |
432 | | // ---------------------------------------------------------------------- |
433 | | string SimpleDtoa(double value); |
434 | | string SimpleFtoa(float value); |
435 | | |
436 | | int DoubleToBuffer(double i, int width, char* buffer); |
437 | | int FloatToBuffer(float i, int width, char* buffer); |
438 | | |
439 | | char* DoubleToBuffer(double i, char* buffer); |
440 | | char* FloatToBuffer(float i, char* buffer); |
441 | | |
442 | | int FastDoubleToBuffer(double i, char* buffer); |
443 | | int FastFloatToBuffer(float i, char* buffer); |
444 | | // In practice, doubles should never need more than 24 bytes and floats |
445 | | // should never need more than 14 (including null terminators), but we |
446 | | // overestimate to be safe. |
447 | | static const int kDoubleToBufferSize = 32; |
448 | | static const int kFloatToBufferSize = 24; |
449 | | |
450 | | // ---------------------------------------------------------------------- |
451 | | // SimpleItoaWithCommas() |
452 | | // Description: converts an integer to a string. |
453 | | // Puts commas every 3 spaces. |
454 | | // Faster than printf("%d")? |
455 | | // |
456 | | // Return value: string |
457 | | // ---------------------------------------------------------------------- |
458 | | string SimpleItoaWithCommas(int32 i); |
459 | | string SimpleItoaWithCommas(uint32 i); |
460 | | string SimpleItoaWithCommas(int64 i); |
461 | | string SimpleItoaWithCommas(uint64 i); |
462 | | |
463 | | char* SimpleItoaWithCommas(int64_t i, char* buffer, int32_t buffer_size); |
464 | | char* SimpleItoaWithCommas(__int128_t i, char* buffer, int32_t buffer_size); |
465 | | |
466 | | // ---------------------------------------------------------------------- |
467 | | // ItoaKMGT() |
468 | | // Description: converts an integer to a string |
469 | | // Truncates values to K, G, M or T as appropriate |
470 | | // Opposite of atoi_kmgt() |
471 | | // e.g. 3000 -> 2K 57185920 -> 45M |
472 | | // |
473 | | // Return value: string |
474 | | // |
475 | | // AccurateItoaKMGT() |
476 | | // Description: preserve accuracy |
477 | | // ---------------------------------------------------------------------- |
478 | | string ItoaKMGT(int64 i); |
479 | | string AccurateItoaKMGT(int64 i); |
480 | | |
481 | | // ---------------------------------------------------------------------- |
482 | | // ParseDoubleRange() |
483 | | // Parse an expression in 'text' of the form: <double><sep><double> |
484 | | // where <double> may be a double-precision number and <sep> is a |
485 | | // single char or "..", and must be one of the chars in parameter |
486 | | // 'separators', which may contain '-' or '.' (which means "..") or |
487 | | // any chars not allowed in a double. If allow_unbounded_markers, |
488 | | // <double> may also be a '?' to indicate unboundedness (if on the |
489 | | // left of <sep>, means unbounded below; if on the right, means |
490 | | // unbounded above). Depending on num_required_bounds, which may be |
491 | | // 0, 1, or 2, <double> may also be the empty string, indicating |
492 | | // unboundedness. If require_separator is false, then a single |
493 | | // <double> is acceptable and is parsed as a range bounded from |
494 | | // below. We also check that the character following the range must |
495 | | // be in acceptable_terminators. If null_terminator_ok, then it is |
496 | | // also OK if the range ends in \0 or after len chars. If |
497 | | // allow_currency is true, the first <double> may be optionally |
498 | | // preceded by a '$', in which case *is_currency will be true, and |
499 | | // the second <double> may similarly be preceded by a '$'. In these |
500 | | // cases, the '$' will be ignored (otherwise it's an error). If |
501 | | // allow_comparators is true, the expression in 'text' may also be |
502 | | // of the form <comparator><double>, where <comparator> is '<' or |
503 | | // '>' or '<=' or '>='. separators and require_separator are |
504 | | // ignored in this format, but all other parameters function as for |
505 | | // the first format. Return true if the expression parsed |
506 | | // successfully; false otherwise. If successful, output params are: |
507 | | // 'end', which points to the char just beyond the expression; |
508 | | // 'from' and 'to' are set to the values of the <double>s, and are |
509 | | // -inf and inf (or unchanged, depending on dont_modify_unbounded) |
510 | | // if unbounded. Output params are undefined if false is |
511 | | // returned. len is the input length, or -1 if text is |
512 | | // '\0'-terminated, which is more efficient. |
513 | | // ---------------------------------------------------------------------- |
514 | | struct DoubleRangeOptions { |
515 | | const char* separators = nullptr; |
516 | | bool require_separator; |
517 | | const char* acceptable_terminators = nullptr; |
518 | | bool null_terminator_ok; |
519 | | bool allow_unbounded_markers; |
520 | | uint32 num_required_bounds; |
521 | | bool dont_modify_unbounded; |
522 | | bool allow_currency; |
523 | | bool allow_comparators; |
524 | | }; |
525 | | |
526 | | // NOTE: The instruction below creates a Module titled |
527 | | // NumbersFunctions within the auto-generated Doxygen documentation. |
528 | | // This instruction is needed to expose global functions that are not |
529 | | // within a namespace. |
530 | | // |
531 | | bool ParseDoubleRange(const char* text, int len, const char** end, double* from, double* to, |
532 | | bool* is_currency, const DoubleRangeOptions& opts); |
533 | | |
534 | | // END DOXYGEN SplitFunctions grouping |
535 | | /* @} */ |
536 | | |
537 | | // These functions are deprecated. |
538 | | // Do not use in new code. |
539 | | |
540 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf or SimpleFtoa. |
541 | | // string FloatToString(float f, const char* format); |
542 | | |
543 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf or SimpleItoa. |
544 | | // string IntToString(int i, const char* format); |
545 | | |
546 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf or SimpleItoa. |
547 | | // string Int64ToString(int64 i64, const char* format); |
548 | | |
549 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf or SimpleItoa. |
550 | | // string UInt64ToString(uint64 ui64, const char* format); |
551 | | |
552 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf. |
553 | | // inline string FloatToString(float f) { |
554 | | // return StringPrintf("%7f", f); |
555 | | // } |
556 | | |
557 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf. |
558 | | // inline string IntToString(int i) { |
559 | | // return StringPrintf("%7d", i); |
560 | | // } |
561 | | |
562 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf. |
563 | | // inline string Int64ToString(int64 i64) { |
564 | | // return StringPrintf("%7" PRId64, i64); |
565 | | // } |
566 | | |
567 | | // // DEPRECATED(wadetregaskis). Just call StringPrintf. |
568 | | // inline string UInt64ToString(uint64 ui64) { |
569 | | // return StringPrintf("%7" PRIu64, ui64); |
570 | | // } |