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