diff --git a/lib/std/core/string_to_real.c3 b/lib/std/core/string_to_real.c3 index ab9a019c8..3c715f309 100644 --- a/lib/std/core/string_to_real.c3 +++ b/lib/std/core/string_to_real.c3 @@ -462,7 +462,7 @@ macro String.to_real(chars, $Type) @private $error "Unexpected type"; $endswitch - while (chars.len && chars[0] == ' ') chars = chars[1..]; + chars = chars.trim(); if (!chars.len) return MALFORMED_FLOAT?; if (chars.len != 1) @@ -476,6 +476,9 @@ macro String.to_real(chars, $Type) @private chars = chars[1..]; } } + chars = chars.trim(); + if (!chars.len) return MALFORMED_FLOAT?; + if (chars == "infinity" || chars == "INFINITY") return sign * $Type.inf; if (chars == "NAN" || chars == "nan") return $Type.nan; diff --git a/releasenotes.md b/releasenotes.md index 51ee28c09..2e27b438a 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -27,6 +27,7 @@ - Initialize pool correctly in print_backtrace. - `--max-mem` now works correctly again. - Casting a fault to a pointer would trigger an assert. +- Make `to_float` more tolerant to spaces. ### Stdlib changes diff --git a/test/unit/stdlib/core/string.c3 b/test/unit/stdlib/core/string.c3 index 3e37225ec..1505ee7ec 100644 --- a/test/unit/stdlib/core/string.c3 +++ b/test/unit/stdlib/core/string.c3 @@ -306,3 +306,9 @@ fn void tokenize_all_skip_last() } test::eq(str.str_view(), "foo--bar-baz-"); } + +fn void test_float() +{ + test::eq_approx(- 2.04632e-05," - 2.04632e-05 ".to_float()!!, delta: 0.00001e-5); + test::eq_approx(2.04632e-05," + 2.04632e-05 ".to_float()!!, delta: 0.00001e-5); +} \ No newline at end of file