#ifndef LLVM_LIBC_SRC___SUPPORT_STR_TO_INTEGER_H
#define LLVM_LIBC_SRC___SUPPORT_STR_TO_INTEGER_H
#include "hdr/errno_macros.h"
#include "src/__support/CPP/limits.h"
#include "src/__support/CPP/type_traits.h"
#include "src/__support/CPP/type_traits/make_unsigned.h"
#include "src/__support/big_int.h"
#include "src/__support/common.h"
#include "src/__support/ctype_utils.h"
#include "src/__support/macros/config.h"
#include "src/__support/str_to_num_result.h"
#include "src/__support/uint128.h"
#include "src/__support/wctype_utils.h"
namespace LIBC_NAMESPACE_DECL {
namespace internal {
template <typename CharType>
LIBC_INLINE size_t
first_non_whitespace(const CharType *__restrict src,
size_t src_len = cpp::numeric_limits<size_t>::max()) {
size_t src_cur = 0;
for (; src_cur < src_len && internal::isspace(src[src_cur]); ++src_cur)
;
return src_cur;
}
template <typename CharType>
LIBC_INLINE static int get_sign(const CharType *__restrict src) {
if (is_char_or_wchar(src[0], '+', L'+'))
return 1;
if (is_char_or_wchar(src[0], '-', L'-'))
return -1;
return 0;
}
template <typename CharType>
LIBC_INLINE static bool is_hex_start(const CharType *__restrict src,
size_t src_len) {
if (src_len < 3)
return false;
return is_char_or_wchar(src[0], '0', L'0') &&
is_char_or_wchar(tolower(src[1]), 'x', L'x') && isalnum(src[2]) &&
b36_char_to_int(src[2]) < 16;
}
template <typename CharType>
LIBC_INLINE static int infer_base(const CharType *__restrict src,
size_t src_len) {
if (is_hex_start(src, src_len))
return 16;
if (src_len > 0 && is_char_or_wchar(src[0], '0', L'0')) {
return 8;
}
return 10;
}
template <typename T, typename CharType>
LIBC_INLINE StrToNumResult<T>
strtointeger(const CharType *__restrict src, int base,
const size_t src_len = cpp::numeric_limits<size_t>::max()) {
using ResultType = make_integral_or_big_int_unsigned_t<T>;
if (src_len == 0)
return {0, 0, 0};
if (base < 0 || base == 1 || base > 36)
return {0, 0, EINVAL};
size_t src_cur = first_non_whitespace(src, src_len);
if (src_cur == src_len) {
return {0, 0, 0};
}
int sign = get_sign(src + src_cur);
bool is_positive = (sign >= 0);
src_cur += (sign != 0);
if (base == 0)
base = infer_base(src + src_cur, src_len - src_cur);
if (base == 16 && is_hex_start(src + src_cur, src_len - src_cur))
src_cur = src_cur + 2;
constexpr bool IS_UNSIGNED = cpp::is_unsigned_v<T>;
ResultType constexpr NEGATIVE_MAX =
!IS_UNSIGNED ? static_cast<ResultType>(cpp::numeric_limits<T>::max()) + 1
: cpp::numeric_limits<T>::max();
ResultType const abs_max =
(is_positive ? cpp::numeric_limits<T>::max() : NEGATIVE_MAX);
ResultType const abs_max_div_by_base =
abs_max / static_cast<ResultType>(base);
bool is_number = false;
int error_val = 0;
ResultType result = 0;
while (src_cur < src_len && isalnum(src[src_cur])) {
int cur_digit = b36_char_to_int(src[src_cur]);
if (cur_digit >= base)
break;
is_number = true;
++src_cur;
if (result == abs_max) {
error_val = ERANGE;
continue;
}
if (result > abs_max_div_by_base) {
result = abs_max;
error_val = ERANGE;
} else {
result = result * static_cast<ResultType>(base);
}
if (result > abs_max - static_cast<ResultType>(cur_digit)) {
result = abs_max;
error_val = ERANGE;
} else {
result = result + static_cast<ResultType>(cur_digit);
}
}
ptrdiff_t str_len = is_number ? static_cast<ptrdiff_t>(src_cur) : 0;
if (error_val == ERANGE) {
if (is_positive || IS_UNSIGNED)
return {cpp::numeric_limits<T>::max(), str_len, error_val};
else
return {cpp::numeric_limits<T>::min(), str_len, error_val};
}
return {static_cast<T>(is_positive ? result : -result), str_len, error_val};
}
}
}
#endif