summaryrefslogtreecommitdiff
path: root/py/parsenum.c
diff options
context:
space:
mode:
authorJeff Epler <jepler@gmail.com>2025-05-30 07:54:08 +0200
committerDamien George <damien@micropython.org>2025-06-10 15:21:18 +1000
commit5eb97552591c6d2681b3e452b4b7d8445354b138 (patch)
tree46067fb0e25f7158d586fd6dfe3d9534a2649801 /py/parsenum.c
parent745bec9ce3cc4c95d8e80fcc8f64ffecde1d91bc (diff)
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 <jepler@gmail.com>
Diffstat (limited to 'py/parsenum.c')
-rw-r--r--py/parsenum.c8
1 files 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)("");