fix: reject trailing non-number characters in Num.fromString parsing

Previously, `Num.fromString` accepted strings like `"1.2prefix"` and returned the
leading numeric portion (`1.2`). This change makes parsing stricter by requiring
that the entire string be consumed after `strtod`, skipping only trailing
whitespace. If any non-whitespace characters remain after the number, the
function now returns `null` instead of a partial result.

The fix also adds an explicit empty-string check and updates the test suite to
reflect the new behavior, replacing the old `"1.2prefix"` success test with a
`"1.2suffix"` null-return test.
This commit is contained in:
Bob Nystrom
2015-02-25 15:07:54 +00:00
parent f0a73b6ce2
commit 3aa82e7960
2 changed files with 14 additions and 5 deletions
+2 -1
View File
@@ -4,10 +4,11 @@ IO.print(Num.fromString("-0") == -0) // expect: true
IO.print(Num.fromString("12.34") == 12.34) // expect: true
IO.print(Num.fromString("-0.0001") == -0.0001) // expect: true
IO.print(Num.fromString(" 12 ") == 12) // expect: true
IO.print(Num.fromString("1.2prefix") == 1.2) // expect: true
// Test some non-number literals and ensure they return null.
IO.print(Num.fromString("test1") == null) // expect: true
IO.print(Num.fromString("") == null) // expect: true
IO.print(Num.fromString("prefix1.2") == null) // expect: true
IO.print(Num.fromString("1.2suffix") == null) // expect: true
// TODO: Parse hex and scientific numbers.