Ignore leading and trailing whitespace when parsing floats

This commit is contained in:
K. Lange 2024-03-06 09:50:43 +09:00
parent c865e0dc78
commit 6b134d11ff

View File

@ -2821,15 +2821,16 @@ KrkValue krk_parse_float(const char * s, size_t l) {
union Float { double d; uint64_t i; }; 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. */ /* Collect a leading sign. */
if (s[c] == '-') { if (s[c] == '-') {
sign = -1; sign = -1;
c++; c++;
ps = 1;
} else if (s[c] == '+') { } else if (s[c] == '+') {
c++; c++;
ps = 1;
} }
ps = c;
/* Case-insensitive check for stringy floats: nan, inf */ /* Case-insensitive check for stringy floats: nan, inf */
if (c + 3 == l) { if (c + 3 == l) {
@ -2867,6 +2868,8 @@ KrkValue krk_parse_float(const char * s, size_t l) {
ee = c; 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 we're not at the end here, we have invalid characters. */
if (c != l) return krk_runtimeError(vm.exceptions->valueError, "invalid literal for float"); if (c != l) return krk_runtimeError(vm.exceptions->valueError, "invalid literal for float");