1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This file contains string processing functions related to
16 // numeric values.
17
18 #include "absl/strings/numbers.h"
19
20 #include <algorithm>
21 #include <cassert>
22 #include <cfloat> // for DBL_DIG and FLT_DIG
23 #include <cmath> // for HUGE_VAL
24 #include <cstdint>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <iterator>
29 #include <limits>
30 #include <memory>
31 #include <utility>
32
33 #include "absl/base/attributes.h"
34 #include "absl/base/internal/raw_logging.h"
35 #include "absl/numeric/bits.h"
36 #include "absl/strings/ascii.h"
37 #include "absl/strings/charconv.h"
38 #include "absl/strings/escaping.h"
39 #include "absl/strings/internal/memutil.h"
40 #include "absl/strings/match.h"
41 #include "absl/strings/str_cat.h"
42
43 namespace absl {
44 ABSL_NAMESPACE_BEGIN
45
SimpleAtof(absl::string_view str,float * out)46 bool SimpleAtof(absl::string_view str, float* out) {
47 *out = 0.0;
48 str = StripAsciiWhitespace(str);
49 // std::from_chars doesn't accept an initial +, but SimpleAtof does, so if one
50 // is present, skip it, while avoiding accepting "+-0" as valid.
51 if (!str.empty() && str[0] == '+') {
52 str.remove_prefix(1);
53 if (!str.empty() && str[0] == '-') {
54 return false;
55 }
56 }
57 auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
58 if (result.ec == std::errc::invalid_argument) {
59 return false;
60 }
61 if (result.ptr != str.data() + str.size()) {
62 // not all non-whitespace characters consumed
63 return false;
64 }
65 // from_chars() with DR 3081's current wording will return max() on
66 // overflow. SimpleAtof returns infinity instead.
67 if (result.ec == std::errc::result_out_of_range) {
68 if (*out > 1.0) {
69 *out = std::numeric_limits<float>::infinity();
70 } else if (*out < -1.0) {
71 *out = -std::numeric_limits<float>::infinity();
72 }
73 }
74 return true;
75 }
76
SimpleAtod(absl::string_view str,double * out)77 bool SimpleAtod(absl::string_view str, double* out) {
78 *out = 0.0;
79 str = StripAsciiWhitespace(str);
80 // std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one
81 // is present, skip it, while avoiding accepting "+-0" as valid.
82 if (!str.empty() && str[0] == '+') {
83 str.remove_prefix(1);
84 if (!str.empty() && str[0] == '-') {
85 return false;
86 }
87 }
88 auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
89 if (result.ec == std::errc::invalid_argument) {
90 return false;
91 }
92 if (result.ptr != str.data() + str.size()) {
93 // not all non-whitespace characters consumed
94 return false;
95 }
96 // from_chars() with DR 3081's current wording will return max() on
97 // overflow. SimpleAtod returns infinity instead.
98 if (result.ec == std::errc::result_out_of_range) {
99 if (*out > 1.0) {
100 *out = std::numeric_limits<double>::infinity();
101 } else if (*out < -1.0) {
102 *out = -std::numeric_limits<double>::infinity();
103 }
104 }
105 return true;
106 }
107
SimpleAtob(absl::string_view str,bool * out)108 bool SimpleAtob(absl::string_view str, bool* out) {
109 ABSL_RAW_CHECK(out != nullptr, "Output pointer must not be nullptr.");
110 if (EqualsIgnoreCase(str, "true") || EqualsIgnoreCase(str, "t") ||
111 EqualsIgnoreCase(str, "yes") || EqualsIgnoreCase(str, "y") ||
112 EqualsIgnoreCase(str, "1")) {
113 *out = true;
114 return true;
115 }
116 if (EqualsIgnoreCase(str, "false") || EqualsIgnoreCase(str, "f") ||
117 EqualsIgnoreCase(str, "no") || EqualsIgnoreCase(str, "n") ||
118 EqualsIgnoreCase(str, "0")) {
119 *out = false;
120 return true;
121 }
122 return false;
123 }
124
125 // ----------------------------------------------------------------------
126 // FastIntToBuffer() overloads
127 //
128 // Like the Fast*ToBuffer() functions above, these are intended for speed.
129 // Unlike the Fast*ToBuffer() functions, however, these functions write
130 // their output to the beginning of the buffer. The caller is responsible
131 // for ensuring that the buffer has enough space to hold the output.
132 //
133 // Returns a pointer to the end of the string (i.e. the null character
134 // terminating the string).
135 // ----------------------------------------------------------------------
136
137 namespace {
138
139 // Used to optimize printing a decimal number's final digit.
140 const char one_ASCII_final_digits[10][2] {
141 {'0', 0}, {'1', 0}, {'2', 0}, {'3', 0}, {'4', 0},
142 {'5', 0}, {'6', 0}, {'7', 0}, {'8', 0}, {'9', 0},
143 };
144
145 } // namespace
146
FastIntToBuffer(uint32_t i,char * buffer)147 char* numbers_internal::FastIntToBuffer(uint32_t i, char* buffer) {
148 uint32_t digits;
149 // The idea of this implementation is to trim the number of divides to as few
150 // as possible, and also reducing memory stores and branches, by going in
151 // steps of two digits at a time rather than one whenever possible.
152 // The huge-number case is first, in the hopes that the compiler will output
153 // that case in one branch-free block of code, and only output conditional
154 // branches into it from below.
155 if (i >= 1000000000) { // >= 1,000,000,000
156 digits = i / 100000000; // 100,000,000
157 i -= digits * 100000000;
158 PutTwoDigits(digits, buffer);
159 buffer += 2;
160 lt100_000_000:
161 digits = i / 1000000; // 1,000,000
162 i -= digits * 1000000;
163 PutTwoDigits(digits, buffer);
164 buffer += 2;
165 lt1_000_000:
166 digits = i / 10000; // 10,000
167 i -= digits * 10000;
168 PutTwoDigits(digits, buffer);
169 buffer += 2;
170 lt10_000:
171 digits = i / 100;
172 i -= digits * 100;
173 PutTwoDigits(digits, buffer);
174 buffer += 2;
175 lt100:
176 digits = i;
177 PutTwoDigits(digits, buffer);
178 buffer += 2;
179 *buffer = 0;
180 return buffer;
181 }
182
183 if (i < 100) {
184 digits = i;
185 if (i >= 10) goto lt100;
186 memcpy(buffer, one_ASCII_final_digits[i], 2);
187 return buffer + 1;
188 }
189 if (i < 10000) { // 10,000
190 if (i >= 1000) goto lt10_000;
191 digits = i / 100;
192 i -= digits * 100;
193 *buffer++ = '0' + digits;
194 goto lt100;
195 }
196 if (i < 1000000) { // 1,000,000
197 if (i >= 100000) goto lt1_000_000;
198 digits = i / 10000; // 10,000
199 i -= digits * 10000;
200 *buffer++ = '0' + digits;
201 goto lt10_000;
202 }
203 if (i < 100000000) { // 100,000,000
204 if (i >= 10000000) goto lt100_000_000;
205 digits = i / 1000000; // 1,000,000
206 i -= digits * 1000000;
207 *buffer++ = '0' + digits;
208 goto lt1_000_000;
209 }
210 // we already know that i < 1,000,000,000
211 digits = i / 100000000; // 100,000,000
212 i -= digits * 100000000;
213 *buffer++ = '0' + digits;
214 goto lt100_000_000;
215 }
216
FastIntToBuffer(int32_t i,char * buffer)217 char* numbers_internal::FastIntToBuffer(int32_t i, char* buffer) {
218 uint32_t u = i;
219 if (i < 0) {
220 *buffer++ = '-';
221 // We need to do the negation in modular (i.e., "unsigned")
222 // arithmetic; MSVC++ apprently warns for plain "-u", so
223 // we write the equivalent expression "0 - u" instead.
224 u = 0 - u;
225 }
226 return numbers_internal::FastIntToBuffer(u, buffer);
227 }
228
FastIntToBuffer(uint64_t i,char * buffer)229 char* numbers_internal::FastIntToBuffer(uint64_t i, char* buffer) {
230 uint32_t u32 = static_cast<uint32_t>(i);
231 if (u32 == i) return numbers_internal::FastIntToBuffer(u32, buffer);
232
233 // Here we know i has at least 10 decimal digits.
234 uint64_t top_1to11 = i / 1000000000;
235 u32 = static_cast<uint32_t>(i - top_1to11 * 1000000000);
236 uint32_t top_1to11_32 = static_cast<uint32_t>(top_1to11);
237
238 if (top_1to11_32 == top_1to11) {
239 buffer = numbers_internal::FastIntToBuffer(top_1to11_32, buffer);
240 } else {
241 // top_1to11 has more than 32 bits too; print it in two steps.
242 uint32_t top_8to9 = static_cast<uint32_t>(top_1to11 / 100);
243 uint32_t mid_2 = static_cast<uint32_t>(top_1to11 - top_8to9 * 100);
244 buffer = numbers_internal::FastIntToBuffer(top_8to9, buffer);
245 PutTwoDigits(mid_2, buffer);
246 buffer += 2;
247 }
248
249 // We have only 9 digits now, again the maximum uint32_t can handle fully.
250 uint32_t digits = u32 / 10000000; // 10,000,000
251 u32 -= digits * 10000000;
252 PutTwoDigits(digits, buffer);
253 buffer += 2;
254 digits = u32 / 100000; // 100,000
255 u32 -= digits * 100000;
256 PutTwoDigits(digits, buffer);
257 buffer += 2;
258 digits = u32 / 1000; // 1,000
259 u32 -= digits * 1000;
260 PutTwoDigits(digits, buffer);
261 buffer += 2;
262 digits = u32 / 10;
263 u32 -= digits * 10;
264 PutTwoDigits(digits, buffer);
265 buffer += 2;
266 memcpy(buffer, one_ASCII_final_digits[u32], 2);
267 return buffer + 1;
268 }
269
FastIntToBuffer(int64_t i,char * buffer)270 char* numbers_internal::FastIntToBuffer(int64_t i, char* buffer) {
271 uint64_t u = i;
272 if (i < 0) {
273 *buffer++ = '-';
274 u = 0 - u;
275 }
276 return numbers_internal::FastIntToBuffer(u, buffer);
277 }
278
279 // Given a 128-bit number expressed as a pair of uint64_t, high half first,
280 // return that number multiplied by the given 32-bit value. If the result is
281 // too large to fit in a 128-bit number, divide it by 2 until it fits.
Mul32(std::pair<uint64_t,uint64_t> num,uint32_t mul)282 static std::pair<uint64_t, uint64_t> Mul32(std::pair<uint64_t, uint64_t> num,
283 uint32_t mul) {
284 uint64_t bits0_31 = num.second & 0xFFFFFFFF;
285 uint64_t bits32_63 = num.second >> 32;
286 uint64_t bits64_95 = num.first & 0xFFFFFFFF;
287 uint64_t bits96_127 = num.first >> 32;
288
289 // The picture so far: each of these 64-bit values has only the lower 32 bits
290 // filled in.
291 // bits96_127: [ 00000000 xxxxxxxx ]
292 // bits64_95: [ 00000000 xxxxxxxx ]
293 // bits32_63: [ 00000000 xxxxxxxx ]
294 // bits0_31: [ 00000000 xxxxxxxx ]
295
296 bits0_31 *= mul;
297 bits32_63 *= mul;
298 bits64_95 *= mul;
299 bits96_127 *= mul;
300
301 // Now the top halves may also have value, though all 64 of their bits will
302 // never be set at the same time, since they are a result of a 32x32 bit
303 // multiply. This makes the carry calculation slightly easier.
304 // bits96_127: [ mmmmmmmm | mmmmmmmm ]
305 // bits64_95: [ | mmmmmmmm mmmmmmmm | ]
306 // bits32_63: | [ mmmmmmmm | mmmmmmmm ]
307 // bits0_31: | [ | mmmmmmmm mmmmmmmm ]
308 // eventually: [ bits128_up | ...bits64_127.... | ..bits0_63... ]
309
310 uint64_t bits0_63 = bits0_31 + (bits32_63 << 32);
311 uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) +
312 (bits0_63 < bits0_31);
313 uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95);
314 if (bits128_up == 0) return {bits64_127, bits0_63};
315
316 auto shift = static_cast<unsigned>(bit_width(bits128_up));
317 uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift));
318 uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift));
319 return {hi, lo};
320 }
321
322 // Compute num * 5 ^ expfive, and return the first 128 bits of the result,
323 // where the first bit is always a one. So PowFive(1, 0) starts 0b100000,
324 // PowFive(1, 1) starts 0b101000, PowFive(1, 2) starts 0b110010, etc.
PowFive(uint64_t num,int expfive)325 static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) {
326 std::pair<uint64_t, uint64_t> result = {num, 0};
327 while (expfive >= 13) {
328 // 5^13 is the highest power of five that will fit in a 32-bit integer.
329 result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5);
330 expfive -= 13;
331 }
332 constexpr int powers_of_five[13] = {
333 1,
334 5,
335 5 * 5,
336 5 * 5 * 5,
337 5 * 5 * 5 * 5,
338 5 * 5 * 5 * 5 * 5,
339 5 * 5 * 5 * 5 * 5 * 5,
340 5 * 5 * 5 * 5 * 5 * 5 * 5,
341 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
342 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
343 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
344 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
345 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
346 result = Mul32(result, powers_of_five[expfive & 15]);
347 int shift = countl_zero(result.first);
348 if (shift != 0) {
349 result.first = (result.first << shift) + (result.second >> (64 - shift));
350 result.second = (result.second << shift);
351 }
352 return result;
353 }
354
355 struct ExpDigits {
356 int32_t exponent;
357 char digits[6];
358 };
359
360 // SplitToSix converts value, a positive double-precision floating-point number,
361 // into a base-10 exponent and 6 ASCII digits, where the first digit is never
362 // zero. For example, SplitToSix(1) returns an exponent of zero and a digits
363 // array of {'1', '0', '0', '0', '0', '0'}. If value is exactly halfway between
364 // two possible representations, e.g. value = 100000.5, then "round to even" is
365 // performed.
SplitToSix(const double value)366 static ExpDigits SplitToSix(const double value) {
367 ExpDigits exp_dig;
368 int exp = 5;
369 double d = value;
370 // First step: calculate a close approximation of the output, where the
371 // value d will be between 100,000 and 999,999, representing the digits
372 // in the output ASCII array, and exp is the base-10 exponent. It would be
373 // faster to use a table here, and to look up the base-2 exponent of value,
374 // however value is an IEEE-754 64-bit number, so the table would have 2,000
375 // entries, which is not cache-friendly.
376 if (d >= 999999.5) {
377 if (d >= 1e+261) exp += 256, d *= 1e-256;
378 if (d >= 1e+133) exp += 128, d *= 1e-128;
379 if (d >= 1e+69) exp += 64, d *= 1e-64;
380 if (d >= 1e+37) exp += 32, d *= 1e-32;
381 if (d >= 1e+21) exp += 16, d *= 1e-16;
382 if (d >= 1e+13) exp += 8, d *= 1e-8;
383 if (d >= 1e+9) exp += 4, d *= 1e-4;
384 if (d >= 1e+7) exp += 2, d *= 1e-2;
385 if (d >= 1e+6) exp += 1, d *= 1e-1;
386 } else {
387 if (d < 1e-250) exp -= 256, d *= 1e256;
388 if (d < 1e-122) exp -= 128, d *= 1e128;
389 if (d < 1e-58) exp -= 64, d *= 1e64;
390 if (d < 1e-26) exp -= 32, d *= 1e32;
391 if (d < 1e-10) exp -= 16, d *= 1e16;
392 if (d < 1e-2) exp -= 8, d *= 1e8;
393 if (d < 1e+2) exp -= 4, d *= 1e4;
394 if (d < 1e+4) exp -= 2, d *= 1e2;
395 if (d < 1e+5) exp -= 1, d *= 1e1;
396 }
397 // At this point, d is in the range [99999.5..999999.5) and exp is in the
398 // range [-324..308]. Since we need to round d up, we want to add a half
399 // and truncate.
400 // However, the technique above may have lost some precision, due to its
401 // repeated multiplication by constants that each may be off by half a bit
402 // of precision. This only matters if we're close to the edge though.
403 // Since we'd like to know if the fractional part of d is close to a half,
404 // we multiply it by 65536 and see if the fractional part is close to 32768.
405 // (The number doesn't have to be a power of two,but powers of two are faster)
406 uint64_t d64k = d * 65536;
407 int dddddd; // A 6-digit decimal integer.
408 if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
409 // OK, it's fairly likely that precision was lost above, which is
410 // not a surprise given only 52 mantissa bits are available. Therefore
411 // redo the calculation using 128-bit numbers. (64 bits are not enough).
412
413 // Start out with digits rounded down; maybe add one below.
414 dddddd = static_cast<int>(d64k / 65536);
415
416 // mantissa is a 64-bit integer representing M.mmm... * 2^63. The actual
417 // value we're representing, of course, is M.mmm... * 2^exp2.
418 int exp2;
419 double m = std::frexp(value, &exp2);
420 uint64_t mantissa = m * (32768.0 * 65536.0 * 65536.0 * 65536.0);
421 // std::frexp returns an m value in the range [0.5, 1.0), however we
422 // can't multiply it by 2^64 and convert to an integer because some FPUs
423 // throw an exception when converting an number higher than 2^63 into an
424 // integer - even an unsigned 64-bit integer! Fortunately it doesn't matter
425 // since m only has 52 significant bits anyway.
426 mantissa <<= 1;
427 exp2 -= 64; // not needed, but nice for debugging
428
429 // OK, we are here to compare:
430 // (dddddd + 0.5) * 10^(exp-5) vs. mantissa * 2^exp2
431 // so we can round up dddddd if appropriate. Those values span the full
432 // range of 600 orders of magnitude of IEE 64-bit floating-point.
433 // Fortunately, we already know they are very close, so we don't need to
434 // track the base-2 exponent of both sides. This greatly simplifies the
435 // the math since the 2^exp2 calculation is unnecessary and the power-of-10
436 // calculation can become a power-of-5 instead.
437
438 std::pair<uint64_t, uint64_t> edge, val;
439 if (exp >= 6) {
440 // Compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa
441 // Since we're tossing powers of two, 2 * dddddd + 1 is the
442 // same as dddddd + 0.5
443 edge = PowFive(2 * dddddd + 1, exp - 5);
444
445 val.first = mantissa;
446 val.second = 0;
447 } else {
448 // We can't compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa as we did
449 // above because (exp - 5) is negative. So we compare (dddddd + 0.5) to
450 // mantissa * 5 ^ (5 - exp)
451 edge = PowFive(2 * dddddd + 1, 0);
452
453 val = PowFive(mantissa, 5 - exp);
454 }
455 // printf("exp=%d %016lx %016lx vs %016lx %016lx\n", exp, val.first,
456 // val.second, edge.first, edge.second);
457 if (val > edge) {
458 dddddd++;
459 } else if (val == edge) {
460 dddddd += (dddddd & 1);
461 }
462 } else {
463 // Here, we are not close to the edge.
464 dddddd = static_cast<int>((d64k + 32768) / 65536);
465 }
466 if (dddddd == 1000000) {
467 dddddd = 100000;
468 exp += 1;
469 }
470 exp_dig.exponent = exp;
471
472 int two_digits = dddddd / 10000;
473 dddddd -= two_digits * 10000;
474 numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]);
475
476 two_digits = dddddd / 100;
477 dddddd -= two_digits * 100;
478 numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[2]);
479
480 numbers_internal::PutTwoDigits(dddddd, &exp_dig.digits[4]);
481 return exp_dig;
482 }
483
484 // Helper function for fast formatting of floating-point.
485 // The result is the same as "%g", a.k.a. "%.6g".
SixDigitsToBuffer(double d,char * const buffer)486 size_t numbers_internal::SixDigitsToBuffer(double d, char* const buffer) {
487 static_assert(std::numeric_limits<float>::is_iec559,
488 "IEEE-754/IEC-559 support only");
489
490 char* out = buffer; // we write data to out, incrementing as we go, but
491 // FloatToBuffer always returns the address of the buffer
492 // passed in.
493
494 if (std::isnan(d)) {
495 strcpy(out, "nan"); // NOLINT(runtime/printf)
496 return 3;
497 }
498 if (d == 0) { // +0 and -0 are handled here
499 if (std::signbit(d)) *out++ = '-';
500 *out++ = '0';
501 *out = 0;
502 return out - buffer;
503 }
504 if (d < 0) {
505 *out++ = '-';
506 d = -d;
507 }
508 if (d > std::numeric_limits<double>::max()) {
509 strcpy(out, "inf"); // NOLINT(runtime/printf)
510 return out + 3 - buffer;
511 }
512
513 auto exp_dig = SplitToSix(d);
514 int exp = exp_dig.exponent;
515 const char* digits = exp_dig.digits;
516 out[0] = '0';
517 out[1] = '.';
518 switch (exp) {
519 case 5:
520 memcpy(out, &digits[0], 6), out += 6;
521 *out = 0;
522 return out - buffer;
523 case 4:
524 memcpy(out, &digits[0], 5), out += 5;
525 if (digits[5] != '0') {
526 *out++ = '.';
527 *out++ = digits[5];
528 }
529 *out = 0;
530 return out - buffer;
531 case 3:
532 memcpy(out, &digits[0], 4), out += 4;
533 if ((digits[5] | digits[4]) != '0') {
534 *out++ = '.';
535 *out++ = digits[4];
536 if (digits[5] != '0') *out++ = digits[5];
537 }
538 *out = 0;
539 return out - buffer;
540 case 2:
541 memcpy(out, &digits[0], 3), out += 3;
542 *out++ = '.';
543 memcpy(out, &digits[3], 3);
544 out += 3;
545 while (out[-1] == '0') --out;
546 if (out[-1] == '.') --out;
547 *out = 0;
548 return out - buffer;
549 case 1:
550 memcpy(out, &digits[0], 2), out += 2;
551 *out++ = '.';
552 memcpy(out, &digits[2], 4);
553 out += 4;
554 while (out[-1] == '0') --out;
555 if (out[-1] == '.') --out;
556 *out = 0;
557 return out - buffer;
558 case 0:
559 memcpy(out, &digits[0], 1), out += 1;
560 *out++ = '.';
561 memcpy(out, &digits[1], 5);
562 out += 5;
563 while (out[-1] == '0') --out;
564 if (out[-1] == '.') --out;
565 *out = 0;
566 return out - buffer;
567 case -4:
568 out[2] = '0';
569 ++out;
570 ABSL_FALLTHROUGH_INTENDED;
571 case -3:
572 out[2] = '0';
573 ++out;
574 ABSL_FALLTHROUGH_INTENDED;
575 case -2:
576 out[2] = '0';
577 ++out;
578 ABSL_FALLTHROUGH_INTENDED;
579 case -1:
580 out += 2;
581 memcpy(out, &digits[0], 6);
582 out += 6;
583 while (out[-1] == '0') --out;
584 *out = 0;
585 return out - buffer;
586 }
587 assert(exp < -4 || exp >= 6);
588 out[0] = digits[0];
589 assert(out[1] == '.');
590 out += 2;
591 memcpy(out, &digits[1], 5), out += 5;
592 while (out[-1] == '0') --out;
593 if (out[-1] == '.') --out;
594 *out++ = 'e';
595 if (exp > 0) {
596 *out++ = '+';
597 } else {
598 *out++ = '-';
599 exp = -exp;
600 }
601 if (exp > 99) {
602 int dig1 = exp / 100;
603 exp -= dig1 * 100;
604 *out++ = '0' + dig1;
605 }
606 PutTwoDigits(exp, out);
607 out += 2;
608 *out = 0;
609 return out - buffer;
610 }
611
612 namespace {
613 // Represents integer values of digits.
614 // Uses 36 to indicate an invalid character since we support
615 // bases up to 36.
616 static const int8_t kAsciiToInt[256] = {
617 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, // 16 36s.
618 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
619 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0, 1, 2, 3, 4, 5,
620 6, 7, 8, 9, 36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17,
621 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
622 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
623 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36,
624 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
625 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
626 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
627 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
628 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
629 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
630 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36};
631
632 // Parse the sign and optional hex or oct prefix in text.
safe_parse_sign_and_base(absl::string_view * text,int * base_ptr,bool * negative_ptr)633 inline bool safe_parse_sign_and_base(absl::string_view* text /*inout*/,
634 int* base_ptr /*inout*/,
635 bool* negative_ptr /*output*/) {
636 if (text->data() == nullptr) {
637 return false;
638 }
639
640 const char* start = text->data();
641 const char* end = start + text->size();
642 int base = *base_ptr;
643
644 // Consume whitespace.
645 while (start < end && absl::ascii_isspace(start[0])) {
646 ++start;
647 }
648 while (start < end && absl::ascii_isspace(end[-1])) {
649 --end;
650 }
651 if (start >= end) {
652 return false;
653 }
654
655 // Consume sign.
656 *negative_ptr = (start[0] == '-');
657 if (*negative_ptr || start[0] == '+') {
658 ++start;
659 if (start >= end) {
660 return false;
661 }
662 }
663
664 // Consume base-dependent prefix.
665 // base 0: "0x" -> base 16, "0" -> base 8, default -> base 10
666 // base 16: "0x" -> base 16
667 // Also validate the base.
668 if (base == 0) {
669 if (end - start >= 2 && start[0] == '0' &&
670 (start[1] == 'x' || start[1] == 'X')) {
671 base = 16;
672 start += 2;
673 if (start >= end) {
674 // "0x" with no digits after is invalid.
675 return false;
676 }
677 } else if (end - start >= 1 && start[0] == '0') {
678 base = 8;
679 start += 1;
680 } else {
681 base = 10;
682 }
683 } else if (base == 16) {
684 if (end - start >= 2 && start[0] == '0' &&
685 (start[1] == 'x' || start[1] == 'X')) {
686 start += 2;
687 if (start >= end) {
688 // "0x" with no digits after is invalid.
689 return false;
690 }
691 }
692 } else if (base >= 2 && base <= 36) {
693 // okay
694 } else {
695 return false;
696 }
697 *text = absl::string_view(start, end - start);
698 *base_ptr = base;
699 return true;
700 }
701
702 // Consume digits.
703 //
704 // The classic loop:
705 //
706 // for each digit
707 // value = value * base + digit
708 // value *= sign
709 //
710 // The classic loop needs overflow checking. It also fails on the most
711 // negative integer, -2147483648 in 32-bit two's complement representation.
712 //
713 // My improved loop:
714 //
715 // if (!negative)
716 // for each digit
717 // value = value * base
718 // value = value + digit
719 // else
720 // for each digit
721 // value = value * base
722 // value = value - digit
723 //
724 // Overflow checking becomes simple.
725
726 // Lookup tables per IntType:
727 // vmax/base and vmin/base are precomputed because division costs at least 8ns.
728 // TODO(junyer): Doing this per base instead (i.e. an array of structs, not a
729 // struct of arrays) would probably be better in terms of d-cache for the most
730 // commonly used bases.
731 template <typename IntType>
732 struct LookupTables {
733 ABSL_CONST_INIT static const IntType kVmaxOverBase[];
734 ABSL_CONST_INIT static const IntType kVminOverBase[];
735 };
736
737 // An array initializer macro for X/base where base in [0, 36].
738 // However, note that lookups for base in [0, 1] should never happen because
739 // base has been validated to be in [2, 36] by safe_parse_sign_and_base().
740 #define X_OVER_BASE_INITIALIZER(X) \
741 { \
742 0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \
743 X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18, \
744 X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26, \
745 X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34, \
746 X / 35, X / 36, \
747 }
748
749 // This kVmaxOverBase is generated with
750 // for (int base = 2; base < 37; ++base) {
751 // absl::uint128 max = std::numeric_limits<absl::uint128>::max();
752 // auto result = max / base;
753 // std::cout << " MakeUint128(" << absl::Uint128High64(result) << "u, "
754 // << absl::Uint128Low64(result) << "u),\n";
755 // }
756 // See https://godbolt.org/z/aneYsb
757 //
758 // uint128& operator/=(uint128) is not constexpr, so hardcode the resulting
759 // array to avoid a static initializer.
760 template<>
761 const uint128 LookupTables<uint128>::kVmaxOverBase[] = {
762 0,
763 0,
764 MakeUint128(9223372036854775807u, 18446744073709551615u),
765 MakeUint128(6148914691236517205u, 6148914691236517205u),
766 MakeUint128(4611686018427387903u, 18446744073709551615u),
767 MakeUint128(3689348814741910323u, 3689348814741910323u),
768 MakeUint128(3074457345618258602u, 12297829382473034410u),
769 MakeUint128(2635249153387078802u, 5270498306774157604u),
770 MakeUint128(2305843009213693951u, 18446744073709551615u),
771 MakeUint128(2049638230412172401u, 14347467612885206812u),
772 MakeUint128(1844674407370955161u, 11068046444225730969u),
773 MakeUint128(1676976733973595601u, 8384883669867978007u),
774 MakeUint128(1537228672809129301u, 6148914691236517205u),
775 MakeUint128(1418980313362273201u, 4256940940086819603u),
776 MakeUint128(1317624576693539401u, 2635249153387078802u),
777 MakeUint128(1229782938247303441u, 1229782938247303441u),
778 MakeUint128(1152921504606846975u, 18446744073709551615u),
779 MakeUint128(1085102592571150095u, 1085102592571150095u),
780 MakeUint128(1024819115206086200u, 16397105843297379214u),
781 MakeUint128(970881267037344821u, 16504981539634861972u),
782 MakeUint128(922337203685477580u, 14757395258967641292u),
783 MakeUint128(878416384462359600u, 14054662151397753612u),
784 MakeUint128(838488366986797800u, 13415813871788764811u),
785 MakeUint128(802032351030850070u, 4812194106185100421u),
786 MakeUint128(768614336404564650u, 12297829382473034410u),
787 MakeUint128(737869762948382064u, 11805916207174113034u),
788 MakeUint128(709490156681136600u, 11351842506898185609u),
789 MakeUint128(683212743470724133u, 17080318586768103348u),
790 MakeUint128(658812288346769700u, 10540996613548315209u),
791 MakeUint128(636094623231363848u, 15266270957552732371u),
792 MakeUint128(614891469123651720u, 9838263505978427528u),
793 MakeUint128(595056260442243600u, 9520900167075897608u),
794 MakeUint128(576460752303423487u, 18446744073709551615u),
795 MakeUint128(558992244657865200u, 8943875914525843207u),
796 MakeUint128(542551296285575047u, 9765923333140350855u),
797 MakeUint128(527049830677415760u, 8432797290838652167u),
798 MakeUint128(512409557603043100u, 8198552921648689607u),
799 };
800
801 // This kVmaxOverBase generated with
802 // for (int base = 2; base < 37; ++base) {
803 // absl::int128 max = std::numeric_limits<absl::int128>::max();
804 // auto result = max / base;
805 // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", "
806 // << absl::Int128Low64(result) << "u),\n";
807 // }
808 // See https://godbolt.org/z/7djYWz
809 //
810 // int128& operator/=(int128) is not constexpr, so hardcode the resulting array
811 // to avoid a static initializer.
812 template<>
813 const int128 LookupTables<int128>::kVmaxOverBase[] = {
814 0,
815 0,
816 MakeInt128(4611686018427387903, 18446744073709551615u),
817 MakeInt128(3074457345618258602, 12297829382473034410u),
818 MakeInt128(2305843009213693951, 18446744073709551615u),
819 MakeInt128(1844674407370955161, 11068046444225730969u),
820 MakeInt128(1537228672809129301, 6148914691236517205u),
821 MakeInt128(1317624576693539401, 2635249153387078802u),
822 MakeInt128(1152921504606846975, 18446744073709551615u),
823 MakeInt128(1024819115206086200, 16397105843297379214u),
824 MakeInt128(922337203685477580, 14757395258967641292u),
825 MakeInt128(838488366986797800, 13415813871788764811u),
826 MakeInt128(768614336404564650, 12297829382473034410u),
827 MakeInt128(709490156681136600, 11351842506898185609u),
828 MakeInt128(658812288346769700, 10540996613548315209u),
829 MakeInt128(614891469123651720, 9838263505978427528u),
830 MakeInt128(576460752303423487, 18446744073709551615u),
831 MakeInt128(542551296285575047, 9765923333140350855u),
832 MakeInt128(512409557603043100, 8198552921648689607u),
833 MakeInt128(485440633518672410, 17475862806672206794u),
834 MakeInt128(461168601842738790, 7378697629483820646u),
835 MakeInt128(439208192231179800, 7027331075698876806u),
836 MakeInt128(419244183493398900, 6707906935894382405u),
837 MakeInt128(401016175515425035, 2406097053092550210u),
838 MakeInt128(384307168202282325, 6148914691236517205u),
839 MakeInt128(368934881474191032, 5902958103587056517u),
840 MakeInt128(354745078340568300, 5675921253449092804u),
841 MakeInt128(341606371735362066, 17763531330238827482u),
842 MakeInt128(329406144173384850, 5270498306774157604u),
843 MakeInt128(318047311615681924, 7633135478776366185u),
844 MakeInt128(307445734561825860, 4919131752989213764u),
845 MakeInt128(297528130221121800, 4760450083537948804u),
846 MakeInt128(288230376151711743, 18446744073709551615u),
847 MakeInt128(279496122328932600, 4471937957262921603u),
848 MakeInt128(271275648142787523, 14106333703424951235u),
849 MakeInt128(263524915338707880, 4216398645419326083u),
850 MakeInt128(256204778801521550, 4099276460824344803u),
851 };
852
853 // This kVminOverBase generated with
854 // for (int base = 2; base < 37; ++base) {
855 // absl::int128 min = std::numeric_limits<absl::int128>::min();
856 // auto result = min / base;
857 // std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", "
858 // << absl::Int128Low64(result) << "u),\n";
859 // }
860 //
861 // See https://godbolt.org/z/7djYWz
862 //
863 // int128& operator/=(int128) is not constexpr, so hardcode the resulting array
864 // to avoid a static initializer.
865 template<>
866 const int128 LookupTables<int128>::kVminOverBase[] = {
867 0,
868 0,
869 MakeInt128(-4611686018427387904, 0u),
870 MakeInt128(-3074457345618258603, 6148914691236517206u),
871 MakeInt128(-2305843009213693952, 0u),
872 MakeInt128(-1844674407370955162, 7378697629483820647u),
873 MakeInt128(-1537228672809129302, 12297829382473034411u),
874 MakeInt128(-1317624576693539402, 15811494920322472814u),
875 MakeInt128(-1152921504606846976, 0u),
876 MakeInt128(-1024819115206086201, 2049638230412172402u),
877 MakeInt128(-922337203685477581, 3689348814741910324u),
878 MakeInt128(-838488366986797801, 5030930201920786805u),
879 MakeInt128(-768614336404564651, 6148914691236517206u),
880 MakeInt128(-709490156681136601, 7094901566811366007u),
881 MakeInt128(-658812288346769701, 7905747460161236407u),
882 MakeInt128(-614891469123651721, 8608480567731124088u),
883 MakeInt128(-576460752303423488, 0u),
884 MakeInt128(-542551296285575048, 8680820740569200761u),
885 MakeInt128(-512409557603043101, 10248191152060862009u),
886 MakeInt128(-485440633518672411, 970881267037344822u),
887 MakeInt128(-461168601842738791, 11068046444225730970u),
888 MakeInt128(-439208192231179801, 11419412998010674810u),
889 MakeInt128(-419244183493398901, 11738837137815169211u),
890 MakeInt128(-401016175515425036, 16040647020617001406u),
891 MakeInt128(-384307168202282326, 12297829382473034411u),
892 MakeInt128(-368934881474191033, 12543785970122495099u),
893 MakeInt128(-354745078340568301, 12770822820260458812u),
894 MakeInt128(-341606371735362067, 683212743470724134u),
895 MakeInt128(-329406144173384851, 13176245766935394012u),
896 MakeInt128(-318047311615681925, 10813608594933185431u),
897 MakeInt128(-307445734561825861, 13527612320720337852u),
898 MakeInt128(-297528130221121801, 13686293990171602812u),
899 MakeInt128(-288230376151711744, 0u),
900 MakeInt128(-279496122328932601, 13974806116446630013u),
901 MakeInt128(-271275648142787524, 4340410370284600381u),
902 MakeInt128(-263524915338707881, 14230345428290225533u),
903 MakeInt128(-256204778801521551, 14347467612885206813u),
904 };
905
906 template <typename IntType>
907 const IntType LookupTables<IntType>::kVmaxOverBase[] =
908 X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max());
909
910 template <typename IntType>
911 const IntType LookupTables<IntType>::kVminOverBase[] =
912 X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min());
913
914 #undef X_OVER_BASE_INITIALIZER
915
916 template <typename IntType>
safe_parse_positive_int(absl::string_view text,int base,IntType * value_p)917 inline bool safe_parse_positive_int(absl::string_view text, int base,
918 IntType* value_p) {
919 IntType value = 0;
920 const IntType vmax = std::numeric_limits<IntType>::max();
921 assert(vmax > 0);
922 assert(base >= 0);
923 assert(vmax >= static_cast<IntType>(base));
924 const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base];
925 assert(base < 2 ||
926 std::numeric_limits<IntType>::max() / base == vmax_over_base);
927 const char* start = text.data();
928 const char* end = start + text.size();
929 // loop over digits
930 for (; start < end; ++start) {
931 unsigned char c = static_cast<unsigned char>(start[0]);
932 int digit = kAsciiToInt[c];
933 if (digit >= base) {
934 *value_p = value;
935 return false;
936 }
937 if (value > vmax_over_base) {
938 *value_p = vmax;
939 return false;
940 }
941 value *= base;
942 if (value > vmax - digit) {
943 *value_p = vmax;
944 return false;
945 }
946 value += digit;
947 }
948 *value_p = value;
949 return true;
950 }
951
952 template <typename IntType>
safe_parse_negative_int(absl::string_view text,int base,IntType * value_p)953 inline bool safe_parse_negative_int(absl::string_view text, int base,
954 IntType* value_p) {
955 IntType value = 0;
956 const IntType vmin = std::numeric_limits<IntType>::min();
957 assert(vmin < 0);
958 assert(vmin <= 0 - base);
959 IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base];
960 assert(base < 2 ||
961 std::numeric_limits<IntType>::min() / base == vmin_over_base);
962 // 2003 c++ standard [expr.mul]
963 // "... the sign of the remainder is implementation-defined."
964 // Although (vmin/base)*base + vmin%base is always vmin.
965 // 2011 c++ standard tightens the spec but we cannot rely on it.
966 // TODO(junyer): Handle this in the lookup table generation.
967 if (vmin % base > 0) {
968 vmin_over_base += 1;
969 }
970 const char* start = text.data();
971 const char* end = start + text.size();
972 // loop over digits
973 for (; start < end; ++start) {
974 unsigned char c = static_cast<unsigned char>(start[0]);
975 int digit = kAsciiToInt[c];
976 if (digit >= base) {
977 *value_p = value;
978 return false;
979 }
980 if (value < vmin_over_base) {
981 *value_p = vmin;
982 return false;
983 }
984 value *= base;
985 if (value < vmin + digit) {
986 *value_p = vmin;
987 return false;
988 }
989 value -= digit;
990 }
991 *value_p = value;
992 return true;
993 }
994
995 // Input format based on POSIX.1-2008 strtol
996 // http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
997 template <typename IntType>
safe_int_internal(absl::string_view text,IntType * value_p,int base)998 inline bool safe_int_internal(absl::string_view text, IntType* value_p,
999 int base) {
1000 *value_p = 0;
1001 bool negative;
1002 if (!safe_parse_sign_and_base(&text, &base, &negative)) {
1003 return false;
1004 }
1005 if (!negative) {
1006 return safe_parse_positive_int(text, base, value_p);
1007 } else {
1008 return safe_parse_negative_int(text, base, value_p);
1009 }
1010 }
1011
1012 template <typename IntType>
safe_uint_internal(absl::string_view text,IntType * value_p,int base)1013 inline bool safe_uint_internal(absl::string_view text, IntType* value_p,
1014 int base) {
1015 *value_p = 0;
1016 bool negative;
1017 if (!safe_parse_sign_and_base(&text, &base, &negative) || negative) {
1018 return false;
1019 }
1020 return safe_parse_positive_int(text, base, value_p);
1021 }
1022 } // anonymous namespace
1023
1024 namespace numbers_internal {
1025
1026 // Digit conversion.
1027 ABSL_CONST_INIT ABSL_DLL const char kHexChar[] =
1028 "0123456789abcdef";
1029
1030 ABSL_CONST_INIT ABSL_DLL const char kHexTable[513] =
1031 "000102030405060708090a0b0c0d0e0f"
1032 "101112131415161718191a1b1c1d1e1f"
1033 "202122232425262728292a2b2c2d2e2f"
1034 "303132333435363738393a3b3c3d3e3f"
1035 "404142434445464748494a4b4c4d4e4f"
1036 "505152535455565758595a5b5c5d5e5f"
1037 "606162636465666768696a6b6c6d6e6f"
1038 "707172737475767778797a7b7c7d7e7f"
1039 "808182838485868788898a8b8c8d8e8f"
1040 "909192939495969798999a9b9c9d9e9f"
1041 "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
1042 "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
1043 "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
1044 "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
1045 "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
1046 "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
1047
1048 ABSL_CONST_INIT ABSL_DLL const char two_ASCII_digits[100][2] = {
1049 {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
1050 {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
1051 {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
1052 {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
1053 {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
1054 {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
1055 {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
1056 {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
1057 {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
1058 {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
1059 {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
1060 {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
1061 {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
1062 {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
1063 {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
1064 {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
1065 {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}};
1066
safe_strto32_base(absl::string_view text,int32_t * value,int base)1067 bool safe_strto32_base(absl::string_view text, int32_t* value, int base) {
1068 return safe_int_internal<int32_t>(text, value, base);
1069 }
1070
safe_strto64_base(absl::string_view text,int64_t * value,int base)1071 bool safe_strto64_base(absl::string_view text, int64_t* value, int base) {
1072 return safe_int_internal<int64_t>(text, value, base);
1073 }
1074
safe_strto128_base(absl::string_view text,int128 * value,int base)1075 bool safe_strto128_base(absl::string_view text, int128* value, int base) {
1076 return safe_int_internal<absl::int128>(text, value, base);
1077 }
1078
safe_strtou32_base(absl::string_view text,uint32_t * value,int base)1079 bool safe_strtou32_base(absl::string_view text, uint32_t* value, int base) {
1080 return safe_uint_internal<uint32_t>(text, value, base);
1081 }
1082
safe_strtou64_base(absl::string_view text,uint64_t * value,int base)1083 bool safe_strtou64_base(absl::string_view text, uint64_t* value, int base) {
1084 return safe_uint_internal<uint64_t>(text, value, base);
1085 }
1086
safe_strtou128_base(absl::string_view text,uint128 * value,int base)1087 bool safe_strtou128_base(absl::string_view text, uint128* value, int base) {
1088 return safe_uint_internal<absl::uint128>(text, value, base);
1089 }
1090
1091 } // namespace numbers_internal
1092 ABSL_NAMESPACE_END
1093 } // namespace absl
1094