Coverage Report

Created: 2026-05-23 07:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/abseil-cpp/absl/strings/numbers.cc
Line
Count
Source
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/macros.h"
39
#include "absl/base/nullability.h"
40
#include "absl/base/optimization.h"
41
#include "absl/numeric/bits.h"
42
#include "absl/numeric/int128.h"
43
#include "absl/strings/ascii.h"
44
#include "absl/strings/charconv.h"
45
#include "absl/strings/match.h"
46
#include "absl/strings/string_view.h"
47
48
namespace absl {
49
ABSL_NAMESPACE_BEGIN
50
51
0
bool SimpleAtof(absl::string_view str, float* absl_nonnull out) {
52
0
  *out = 0.0;
53
0
  str = StripAsciiWhitespace(str);
54
0
  if (str.empty()) {
55
    // absl::from_chars doesn't accept empty strings.
56
0
    return false;
57
0
  }
58
  // std::from_chars doesn't accept an initial +, but SimpleAtof does, so if one
59
  // is present, skip it, while avoiding accepting "+-0" as valid.
60
0
  if (str[0] == '+') {
61
0
    str.remove_prefix(1);
62
0
    if (str.empty() || str[0] == '-') {
63
0
      return false;
64
0
    }
65
0
  }
66
0
  auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
67
0
  if (result.ec == std::errc::invalid_argument) {
68
0
    return false;
69
0
  }
70
0
  if (result.ptr != str.data() + str.size()) {
71
    // not all non-whitespace characters consumed
72
0
    return false;
73
0
  }
74
  // from_chars() with DR 3081's current wording will return max() on
75
  // overflow.  SimpleAtof returns infinity instead.
76
0
  if (result.ec == std::errc::result_out_of_range) {
77
0
    if (*out > 1.0) {
78
0
      *out = std::numeric_limits<float>::infinity();
79
0
    } else if (*out < -1.0) {
80
0
      *out = -std::numeric_limits<float>::infinity();
81
0
    }
82
0
  }
83
0
  return true;
84
0
}
85
86
0
bool SimpleAtod(absl::string_view str, double* absl_nonnull out) {
87
0
  *out = 0.0;
88
0
  str = StripAsciiWhitespace(str);
89
0
  if (str.empty()) {
90
    // absl::from_chars doesn't accept empty strings.
91
0
    return false;
92
0
  }
93
  // std::from_chars doesn't accept an initial +, but SimpleAtod does, so if one
94
  // is present, skip it, while avoiding accepting "+-0" as valid.
95
0
  if (str[0] == '+') {
96
0
    str.remove_prefix(1);
97
0
    if (str.empty() || str[0] == '-') {
98
0
      return false;
99
0
    }
100
0
  }
101
0
  auto result = absl::from_chars(str.data(), str.data() + str.size(), *out);
102
0
  if (result.ec == std::errc::invalid_argument) {
103
0
    return false;
104
0
  }
105
0
  if (result.ptr != str.data() + str.size()) {
106
    // not all non-whitespace characters consumed
107
0
    return false;
108
0
  }
109
  // from_chars() with DR 3081's current wording will return max() on
110
  // overflow.  SimpleAtod returns infinity instead.
111
0
  if (result.ec == std::errc::result_out_of_range) {
112
0
    if (*out > 1.0) {
113
0
      *out = std::numeric_limits<double>::infinity();
114
0
    } else if (*out < -1.0) {
115
0
      *out = -std::numeric_limits<double>::infinity();
116
0
    }
117
0
  }
118
0
  return true;
119
0
}
120
121
0
bool SimpleAtob(absl::string_view str, bool* absl_nonnull out) {
122
0
  ABSL_RAW_CHECK(out != nullptr, "Output pointer must not be nullptr.");
123
0
  if (EqualsIgnoreCase(str, "true") || EqualsIgnoreCase(str, "t") ||
124
0
      EqualsIgnoreCase(str, "yes") || EqualsIgnoreCase(str, "y") ||
125
0
      EqualsIgnoreCase(str, "1")) {
126
0
    *out = true;
127
0
    return true;
128
0
  }
129
0
  if (EqualsIgnoreCase(str, "false") || EqualsIgnoreCase(str, "f") ||
130
0
      EqualsIgnoreCase(str, "no") || EqualsIgnoreCase(str, "n") ||
131
0
      EqualsIgnoreCase(str, "0")) {
132
0
    *out = false;
133
0
    return true;
134
0
  }
135
0
  return false;
136
0
}
137
138
// ----------------------------------------------------------------------
139
// FastIntToBuffer() overloads
140
//
141
// Like the Fast*ToBuffer() functions above, these are intended for speed.
142
// Unlike the Fast*ToBuffer() functions, however, these functions write
143
// their output to the beginning of the buffer.  The caller is responsible
144
// for ensuring that the buffer has enough space to hold the output.
145
//
146
// Returns a pointer to the end of the string (i.e. the null character
147
// terminating the string).
148
// ----------------------------------------------------------------------
149
150
namespace {
151
152
// Various routines to encode integers to strings.
153
154
// We split data encodings into a group of 2 digits, 4 digits, 8 digits as
155
// it's easier to combine powers of two into scalar arithmetic.
156
157
// Previous implementation used a lookup table of 200 bytes for every 2 bytes
158
// and it was memory bound, any L1 cache miss would result in a much slower
159
// result. When benchmarking with a cache eviction rate of several percent,
160
// this implementation proved to be better.
161
162
// These constants represent '00', '0000' and '00000000' as ascii strings in
163
// integers. We can add these numbers if we encode to bytes from 0 to 9. as
164
// 'i' = '0' + i for 0 <= i <= 9.
165
constexpr uint32_t kTwoZeroBytes = 0x0101 * '0';
166
constexpr uint64_t kFourZeroBytes = 0x01010101 * '0';
167
constexpr uint64_t kEightZeroBytes = 0x0101010101010101ull * '0';
168
169
// * 103 / 1024 is a division by 10 for values from 0 to 99. It's also a
170
// division of a structure [k takes 2 bytes][m takes 2 bytes], then * 103 / 1024
171
// will be [k / 10][m / 10]. It allows parallel division.
172
constexpr uint64_t kDivisionBy10Mul = 103u;
173
constexpr uint64_t kDivisionBy10Div = 1 << 10;
174
175
// * 10486 / 1048576 is a division by 100 for values from 0 to 9999.
176
constexpr uint64_t kDivisionBy100Mul = 10486u;
177
constexpr uint64_t kDivisionBy100Div = 1 << 20;
178
179
// Encode functions write the ASCII output of input `n` to `out_str`.
180
0
inline char* EncodeHundred(uint32_t n, char* absl_nonnull out_str) {
181
0
  int num_digits = static_cast<int>(n - 10) >> 8;
182
0
  uint32_t div10 = (n * kDivisionBy10Mul) / kDivisionBy10Div;
183
0
  uint32_t mod10 = n - 10u * div10;
184
0
  uint32_t base = kTwoZeroBytes + div10 + (mod10 << 8);
185
0
  base >>= num_digits & 8;
186
0
  little_endian::Store16(out_str, static_cast<uint16_t>(base));
187
0
  return out_str + 2 + num_digits;
188
0
}
189
190
0
inline char* EncodeTenThousand(uint32_t n, char* absl_nonnull out_str) {
191
  // We split lower 2 digits and upper 2 digits of n into 2 byte consecutive
192
  // blocks. 123 ->  [\0\1][\0\23]. We divide by 10 both blocks
193
  // (it's 1 division + zeroing upper bits), and compute modulo 10 as well "in
194
  // parallel". Then we combine both results to have both ASCII digits,
195
  // strip trailing zeros, add ASCII '0000' and return.
196
0
  uint32_t div100 = (n * kDivisionBy100Mul) / kDivisionBy100Div;
197
0
  uint32_t mod100 = n - 100ull * div100;
198
0
  uint32_t hundreds = (mod100 << 16) + div100;
199
0
  uint32_t tens = (hundreds * kDivisionBy10Mul) / kDivisionBy10Div;
200
0
  tens &= (0xFull << 16) | 0xFull;
201
0
  tens += (hundreds - 10ull * tens) << 8;
202
0
  ABSL_ASSUME(tens != 0);
203
  // The result can contain trailing zero bits, we need to strip them to a first
204
  // significant byte in a final representation. For example, for n = 123, we
205
  // have tens to have representation \0\1\2\3. We do `& -8` to round
206
  // to a multiple to 8 to strip zero bytes, not all zero bits.
207
  // countr_zero to help.
208
  // 0 minus 8 to make MSVC happy.
209
0
  uint32_t zeroes = static_cast<uint32_t>(absl::countr_zero(tens)) & (0 - 8u);
210
0
  tens += kFourZeroBytes;
211
0
  tens >>= zeroes;
212
0
  little_endian::Store32(out_str, tens);
213
0
  return out_str + sizeof(tens) - zeroes / 8;
214
0
}
215
216
// Helper function to produce an ASCII representation of `i`.
217
//
218
// Function returns an 8-byte integer which when summed with `kEightZeroBytes`,
219
// can be treated as a printable buffer with ascii representation of `i`,
220
// possibly with leading zeros.
221
//
222
// Example:
223
//
224
//  uint64_t buffer = PrepareEightDigits(102030) + kEightZeroBytes;
225
//  char* ascii = reinterpret_cast<char*>(&buffer);
226
//  // Note two leading zeros:
227
//  EXPECT_EQ(absl::string_view(ascii, 8), "00102030");
228
//
229
// Pre-condition: `i` must be less than 100000000.
230
731
inline uint64_t PrepareEightDigits(uint32_t i) {
231
731
  ABSL_ASSUME(i < 10000'0000);
232
  // Prepare 2 blocks of 4 digits "in parallel".
233
731
  uint32_t hi = i / 10000;
234
731
  uint32_t lo = i % 10000;
235
731
  uint64_t merged = hi | (uint64_t{lo} << 32);
236
731
  uint64_t div100 = ((merged * kDivisionBy100Mul) / kDivisionBy100Div) &
237
731
                    ((0x7Full << 32) | 0x7Full);
238
731
  uint64_t mod100 = merged - 100ull * div100;
239
731
  uint64_t hundreds = (mod100 << 16) + div100;
240
731
  uint64_t tens = (hundreds * kDivisionBy10Mul) / kDivisionBy10Div;
241
731
  tens &= (0xFull << 48) | (0xFull << 32) | (0xFull << 16) | 0xFull;
242
731
  tens += (hundreds - 10ull * tens) << 8;
243
731
  return tens;
244
731
}
245
246
247
// Encodes v to buffer as 16 digits padded with leading zeros.
248
// Pre-condition: v must be < 10^16.
249
0
inline char* EncodePadded16(uint64_t v, char* absl_nonnull buffer) {
250
0
  constexpr uint64_t k1e8 = 100000000;
251
0
  uint32_t hi = static_cast<uint32_t>(v / k1e8);
252
0
  uint32_t lo = static_cast<uint32_t>(v % k1e8);
253
0
  little_endian::Store64(buffer, PrepareEightDigits(hi) + kEightZeroBytes);
254
0
  little_endian::Store64(buffer + 8, PrepareEightDigits(lo) + kEightZeroBytes);
255
0
  return buffer + 16;
256
0
}
257
258
inline ABSL_ATTRIBUTE_ALWAYS_INLINE char* absl_nonnull EncodeFullU32(
259
14.7k
    uint32_t n, char* absl_nonnull out_str) {
260
14.7k
  if (n < 10) {
261
14.0k
    *out_str = static_cast<char>('0' + n);
262
14.0k
    return out_str + 1;
263
14.0k
  }
264
731
  if (n < 100'000'000) {
265
731
    uint64_t bottom = PrepareEightDigits(n);
266
731
    ABSL_ASSUME(bottom != 0);
267
    // 0 minus 8 to make MSVC happy.
268
731
    uint32_t zeroes =
269
731
        static_cast<uint32_t>(absl::countr_zero(bottom)) & (0 - 8u);
270
731
    little_endian::Store64(out_str, (bottom + kEightZeroBytes) >> zeroes);
271
731
    return out_str + sizeof(bottom) - zeroes / 8;
272
731
  }
273
0
  uint32_t div08 = n / 100'000'000;
274
0
  uint32_t mod08 = n % 100'000'000;
275
0
  uint64_t bottom = PrepareEightDigits(mod08) + kEightZeroBytes;
276
0
  out_str = EncodeHundred(div08, out_str);
277
0
  little_endian::Store64(out_str, bottom);
278
0
  return out_str + sizeof(bottom);
279
731
}
280
281
inline ABSL_ATTRIBUTE_ALWAYS_INLINE char* absl_nonnull EncodeFullU64(
282
0
    uint64_t i, char* absl_nonnull buffer) {
283
0
  if (i <= std::numeric_limits<uint32_t>::max()) {
284
0
    return EncodeFullU32(static_cast<uint32_t>(i), buffer);
285
0
  }
286
0
  uint32_t mod08;
287
0
  if (i < 1'0000'0000'0000'0000ull) {
288
0
    uint32_t div08 = static_cast<uint32_t>(i / 100'000'000ull);
289
0
    mod08 = static_cast<uint32_t>(i % 100'000'000ull);
290
0
    buffer = EncodeFullU32(div08, buffer);
291
0
  } else {
292
0
    uint64_t div08 = i / 100'000'000ull;
293
0
    mod08 = static_cast<uint32_t>(i % 100'000'000ull);
294
0
    uint32_t div016 = static_cast<uint32_t>(div08 / 100'000'000ull);
295
0
    uint32_t div08mod08 = static_cast<uint32_t>(div08 % 100'000'000ull);
296
0
    uint64_t mid_result = PrepareEightDigits(div08mod08) + kEightZeroBytes;
297
0
    buffer = EncodeTenThousand(div016, buffer);
298
0
    little_endian::Store64(buffer, mid_result);
299
0
    buffer += sizeof(mid_result);
300
0
  }
301
0
  uint64_t mod_result = PrepareEightDigits(mod08) + kEightZeroBytes;
302
0
  little_endian::Store64(buffer, mod_result);
303
0
  return buffer + sizeof(mod_result);
304
0
}
305
306
inline ABSL_ATTRIBUTE_ALWAYS_INLINE char* absl_nonnull EncodeFullU128(
307
0
    uint128 i, char* absl_nonnull buffer) {
308
0
  if (absl::Uint128High64(i) == 0) {
309
0
    return EncodeFullU64(absl::Uint128Low64(i), buffer);
310
0
  }
311
  // We divide the number into 16-digit chunks because `EncodePadded16` is
312
  // optimized to handle 16 digits at a time (as two 8-digit chunks).
313
0
  constexpr uint64_t k1e16 = uint64_t{10'000'000'000'000'000};
314
0
  uint128 high = i / k1e16;
315
0
  uint64_t low = absl::Uint128Low64(i % k1e16);
316
0
  uint64_t mid = absl::Uint128Low64(high % k1e16);
317
0
  high /= k1e16;
318
319
0
  if (high == 0) {
320
0
    buffer = EncodeFullU64(mid, buffer);
321
0
    buffer = EncodePadded16(low, buffer);
322
0
  } else {
323
0
    buffer = EncodeFullU64(absl::Uint128Low64(high), buffer);
324
0
    buffer = EncodePadded16(mid, buffer);
325
0
    buffer = EncodePadded16(low, buffer);
326
0
  }
327
0
  return buffer;
328
0
}
329
330
}  // namespace
331
332
0
void numbers_internal::PutTwoDigits(uint32_t i, char* absl_nonnull buf) {
333
0
  assert(i < 100);
334
0
  uint32_t base = kTwoZeroBytes;
335
0
  uint32_t div10 = (i * kDivisionBy10Mul) / kDivisionBy10Div;
336
0
  uint32_t mod10 = i - 10u * div10;
337
0
  base += div10 + (mod10 << 8);
338
0
  little_endian::Store16(buf, static_cast<uint16_t>(base));
339
0
}
340
341
char* absl_nonnull numbers_internal::FastIntToBuffer(
342
0
    uint32_t n, char* absl_nonnull out_str) {
343
0
  out_str = EncodeFullU32(n, out_str);
344
0
  *out_str = '\0';
345
0
  return out_str;
346
0
}
347
348
char* absl_nonnull numbers_internal::FastIntToBuffer(
349
14.7k
    int32_t i, char* absl_nonnull buffer) {
350
14.7k
  uint32_t u = static_cast<uint32_t>(i);
351
14.7k
  if (i < 0) {
352
0
    *buffer++ = '-';
353
    // We need to do the negation in modular (i.e., "unsigned")
354
    // arithmetic; MSVC++ apparently warns for plain "-u", so
355
    // we write the equivalent expression "0 - u" instead.
356
0
    u = 0 - u;
357
0
  }
358
14.7k
  buffer = EncodeFullU32(u, buffer);
359
14.7k
  *buffer = '\0';
360
14.7k
  return buffer;
361
14.7k
}
362
363
char* absl_nonnull numbers_internal::FastIntToBuffer(
364
0
    uint64_t i, char* absl_nonnull buffer) {
365
0
  buffer = EncodeFullU64(i, buffer);
366
0
  *buffer = '\0';
367
0
  return buffer;
368
0
}
369
370
char* absl_nonnull numbers_internal::FastIntToBuffer(
371
0
    int64_t i, char* absl_nonnull buffer) {
372
0
  uint64_t u = static_cast<uint64_t>(i);
373
0
  if (i < 0) {
374
0
    *buffer++ = '-';
375
    // We need to do the negation in modular (i.e., "unsigned")
376
    // arithmetic; MSVC++ apparently warns for plain "-u", so
377
    // we write the equivalent expression "0 - u" instead.
378
0
    u = 0 - u;
379
0
  }
380
0
  buffer = EncodeFullU64(u, buffer);
381
0
  *buffer = '\0';
382
0
  return buffer;
383
0
}
384
385
char* absl_nonnull numbers_internal::FastIntToBuffer(
386
0
    uint128 i, char* absl_nonnull buffer) {
387
0
  buffer = EncodeFullU128(i, buffer);
388
0
  *buffer = '\0';
389
0
  return buffer;
390
0
}
391
392
char* absl_nonnull numbers_internal::FastIntToBuffer(
393
0
    int128 i, char* absl_nonnull buffer) {
394
0
  uint128 u = static_cast<uint128>(i);
395
0
  if (i < 0) {
396
0
    *buffer++ = '-';
397
0
    u = -u;
398
0
  }
399
0
  buffer = EncodeFullU128(u, buffer);
400
0
  *buffer = '\0';
401
0
  return buffer;
402
0
}
403
404
// Although DBL_DIG is typically 15, DBL_MAX is normally represented with 17
405
// digits of precision. When converted to a string value with fewer digits
406
// of precision using strtod(), the result can be bigger than DBL_MAX due to
407
// a rounding error. Converting this value back to a double will produce an
408
// Inf which will trigger a SIGFPE if FP exceptions are enabled. We skip
409
// the precision check for sufficiently large values to avoid the SIGFPE.
410
static constexpr double kDoublePrecisionCheckMax =
411
    std::numeric_limits<double>::max() / 1.000000000000001;
412
413
char* absl_nonnull numbers_internal::RoundTripDoubleToBuffer(
414
0
    double d, char* absl_nonnull buffer) {
415
  // DBL_DIG is 15 for IEEE-754 doubles, which are used on almost all
416
  // platforms these days.  Just in case some system exists where DBL_DIG
417
  // is significantly larger -- and risks overflowing our buffer -- we have
418
  // this assert.
419
0
  static_assert(std::numeric_limits<double>::digits10 < 20,
420
0
                "std::numeric_limits<double>::digits10 is too big");
421
422
  // We avoid snprintf for NaNs because it inconsistently outputs "-nan"
423
  // or "nan" when given a nan with the sign bit set.
424
0
  if (std::isnan(d)) {
425
0
    strcpy(buffer, "nan");  // NOLINT(runtime/printf)
426
0
    return buffer;
427
0
  }
428
0
  bool full_precision_needed = true;
429
0
  if (std::abs(d) <= kDoublePrecisionCheckMax) {
430
0
    int snprintf_result =
431
0
        snprintf(buffer, numbers_internal::kFastToBufferSize, "%.*g",
432
0
                 std::numeric_limits<double>::digits10, d);
433
434
    // The snprintf should never overflow because the buffer is significantly
435
    // larger than the precision we asked for.
436
0
    ABSL_ASSERT(snprintf_result > 0 &&
437
0
                snprintf_result < numbers_internal::kFastToBufferSize);
438
439
0
    full_precision_needed = strtod(buffer, nullptr) != d;
440
0
  }
441
442
0
  if (full_precision_needed) {
443
0
    int snprintf_result =
444
0
        snprintf(buffer, numbers_internal::kFastToBufferSize, "%.*g",
445
0
                 std::numeric_limits<double>::digits10 + 2, d);
446
447
    // Should never overflow; see above.
448
0
    ABSL_ASSERT(snprintf_result > 0 &&
449
0
                snprintf_result < numbers_internal::kFastToBufferSize);
450
0
  }
451
0
  return buffer;
452
0
}
453
454
namespace {
455
456
// This table is used to quickly calculate the base-ten exponent of a given
457
// float, and then to provide a multiplier to bring that number into the
458
// range 1-999,999,999, that is, into uint32_t range.  Finally, the exp
459
// string is made available so there is one less int-to-string conversion
460
// to be done.
461
struct Spec {
462
  double min_range;
463
  double multiplier;
464
  const char expstr[5];
465
};
466
467
// clang-format off
468
constexpr Spec kNegExpTable[] = {
469
    Spec{1.4e-45f, 1e+55, "e-45"},
470
    Spec{1e-44f, 1e+54, "e-44"},
471
    Spec{1e-43f, 1e+53, "e-43"},
472
    Spec{1e-42f, 1e+52, "e-42"},
473
    Spec{1e-41f, 1e+51, "e-41"},
474
    Spec{1e-40f, 1e+50, "e-40"},
475
    Spec{1e-39f, 1e+49, "e-39"},
476
    Spec{1e-38f, 1e+48, "e-38"},
477
    Spec{1e-37f, 1e+47, "e-37"},
478
    Spec{1e-36f, 1e+46, "e-36"},
479
    Spec{1e-35f, 1e+45, "e-35"},
480
    Spec{1e-34f, 1e+44, "e-34"},
481
    Spec{1e-33f, 1e+43, "e-33"},
482
    Spec{1e-32f, 1e+42, "e-32"},
483
    Spec{1e-31f, 1e+41, "e-31"},
484
    Spec{1e-30f, 1e+40, "e-30"},
485
    Spec{1e-29f, 1e+39, "e-29"},
486
    Spec{1e-28f, 1e+38, "e-28"},
487
    Spec{1e-27f, 1e+37, "e-27"},
488
    Spec{1e-26f, 1e+36, "e-26"},
489
    Spec{1e-25f, 1e+35, "e-25"},
490
    Spec{1e-24f, 1e+34, "e-24"},
491
    Spec{1e-23f, 1e+33, "e-23"},
492
    Spec{1e-22f, 1e+32, "e-22"},
493
    Spec{1e-21f, 1e+31, "e-21"},
494
    Spec{1e-20f, 1e+30, "e-20"},
495
    Spec{1e-19f, 1e+29, "e-19"},
496
    Spec{1e-18f, 1e+28, "e-18"},
497
    Spec{1e-17f, 1e+27, "e-17"},
498
    Spec{1e-16f, 1e+26, "e-16"},
499
    Spec{1e-15f, 1e+25, "e-15"},
500
    Spec{1e-14f, 1e+24, "e-14"},
501
    Spec{1e-13f, 1e+23, "e-13"},
502
    Spec{1e-12f, 1e+22, "e-12"},
503
    Spec{1e-11f, 1e+21, "e-11"},
504
    Spec{1e-10f, 1e+20, "e-10"},
505
    Spec{1e-09f, 1e+19, "e-09"},
506
    Spec{1e-08f, 1e+18, "e-08"},
507
    Spec{1e-07f, 1e+17, "e-07"},
508
    Spec{1e-06f, 1e+16, "e-06"},
509
    Spec{1e-05f, 1e+15, "e-05"},
510
    Spec{1e-04f, 1e+14, "e-04"},
511
};
512
// clang-format on
513
514
// clang-format off
515
constexpr Spec kPosExpTable[] = {
516
    Spec{1e+08f, 1e+02, "e+08"},
517
    Spec{1e+09f, 1e+01, "e+09"},
518
    Spec{1e+10f, 1e+00, "e+10"},
519
    Spec{1e+11f, 1e-01, "e+11"},
520
    Spec{1e+12f, 1e-02, "e+12"},
521
    Spec{1e+13f, 1e-03, "e+13"},
522
    Spec{1e+14f, 1e-04, "e+14"},
523
    Spec{1e+15f, 1e-05, "e+15"},
524
    Spec{1e+16f, 1e-06, "e+16"},
525
    Spec{1e+17f, 1e-07, "e+17"},
526
    Spec{1e+18f, 1e-08, "e+18"},
527
    Spec{1e+19f, 1e-09, "e+19"},
528
    Spec{1e+20f, 1e-10, "e+20"},
529
    Spec{1e+21f, 1e-11, "e+21"},
530
    Spec{1e+22f, 1e-12, "e+22"},
531
    Spec{1e+23f, 1e-13, "e+23"},
532
    Spec{1e+24f, 1e-14, "e+24"},
533
    Spec{1e+25f, 1e-15, "e+25"},
534
    Spec{1e+26f, 1e-16, "e+26"},
535
    Spec{1e+27f, 1e-17, "e+27"},
536
    Spec{1e+28f, 1e-18, "e+28"},
537
    Spec{1e+29f, 1e-19, "e+29"},
538
    Spec{1e+30f, 1e-20, "e+30"},
539
    Spec{1e+31f, 1e-21, "e+31"},
540
    Spec{1e+32f, 1e-22, "e+32"},
541
    Spec{1e+33f, 1e-23, "e+33"},
542
    Spec{1e+34f, 1e-24, "e+34"},
543
    Spec{1e+35f, 1e-25, "e+35"},
544
    Spec{1e+36f, 1e-26, "e+36"},
545
    Spec{1e+37f, 1e-27, "e+37"},
546
    Spec{1e+38f, 1e-28, "e+38"},
547
    Spec{1e+39,  1e-29, "e+39"},
548
};
549
// clang-format on
550
551
struct ExpCompare {
552
0
  bool operator()(const Spec& spec, double d) const {
553
0
    return spec.min_range < d;
554
0
  }
555
};
556
557
}  // namespace
558
559
// Utility routine(s) for RoundTripFloatToBuffer:
560
// OutputNecessaryDigits takes two 11-digit numbers, whose integer portion
561
// represents the fractional part of a floating-point number, and outputs a
562
// number that is in-between them, with the fewest digits possible. For
563
// instance, given 12345678900 and 12345876900, it would output "0123457".
564
// When there are multiple final digits that would satisfy this requirement,
565
// this routine attempts to use a digit that would represent the average of
566
// lower_double and upper_double.
567
//
568
// Although the routine works using integers, all callers use doubles, so
569
// for their convenience this routine accepts doubles.
570
static char* absl_nonnull OutputNecessaryDigits(double lower_double,
571
                                                double upper_double,
572
0
                                                char* absl_nonnull out) {
573
0
  assert(lower_double > 0);
574
0
  assert(lower_double < upper_double - 10);
575
0
  assert(upper_double < 100000000000.0);
576
577
  // Narrow the range a bit; without this bias, an input of lower=87654320010.0
578
  // and upper=87654320100.0 would produce an output of 876543201
579
  //
580
  // We do this in three steps: first, we lower the upper bound and truncate it
581
  // to an integer.  Then, we increase the lower bound by exactly the amount we
582
  // just decreased the upper bound by - at that point, the midpoint is exactly
583
  // where it used to be.  Then we truncate the lower bound.
584
585
0
  uint64_t upper64 = static_cast<uint64_t>(upper_double - (1.0 / 1024));
586
0
  double shrink = upper_double - upper64;
587
0
  uint64_t lower64 = static_cast<uint64_t>(lower_double + shrink);
588
589
  // Theory of operation: we convert the lower number to ascii representation,
590
  // two digits at a time.  As we go, we remove the same digits from the upper
591
  // number.  When we see the upper number does not share those same digits, we
592
  // know we can stop converting. When we stop, the last digit we output is
593
  // taken from the average of upper and lower values, rounded up.
594
0
  char buf[2];
595
0
  uint32_t lodigits =
596
0
      static_cast<uint32_t>(lower64 / 1000000000);  // 1,000,000,000
597
0
  uint64_t mul64 = lodigits * uint64_t{1000000000};
598
599
0
  numbers_internal::PutTwoDigits(lodigits, out);
600
0
  out += 2;
601
0
  if (upper64 - mul64 >= 1000000000) {  // digit mismatch!
602
0
    numbers_internal::PutTwoDigits(static_cast<uint32_t>(upper64 / 1000000000),
603
0
                                   buf);
604
0
    if (out[-2] != buf[0]) {
605
0
      out[-2] = static_cast<char>('0' + (upper64 + lower64 + 10000000000) /
606
0
                                            20000000000);
607
0
      --out;
608
0
    } else {
609
0
      numbers_internal::PutTwoDigits(
610
0
          static_cast<uint32_t>((upper64 + lower64 + 1000000000) / 2000000000),
611
0
          out - 2);
612
0
    }
613
0
    *out = '\0';
614
0
    return out;
615
0
  }
616
0
  uint32_t lower = static_cast<uint32_t>(lower64 - mul64);
617
0
  uint32_t upper = static_cast<uint32_t>(upper64 - mul64);
618
619
0
  lodigits = lower / 10000000;  // 10,000,000
620
0
  uint32_t mul = lodigits * 10000000;
621
0
  numbers_internal::PutTwoDigits(lodigits, out);
622
0
  out += 2;
623
0
  if (upper - mul >= 10000000) {  // digit mismatch!
624
0
    numbers_internal::PutTwoDigits(upper / 10000000, buf);
625
0
    if (out[-2] != buf[0]) {
626
0
      out[-2] = '0' + (upper + lower + 100000000) / 200000000;
627
0
      --out;
628
0
    } else {
629
0
      numbers_internal::PutTwoDigits((upper + lower + 10000000) / 20000000,
630
0
                                      out - 2);
631
0
    }
632
0
    *out = '\0';
633
0
    return out;
634
0
  }
635
0
  lower -= mul;
636
0
  upper -= mul;
637
638
0
  lodigits = lower / 100000;  // 100,000
639
0
  mul = lodigits * 100000;
640
0
  numbers_internal::PutTwoDigits(lodigits, out);
641
0
  out += 2;
642
0
  if (upper - mul >= 100000) {  // digit mismatch!
643
0
    numbers_internal::PutTwoDigits(upper / 100000, buf);
644
0
    if (out[-2] != buf[0]) {
645
0
      out[-2] = static_cast<char>('0' + (upper + lower + 1000000) / 2000000);
646
0
      --out;
647
0
    } else {
648
0
      numbers_internal::PutTwoDigits((upper + lower + 100000) / 200000,
649
0
                                      out - 2);
650
0
    }
651
0
    *out = '\0';
652
0
    return out;
653
0
  }
654
0
  lower -= mul;
655
0
  upper -= mul;
656
657
0
  lodigits = lower / 1000;
658
0
  mul = lodigits * 1000;
659
0
  numbers_internal::PutTwoDigits(lodigits, out);
660
0
  out += 2;
661
0
  if (upper - mul >= 1000) {  // digit mismatch!
662
0
    numbers_internal::PutTwoDigits(upper / 1000, buf);
663
0
    if (out[-2] != buf[0]) {
664
0
      out[-2] = static_cast<char>('0' + (upper + lower + 10000) / 20000);
665
0
      --out;
666
0
    } else {
667
0
      numbers_internal::PutTwoDigits((upper + lower + 1000) / 2000, out - 2);
668
0
    }
669
0
    *out = '\0';
670
0
    return out;
671
0
  }
672
0
  lower -= mul;
673
0
  upper -= mul;
674
675
0
  numbers_internal::PutTwoDigits(lower / 10, out);
676
0
  out += 2;
677
0
  numbers_internal::PutTwoDigits(upper / 10, buf);
678
0
  if (out[-2] != buf[0]) {
679
0
    out[-2] = static_cast<char>('0' + (upper + lower + 100) / 200);
680
0
    --out;
681
0
  } else {
682
0
    numbers_internal::PutTwoDigits((upper + lower + 10) / 20, out - 2);
683
0
  }
684
0
  *out = '\0';
685
0
  return out;
686
0
}
687
688
// RoundTripFloatToBuffer converts the given float into a string which, if
689
// passed to strtof, will produce the exact same original float.  It does this
690
// by computing the range of possible doubles which map to the given float, and
691
// then examining the digits of the doubles in that range.  If all the doubles
692
// in the range start with "2.37", then clearly our float does, too.  As soon as
693
// they diverge, only one more digit is needed.
694
char* absl_nonnull numbers_internal::RoundTripFloatToBuffer(
695
0
    float f, char* absl_nonnull buffer) {
696
0
  static_assert(std::numeric_limits<float>::is_iec559,
697
0
                "IEEE-754/IEC-559 support only");
698
699
  // We write data to out, incrementing as we go, but FloatToBuffer always
700
  // returns the address of the buffer passed in.
701
0
  char* out = buffer;
702
703
0
  if (std::isnan(f)) {
704
0
    strcpy(out, "nan");  // NOLINT(runtime/printf)
705
0
    return buffer;
706
0
  }
707
0
  if (f == 0) {  // +0 and -0 are handled here
708
0
    if (std::signbit(f)) {
709
0
      strcpy(out, "-0");  // NOLINT(runtime/printf)
710
0
    } else {
711
0
      strcpy(out, "0");  // NOLINT(runtime/printf)
712
0
    }
713
0
    return buffer;
714
0
  }
715
0
  if (f < 0) {
716
0
    *out++ = '-';
717
0
    f = -f;
718
0
  }
719
0
  if (f > std::numeric_limits<float>::max()) {
720
0
    strcpy(out, "inf");  // NOLINT(runtime/printf)
721
0
    return buffer;
722
0
  }
723
724
0
  double next_lower = nextafterf(f, 0.0f);
725
  // For all doubles in the range lower_bound < f < upper_bound, the
726
  // nearest float is f.
727
0
  double lower_bound = (f + next_lower) * 0.5;
728
0
  double upper_bound = f + (f - lower_bound);
729
  // Note: because std::nextafter is slow, we calculate upper_bound
730
  // assuming that it is the same distance from f as lower_bound is.
731
  // For exact powers of two, upper_bound is actually twice as far
732
  // from f as lower_bound is, but this turns out not to matter.
733
734
  // Most callers pass floats that are either 0 or within the
735
  // range 0.0001 through 100,000,000, so handle those first,
736
  // since they don't need exponential notation.
737
0
  const Spec* spec = nullptr;
738
0
  if (f < 1.0) {
739
0
    if (f >= 0.0001f) {
740
      // For fractional values, we set up the multiplier at the same
741
      // time as we output the leading "0." / "0.0" / "0.00" / "0.000"
742
0
      double multiplier = 1e+11;
743
0
      *out++ = '0';
744
0
      *out++ = '.';
745
0
      if (f < 0.1f) {
746
0
        multiplier = 1e+12;
747
0
        *out++ = '0';
748
0
        if (f < 0.01f) {
749
0
          multiplier = 1e+13;
750
0
          *out++ = '0';
751
0
          if (f < 0.001f) {
752
0
            multiplier = 1e+14;
753
0
            *out++ = '0';
754
0
          }
755
0
        }
756
0
      }
757
0
      OutputNecessaryDigits(lower_bound * multiplier, upper_bound * multiplier,
758
0
                            out);
759
0
      return buffer;
760
0
    }
761
0
    spec = std::lower_bound(std::begin(kNegExpTable), std::end(kNegExpTable),
762
0
                            double{f}, ExpCompare());
763
0
    if (spec == std::end(kNegExpTable)) --spec;
764
0
  } else if (f < 1e8) {
765
    // Handling non-exponential format greater than 1.0 is similar to the above,
766
    // but instead of 0.0 / 0.00 / 0.000, the prefix is simply the truncated
767
    // integer part of f.
768
0
    int32_t as_int = static_cast<int32_t>(f);
769
0
    out = numbers_internal::FastIntToBuffer(as_int, out);
770
    // Easy: if the integer part is within (lower_bound, upper_bound), then we
771
    // are already done.
772
0
    if (as_int > lower_bound && as_int < upper_bound) {
773
0
      return buffer;
774
0
    }
775
0
    *out++ = '.';
776
0
    OutputNecessaryDigits((lower_bound - as_int) * 1e11,
777
0
                          (upper_bound - as_int) * 1e11, out);
778
0
    return buffer;
779
0
  } else {
780
0
    spec = std::lower_bound(std::begin(kPosExpTable), std::end(kPosExpTable),
781
0
                            double{f}, ExpCompare());
782
0
    if (spec == std::end(kPosExpTable)) --spec;
783
0
  }
784
  // Exponential notation from here on.  "spec" was computed using lower_bound,
785
  // which means it's the first spec from the table where min_range is greater
786
  // or equal to f.
787
  // Unfortunately that's not quite what we want; we want a min_range that is
788
  // less or equal.  So first thing, if it was greater, back up one entry.
789
0
  if (spec->min_range > f) --spec;
790
791
  // The digits might be "237000123", but we want "2.37000123",
792
  // so we output the digits one character later, and then move the first
793
  // digit back so we can stick the "." in.
794
0
  char* start = out;
795
0
  out = OutputNecessaryDigits(lower_bound * spec->multiplier,
796
0
                              upper_bound * spec->multiplier, start + 1);
797
0
  start[0] = start[1];
798
0
  start[1] = '.';
799
800
  // If it turns out there was only one digit output, then back up over the '.'
801
0
  if (out == &start[2]) --out;
802
803
  // Now add the "e+NN" part.
804
0
  memcpy(out, spec->expstr, 4);
805
0
  out[4] = '\0';
806
0
  return buffer;
807
0
}
808
809
// Given a 128-bit number expressed as a pair of uint64_t, high half first,
810
// return that number multiplied by the given 32-bit value.  If the result is
811
// too large to fit in a 128-bit number, divide it by 2 until it fits.
812
static std::pair<uint64_t, uint64_t> Mul32(std::pair<uint64_t, uint64_t> num,
813
0
                                           uint32_t mul) {
814
0
  uint64_t bits0_31 = num.second & 0xFFFFFFFF;
815
0
  uint64_t bits32_63 = num.second >> 32;
816
0
  uint64_t bits64_95 = num.first & 0xFFFFFFFF;
817
0
  uint64_t bits96_127 = num.first >> 32;
818
819
  // The picture so far: each of these 64-bit values has only the lower 32 bits
820
  // filled in.
821
  // bits96_127:          [ 00000000 xxxxxxxx ]
822
  // bits64_95:                    [ 00000000 xxxxxxxx ]
823
  // bits32_63:                             [ 00000000 xxxxxxxx ]
824
  // bits0_31:                                       [ 00000000 xxxxxxxx ]
825
826
0
  bits0_31 *= mul;
827
0
  bits32_63 *= mul;
828
0
  bits64_95 *= mul;
829
0
  bits96_127 *= mul;
830
831
  // Now the top halves may also have value, though all 64 of their bits will
832
  // never be set at the same time, since they are a result of a 32x32 bit
833
  // multiply.  This makes the carry calculation slightly easier.
834
  // bits96_127:          [ mmmmmmmm | mmmmmmmm ]
835
  // bits64_95:                    [ | mmmmmmmm mmmmmmmm | ]
836
  // bits32_63:                      |        [ mmmmmmmm | mmmmmmmm ]
837
  // bits0_31:                       |                 [ | mmmmmmmm mmmmmmmm ]
838
  // eventually:        [ bits128_up | ...bits64_127.... | ..bits0_63... ]
839
840
0
  uint64_t bits0_63 = bits0_31 + (bits32_63 << 32);
841
0
  uint64_t bits64_127 = bits64_95 + (bits96_127 << 32) + (bits32_63 >> 32) +
842
0
                        (bits0_63 < bits0_31);
843
0
  uint64_t bits128_up = (bits96_127 >> 32) + (bits64_127 < bits64_95);
844
0
  if (bits128_up == 0) return {bits64_127, bits0_63};
845
846
0
  auto shift = static_cast<unsigned>(bit_width(bits128_up));
847
0
  uint64_t lo = (bits0_63 >> shift) + (bits64_127 << (64 - shift));
848
0
  uint64_t hi = (bits64_127 >> shift) + (bits128_up << (64 - shift));
849
0
  return {hi, lo};
850
0
}
851
852
// Compute num * 5 ^ expfive, and return the first 128 bits of the result,
853
// where the first bit is always a one.  So PowFive(1, 0) starts 0b100000,
854
// PowFive(1, 1) starts 0b101000, PowFive(1, 2) starts 0b110010, etc.
855
0
static std::pair<uint64_t, uint64_t> PowFive(uint64_t num, int expfive) {
856
0
  std::pair<uint64_t, uint64_t> result = {num, 0};
857
0
  while (expfive >= 13) {
858
    // 5^13 is the highest power of five that will fit in a 32-bit integer.
859
0
    result = Mul32(result, 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5);
860
0
    expfive -= 13;
861
0
  }
862
0
  constexpr uint32_t powers_of_five[13] = {
863
0
      1,
864
0
      5,
865
0
      5 * 5,
866
0
      5 * 5 * 5,
867
0
      5 * 5 * 5 * 5,
868
0
      5 * 5 * 5 * 5 * 5,
869
0
      5 * 5 * 5 * 5 * 5 * 5,
870
0
      5 * 5 * 5 * 5 * 5 * 5 * 5,
871
0
      5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
872
0
      5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
873
0
      5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
874
0
      5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5,
875
0
      5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5};
876
0
  result = Mul32(result, powers_of_five[expfive & 15]);
877
0
  int shift = countl_zero(result.first);
878
0
  if (shift != 0) {
879
0
    result.first = (result.first << shift) + (result.second >> (64 - shift));
880
0
    result.second = (result.second << shift);
881
0
  }
882
0
  return result;
883
0
}
884
885
struct ExpDigits {
886
  int32_t exponent;
887
  char digits[6];
888
};
889
890
// SplitToSix converts value, a positive double-precision floating-point number,
891
// into a base-10 exponent and 6 ASCII digits, where the first digit is never
892
// zero.  For example, SplitToSix(1) returns an exponent of zero and a digits
893
// array of {'1', '0', '0', '0', '0', '0'}.  If value is exactly halfway between
894
// two possible representations, e.g. value = 100000.5, then "round to even" is
895
// performed.
896
0
static ExpDigits SplitToSix(const double value) {
897
0
  ExpDigits exp_dig;
898
0
  int exp = 5;
899
0
  double d = value;
900
  // First step: calculate a close approximation of the output, where the
901
  // value d will be between 100,000 and 999,999, representing the digits
902
  // in the output ASCII array, and exp is the base-10 exponent.  It would be
903
  // faster to use a table here, and to look up the base-2 exponent of value,
904
  // however value is an IEEE-754 64-bit number, so the table would have 2,000
905
  // entries, which is not cache-friendly.
906
0
  if (d >= 999999.5) {
907
0
    if (d >= 1e+261) exp += 256, d *= 1e-256;
908
0
    if (d >= 1e+133) exp += 128, d *= 1e-128;
909
0
    if (d >= 1e+69) exp += 64, d *= 1e-64;
910
0
    if (d >= 1e+37) exp += 32, d *= 1e-32;
911
0
    if (d >= 1e+21) exp += 16, d *= 1e-16;
912
0
    if (d >= 1e+13) exp += 8, d *= 1e-8;
913
0
    if (d >= 1e+9) exp += 4, d *= 1e-4;
914
0
    if (d >= 1e+7) exp += 2, d *= 1e-2;
915
0
    if (d >= 1e+6) exp += 1, d *= 1e-1;
916
0
  } else {
917
0
    if (d < 1e-250) exp -= 256, d *= 1e256;
918
0
    if (d < 1e-122) exp -= 128, d *= 1e128;
919
0
    if (d < 1e-58) exp -= 64, d *= 1e64;
920
0
    if (d < 1e-26) exp -= 32, d *= 1e32;
921
0
    if (d < 1e-10) exp -= 16, d *= 1e16;
922
0
    if (d < 1e-2) exp -= 8, d *= 1e8;
923
0
    if (d < 1e+2) exp -= 4, d *= 1e4;
924
0
    if (d < 1e+4) exp -= 2, d *= 1e2;
925
0
    if (d < 1e+5) exp -= 1, d *= 1e1;
926
0
  }
927
  // At this point, d is in the range [99999.5..999999.5) and exp is in the
928
  // range [-324..308]. Since we need to round d up, we want to add a half
929
  // and truncate.
930
  // However, the technique above may have lost some precision, due to its
931
  // repeated multiplication by constants that each may be off by half a bit
932
  // of precision.  This only matters if we're close to the edge though.
933
  // Since we'd like to know if the fractional part of d is close to a half,
934
  // we multiply it by 65536 and see if the fractional part is close to 32768.
935
  // (The number doesn't have to be a power of two,but powers of two are faster)
936
0
  uint64_t d64k = static_cast<uint64_t>(d * 65536);
937
0
  uint32_t dddddd;  // A 6-digit decimal integer.
938
0
  if ((d64k % 65536) == 32767 || (d64k % 65536) == 32768) {
939
    // OK, it's fairly likely that precision was lost above, which is
940
    // not a surprise given only 52 mantissa bits are available.  Therefore
941
    // redo the calculation using 128-bit numbers.  (64 bits are not enough).
942
943
    // Start out with digits rounded down; maybe add one below.
944
0
    dddddd = static_cast<uint32_t>(d64k / 65536);
945
946
    // mantissa is a 64-bit integer representing M.mmm... * 2^63.  The actual
947
    // value we're representing, of course, is M.mmm... * 2^exp2.
948
0
    int exp2;
949
0
    double m = std::frexp(value, &exp2);
950
0
    uint64_t mantissa =
951
0
        static_cast<uint64_t>(m * (32768.0 * 65536.0 * 65536.0 * 65536.0));
952
    // std::frexp returns an m value in the range [0.5, 1.0), however we
953
    // can't multiply it by 2^64 and convert to an integer because some FPUs
954
    // throw an exception when converting an number higher than 2^63 into an
955
    // integer - even an unsigned 64-bit integer!  Fortunately it doesn't matter
956
    // since m only has 52 significant bits anyway.
957
0
    mantissa <<= 1;
958
0
    exp2 -= 64;  // not needed, but nice for debugging
959
960
    // OK, we are here to compare:
961
    //     (dddddd + 0.5) * 10^(exp-5)  vs.  mantissa * 2^exp2
962
    // so we can round up dddddd if appropriate.  Those values span the full
963
    // range of 600 orders of magnitude of IEE 64-bit floating-point.
964
    // Fortunately, we already know they are very close, so we don't need to
965
    // track the base-2 exponent of both sides.  This greatly simplifies the
966
    // the math since the 2^exp2 calculation is unnecessary and the power-of-10
967
    // calculation can become a power-of-5 instead.
968
969
0
    std::pair<uint64_t, uint64_t> edge, val;
970
0
    if (exp >= 6) {
971
      // Compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa
972
      // Since we're tossing powers of two, 2 * dddddd + 1 is the
973
      // same as dddddd + 0.5
974
0
      edge = PowFive(2 * dddddd + 1, exp - 5);
975
976
0
      val.first = mantissa;
977
0
      val.second = 0;
978
0
    } else {
979
      // We can't compare (dddddd + 0.5) * 5 ^ (exp - 5) to mantissa as we did
980
      // above because (exp - 5) is negative.  So we compare (dddddd + 0.5) to
981
      // mantissa * 5 ^ (5 - exp)
982
0
      edge = PowFive(2 * dddddd + 1, 0);
983
984
0
      val = PowFive(mantissa, 5 - exp);
985
0
    }
986
    // printf("exp=%d %016lx %016lx vs %016lx %016lx\n", exp, val.first,
987
    //        val.second, edge.first, edge.second);
988
0
    if (val > edge) {
989
0
      dddddd++;
990
0
    } else if (val == edge) {
991
0
      dddddd += (dddddd & 1);
992
0
    }
993
0
  } else {
994
    // Here, we are not close to the edge.
995
0
    dddddd = static_cast<uint32_t>((d64k + 32768) / 65536);
996
0
  }
997
0
  if (dddddd == 1000000) {
998
0
    dddddd = 100000;
999
0
    exp += 1;
1000
0
  }
1001
0
  exp_dig.exponent = exp;
1002
1003
0
  uint32_t two_digits = dddddd / 10000;
1004
0
  dddddd -= two_digits * 10000;
1005
0
  numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[0]);
1006
1007
0
  two_digits = dddddd / 100;
1008
0
  dddddd -= two_digits * 100;
1009
0
  numbers_internal::PutTwoDigits(two_digits, &exp_dig.digits[2]);
1010
1011
0
  numbers_internal::PutTwoDigits(dddddd, &exp_dig.digits[4]);
1012
0
  return exp_dig;
1013
0
}
1014
1015
// Helper function for fast formatting of floating-point.
1016
// The result is the same as "%g", a.k.a. "%.6g".
1017
size_t numbers_internal::SixDigitsToBuffer(double d,
1018
0
                                           char* absl_nonnull const buffer) {
1019
0
  static_assert(std::numeric_limits<float>::is_iec559,
1020
0
                "IEEE-754/IEC-559 support only");
1021
1022
0
  char* out = buffer;  // we write data to out, incrementing as we go, but
1023
                       // FloatToBuffer always returns the address of the buffer
1024
                       // passed in.
1025
1026
0
  if (std::isnan(d)) {
1027
0
    strcpy(out, "nan");  // NOLINT(runtime/printf)
1028
0
    return 3;
1029
0
  }
1030
0
  if (d == 0) {  // +0 and -0 are handled here
1031
0
    if (std::signbit(d)) *out++ = '-';
1032
0
    *out++ = '0';
1033
0
    *out = 0;
1034
0
    return static_cast<size_t>(out - buffer);
1035
0
  }
1036
0
  if (d < 0) {
1037
0
    *out++ = '-';
1038
0
    d = -d;
1039
0
  }
1040
0
  if (d > std::numeric_limits<double>::max()) {
1041
0
    strcpy(out, "inf");  // NOLINT(runtime/printf)
1042
0
    return static_cast<size_t>(out + 3 - buffer);
1043
0
  }
1044
1045
0
  auto exp_dig = SplitToSix(d);
1046
0
  int exp = exp_dig.exponent;
1047
0
  const char* digits = exp_dig.digits;
1048
0
  out[0] = '0';
1049
0
  out[1] = '.';
1050
0
  switch (exp) {
1051
0
    case 5:
1052
0
      memcpy(out, &digits[0], 6), out += 6;
1053
0
      *out = 0;
1054
0
      return static_cast<size_t>(out - buffer);
1055
0
    case 4:
1056
0
      memcpy(out, &digits[0], 5), out += 5;
1057
0
      if (digits[5] != '0') {
1058
0
        *out++ = '.';
1059
0
        *out++ = digits[5];
1060
0
      }
1061
0
      *out = 0;
1062
0
      return static_cast<size_t>(out - buffer);
1063
0
    case 3:
1064
0
      memcpy(out, &digits[0], 4), out += 4;
1065
0
      if ((digits[5] | digits[4]) != '0') {
1066
0
        *out++ = '.';
1067
0
        *out++ = digits[4];
1068
0
        if (digits[5] != '0') *out++ = digits[5];
1069
0
      }
1070
0
      *out = 0;
1071
0
      return static_cast<size_t>(out - buffer);
1072
0
    case 2:
1073
0
      memcpy(out, &digits[0], 3), out += 3;
1074
0
      *out++ = '.';
1075
0
      memcpy(out, &digits[3], 3);
1076
0
      out += 3;
1077
0
      while (out[-1] == '0') --out;
1078
0
      if (out[-1] == '.') --out;
1079
0
      *out = 0;
1080
0
      return static_cast<size_t>(out - buffer);
1081
0
    case 1:
1082
0
      memcpy(out, &digits[0], 2), out += 2;
1083
0
      *out++ = '.';
1084
0
      memcpy(out, &digits[2], 4);
1085
0
      out += 4;
1086
0
      while (out[-1] == '0') --out;
1087
0
      if (out[-1] == '.') --out;
1088
0
      *out = 0;
1089
0
      return static_cast<size_t>(out - buffer);
1090
0
    case 0:
1091
0
      memcpy(out, &digits[0], 1), out += 1;
1092
0
      *out++ = '.';
1093
0
      memcpy(out, &digits[1], 5);
1094
0
      out += 5;
1095
0
      while (out[-1] == '0') --out;
1096
0
      if (out[-1] == '.') --out;
1097
0
      *out = 0;
1098
0
      return static_cast<size_t>(out - buffer);
1099
0
    case -4:
1100
0
      out[2] = '0';
1101
0
      ++out;
1102
0
      ABSL_FALLTHROUGH_INTENDED;
1103
0
    case -3:
1104
0
      out[2] = '0';
1105
0
      ++out;
1106
0
      ABSL_FALLTHROUGH_INTENDED;
1107
0
    case -2:
1108
0
      out[2] = '0';
1109
0
      ++out;
1110
0
      ABSL_FALLTHROUGH_INTENDED;
1111
0
    case -1:
1112
0
      out += 2;
1113
0
      memcpy(out, &digits[0], 6);
1114
0
      out += 6;
1115
0
      while (out[-1] == '0') --out;
1116
0
      *out = 0;
1117
0
      return static_cast<size_t>(out - buffer);
1118
0
  }
1119
0
  assert(exp < -4 || exp >= 6);
1120
0
  out[0] = digits[0];
1121
0
  assert(out[1] == '.');
1122
0
  out += 2;
1123
0
  memcpy(out, &digits[1], 5), out += 5;
1124
0
  while (out[-1] == '0') --out;
1125
0
  if (out[-1] == '.') --out;
1126
0
  *out++ = 'e';
1127
0
  if (exp > 0) {
1128
0
    *out++ = '+';
1129
0
  } else {
1130
0
    *out++ = '-';
1131
0
    exp = -exp;
1132
0
  }
1133
0
  if (exp > 99) {
1134
0
    int dig1 = exp / 100;
1135
0
    exp -= dig1 * 100;
1136
0
    *out++ = '0' + static_cast<char>(dig1);
1137
0
  }
1138
0
  PutTwoDigits(static_cast<uint32_t>(exp), out);
1139
0
  out += 2;
1140
0
  *out = 0;
1141
0
  return static_cast<size_t>(out - buffer);
1142
0
}
1143
1144
namespace {
1145
// Represents integer values of digits.
1146
// Uses 36 to indicate an invalid character since we support
1147
// bases up to 36.
1148
static constexpr std::array<int8_t, 256> kAsciiToInt = {
1149
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,  // 16 36s.
1150
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1151
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 0,  1,  2,  3,  4,  5,
1152
    6,  7,  8,  9,  36, 36, 36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17,
1153
    18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36,
1154
    36, 36, 36, 36, 36, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
1155
    24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 36, 36, 36, 36, 36, 36,
1156
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1157
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1158
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1159
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1160
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1161
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36,
1162
    36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36, 36};
1163
1164
// Parse the sign and optional hex or oct prefix in text.
1165
inline bool safe_parse_sign_and_base(
1166
    absl::string_view* absl_nonnull text /*inout*/,
1167
    int* absl_nonnull base_ptr /*inout*/,
1168
0
    bool* absl_nonnull negative_ptr /*output*/) {
1169
0
  if (text->data() == nullptr) {
1170
0
    return false;
1171
0
  }
1172
1173
0
  const char* start = text->data();
1174
0
  const char* end = start + text->size();
1175
0
  int base = *base_ptr;
1176
1177
  // Consume whitespace.
1178
0
  while (start < end &&
1179
0
         absl::ascii_isspace(static_cast<unsigned char>(start[0]))) {
1180
0
    ++start;
1181
0
  }
1182
0
  while (start < end &&
1183
0
         absl::ascii_isspace(static_cast<unsigned char>(end[-1]))) {
1184
0
    --end;
1185
0
  }
1186
0
  if (start >= end) {
1187
0
    return false;
1188
0
  }
1189
1190
  // Consume sign.
1191
0
  *negative_ptr = (start[0] == '-');
1192
0
  if (*negative_ptr || start[0] == '+') {
1193
0
    ++start;
1194
0
    if (start >= end) {
1195
0
      return false;
1196
0
    }
1197
0
  }
1198
1199
  // Consume base-dependent prefix.
1200
  //  base 0: "0x" -> base 16, "0" -> base 8, default -> base 10
1201
  //  base 16: "0x" -> base 16
1202
  // Also validate the base.
1203
0
  if (base == 0) {
1204
0
    if (end - start >= 2 && start[0] == '0' &&
1205
0
        (start[1] == 'x' || start[1] == 'X')) {
1206
0
      base = 16;
1207
0
      start += 2;
1208
0
      if (start >= end) {
1209
        // "0x" with no digits after is invalid.
1210
0
        return false;
1211
0
      }
1212
0
    } else if (end - start >= 1 && start[0] == '0') {
1213
0
      base = 8;
1214
0
      start += 1;
1215
0
    } else {
1216
0
      base = 10;
1217
0
    }
1218
0
  } else if (base == 16) {
1219
0
    if (end - start >= 2 && start[0] == '0' &&
1220
0
        (start[1] == 'x' || start[1] == 'X')) {
1221
0
      start += 2;
1222
0
      if (start >= end) {
1223
        // "0x" with no digits after is invalid.
1224
0
        return false;
1225
0
      }
1226
0
    }
1227
0
  } else if (base >= 2 && base <= 36) {
1228
    // okay
1229
0
  } else {
1230
0
    return false;
1231
0
  }
1232
0
  *text = absl::string_view(start, static_cast<size_t>(end - start));
1233
0
  *base_ptr = base;
1234
0
  return true;
1235
0
}
1236
1237
// Consume digits.
1238
//
1239
// The classic loop:
1240
//
1241
//   for each digit
1242
//     value = value * base + digit
1243
//   value *= sign
1244
//
1245
// The classic loop needs overflow checking.  It also fails on the most
1246
// negative integer, -2147483648 in 32-bit two's complement representation.
1247
//
1248
// My improved loop:
1249
//
1250
//  if (!negative)
1251
//    for each digit
1252
//      value = value * base
1253
//      value = value + digit
1254
//  else
1255
//    for each digit
1256
//      value = value * base
1257
//      value = value - digit
1258
//
1259
// Overflow checking becomes simple.
1260
1261
// Lookup tables per IntType:
1262
// vmax/base and vmin/base are precomputed because division costs at least 8ns.
1263
// TODO(junyer): Doing this per base instead (i.e. an array of structs, not a
1264
// struct of arrays) would probably be better in terms of d-cache for the most
1265
// commonly used bases.
1266
template <typename IntType>
1267
struct LookupTables {
1268
  ABSL_CONST_INIT static const IntType kVmaxOverBase[];
1269
  ABSL_CONST_INIT static const IntType kVminOverBase[];
1270
};
1271
1272
// An array initializer macro for X/base where base in [0, 36].
1273
// However, note that lookups for base in [0, 1] should never happen because
1274
// base has been validated to be in [2, 36] by safe_parse_sign_and_base().
1275
#define X_OVER_BASE_INITIALIZER(X)                                        \
1276
  {                                                                       \
1277
    0, 0, X / 2, X / 3, X / 4, X / 5, X / 6, X / 7, X / 8, X / 9, X / 10, \
1278
        X / 11, X / 12, X / 13, X / 14, X / 15, X / 16, X / 17, X / 18,   \
1279
        X / 19, X / 20, X / 21, X / 22, X / 23, X / 24, X / 25, X / 26,   \
1280
        X / 27, X / 28, X / 29, X / 30, X / 31, X / 32, X / 33, X / 34,   \
1281
        X / 35, X / 36,                                                   \
1282
  }
1283
1284
// This kVmaxOverBase is generated with
1285
//  for (int base = 2; base < 37; ++base) {
1286
//    absl::uint128 max = std::numeric_limits<absl::uint128>::max();
1287
//    auto result = max / base;
1288
//    std::cout << "    MakeUint128(" << absl::Uint128High64(result) << "u, "
1289
//              << absl::Uint128Low64(result) << "u),\n";
1290
//  }
1291
// See https://godbolt.org/z/aneYsb
1292
//
1293
// uint128& operator/=(uint128) is not constexpr, so hardcode the resulting
1294
// array to avoid a static initializer.
1295
template <>
1296
ABSL_CONST_INIT const uint128 LookupTables<uint128>::kVmaxOverBase[] = {
1297
    0,
1298
    0,
1299
    MakeUint128(9223372036854775807u, 18446744073709551615u),
1300
    MakeUint128(6148914691236517205u, 6148914691236517205u),
1301
    MakeUint128(4611686018427387903u, 18446744073709551615u),
1302
    MakeUint128(3689348814741910323u, 3689348814741910323u),
1303
    MakeUint128(3074457345618258602u, 12297829382473034410u),
1304
    MakeUint128(2635249153387078802u, 5270498306774157604u),
1305
    MakeUint128(2305843009213693951u, 18446744073709551615u),
1306
    MakeUint128(2049638230412172401u, 14347467612885206812u),
1307
    MakeUint128(1844674407370955161u, 11068046444225730969u),
1308
    MakeUint128(1676976733973595601u, 8384883669867978007u),
1309
    MakeUint128(1537228672809129301u, 6148914691236517205u),
1310
    MakeUint128(1418980313362273201u, 4256940940086819603u),
1311
    MakeUint128(1317624576693539401u, 2635249153387078802u),
1312
    MakeUint128(1229782938247303441u, 1229782938247303441u),
1313
    MakeUint128(1152921504606846975u, 18446744073709551615u),
1314
    MakeUint128(1085102592571150095u, 1085102592571150095u),
1315
    MakeUint128(1024819115206086200u, 16397105843297379214u),
1316
    MakeUint128(970881267037344821u, 16504981539634861972u),
1317
    MakeUint128(922337203685477580u, 14757395258967641292u),
1318
    MakeUint128(878416384462359600u, 14054662151397753612u),
1319
    MakeUint128(838488366986797800u, 13415813871788764811u),
1320
    MakeUint128(802032351030850070u, 4812194106185100421u),
1321
    MakeUint128(768614336404564650u, 12297829382473034410u),
1322
    MakeUint128(737869762948382064u, 11805916207174113034u),
1323
    MakeUint128(709490156681136600u, 11351842506898185609u),
1324
    MakeUint128(683212743470724133u, 17080318586768103348u),
1325
    MakeUint128(658812288346769700u, 10540996613548315209u),
1326
    MakeUint128(636094623231363848u, 15266270957552732371u),
1327
    MakeUint128(614891469123651720u, 9838263505978427528u),
1328
    MakeUint128(595056260442243600u, 9520900167075897608u),
1329
    MakeUint128(576460752303423487u, 18446744073709551615u),
1330
    MakeUint128(558992244657865200u, 8943875914525843207u),
1331
    MakeUint128(542551296285575047u, 9765923333140350855u),
1332
    MakeUint128(527049830677415760u, 8432797290838652167u),
1333
    MakeUint128(512409557603043100u, 8198552921648689607u),
1334
};
1335
1336
// This kVmaxOverBase generated with
1337
//   for (int base = 2; base < 37; ++base) {
1338
//    absl::int128 max = std::numeric_limits<absl::int128>::max();
1339
//    auto result = max / base;
1340
//    std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", "
1341
//              << absl::Int128Low64(result) << "u),\n";
1342
//  }
1343
// See https://godbolt.org/z/7djYWz
1344
//
1345
// int128& operator/=(int128) is not constexpr, so hardcode the resulting array
1346
// to avoid a static initializer.
1347
template <>
1348
ABSL_CONST_INIT const int128 LookupTables<int128>::kVmaxOverBase[] = {
1349
    0,
1350
    0,
1351
    MakeInt128(4611686018427387903, 18446744073709551615u),
1352
    MakeInt128(3074457345618258602, 12297829382473034410u),
1353
    MakeInt128(2305843009213693951, 18446744073709551615u),
1354
    MakeInt128(1844674407370955161, 11068046444225730969u),
1355
    MakeInt128(1537228672809129301, 6148914691236517205u),
1356
    MakeInt128(1317624576693539401, 2635249153387078802u),
1357
    MakeInt128(1152921504606846975, 18446744073709551615u),
1358
    MakeInt128(1024819115206086200, 16397105843297379214u),
1359
    MakeInt128(922337203685477580, 14757395258967641292u),
1360
    MakeInt128(838488366986797800, 13415813871788764811u),
1361
    MakeInt128(768614336404564650, 12297829382473034410u),
1362
    MakeInt128(709490156681136600, 11351842506898185609u),
1363
    MakeInt128(658812288346769700, 10540996613548315209u),
1364
    MakeInt128(614891469123651720, 9838263505978427528u),
1365
    MakeInt128(576460752303423487, 18446744073709551615u),
1366
    MakeInt128(542551296285575047, 9765923333140350855u),
1367
    MakeInt128(512409557603043100, 8198552921648689607u),
1368
    MakeInt128(485440633518672410, 17475862806672206794u),
1369
    MakeInt128(461168601842738790, 7378697629483820646u),
1370
    MakeInt128(439208192231179800, 7027331075698876806u),
1371
    MakeInt128(419244183493398900, 6707906935894382405u),
1372
    MakeInt128(401016175515425035, 2406097053092550210u),
1373
    MakeInt128(384307168202282325, 6148914691236517205u),
1374
    MakeInt128(368934881474191032, 5902958103587056517u),
1375
    MakeInt128(354745078340568300, 5675921253449092804u),
1376
    MakeInt128(341606371735362066, 17763531330238827482u),
1377
    MakeInt128(329406144173384850, 5270498306774157604u),
1378
    MakeInt128(318047311615681924, 7633135478776366185u),
1379
    MakeInt128(307445734561825860, 4919131752989213764u),
1380
    MakeInt128(297528130221121800, 4760450083537948804u),
1381
    MakeInt128(288230376151711743, 18446744073709551615u),
1382
    MakeInt128(279496122328932600, 4471937957262921603u),
1383
    MakeInt128(271275648142787523, 14106333703424951235u),
1384
    MakeInt128(263524915338707880, 4216398645419326083u),
1385
    MakeInt128(256204778801521550, 4099276460824344803u),
1386
};
1387
1388
// This kVminOverBase generated with
1389
//  for (int base = 2; base < 37; ++base) {
1390
//    absl::int128 min = std::numeric_limits<absl::int128>::min();
1391
//    auto result = min / base;
1392
//    std::cout << "\tMakeInt128(" << absl::Int128High64(result) << ", "
1393
//              << absl::Int128Low64(result) << "u),\n";
1394
//  }
1395
//
1396
// See https://godbolt.org/z/7djYWz
1397
//
1398
// int128& operator/=(int128) is not constexpr, so hardcode the resulting array
1399
// to avoid a static initializer.
1400
template <>
1401
ABSL_CONST_INIT const int128 LookupTables<int128>::kVminOverBase[] = {
1402
    0,
1403
    0,
1404
    MakeInt128(-4611686018427387904, 0u),
1405
    MakeInt128(-3074457345618258603, 6148914691236517206u),
1406
    MakeInt128(-2305843009213693952, 0u),
1407
    MakeInt128(-1844674407370955162, 7378697629483820647u),
1408
    MakeInt128(-1537228672809129302, 12297829382473034411u),
1409
    MakeInt128(-1317624576693539402, 15811494920322472814u),
1410
    MakeInt128(-1152921504606846976, 0u),
1411
    MakeInt128(-1024819115206086201, 2049638230412172402u),
1412
    MakeInt128(-922337203685477581, 3689348814741910324u),
1413
    MakeInt128(-838488366986797801, 5030930201920786805u),
1414
    MakeInt128(-768614336404564651, 6148914691236517206u),
1415
    MakeInt128(-709490156681136601, 7094901566811366007u),
1416
    MakeInt128(-658812288346769701, 7905747460161236407u),
1417
    MakeInt128(-614891469123651721, 8608480567731124088u),
1418
    MakeInt128(-576460752303423488, 0u),
1419
    MakeInt128(-542551296285575048, 8680820740569200761u),
1420
    MakeInt128(-512409557603043101, 10248191152060862009u),
1421
    MakeInt128(-485440633518672411, 970881267037344822u),
1422
    MakeInt128(-461168601842738791, 11068046444225730970u),
1423
    MakeInt128(-439208192231179801, 11419412998010674810u),
1424
    MakeInt128(-419244183493398901, 11738837137815169211u),
1425
    MakeInt128(-401016175515425036, 16040647020617001406u),
1426
    MakeInt128(-384307168202282326, 12297829382473034411u),
1427
    MakeInt128(-368934881474191033, 12543785970122495099u),
1428
    MakeInt128(-354745078340568301, 12770822820260458812u),
1429
    MakeInt128(-341606371735362067, 683212743470724134u),
1430
    MakeInt128(-329406144173384851, 13176245766935394012u),
1431
    MakeInt128(-318047311615681925, 10813608594933185431u),
1432
    MakeInt128(-307445734561825861, 13527612320720337852u),
1433
    MakeInt128(-297528130221121801, 13686293990171602812u),
1434
    MakeInt128(-288230376151711744, 0u),
1435
    MakeInt128(-279496122328932601, 13974806116446630013u),
1436
    MakeInt128(-271275648142787524, 4340410370284600381u),
1437
    MakeInt128(-263524915338707881, 14230345428290225533u),
1438
    MakeInt128(-256204778801521551, 14347467612885206813u),
1439
};
1440
1441
template <typename IntType>
1442
ABSL_CONST_INIT const IntType LookupTables<IntType>::kVmaxOverBase[] =
1443
    X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::max());
1444
1445
template <typename IntType>
1446
ABSL_CONST_INIT const IntType LookupTables<IntType>::kVminOverBase[] =
1447
    X_OVER_BASE_INITIALIZER(std::numeric_limits<IntType>::min());
1448
1449
#undef X_OVER_BASE_INITIALIZER
1450
1451
template <typename IntType>
1452
inline bool safe_parse_positive_int(absl::string_view text, int base,
1453
0
                                    IntType* absl_nonnull value_p) {
1454
0
  IntType value = 0;
1455
0
  const IntType vmax = std::numeric_limits<IntType>::max();
1456
0
  assert(vmax > 0);
1457
0
  assert(base >= 0);
1458
0
  const IntType base_inttype = static_cast<IntType>(base);
1459
0
  assert(vmax >= base_inttype);
1460
0
  const IntType vmax_over_base = LookupTables<IntType>::kVmaxOverBase[base];
1461
0
  assert(base < 2 ||
1462
0
         std::numeric_limits<IntType>::max() / base_inttype == vmax_over_base);
1463
0
  const char* start = text.data();
1464
0
  const char* end = start + text.size();
1465
  // loop over digits
1466
0
  for (; start < end; ++start) {
1467
0
    unsigned char c = static_cast<unsigned char>(start[0]);
1468
0
    IntType digit = static_cast<IntType>(kAsciiToInt[c]);
1469
0
    if (digit >= base_inttype) {
1470
0
      *value_p = value;
1471
0
      return false;
1472
0
    }
1473
0
    if (value > vmax_over_base) {
1474
0
      *value_p = vmax;
1475
0
      return false;
1476
0
    }
1477
0
    value *= base_inttype;
1478
0
    if (value > vmax - digit) {
1479
0
      *value_p = vmax;
1480
0
      return false;
1481
0
    }
1482
0
    value += digit;
1483
0
  }
1484
0
  *value_p = value;
1485
0
  return true;
1486
0
}
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<signed char>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, signed char*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<short>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, short*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, int*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<long>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, long*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<absl::int128>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, absl::int128*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<unsigned char>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, unsigned char*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<unsigned short>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, unsigned short*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<unsigned int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, unsigned int*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<unsigned long>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, unsigned long*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_positive_int<absl::uint128>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, absl::uint128*)
1487
1488
template <typename IntType>
1489
inline bool safe_parse_negative_int(absl::string_view text, int base,
1490
0
                                    IntType* absl_nonnull value_p) {
1491
0
  IntType value = 0;
1492
0
  const IntType vmin = std::numeric_limits<IntType>::min();
1493
0
  assert(vmin < 0);
1494
0
  assert(vmin <= 0 - base);
1495
0
  IntType vmin_over_base = LookupTables<IntType>::kVminOverBase[base];
1496
0
  assert(base < 2 ||
1497
0
         std::numeric_limits<IntType>::min() / base == vmin_over_base);
1498
  // 2003 c++ standard [expr.mul]
1499
  // "... the sign of the remainder is implementation-defined."
1500
  // Although (vmin/base)*base + vmin%base is always vmin.
1501
  // 2011 c++ standard tightens the spec but we cannot rely on it.
1502
  // TODO(junyer): Handle this in the lookup table generation.
1503
0
  if (vmin % base > 0) {
1504
0
    vmin_over_base += 1;
1505
0
  }
1506
0
  const char* start = text.data();
1507
0
  const char* end = start + text.size();
1508
  // loop over digits
1509
0
  for (; start < end; ++start) {
1510
0
    unsigned char c = static_cast<unsigned char>(start[0]);
1511
0
    int digit = kAsciiToInt[c];
1512
0
    if (digit >= base) {
1513
0
      *value_p = value;
1514
0
      return false;
1515
0
    }
1516
0
    if (value < vmin_over_base) {
1517
0
      *value_p = vmin;
1518
0
      return false;
1519
0
    }
1520
0
    value *= base;
1521
0
    if (value < vmin + digit) {
1522
0
      *value_p = vmin;
1523
0
      return false;
1524
0
    }
1525
0
    value -= digit;
1526
0
  }
1527
0
  *value_p = value;
1528
0
  return true;
1529
0
}
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_negative_int<signed char>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, signed char*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_negative_int<short>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, short*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_negative_int<int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, int*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_negative_int<long>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, long*)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_parse_negative_int<absl::int128>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int, absl::int128*)
1530
1531
// Input format based on POSIX.1-2008 strtol
1532
// http://pubs.opengroup.org/onlinepubs/9699919799/functions/strtol.html
1533
template <typename IntType>
1534
inline bool safe_int_internal(absl::string_view text,
1535
0
                              IntType* absl_nonnull value_p, int base) {
1536
0
  *value_p = 0;
1537
0
  bool negative;
1538
0
  if (!safe_parse_sign_and_base(&text, &base, &negative)) {
1539
0
    return false;
1540
0
  }
1541
0
  if (!negative) {
1542
0
    return safe_parse_positive_int(text, base, value_p);
1543
0
  } else {
1544
0
    return safe_parse_negative_int(text, base, value_p);
1545
0
  }
1546
0
}
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_int_internal<signed char>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, signed char*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_int_internal<short>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, short*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_int_internal<int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, int*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_int_internal<long>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, long*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_int_internal<absl::int128>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::int128*, int)
1547
1548
template <typename IntType>
1549
inline bool safe_uint_internal(absl::string_view text,
1550
0
                               IntType* absl_nonnull value_p, int base) {
1551
0
  *value_p = 0;
1552
0
  bool negative;
1553
0
  if (!safe_parse_sign_and_base(&text, &base, &negative) || negative) {
1554
0
    return false;
1555
0
  }
1556
0
  return safe_parse_positive_int(text, base, value_p);
1557
0
}
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_uint_internal<unsigned char>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, unsigned char*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_uint_internal<unsigned short>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, unsigned short*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_uint_internal<unsigned int>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, unsigned int*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_uint_internal<unsigned long>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, unsigned long*, int)
Unexecuted instantiation: numbers.cc:bool absl::(anonymous namespace)::safe_uint_internal<absl::uint128>(std::__1::basic_string_view<char, std::__1::char_traits<char> >, absl::uint128*, int)
1558
}  // anonymous namespace
1559
1560
namespace numbers_internal {
1561
1562
// Digit conversion.
1563
ABSL_CONST_INIT ABSL_DLL const char kHexChar[] =
1564
    "0123456789abcdef";
1565
1566
ABSL_CONST_INIT ABSL_DLL const char kHexTable[513] =
1567
    "000102030405060708090a0b0c0d0e0f"
1568
    "101112131415161718191a1b1c1d1e1f"
1569
    "202122232425262728292a2b2c2d2e2f"
1570
    "303132333435363738393a3b3c3d3e3f"
1571
    "404142434445464748494a4b4c4d4e4f"
1572
    "505152535455565758595a5b5c5d5e5f"
1573
    "606162636465666768696a6b6c6d6e6f"
1574
    "707172737475767778797a7b7c7d7e7f"
1575
    "808182838485868788898a8b8c8d8e8f"
1576
    "909192939495969798999a9b9c9d9e9f"
1577
    "a0a1a2a3a4a5a6a7a8a9aaabacadaeaf"
1578
    "b0b1b2b3b4b5b6b7b8b9babbbcbdbebf"
1579
    "c0c1c2c3c4c5c6c7c8c9cacbcccdcecf"
1580
    "d0d1d2d3d4d5d6d7d8d9dadbdcdddedf"
1581
    "e0e1e2e3e4e5e6e7e8e9eaebecedeeef"
1582
    "f0f1f2f3f4f5f6f7f8f9fafbfcfdfeff";
1583
1584
bool safe_strto8_base(absl::string_view text, int8_t* absl_nonnull value,
1585
0
                      int base) {
1586
0
  return safe_int_internal<int8_t>(text, value, base);
1587
0
}
1588
1589
bool safe_strto16_base(absl::string_view text, int16_t* absl_nonnull value,
1590
0
                       int base) {
1591
0
  return safe_int_internal<int16_t>(text, value, base);
1592
0
}
1593
1594
bool safe_strto32_base(absl::string_view text, int32_t* absl_nonnull value,
1595
0
                       int base) {
1596
0
  return safe_int_internal<int32_t>(text, value, base);
1597
0
}
1598
1599
bool safe_strto64_base(absl::string_view text, int64_t* absl_nonnull value,
1600
0
                       int base) {
1601
0
  return safe_int_internal<int64_t>(text, value, base);
1602
0
}
1603
1604
bool safe_strto128_base(absl::string_view text, int128* absl_nonnull value,
1605
0
                        int base) {
1606
0
  return safe_int_internal<absl::int128>(text, value, base);
1607
0
}
1608
1609
bool safe_strtou8_base(absl::string_view text, uint8_t* absl_nonnull value,
1610
0
                       int base) {
1611
0
  return safe_uint_internal<uint8_t>(text, value, base);
1612
0
}
1613
1614
bool safe_strtou16_base(absl::string_view text, uint16_t* absl_nonnull value,
1615
0
                        int base) {
1616
0
  return safe_uint_internal<uint16_t>(text, value, base);
1617
0
}
1618
1619
bool safe_strtou32_base(absl::string_view text, uint32_t* absl_nonnull value,
1620
0
                        int base) {
1621
0
  return safe_uint_internal<uint32_t>(text, value, base);
1622
0
}
1623
1624
bool safe_strtou64_base(absl::string_view text, uint64_t* absl_nonnull value,
1625
0
                        int base) {
1626
0
  return safe_uint_internal<uint64_t>(text, value, base);
1627
0
}
1628
1629
bool safe_strtou128_base(absl::string_view text, uint128* absl_nonnull value,
1630
0
                         int base) {
1631
0
  return safe_uint_internal<absl::uint128>(text, value, base);
1632
0
}
1633
1634
}  // namespace numbers_internal
1635
ABSL_NAMESPACE_END
1636
}  // namespace absl