summaryrefslogtreecommitdiff
path: root/py/parsenum.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2025-05-30 07:56:52 +0200
committerDamien George <damien@micropython.org>2025-06-10 15:22:34 +1000
commitc1629dc2ff12a92d600b5988827566f8d693cad1 (patch)
treef5aaaaa68d46fca1be26b078fbb8565f0c8079bc /py/parsenum.c
parent5eb97552591c6d2681b3e452b4b7d8445354b138 (diff)
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 <jepler@gmail.com>
Diffstat (limited to 'py/parsenum.c')
-rw-r--r--py/parsenum.c28
1 files changed, 11 insertions, 17 deletions
diff --git a/py/parsenum.c b/py/parsenum.c
index e5c08b028..300b5f28d 100644
--- a/py/parsenum.c
+++ b/py/parsenum.c
@@ -252,24 +252,18 @@ parse_start:
const char *str_val_start = str;
// determine what the string is
- if (str + 2 < top && (str[0] | 0x20) == 'i') {
- // string starts with 'i', should be 'inf' or 'infinity' (case insensitive)
- if ((str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
- // inf
- str += 3;
- dec_val = (mp_float_t)INFINITY;
- if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
- // infinity
- str += 5;
- }
- }
- } else if (str + 2 < top && (str[0] | 0x20) == 'n') {
- // string starts with 'n', should be 'nan' (case insensitive)
- if ((str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
- // NaN
- str += 3;
- dec_val = MICROPY_FLOAT_C_FUN(nan)("");
+ if (str + 2 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') {
+ // 'inf' or 'infinity' (case insensitive)
+ str += 3;
+ dec_val = (mp_float_t)INFINITY;
+ if (str + 4 < top && (str[0] | 0x20) == 'i' && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'i' && (str[3] | 0x20) == 't' && (str[4] | 0x20) == 'y') {
+ // infinity
+ str += 5;
}
+ } else if (str + 2 < top && (str[0] | 0x20) == 'n' && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') {
+ // 'nan' (case insensitive)
+ str += 3;
+ dec_val = MICROPY_FLOAT_C_FUN(nan)("");
} else {
// string should be a decimal number
parse_dec_in_t in = PARSE_DEC_IN_INTG;