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 // -----------------------------------------------------------------------------
16 // File: numbers.h
17 // -----------------------------------------------------------------------------
18 //
19 // This package contains functions for converting strings to numbers. For
20 // converting numbers to strings, use `StrCat()` or `StrAppend()` in str_cat.h,
21 // which automatically detect and convert most number values appropriately.
22
23 #ifndef ABSL_STRINGS_NUMBERS_H_
24 #define ABSL_STRINGS_NUMBERS_H_
25
26 #ifdef __SSSE3__
27 #include <tmmintrin.h>
28 #endif
29
30 #ifdef _MSC_VER
31 #include <intrin.h>
32 #endif
33
34 #include <cstddef>
35 #include <cstdint>
36 #include <cstdlib>
37 #include <cstring>
38 #include <ctime>
39 #include <limits>
40 #include <string>
41 #include <type_traits>
42
43 #include "absl/base/attributes.h"
44 #include "absl/base/config.h"
45 #include "absl/base/internal/endian.h"
46 #include "absl/base/macros.h"
47 #include "absl/base/nullability.h"
48 #include "absl/base/optimization.h"
49 #include "absl/base/port.h"
50 #include "absl/numeric/bits.h"
51 #include "absl/numeric/int128.h"
52 #include "absl/strings/string_view.h"
53
54 namespace absl {
55 ABSL_NAMESPACE_BEGIN
56
57 // SimpleAtoi()
58 //
59 // Converts the given string (optionally followed or preceded by ASCII
60 // whitespace) into an integer value, returning `true` if successful. The string
61 // must reflect a base-10 integer whose value falls within the range of the
62 // integer type (optionally preceded by a `+` or `-`). If any errors are
63 // encountered, this function returns `false`, leaving `out` in an unspecified
64 // state.
65 template <typename int_type>
66 ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str,
67 absl::Nonnull<int_type*> out);
68
69 // SimpleAtof()
70 //
71 // Converts the given string (optionally followed or preceded by ASCII
72 // whitespace) into a float, which may be rounded on overflow or underflow,
73 // returning `true` if successful.
74 // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
75 // allowed formats for `str`, except SimpleAtof() is locale-independent and will
76 // always use the "C" locale. If any errors are encountered, this function
77 // returns `false`, leaving `out` in an unspecified state.
78 ABSL_MUST_USE_RESULT bool SimpleAtof(absl::string_view str,
79 absl::Nonnull<float*> out);
80
81 // SimpleAtod()
82 //
83 // Converts the given string (optionally followed or preceded by ASCII
84 // whitespace) into a double, which may be rounded on overflow or underflow,
85 // returning `true` if successful.
86 // See https://en.cppreference.com/w/c/string/byte/strtof for details about the
87 // allowed formats for `str`, except SimpleAtod is locale-independent and will
88 // always use the "C" locale. If any errors are encountered, this function
89 // returns `false`, leaving `out` in an unspecified state.
90 ABSL_MUST_USE_RESULT bool SimpleAtod(absl::string_view str,
91 absl::Nonnull<double*> out);
92
93 // SimpleAtob()
94 //
95 // Converts the given string into a boolean, returning `true` if successful.
96 // The following case-insensitive strings are interpreted as boolean `true`:
97 // "true", "t", "yes", "y", "1". The following case-insensitive strings
98 // are interpreted as boolean `false`: "false", "f", "no", "n", "0". If any
99 // errors are encountered, this function returns `false`, leaving `out` in an
100 // unspecified state.
101 ABSL_MUST_USE_RESULT bool SimpleAtob(absl::string_view str,
102 absl::Nonnull<bool*> out);
103
104 // SimpleHexAtoi()
105 //
106 // Converts a hexadecimal string (optionally followed or preceded by ASCII
107 // whitespace) to an integer, returning `true` if successful. Only valid base-16
108 // hexadecimal integers whose value falls within the range of the integer type
109 // (optionally preceded by a `+` or `-`) can be converted. A valid hexadecimal
110 // value may include both upper and lowercase character symbols, and may
111 // optionally include a leading "0x" (or "0X") number prefix, which is ignored
112 // by this function. If any errors are encountered, this function returns
113 // `false`, leaving `out` in an unspecified state.
114 template <typename int_type>
115 ABSL_MUST_USE_RESULT bool SimpleHexAtoi(absl::string_view str,
116 absl::Nonnull<int_type*> out);
117
118 // Overloads of SimpleHexAtoi() for 128 bit integers.
119 ABSL_MUST_USE_RESULT inline bool SimpleHexAtoi(
120 absl::string_view str, absl::Nonnull<absl::int128*> out);
121 ABSL_MUST_USE_RESULT inline bool SimpleHexAtoi(
122 absl::string_view str, absl::Nonnull<absl::uint128*> out);
123
124 ABSL_NAMESPACE_END
125 } // namespace absl
126
127 // End of public API. Implementation details follow.
128
129 namespace absl {
130 ABSL_NAMESPACE_BEGIN
131 namespace numbers_internal {
132
133 // Digit conversion.
134 ABSL_DLL extern const char kHexChar[17]; // 0123456789abcdef
135 ABSL_DLL extern const char
136 kHexTable[513]; // 000102030405060708090a0b0c0d0e0f1011...
137
138 // Writes a two-character representation of 'i' to 'buf'. 'i' must be in the
139 // range 0 <= i < 100, and buf must have space for two characters. Example:
140 // char buf[2];
141 // PutTwoDigits(42, buf);
142 // // buf[0] == '4'
143 // // buf[1] == '2'
144 void PutTwoDigits(uint32_t i, absl::Nonnull<char*> buf);
145
146 // safe_strto?() functions for implementing SimpleAtoi()
147
148 bool safe_strto32_base(absl::string_view text, absl::Nonnull<int32_t*> value,
149 int base);
150 bool safe_strto64_base(absl::string_view text, absl::Nonnull<int64_t*> value,
151 int base);
152 bool safe_strto128_base(absl::string_view text,
153 absl::Nonnull<absl::int128*> value, int base);
154 bool safe_strtou32_base(absl::string_view text, absl::Nonnull<uint32_t*> value,
155 int base);
156 bool safe_strtou64_base(absl::string_view text, absl::Nonnull<uint64_t*> value,
157 int base);
158 bool safe_strtou128_base(absl::string_view text,
159 absl::Nonnull<absl::uint128*> value, int base);
160
161 static const int kFastToBufferSize = 32;
162 static const int kSixDigitsToBufferSize = 16;
163
164 template <class T>
IsNegative(const T & v)165 std::enable_if_t<!std::is_unsigned<T>::value, bool> IsNegative(const T& v) {
166 return v < T();
167 }
168
169 template <class T>
IsNegative(const T &)170 std::enable_if_t<std::is_unsigned<T>::value, std::false_type> IsNegative(
171 const T&) {
172 // The integer is unsigned, so return a compile-time constant.
173 // This can help the optimizer avoid having to prove bool to be false later.
174 return std::false_type();
175 }
176
177 template <class T>
178 std::enable_if_t<std::is_unsigned<std::decay_t<T>>::value, T&&>
UnsignedAbsoluteValue(T && v ABSL_ATTRIBUTE_LIFETIME_BOUND)179 UnsignedAbsoluteValue(T&& v ABSL_ATTRIBUTE_LIFETIME_BOUND) {
180 // The value is unsigned; just return the original.
181 return std::forward<T>(v);
182 }
183
184 template <class T>
185 ABSL_ATTRIBUTE_CONST_FUNCTION
186 std::enable_if_t<!std::is_unsigned<T>::value, std::make_unsigned_t<T>>
UnsignedAbsoluteValue(T v)187 UnsignedAbsoluteValue(T v) {
188 using U = std::make_unsigned_t<T>;
189 return IsNegative(v) ? U() - static_cast<U>(v) : static_cast<U>(v);
190 }
191
192 // Returns the number of base-10 digits in the given number.
193 // Note that this strictly counts digits. It does not count the sign.
194 // The `initial_digits` parameter is the starting point, which is normally equal
195 // to 1 because the number of digits in 0 is 1 (a special case).
196 // However, callers may e.g. wish to change it to 2 to account for the sign.
197 template <typename T>
198 std::enable_if_t<std::is_unsigned<T>::value, uint32_t> Base10Digits(
199 T v, const uint32_t initial_digits = 1) {
200 uint32_t r = initial_digits;
201 // If code size becomes an issue, the 'if' stage can be removed for a minor
202 // performance loss.
203 for (;;) {
204 if (ABSL_PREDICT_TRUE(v < 10 * 10)) {
205 r += (v >= 10);
206 break;
207 }
208 if (ABSL_PREDICT_TRUE(v < 1000 * 10)) {
209 r += (v >= 1000) + 2;
210 break;
211 }
212 if (ABSL_PREDICT_TRUE(v < 100000 * 10)) {
213 r += (v >= 100000) + 4;
214 break;
215 }
216 r += 6;
217 v = static_cast<T>(v / 1000000);
218 }
219 return r;
220 }
221
222 template <typename T>
223 std::enable_if_t<std::is_signed<T>::value, uint32_t> Base10Digits(
224 T v, uint32_t r = 1) {
225 // Branchlessly add 1 to account for a minus sign.
226 r += static_cast<uint32_t>(IsNegative(v));
227 return Base10Digits(UnsignedAbsoluteValue(v), r);
228 }
229
230 // These functions return the number of base-10 digits, but multiplied by -1 if
231 // the input itself is negative. This is handy and efficient for later usage,
232 // since the bitwise complement of the result becomes equal to the number of
233 // characters required.
234 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
235 signed char v);
236 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
237 unsigned char v);
238 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
239 short v); // NOLINT
240 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
241 unsigned short v); // NOLINT
242 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(int v);
243 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
244 unsigned int v);
245 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
246 long v); // NOLINT
247 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
248 unsigned long v); // NOLINT
249 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
250 long long v); // NOLINT
251 ABSL_ATTRIBUTE_CONST_FUNCTION int GetNumDigitsOrNegativeIfNegative(
252 unsigned long long v); // NOLINT
253
254 // Helper function for fast formatting of floating-point values.
255 // The result is the same as printf's "%g", a.k.a. "%.6g"; that is, six
256 // significant digits are returned, trailing zeros are removed, and numbers
257 // outside the range 0.0001-999999 are output using scientific notation
258 // (1.23456e+06). This routine is heavily optimized.
259 // Required buffer size is `kSixDigitsToBufferSize`.
260 size_t SixDigitsToBuffer(double d, absl::Nonnull<char*> buffer);
261
262 // All of these functions take an output buffer
263 // as an argument and return a pointer to the last byte they wrote, which is the
264 // terminating '\0'. At most `kFastToBufferSize` bytes are written.
265 absl::Nonnull<char*> FastIntToBuffer(int32_t i, absl::Nonnull<char*> buffer);
266 absl::Nonnull<char*> FastIntToBuffer(uint32_t i, absl::Nonnull<char*> buffer);
267 absl::Nonnull<char*> FastIntToBuffer(int64_t i, absl::Nonnull<char*> buffer);
268 absl::Nonnull<char*> FastIntToBuffer(uint64_t i, absl::Nonnull<char*> buffer);
269
270 // For enums and integer types that are not an exact match for the types above,
271 // use templates to call the appropriate one of the four overloads above.
272 template <typename int_type>
FastIntToBuffer(int_type i,absl::Nonnull<char * > buffer)273 absl::Nonnull<char*> FastIntToBuffer(int_type i, absl::Nonnull<char*> buffer) {
274 static_assert(sizeof(i) <= 64 / 8,
275 "FastIntToBuffer works only with 64-bit-or-less integers.");
276 // TODO(jorg): This signed-ness check is used because it works correctly
277 // with enums, and it also serves to check that int_type is not a pointer.
278 // If one day something like std::is_signed<enum E> works, switch to it.
279 // These conditions are constexpr bools to suppress MSVC warning C4127.
280 constexpr bool kIsSigned = static_cast<int_type>(1) - 2 < 0;
281 constexpr bool kUse64Bit = sizeof(i) > 32 / 8;
282 if (kIsSigned) {
283 if (kUse64Bit) {
284 return FastIntToBuffer(static_cast<int64_t>(i), buffer);
285 } else {
286 return FastIntToBuffer(static_cast<int32_t>(i), buffer);
287 }
288 } else {
289 if (kUse64Bit) {
290 return FastIntToBuffer(static_cast<uint64_t>(i), buffer);
291 } else {
292 return FastIntToBuffer(static_cast<uint32_t>(i), buffer);
293 }
294 }
295 }
296
297 // These functions do NOT add any null-terminator.
298 // They return a pointer to the beginning of the written string.
299 // The digit counts provided must *exactly* match the number of base-10 digits
300 // in the number, or the behavior is undefined.
301 // (i.e. do NOT count the minus sign, or over- or under-count the digits.)
302 absl::Nonnull<char*> FastIntToBufferBackward(int32_t i,
303 absl::Nonnull<char*> buffer_end,
304 uint32_t exact_digit_count);
305 absl::Nonnull<char*> FastIntToBufferBackward(uint32_t i,
306 absl::Nonnull<char*> buffer_end,
307 uint32_t exact_digit_count);
308 absl::Nonnull<char*> FastIntToBufferBackward(int64_t i,
309 absl::Nonnull<char*> buffer_end,
310 uint32_t exact_digit_count);
311 absl::Nonnull<char*> FastIntToBufferBackward(uint64_t i,
312 absl::Nonnull<char*> buffer_end,
313 uint32_t exact_digit_count);
314
315 // For enums and integer types that are not an exact match for the types above,
316 // use templates to call the appropriate one of the four overloads above.
317 template <typename int_type>
FastIntToBufferBackward(int_type i,absl::Nonnull<char * > buffer_end,uint32_t exact_digit_count)318 absl::Nonnull<char*> FastIntToBufferBackward(int_type i,
319 absl::Nonnull<char*> buffer_end,
320 uint32_t exact_digit_count) {
321 static_assert(
322 sizeof(i) <= 64 / 8,
323 "FastIntToBufferBackward works only with 64-bit-or-less integers.");
324 // This signed-ness check is used because it works correctly
325 // with enums, and it also serves to check that int_type is not a pointer.
326 // If one day something like std::is_signed<enum E> works, switch to it.
327 // These conditions are constexpr bools to suppress MSVC warning C4127.
328 constexpr bool kIsSigned = static_cast<int_type>(1) - 2 < 0;
329 constexpr bool kUse64Bit = sizeof(i) > 32 / 8;
330 if (kIsSigned) {
331 if (kUse64Bit) {
332 return FastIntToBufferBackward(static_cast<int64_t>(i), buffer_end,
333 exact_digit_count);
334 } else {
335 return FastIntToBufferBackward(static_cast<int32_t>(i), buffer_end,
336 exact_digit_count);
337 }
338 } else {
339 if (kUse64Bit) {
340 return FastIntToBufferBackward(static_cast<uint64_t>(i), buffer_end,
341 exact_digit_count);
342 } else {
343 return FastIntToBufferBackward(static_cast<uint32_t>(i), buffer_end,
344 exact_digit_count);
345 }
346 }
347 }
348
349 // Implementation of SimpleAtoi, generalized to support arbitrary base (used
350 // with base different from 10 elsewhere in Abseil implementation).
351 template <typename int_type>
safe_strtoi_base(absl::string_view s,absl::Nonnull<int_type * > out,int base)352 ABSL_MUST_USE_RESULT bool safe_strtoi_base(absl::string_view s,
353 absl::Nonnull<int_type*> out,
354 int base) {
355 static_assert(sizeof(*out) == 4 || sizeof(*out) == 8,
356 "SimpleAtoi works only with 32-bit or 64-bit integers.");
357 static_assert(!std::is_floating_point<int_type>::value,
358 "Use SimpleAtof or SimpleAtod instead.");
359 bool parsed;
360 // TODO(jorg): This signed-ness check is used because it works correctly
361 // with enums, and it also serves to check that int_type is not a pointer.
362 // If one day something like std::is_signed<enum E> works, switch to it.
363 // These conditions are constexpr bools to suppress MSVC warning C4127.
364 constexpr bool kIsSigned = static_cast<int_type>(1) - 2 < 0;
365 constexpr bool kUse64Bit = sizeof(*out) == 64 / 8;
366 if (kIsSigned) {
367 if (kUse64Bit) {
368 int64_t val;
369 parsed = numbers_internal::safe_strto64_base(s, &val, base);
370 *out = static_cast<int_type>(val);
371 } else {
372 int32_t val;
373 parsed = numbers_internal::safe_strto32_base(s, &val, base);
374 *out = static_cast<int_type>(val);
375 }
376 } else {
377 if (kUse64Bit) {
378 uint64_t val;
379 parsed = numbers_internal::safe_strtou64_base(s, &val, base);
380 *out = static_cast<int_type>(val);
381 } else {
382 uint32_t val;
383 parsed = numbers_internal::safe_strtou32_base(s, &val, base);
384 *out = static_cast<int_type>(val);
385 }
386 }
387 return parsed;
388 }
389
390 // FastHexToBufferZeroPad16()
391 //
392 // Outputs `val` into `out` as if by `snprintf(out, 17, "%016x", val)` but
393 // without the terminating null character. Thus `out` must be of length >= 16.
394 // Returns the number of non-pad digits of the output (it can never be zero
395 // since 0 has one digit).
FastHexToBufferZeroPad16(uint64_t val,absl::Nonnull<char * > out)396 inline size_t FastHexToBufferZeroPad16(uint64_t val, absl::Nonnull<char*> out) {
397 #ifdef ABSL_INTERNAL_HAVE_SSSE3
398 uint64_t be = absl::big_endian::FromHost64(val);
399 const auto kNibbleMask = _mm_set1_epi8(0xf);
400 const auto kHexDigits = _mm_setr_epi8('0', '1', '2', '3', '4', '5', '6', '7',
401 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
402 auto v = _mm_loadl_epi64(reinterpret_cast<__m128i*>(&be)); // load lo dword
403 auto v4 = _mm_srli_epi64(v, 4); // shift 4 right
404 auto il = _mm_unpacklo_epi8(v4, v); // interleave bytes
405 auto m = _mm_and_si128(il, kNibbleMask); // mask out nibbles
406 auto hexchars = _mm_shuffle_epi8(kHexDigits, m); // hex chars
407 _mm_storeu_si128(reinterpret_cast<__m128i*>(out), hexchars);
408 #else
409 for (int i = 0; i < 8; ++i) {
410 auto byte = (val >> (56 - 8 * i)) & 0xFF;
411 auto* hex = &absl::numbers_internal::kHexTable[byte * 2];
412 std::memcpy(out + 2 * i, hex, 2);
413 }
414 #endif
415 // | 0x1 so that even 0 has 1 digit.
416 return 16 - static_cast<size_t>(countl_zero(val | 0x1) / 4);
417 }
418
419 } // namespace numbers_internal
420
421 template <typename int_type>
SimpleAtoi(absl::string_view str,absl::Nonnull<int_type * > out)422 ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str,
423 absl::Nonnull<int_type*> out) {
424 return numbers_internal::safe_strtoi_base(str, out, 10);
425 }
426
SimpleAtoi(absl::string_view str,absl::Nonnull<absl::int128 * > out)427 ABSL_MUST_USE_RESULT inline bool SimpleAtoi(absl::string_view str,
428 absl::Nonnull<absl::int128*> out) {
429 return numbers_internal::safe_strto128_base(str, out, 10);
430 }
431
SimpleAtoi(absl::string_view str,absl::Nonnull<absl::uint128 * > out)432 ABSL_MUST_USE_RESULT inline bool SimpleAtoi(absl::string_view str,
433 absl::Nonnull<absl::uint128*> out) {
434 return numbers_internal::safe_strtou128_base(str, out, 10);
435 }
436
437 template <typename int_type>
SimpleHexAtoi(absl::string_view str,absl::Nonnull<int_type * > out)438 ABSL_MUST_USE_RESULT bool SimpleHexAtoi(absl::string_view str,
439 absl::Nonnull<int_type*> out) {
440 return numbers_internal::safe_strtoi_base(str, out, 16);
441 }
442
SimpleHexAtoi(absl::string_view str,absl::Nonnull<absl::int128 * > out)443 ABSL_MUST_USE_RESULT inline bool SimpleHexAtoi(
444 absl::string_view str, absl::Nonnull<absl::int128*> out) {
445 return numbers_internal::safe_strto128_base(str, out, 16);
446 }
447
SimpleHexAtoi(absl::string_view str,absl::Nonnull<absl::uint128 * > out)448 ABSL_MUST_USE_RESULT inline bool SimpleHexAtoi(
449 absl::string_view str, absl::Nonnull<absl::uint128*> out) {
450 return numbers_internal::safe_strtou128_base(str, out, 16);
451 }
452
453 ABSL_NAMESPACE_END
454 } // namespace absl
455
456 #endif // ABSL_STRINGS_NUMBERS_H_
457