Skip to content

Commit c2dd494

Browse files
committed
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).
1 parent 25f44c1 commit c2dd494

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

py/parsenum.c

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,18 @@ mp_obj_t mp_parse_num_integer(const char *restrict str_, size_t len, int base, m
8181
for (; str < top; str++) {
8282
// get next digit as a value
8383
mp_uint_t dig = *str;
84-
if (unichar_isdigit(dig) && (int)dig - '0' < base) {
85-
// 0-9 digit
86-
dig = dig - '0';
87-
} else if (base == 16) {
88-
dig |= 0x20;
89-
if ('a' <= dig && dig <= 'f') {
90-
// a-f hex digit
91-
dig = dig - 'a' + 10;
84+
if ('0' <= dig && dig <= '9') {
85+
dig -= '0';
86+
} else {
87+
dig |= 0x20; // make digit lower-case
88+
if ('a' <= dig && dig <= 'z') {
89+
dig -= 'a' - 10;
9290
} else {
9391
// unknown character
9492
break;
9593
}
96-
} else {
97-
// unknown character
94+
}
95+
if (dig >= base) {
9896
break;
9997
}
10098

0 commit comments

Comments
 (0)