Skip to content

Commit c1629dc

Browse files
jeplerdpgeorge
authored andcommitted
py/parsenum: Further reduce code size in check for inf/nan.
A few more bytes can be saved by not using nested `if`s (4 bytes for `build-MICROBIT/py/parsenum.o`, 8 bytes for RPI_PICO firmware). This commit is better viewed with whitespace changes hidden, because two blocks were reindented (e.g., `git show -b`). Signed-off-by: Jeff Epler <[email protected]>
1 parent 5eb9755 commit c1629dc

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

py/parsenum.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -252,24 +252,18 @@ 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 + 2 < top && (str[0] | 0x20) == 'i') {
256-
// string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
257-
if ((str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
258-
// inf
259-
str += 3;
260-
dec_val = (mp_float_t)INFINITY;
261-
if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
262-
// infinity
263-
str += 5;
264-
}
265-
}
266-
} else if (str + 2 < top && (str[0] | 0x20) == 'n') {
267-
// string starts with 'n', should be 'nan' (case insensitive)
268-
if ((str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
269-
// NaN
270-
str += 3;
271-
dec_val = MICROPY_FLOAT_C_FUN(nan)("");
255+
if (str + 2 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
256+
// 'inf' or 'infinity' (case insensitive)
257+
str += 3;
258+
dec_val = (mp_float_t)INFINITY;
259+
if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
260+
// infinity
261+
str += 5;
272262
}
263+
} else if (str + 2 < top && (str[0] | 0x20) == 'n' && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
264+
// 'nan' (case insensitive)
265+
str += 3;
266+
dec_val = MICROPY_FLOAT_C_FUN(nan)("");
273267
} else {
274268
// string should be a decimal number
275269
parse_dec_in_t in = PARSE_DEC_IN_INTG;

0 commit comments

Comments
 (0)