Skip to content

Commit 6370546

Browse files
[libc] Make strtointeger handle all bigint types (#111926)
Needed for #110894 The strtointeger code was updated to support some bigint types in #85201 but not all. This patch finishes the cleanup so that it can work for a wider range of types.
1 parent f4cf624 commit 6370546

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

libc/src/__support/str_to_integer.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include "src/__support/CPP/limits.h"
1313
#include "src/__support/CPP/type_traits.h"
14+
#include "src/__support/CPP/type_traits/make_unsigned.h"
15+
#include "src/__support/big_int.h"
1416
#include "src/__support/common.h"
1517
#include "src/__support/ctype_utils.h"
1618
#include "src/__support/macros/config.h"
@@ -77,9 +79,7 @@ template <class T>
7779
LIBC_INLINE StrToNumResult<T>
7880
strtointeger(const char *__restrict src, int base,
7981
const size_t src_len = cpp::numeric_limits<size_t>::max()) {
80-
using ResultType = typename cpp::conditional_t<(cpp::is_same_v<T, UInt128> ||
81-
cpp::is_same_v<T, Int128>),
82-
UInt128, unsigned long long>;
82+
using ResultType = make_integral_or_big_int_unsigned_t<T>;
8383

8484
ResultType result = 0;
8585

@@ -137,13 +137,13 @@ strtointeger(const char *__restrict src, int base,
137137
result = abs_max;
138138
error_val = ERANGE;
139139
} else {
140-
result = result * base;
140+
result = static_cast<ResultType>(result * base);
141141
}
142142
if (result > abs_max - cur_digit) {
143143
result = abs_max;
144144
error_val = ERANGE;
145145
} else {
146-
result = result + cur_digit;
146+
result = static_cast<ResultType>(result + cur_digit);
147147
}
148148
}
149149

@@ -156,12 +156,7 @@ strtointeger(const char *__restrict src, int base,
156156
return {cpp::numeric_limits<T>::min(), str_len, error_val};
157157
}
158158

159-
return {
160-
is_positive
161-
? static_cast<T>(result)
162-
: static_cast<T>(
163-
-static_cast<make_integral_or_big_int_unsigned_t<T>>(result)),
164-
str_len, error_val};
159+
return {static_cast<T>(is_positive ? result : -result), str_len, error_val};
165160
}
166161

167162
} // namespace internal

0 commit comments

Comments
 (0)