py/parsenum: Simplify and generalise decoding of digit values.

This function should be able to parse integers with any value for the
base, because it is called by int('xxx', base).
This commit is contained in:
Damien George 2016-12-28 12:02:49 +11:00
parent 25f44c19f1
commit c2dd494bd9

View File

@ -81,20 +81,18 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m
for (; str < top; str++) {
// get next digit as a value
mp_uint_t dig = *str;
if (unichar_isdigit(dig) && (int)dig - '0' < base) {
// 0-9 digit
dig = dig - '0';
} else if (base == 16) {
dig |= 0x20;
if ('a' <= dig && dig <= 'f') {
// a-f hex digit
dig = dig - 'a' + 10;
if ('0' <= dig && dig <= '9') {
dig -= '0';
} else {
dig |= 0x20; // make digit lower-case
if ('a' <= dig && dig <= 'z') {
dig -= 'a' - 10;
} else {
// unknown character
break;
}
} else {
// unknown character
}
if (dig >= base) {
break;
}