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