1 //===-- String to integer conversion utils ----------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #ifndef LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
10 #define LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
12 #include "src/__support/CPP/limits.h"
13 #include "src/__support/CPP/type_traits.h"
14 #include "src/__support/common.h"
15 #include "src/__support/ctype_utils.h"
16 #include "src/__support/str_to_num_result.h"
17 #include "src/errno/libc_errno.h" // For ERANGE
20 namespace __llvm_libc
{
23 // Returns a pointer to the first character in src that is not a whitespace
24 // character (as determined by isspace())
25 LIBC_INLINE
const char *first_non_whitespace(const char *__restrict src
) {
26 while (internal::isspace(*src
)) {
32 LIBC_INLINE
int b36_char_to_int(char input
) {
36 return (input
| 32) + 10 - 'a';
40 // checks if the next 3 characters of the string pointer are the start of a
41 // hexadecimal number. Does not advance the string pointer.
42 LIBC_INLINE
bool is_hex_start(const char *__restrict src
) {
43 return *src
== '0' && (*(src
+ 1) | 32) == 'x' && isalnum(*(src
+ 2)) &&
44 b36_char_to_int(*(src
+ 2)) < 16;
47 // Takes the address of the string pointer and parses the base from the start of
48 // it. This function will advance |src| to the first valid digit in the inferred
50 LIBC_INLINE
int infer_base(const char *__restrict
*__restrict src
) {
51 // A hexadecimal number is defined as "the prefix 0x or 0X followed by a
52 // sequence of the deimal digits and the letters a (or A) through f (or F)
53 // with values 10 through 15 respectively." (C standard 6.4.4.1)
54 if (is_hex_start(*src
)) {
57 } // An octal number is defined as "the prefix 0 optionally followed by a
58 // sequence of the digits 0 through 7 only" (C standard 6.4.4.1) and so any
59 // number that starts with 0, including just 0, is an octal number.
60 else if (**src
== '0') {
62 } // A decimal number is defined as beginning "with a nonzero digit and
63 // consist[ing] of a sequence of decimal digits." (C standard 6.4.4.1)
69 // Takes a pointer to a string and the base to convert to. This function is used
70 // as the backend for all of the string to int functions.
72 LIBC_INLINE StrToNumResult
<T
> strtointeger(const char *__restrict src
,
74 unsigned long long result
= 0;
75 bool is_number
= false;
76 const char *original_src
= src
;
79 if (base
< 0 || base
== 1 || base
> 36) {
81 return {0, 0, error_val
};
84 src
= first_non_whitespace(src
);
86 char result_sign
= '+';
87 if (*src
== '+' || *src
== '-') {
93 base
= infer_base(&src
);
94 } else if (base
== 16 && is_hex_start(src
)) {
98 constexpr bool IS_UNSIGNED
= (cpp::numeric_limits
<T
>::min() == 0);
99 const bool is_positive
= (result_sign
== '+');
100 unsigned long long constexpr NEGATIVE_MAX
=
102 ? static_cast<unsigned long long>(cpp::numeric_limits
<T
>::max()) + 1
103 : cpp::numeric_limits
<T
>::max();
104 unsigned long long const abs_max
=
105 (is_positive
? cpp::numeric_limits
<T
>::max() : NEGATIVE_MAX
);
106 unsigned long long const abs_max_div_by_base
= abs_max
/ base
;
107 while (isalnum(*src
)) {
108 int cur_digit
= b36_char_to_int(*src
);
109 if (cur_digit
>= base
)
115 // If the number has already hit the maximum value for the current type then
116 // the result cannot change, but we still need to advance src to the end of
118 if (result
== abs_max
) {
123 if (result
> abs_max_div_by_base
) {
127 result
= result
* base
;
129 if (result
> abs_max
- cur_digit
) {
133 result
= result
+ cur_digit
;
137 ptrdiff_t str_len
= is_number
? (src
- original_src
) : 0;
139 if (error_val
== ERANGE
) {
140 if (is_positive
|| IS_UNSIGNED
)
141 return {cpp::numeric_limits
<T
>::max(), str_len
, error_val
};
142 else // T is signed and there is a negative overflow
143 return {cpp::numeric_limits
<T
>::min(), str_len
, error_val
};
147 ? static_cast<T
>(result
)
148 : static_cast<T
>(-static_cast<cpp::make_unsigned_t
<T
>>(result
)),
152 } // namespace internal
153 } // namespace __llvm_libc
155 #endif // LIBC_SRC_SUPPORT_STR_TO_INTEGER_H