Skip to content

Commit

Permalink
fastfloat: localize handling of invalid number without integer and fr…
Browse files Browse the repository at this point in the history
…actional parts

This is a follow-up for 6f52d1b
  • Loading branch information
valyala committed Dec 29, 2022
1 parent 6f52d1b commit 93f67d9
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
24 changes: 8 additions & 16 deletions fastfloat/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,9 @@ func ParseBestEffort(s string) float64 {

// the integer part might be elided to remain compliant
// with https://go.dev/ref/spec#Floating-point_literals
intPartElided := s[i] == '.'
if s[i] == '.' && (i+1 >= uint(len(s)) || s[i+1] < '0' || s[i+1] > '9') {
return 0
}

d := uint64(0)
j := i
Expand All @@ -236,7 +238,7 @@ func ParseBestEffort(s string) float64 {
}
break
}
if i <= j && !intPartElided {
if i <= j && s[i] != '.' {
s = s[i:]
if strings.HasPrefix(s, "+") {
s = s[1:]
Expand Down Expand Up @@ -267,9 +269,6 @@ func ParseBestEffort(s string) float64 {
// Parse fractional part.
i++
if i >= uint(len(s)) {
if intPartElided {
return 0
}
// the fractional part may be elided to remain compliant
// with https://go.dev/ref/spec#Floating-point_literals
return f
Expand Down Expand Up @@ -305,9 +304,6 @@ func ParseBestEffort(s string) float64 {
}
}
if s[i] == 'e' || s[i] == 'E' {
if intPartElided {
return 0
}
// Parse exponent part.
i++
if i >= uint(len(s)) {
Expand Down Expand Up @@ -377,7 +373,9 @@ func Parse(s string) (float64, error) {

// the integer part might be elided to remain compliant
// with https://go.dev/ref/spec#Floating-point_literals
intPartElided := s[i] == '.'
if s[i] == '.' && (i+1 >= uint(len(s)) || s[i+1] < '0' || s[i+1] > '9') {
return 0, fmt.Errorf("missing integer and fractional part in %q", s)
}

d := uint64(0)
j := i
Expand All @@ -398,7 +396,7 @@ func Parse(s string) (float64, error) {
}
break
}
if i <= j && !intPartElided {
if i <= j && s[i] != '.' {
ss := s[i:]
if strings.HasPrefix(ss, "+") {
ss = ss[1:]
Expand Down Expand Up @@ -429,9 +427,6 @@ func Parse(s string) (float64, error) {
// Parse fractional part.
i++
if i >= uint(len(s)) {
if intPartElided {
return 0, fmt.Errorf("cannot parse integer or fractional part in %q", s)
}
// the fractional part might be elided to remain compliant
// with https://go.dev/ref/spec#Floating-point_literals
return f, nil
Expand Down Expand Up @@ -467,9 +462,6 @@ func Parse(s string) (float64, error) {
}
}
if s[i] == 'e' || s[i] == 'E' {
if intPartElided {
return 0, fmt.Errorf("cannot parse integer part in %q", s)
}
// Parse exponent part.
i++
if i >= uint(len(s)) {
Expand Down
2 changes: 2 additions & 0 deletions fastfloat/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,8 @@ func TestParseSuccess(t *testing.T) {
f("-123.456E-10", -123.456e-10)
f("1.e4", 1.e4)
f("-1.E-10", -1.e-10)
f(".1e3", 100)
f("-.12e3", -120)

// inf and nan
f("12345678909123456789012e45678", math.Inf(1))
Expand Down
3 changes: 3 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func TestParseRawNumber(t *testing.T) {
f("-12.345E+67 tail", "-12.345E+67", " tail")
f("-12.345E-67,tail", "-12.345E-67", ",tail")
f("-1234567.8e+90tail", "-1234567.8e+90", "tail")
f("12.tail", "12.", "tail")
f(".2tail", ".2", "tail")
f("-.2tail", "-.2", "tail")
f("NaN", "NaN", "")
f("nantail", "nan", "tail")
f("inf", "inf", "")
Expand Down

0 comments on commit 93f67d9

Please sign in to comment.