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