Skip to content

Commit

Permalink
Ignore leading and trailing whitespace when parsing floats
Browse files Browse the repository at this point in the history
  • Loading branch information
klange committed Mar 6, 2024
1 parent c865e0d commit 6b134d1
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/obj_long.c
Original file line number Diff line number Diff line change
Expand Up @@ -2821,15 +2821,16 @@ KrkValue krk_parse_float(const char * s, size_t l) {

union Float { double d; uint64_t i; };

while (c < l && (s[c] == ' ' || s[c] == '\t' || s[c] == '\n' || s[c] == '\r')) c++;

/* Collect a leading sign. */
if (s[c] == '-') {
sign = -1;
c++;
ps = 1;
} else if (s[c] == '+') {
c++;
ps = 1;
}
ps = c;

/* Case-insensitive check for stringy floats: nan, inf */
if (c + 3 == l) {
Expand Down Expand Up @@ -2867,6 +2868,8 @@ KrkValue krk_parse_float(const char * s, size_t l) {
ee = c;
}

while (c < l && (s[c] == ' ' || s[c] == '\t' || s[c] == '\n' || s[c] == '\r')) c++;

/* If we're not at the end here, we have invalid characters. */
if (c != l) return krk_runtimeError(vm.exceptions->valueError, "invalid literal for float");

Expand Down

0 comments on commit 6b134d1

Please sign in to comment.