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++) { for (; str < top; str++) {
// get next digit as a value // get next digit as a value
mp_uint_t dig = *str; mp_uint_t dig = *str;
if (unichar_isdigit(dig) && (int)dig - '0' < base) { if ('0' <= dig && dig <= '9') {
// 0-9 digit dig -= '0';
dig = dig - '0'; } else {
} else if (base == 16) { dig |= 0x20; // make digit lower-case
dig |= 0x20; if ('a' <= dig && dig <= 'z') {
if ('a' <= dig && dig <= 'f') { dig -= 'a' - 10;
// a-f hex digit
dig = dig - 'a' + 10;
} else { } else {
// unknown character // unknown character
break; break;
} }
} else { }
// unknown character if (dig >= base) {
break; break;
} }