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