LibC: Move strtoT implementation to common header

This commit is contained in:
2026-07-04 02:39:16 +03:00
parent d5bc88584d
commit 122c325a5b
2 changed files with 319 additions and 259 deletions

View File

@@ -3,7 +3,6 @@
#include <BAN/Math.h>
#include <BAN/UTF8.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
@@ -16,6 +15,8 @@
#include <sys/syscall.h>
#include <unistd.h>
#include <bits/strtoT.hpp>
#include <icxxabi.h>
void abort(void)
@@ -50,264 +51,6 @@ int atexit(void (*func)(void))
return __cxa_atexit([](void* func_ptr) { reinterpret_cast<void (*)(void)>(func_ptr)(); }, func_addr, nullptr);
}
static constexpr int get_base_digit(char c, int base)
{
int digit = -1;
if (isdigit(c))
digit = c - '0';
else if (isalpha(c))
digit = 10 + tolower(c) - 'a';
if (digit < base)
return digit;
return -1;
}
template<BAN::integral T>
static constexpr bool will_digit_append_overflow(T current, int digit, int base)
{
if (BAN::Math::will_multiplication_overflow<T>(current, base))
return true;
if (BAN::Math::will_addition_overflow<T>(current * base, current < 0 ? -digit : digit))
return true;
return false;
}
template<BAN::integral T>
static T strtoT(const char* str, char** endp, int base, int& error)
{
// validate base
if (base != 0 && (base < 2 || base > 36))
{
if (endp)
*endp = const_cast<char*>(str);
error = EINVAL;
return 0;
}
// skip whitespace
while (isspace(*str))
str++;
// get sign and skip it
bool negative = (*str == '-');
if (*str == '-' || *str == '+')
str++;
// determine base from prefix
if (base == 0)
{
if (strncasecmp(str, "0x", 2) == 0)
base = 16;
else if (*str == '0')
base = 8;
else if (isdigit(*str))
base = 10;
}
// check for invalid conversion
if (get_base_digit(*str, base) == -1)
{
if (endp)
*endp = const_cast<char*>(str);
error = EINVAL;
return 0;
}
// remove "0x" prefix from hexadecimal
if (base == 16 && strncasecmp(str, "0x", 2) == 0 && get_base_digit(str[2], base) != -1)
str += 2;
bool overflow = false;
T result = 0;
// calculate the value of the number in string
while (!overflow)
{
int digit = get_base_digit(*str, base);
if (digit == -1)
break;
str++;
overflow = will_digit_append_overflow(result, digit, base);
if (!overflow)
{
if (negative && !BAN::is_unsigned_v<T>)
digit = -digit;
result = result * base + digit;
}
}
if (negative && BAN::is_unsigned_v<T>)
result = -result;
// save endp if asked
if (endp)
{
while (get_base_digit(*str, base) != -1)
str++;
*endp = const_cast<char*>(str);
}
// return error on overflow
if (overflow)
{
error = ERANGE;
if constexpr(BAN::is_unsigned_v<T>)
return BAN::numeric_limits<T>::max();
return negative ? BAN::numeric_limits<T>::min() : BAN::numeric_limits<T>::max();
}
return result;
}
template<BAN::floating_point T>
static T strtoT(const char* str, char** endp, int& error)
{
// find nan end including possible n-char-sequence
auto get_nan_end = [](const char* str) -> const char*
{
ASSERT(strcasecmp(str, "nan") == 0);
if (str[3] != '(')
return str + 3;
for (size_t i = 4; isalnum(str[i]) || str[i] == '_'; i++)
if (str[i] == ')')
return str + i + 1;
return str + 3;
};
// skip whitespace
while (isspace(*str))
str++;
// get sign and skip it
bool negative = (*str == '-');
if (*str == '-' || *str == '+')
str++;
// check for infinity or nan
{
T result = 0;
if (strncasecmp(str, "inf", 3) == 0)
{
result = BAN::numeric_limits<T>::infinity();
str += strncasecmp(str, "infinity", 8) ? 3 : 8;
}
else if (strncasecmp(str, "nan", 3) == 0)
{
result = BAN::numeric_limits<T>::quiet_NaN();
str = get_nan_end(str);
}
if (result != 0)
{
if (endp)
*endp = const_cast<char*>(str);
return negative ? -result : result;
}
}
// no conversion can be performed -- not ([digit] || .[digit])
if (!(isdigit(*str) || (str[0] == '.' && isdigit(str[1]))))
{
if (endp)
*endp = const_cast<char*>(str);
error = EINVAL;
return 0;
}
int base = 10;
int exponent = 0;
int exponents_per_digit = 1;
// check whether we have base 16 value -- (0x[xdigit] || 0x.[xdigit])
if (strncasecmp(str, "0x", 2) == 0 && (isxdigit(str[2]) || (str[2] == '.' && isxdigit(str[3]))))
{
base = 16;
exponents_per_digit = 4;
str += 2;
}
// parse whole part
T result = 0;
T multiplier = 1;
while (true)
{
int digit = get_base_digit(*str, base);
if (digit == -1)
break;
str++;
if (result)
exponent += exponents_per_digit;
if (digit)
result += multiplier * digit;
if (result)
multiplier /= base;
}
if (*str == '.')
str++;
while (true)
{
int digit = get_base_digit(*str, base);
if (digit == -1)
break;
str++;
if (result == 0)
exponent -= exponents_per_digit;
if (digit)
result += multiplier * digit;
if (result)
multiplier /= base;
}
if (tolower(*str) == (base == 10 ? 'e' : 'p'))
{
char* maybe_end = nullptr;
int exp_error = 0;
int extra_exponent = strtoT<int>(str + 1, &maybe_end, 10, exp_error);
if (exp_error != EINVAL)
{
if (exp_error == ERANGE || BAN::Math::will_addition_overflow(exponent, extra_exponent))
exponent = negative ? BAN::numeric_limits<int>::min() : BAN::numeric_limits<int>::max();
else
exponent += extra_exponent;
str = maybe_end;
}
}
if (endp)
*endp = const_cast<char*>(str);
// no over/underflow can happed with zero
if (result == 0)
return 0;
const int max_exponent = (base == 10) ? BAN::numeric_limits<T>::max_exponent10() : BAN::numeric_limits<T>::max_exponent2();
if (exponent > max_exponent)
{
error = ERANGE;
result = BAN::numeric_limits<T>::infinity();
return negative ? -result : result;
}
const int min_exponent = (base == 10) ? BAN::numeric_limits<T>::min_exponent10() : BAN::numeric_limits<T>::min_exponent2();
if (exponent < min_exponent)
{
error = ERANGE;
result = 0;
return negative ? -result : result;
}
if (exponent)
result *= BAN::Math::pow<T>((base == 10) ? 10 : 2, exponent);
return negative ? -result : result;
}
double atof(const char* str)
{
return strtod(str, nullptr);