78 lines
1.7 KiB
C++
78 lines
1.7 KiB
C++
#include <luna/CType.h>
|
|
#include <luna/NumberParsing.h>
|
|
|
|
// This function assumes you have called is_valid_digit_for_base() to validate the digit first.
|
|
static isize parse_digit_unchecked(char c)
|
|
{
|
|
if (_isdigit(c)) return c - '0';
|
|
if (_islower(c)) return (c - 'a') + 10;
|
|
return (c - 'A') + 10;
|
|
}
|
|
|
|
static bool is_valid_digit_for_base(int base, char c)
|
|
{
|
|
if (!_isalnum(c)) return false;
|
|
if (parse_digit_unchecked(c) >= (isize)base) return false;
|
|
return true;
|
|
}
|
|
|
|
usize parse_unsigned_integer(const char* str, const char** endptr, int base)
|
|
{
|
|
usize val = 0;
|
|
|
|
while (_isspace(*str)) str++;
|
|
|
|
if ((base == 0 || base == 16) && *str == '0')
|
|
{
|
|
str++;
|
|
if (_tolower(*str) == 'x')
|
|
{
|
|
base = 16;
|
|
str++;
|
|
}
|
|
else if (base == 0)
|
|
base = 8;
|
|
}
|
|
else if (base == 0)
|
|
base = 10;
|
|
|
|
while (is_valid_digit_for_base(base, *str))
|
|
{
|
|
val = ((usize)base * val) + (usize)parse_digit_unchecked(*str);
|
|
str++;
|
|
}
|
|
|
|
if (endptr) *endptr = str;
|
|
|
|
return val;
|
|
}
|
|
|
|
isize parse_signed_integer(const char* str, const char** endptr, int base)
|
|
{
|
|
bool negative = false;
|
|
|
|
while (_isspace(*str)) str++;
|
|
|
|
if (*str == '-' || *str == '+')
|
|
{
|
|
if (*str == '-') negative = true;
|
|
str++;
|
|
}
|
|
|
|
usize rc = parse_unsigned_integer(
|
|
str, endptr,
|
|
base); // FIXME: Check for overflow (the unsigned usize value might not fit into a signed isize)
|
|
|
|
return negative ? -(isize)rc : (isize)rc;
|
|
}
|
|
|
|
usize scan_unsigned_integer(const char** str)
|
|
{
|
|
return parse_unsigned_integer(*str, str, 10);
|
|
}
|
|
|
|
isize scan_signed_integer(const char** str)
|
|
{
|
|
return parse_signed_integer(*str, str, 10);
|
|
}
|