From bde5de68ca8c3ca3a1881dae2e6c671b327a47cc Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 20 Nov 2022 09:24:21 +0100 Subject: [PATCH] Simplify is_valid_digit_for_base by reaching out to parse_digit_unchecked --- luna/NumberParsing.cpp | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/luna/NumberParsing.cpp b/luna/NumberParsing.cpp index 428cbfa1..7304a851 100644 --- a/luna/NumberParsing.cpp +++ b/luna/NumberParsing.cpp @@ -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;