Skip to content

Commit 8acd841

Browse files
Fix negative double parsing in 'json::readDouble' (#1359)
Fix #1355: `-1.5` gets parsed as `-1.5` instead of `-0.5` Replaces #1356: I made a new PR so that we can add tests --------- Co-authored-by: Joshua Schlucke <joshua.dev.no-reply@ochre.ovh>
1 parent ad6f80f commit 8acd841

2 files changed

Lines changed: 11 additions & 1 deletion

File tree

examples/stdlib/json.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
a
44

5+
-1.5
6+
57
a
68
b
79
f

libraries/common/json.effekt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def readDouble(): Double / { Scan[Char], Exception[WrongFormat]} = {
109109
}
110110
def fraction(pre: Int): Double / fail = {
111111
if (succeeds { must { readIf('.') } }) {
112-
var b = 0.1
112+
var b = if (pre >= 0) 0.1 else -0.1
113113
var r = pre.toDouble
114114
while (optionally[Int] { must { readDigit() } } is Some(d)) {
115115
r = r + b * d.toDouble
@@ -316,6 +316,14 @@ namespace test {
316316

317317
println("")
318318

319+
feed("-1.5") {
320+
with scanner[Char]
321+
with on[WrongFormat].report
322+
println(readDouble())
323+
}
324+
325+
println("")
326+
319327
// Parse example
320328
feed("""{ "a": null, "b": [true,false,false,true], "f": 12.532 }"""){
321329
with scanner[Char]

0 commit comments

Comments
 (0)