tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

numbers.cc (44035B)


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