Simplify is_valid_digit_for_base by reaching out to parse_digit_unchecked

This commit is contained in:
apio 2022-11-20 09:24:21 +01:00
parent 44f44aedca
commit bde5de68ca

View File

@ -37,24 +37,6 @@ isize _atos(const char** str)
return neg ? -val : val;
}
static bool is_valid_digit_for_base(int base, char c)
{
if (base <= 10)
{
if (!_isdigit(c)) return false; // Bases lower than 10 only use decimal digits.
if ((c - '0') < base) return true;
return false;
}
else
{
if (!_isalnum(c)) return false; // Any valid base (2-36) only uses 0-9 and a-z.
if (_isdigit(c)) return true; // Any base higher than decimal will include all decimal digits.
bool lower = _islower(c);
if (((c - lower ? 'a' : 'A') + 10) < base) return true;
return false;
}
}
// This function assumes you have called is_valid_digit_for_base() to validate the digit first.
static isize parse_digit_unchecked(char c)
{
@ -63,6 +45,13 @@ static isize parse_digit_unchecked(char c)
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 _strtou(const char* str, const char** endptr, int base)
{
usize val = 0;