Jeroen van Rijn 2 years ago
parent
commit
4c78ba2152
2 changed files with 13 additions and 2 deletions
  1. 8 1
      core/strconv/strconv.odin
  2. 5 1
      tests/issues/test_issue_2087.odin

+ 8 - 1
core/strconv/strconv.odin

@@ -575,9 +575,11 @@ parse_f64 :: proc(str: string, n: ^int = nil) -> (value: f64, ok: bool) {
 	i := 0
 	i := 0
 
 
 	sign: f64 = 1
 	sign: f64 = 1
+	seen_sign := true
 	switch s[i] {
 	switch s[i] {
 	case '-': i += 1; sign = -1
 	case '-': i += 1; sign = -1
 	case '+': i += 1
 	case '+': i += 1
+	case: seen_sign = false
 	}
 	}
 
 
 	for ; i < len(s); i += 1 {
 	for ; i < len(s); i += 1 {
@@ -677,8 +679,13 @@ parse_f64 :: proc(str: string, n: ^int = nil) -> (value: f64, ok: bool) {
 			for exp >   0 { scale *=   10; exp -=  1 }
 			for exp >   0 { scale *=   10; exp -=  1 }
 		}
 		}
 	}
 	}
-	s = s[i:]
 
 
+	// If we only consumed a sign, return false
+	if i == 1 && seen_sign {
+		return 0, false
+	}
+
+	s = s[i:]
 	if frac {
 	if frac {
 		value = sign * (value/scale)
 		value = sign * (value/scale)
 	} else {
 	} else {

+ 5 - 1
tests/issues/test_issue_2087.odin

@@ -10,9 +10,13 @@ test_parse_float :: proc(t: ^testing.T) {
 	{
 	{
 		f, ok := strconv.parse_f64("1.2")
 		f, ok := strconv.parse_f64("1.2")
 		testing.expect(t, ok && f == 1.2, "expected f64(1.2), fully consumed")
 		testing.expect(t, ok && f == 1.2, "expected f64(1.2), fully consumed")
-
 		f, ok = strconv.parse_f64("1.2a")
 		f, ok = strconv.parse_f64("1.2a")
 		testing.expect(t, !ok && f == 1.2, "expected f64(1.2), partially consumed")
 		testing.expect(t, !ok && f == 1.2, "expected f64(1.2), partially consumed")
+		f, ok = strconv.parse_f64("+")
+		testing.expect(t, !ok && f == 0.0, "expected f64(0.0), with ok=false")
+		f, ok = strconv.parse_f64("-")
+		testing.expect(t, !ok && f == 0.0, "expected f64(0.0), with ok=false")
+
 
 
 		f, ok = strconv.parse_f64("inf")
 		f, ok = strconv.parse_f64("inf")
 		testing.expect(t, ok && math.classify(f) == math.Float_Class.Inf, "expected f64(+inf), fully consumed")
 		testing.expect(t, ok && math.classify(f) == math.Float_Class.Inf, "expected f64(+inf), fully consumed")