mirror of
https://github.com/json-iterator/go.git
synced 2025-05-16 21:45:43 +02:00
#136 strconv.ParseFloat can not validate 1. , added extra validation for this special case
This commit is contained in:
parent
e066e54964
commit
6b6938829d
@ -1,6 +1,7 @@
|
|||||||
package jsoniter
|
package jsoniter
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"strconv"
|
"strconv"
|
||||||
@ -129,6 +130,9 @@ non_decimal_loop:
|
|||||||
if c == '.' {
|
if c == '.' {
|
||||||
i++
|
i++
|
||||||
decimalPlaces := 0
|
decimalPlaces := 0
|
||||||
|
if i == iter.tail {
|
||||||
|
return iter.readFloat32SlowPath()
|
||||||
|
}
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
@ -197,6 +201,10 @@ func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
|||||||
iter.ReportError("readFloat32SlowPath", "-- is not valid")
|
iter.ReportError("readFloat32SlowPath", "-- is not valid")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if str[len(str)-1] == '.' {
|
||||||
|
iter.ReportError("readFloat32SlowPath", "dot can not be last character")
|
||||||
|
return
|
||||||
|
}
|
||||||
val, err := strconv.ParseFloat(str, 32)
|
val, err := strconv.ParseFloat(str, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
@ -270,6 +278,9 @@ non_decimal_loop:
|
|||||||
if c == '.' {
|
if c == '.' {
|
||||||
i++
|
i++
|
||||||
decimalPlaces := 0
|
decimalPlaces := 0
|
||||||
|
if i == iter.tail {
|
||||||
|
return iter.readFloat64SlowPath()
|
||||||
|
}
|
||||||
for ; i < iter.tail; i++ {
|
for ; i < iter.tail; i++ {
|
||||||
c = iter.buf[i]
|
c = iter.buf[i]
|
||||||
ind := floatDigits[c]
|
ind := floatDigits[c]
|
||||||
@ -309,10 +320,15 @@ func (iter *Iterator) readFloat64SlowPath() (ret float64) {
|
|||||||
iter.ReportError("readFloat64SlowPath", "-- is not valid")
|
iter.ReportError("readFloat64SlowPath", "-- is not valid")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if str[len(str)-1] == '.' {
|
||||||
|
iter.ReportError("readFloat64SlowPath", "dot can not be last character")
|
||||||
|
return
|
||||||
|
}
|
||||||
val, err := strconv.ParseFloat(str, 64)
|
val, err := strconv.ParseFloat(str, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
iter.Error = err
|
iter.Error = err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
fmt.Println(str)
|
||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
|
@ -21,9 +21,18 @@ func (iter *Iterator) trySkipNumber() bool {
|
|||||||
if dotFound {
|
if dotFound {
|
||||||
iter.ReportError("validateNumber", `more than one dot found in number`)
|
iter.ReportError("validateNumber", `more than one dot found in number`)
|
||||||
return true // already failed
|
return true // already failed
|
||||||
} else {
|
|
||||||
dotFound = true
|
|
||||||
}
|
}
|
||||||
|
if i+1 == iter.tail {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
c = iter.buf[i+1]
|
||||||
|
switch c {
|
||||||
|
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
||||||
|
default:
|
||||||
|
iter.ReportError("validateNumber", `missing digit after dot`)
|
||||||
|
return true // already failed
|
||||||
|
}
|
||||||
|
dotFound = true
|
||||||
default:
|
default:
|
||||||
switch c {
|
switch c {
|
||||||
case ',', ']', '}', ' ', '\t', '\n', '\r':
|
case ',', ']', '}', ' ', '\t', '\n', '\r':
|
||||||
|
@ -111,3 +111,21 @@ func Test_empty_as_number(t *testing.T) {
|
|||||||
should.NotEqual(io.EOF, iter.Error)
|
should.NotEqual(io.EOF, iter.Error)
|
||||||
should.NotNil(iter.Error)
|
should.NotNil(iter.Error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_missing_digit_after_dot(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
iter := ParseString(ConfigDefault, `1.,`)
|
||||||
|
iter.Skip()
|
||||||
|
should.NotEqual(io.EOF, iter.Error)
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
v := float64(0)
|
||||||
|
should.NotNil(json.Unmarshal([]byte(`1.`), &v))
|
||||||
|
iter = ParseString(ConfigDefault, `1.`)
|
||||||
|
iter.ReadFloat64()
|
||||||
|
should.NotEqual(io.EOF, iter.Error)
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
iter = ParseString(ConfigDefault, `1.`)
|
||||||
|
iter.ReadFloat32()
|
||||||
|
should.NotEqual(io.EOF, iter.Error)
|
||||||
|
should.NotNil(iter.Error)
|
||||||
|
}
|
||||||
|
@ -13,4 +13,5 @@ var inputs = []string{
|
|||||||
"1E1", // valid, e or E
|
"1E1", // valid, e or E
|
||||||
"1ee1", // invalid
|
"1ee1", // invalid
|
||||||
"100a", // invalid
|
"100a", // invalid
|
||||||
|
"10.", // invalid
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user