[NFC][Py Reformat] Added more commits to .git-blame-ignore-revs
[llvm-project.git] / libc / src / __support / str_to_integer.h
blobb7af39d09bb47e03a40fdce14f1689ffd28a64fb
1 //===-- String to integer conversion utils ----------------------*- C++ -*-===//
2 //
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
6 //
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
18 #include <limits.h>
20 namespace __llvm_libc {
21 namespace internal {
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)) {
27 ++src;
29 return src;
32 LIBC_INLINE int b36_char_to_int(char input) {
33 if (isdigit(input))
34 return input - '0';
35 if (isalpha(input))
36 return (input | 32) + 10 - 'a';
37 return 0;
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
49 // base.
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)) {
55 (*src) += 2;
56 return 16;
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') {
61 return 8;
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)
64 else {
65 return 10;
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.
71 template <class T>
72 LIBC_INLINE StrToNumResult<T> strtointeger(const char *__restrict src,
73 int base) {
74 unsigned long long result = 0;
75 bool is_number = false;
76 const char *original_src = src;
77 int error_val = 0;
79 if (base < 0 || base == 1 || base > 36) {
80 error_val = EINVAL;
81 return {0, 0, error_val};
84 src = first_non_whitespace(src);
86 char result_sign = '+';
87 if (*src == '+' || *src == '-') {
88 result_sign = *src;
89 ++src;
92 if (base == 0) {
93 base = infer_base(&src);
94 } else if (base == 16 && is_hex_start(src)) {
95 src = src + 2;
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 =
101 !IS_UNSIGNED
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)
110 break;
112 is_number = true;
113 ++src;
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
117 // the number.
118 if (result == abs_max) {
119 error_val = ERANGE;
120 continue;
123 if (result > abs_max_div_by_base) {
124 result = abs_max;
125 error_val = ERANGE;
126 } else {
127 result = result * base;
129 if (result > abs_max - cur_digit) {
130 result = abs_max;
131 error_val = ERANGE;
132 } else {
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};
146 return {is_positive
147 ? static_cast<T>(result)
148 : static_cast<T>(-static_cast<cpp::make_unsigned_t<T>>(result)),
149 str_len, error_val};
152 } // namespace internal
153 } // namespace __llvm_libc
155 #endif // LIBC_SRC_SUPPORT_STR_TO_INTEGER_H