From 5eb97552591c6d2681b3e452b4b7d8445354b138 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Fri, 30 May 2025 07:54:08 +0200 Subject: [PATCH] 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 --- py/parsenum.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/py/parsenum.c b/py/parsenum.c index a38ce563f..e5c08b028 100644 --- a/py/parsenum.c +++ b/py/parsenum.c @@ -252,9 +252,9 @@ parse_start: const char *str_val_start = str; // determine what the string is - if (str < top && (str[0] | 0x20) == 'i') { + if (str + 2 < top && (str[0] | 0x20) == 'i') { // string starts with 'i', should be 'inf' or 'infinity' (case insensitive) - if (str + 2 < top && (str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { + if ((str[1] | 0x20) == 'n' && (str[2] | 0x20) == 'f') { // inf str += 3; dec_val = (mp_float_t)INFINITY; @@ -263,9 +263,9 @@ parse_start: str += 5; } } - } else if (str < top && (str[0] | 0x20) == 'n') { + } else if (str + 2 < top && (str[0] | 0x20) == 'n') { // string starts with 'n', should be 'nan' (case insensitive) - if (str + 2 < top && (str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { + if ((str[1] | 0x20) == 'a' && (str[2] | 0x20) == 'n') { // NaN str += 3; dec_val = MICROPY_FLOAT_C_FUN(nan)("");