Skip to content

Commit 5eb9755

Browse files
jeplerdpgeorge
authored andcommitted
py/parsenum: Reduce code size in check for inf/nan.
By avoiding two different checks of the string length, code size is reduced without changing behavior: Some invalid float/complex strings like "ix" will get handled just like "xx" in the main number literal parsing code instead. The optimizer alone couldn't remove the reundant comparisons because it couldn't make a transformation that let an invalid string like "ix" pass into the generic number parsing code. Signed-off-by: Jeff Epler <[email protected]>
1 parent 745bec9 commit 5eb9755

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

py/parsenum.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,9 +252,9 @@ mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lex
252252
const char *str_val_start = str;
253253

254254
// determine what the string is
255-
if (str < top && (str[0] | 0x20) == 'i') {
255+
if (str + 2 < top && (str[0] | 0x20) == 'i') {
256256
// string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
257-
if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
257+
if ((str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
258258
// inf
259259
str += 3;
260260
dec_val = (mp_float_t)INFINITY;
@@ -263,9 +263,9 @@ mp_obj_t mp_parse_num_float(const char *str, size_t len, bool allow_imag, mp_lex
263263
str += 5;
264264
}
265265
}
266-
} else if (str < top && (str[0] | 0x20) == 'n') {
266+
} else if (str + 2 < top && (str[0] | 0x20) == 'n') {
267267
// string starts with 'n', should be 'nan' (case insensitive)
268-
if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
268+
if ((str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
269269
// NaN
270270
str += 3;
271271
dec_val = MICROPY_FLOAT_C_FUN(nan)("");

0 commit comments

Comments
 (0)