1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_iter_float.go

319 lines
6.9 KiB
Go
Raw Normal View History

2017-01-15 11:30:02 +02:00
package jsoniter
import (
"io"
2017-06-06 17:27:00 +02:00
"math/big"
2017-01-15 11:30:02 +02:00
"strconv"
"unsafe"
)
2017-01-15 16:50:31 +02:00
var floatDigits []int8
2017-06-06 17:27:00 +02:00
2017-01-15 11:30:02 +02:00
const invalidCharForNumber = int8(-1)
const endOfNumber = int8(-2)
const dotInNumber = int8(-3)
func init() {
2017-01-15 16:50:31 +02:00
floatDigits = make([]int8, 256)
for i := 0; i < len(floatDigits); i++ {
floatDigits[i] = invalidCharForNumber
2017-01-15 11:30:02 +02:00
}
2017-01-15 16:50:31 +02:00
for i := int8('0'); i <= int8('9'); i++ {
floatDigits[i] = i - int8('0')
2017-01-15 11:30:02 +02:00
}
floatDigits[','] = endOfNumber
floatDigits[']'] = endOfNumber
floatDigits['}'] = endOfNumber
floatDigits[' '] = endOfNumber
floatDigits['\t'] = endOfNumber
floatDigits['\n'] = endOfNumber
floatDigits['.'] = dotInNumber
2017-01-15 11:30:02 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadBigFloat read big.Float
2017-05-31 06:40:50 +02:00
func (iter *Iterator) ReadBigFloat() (ret *big.Float) {
str := iter.readNumberAsString()
if iter.Error != nil && iter.Error != io.EOF {
return nil
}
prec := 64
if len(str) > prec {
prec = len(str)
}
val, _, err := big.ParseFloat(str, 10, uint(prec), big.ToZero)
if err != nil {
iter.Error = err
return nil
}
return val
}
2017-07-09 10:09:23 +02:00
// ReadBigInt read big.Int
2017-05-31 06:40:50 +02:00
func (iter *Iterator) ReadBigInt() (ret *big.Int) {
str := iter.readNumberAsString()
if iter.Error != nil && iter.Error != io.EOF {
return nil
}
ret = big.NewInt(0)
var success bool
ret, success = ret.SetString(str, 10)
if !success {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadBigInt", "invalid big int")
2017-05-31 06:40:50 +02:00
return nil
}
return ret
}
2017-07-09 10:09:23 +02:00
//ReadFloat32 read float32
2017-01-15 11:30:02 +02:00
func (iter *Iterator) ReadFloat32() (ret float32) {
c := iter.nextToken()
if c == '-' {
return -iter.readPositiveFloat32()
}
2017-07-09 10:09:23 +02:00
iter.unreadByte()
return iter.readPositiveFloat32()
2017-01-15 11:30:02 +02:00
}
func (iter *Iterator) readPositiveFloat32() (ret float32) {
value := uint64(0)
c := byte(' ')
i := iter.head
2017-07-18 05:23:29 +02:00
// first char
if i == iter.tail {
return iter.readFloat32SlowPath()
}
c = iter.buf[i]
i++
ind := floatDigits[c]
switch ind {
case invalidCharForNumber:
return iter.readFloat32SlowPath()
case endOfNumber:
iter.ReportError("readFloat32", "empty number")
return
case dotInNumber:
iter.ReportError("readFloat32", "leading dot is invalid")
return
case 0:
if i == iter.tail {
return iter.readFloat32SlowPath()
}
c = iter.buf[i]
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
iter.ReportError("readFloat32", "leading zero is invalid")
return
}
}
value = uint64(ind)
// chars before dot
2017-06-06 17:27:00 +02:00
non_decimal_loop:
2017-01-15 11:30:02 +02:00
for ; i < iter.tail; i++ {
c = iter.buf[i]
2017-01-15 16:50:31 +02:00
ind := floatDigits[c]
2017-01-15 11:30:02 +02:00
switch ind {
case invalidCharForNumber:
return iter.readFloat32SlowPath()
case endOfNumber:
iter.head = i
return float32(value)
case dotInNumber:
break non_decimal_loop
}
2017-01-15 16:50:31 +02:00
if value > uint64SafeToMultiple10 {
2017-01-15 11:30:02 +02:00
return iter.readFloat32SlowPath()
}
2017-06-06 17:27:00 +02:00
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
2017-01-15 11:30:02 +02:00
}
2017-07-18 05:23:29 +02:00
// chars after dot
2017-01-15 11:30:02 +02:00
if c == '.' {
i++
2017-06-06 17:27:00 +02:00
decimalPlaces := 0
2017-01-15 11:30:02 +02:00
for ; i < iter.tail; i++ {
c = iter.buf[i]
2017-06-06 17:27:00 +02:00
ind := floatDigits[c]
2017-01-15 11:30:02 +02:00
switch ind {
case endOfNumber:
if decimalPlaces > 0 && decimalPlaces < len(pow10) {
2017-01-15 11:30:02 +02:00
iter.head = i
return float32(float64(value) / float64(pow10[decimalPlaces]))
2017-01-15 11:30:02 +02:00
}
// too many decimal places
2017-06-06 17:27:00 +02:00
return iter.readFloat32SlowPath()
2017-01-15 11:30:02 +02:00
case invalidCharForNumber:
fallthrough
case dotInNumber:
return iter.readFloat32SlowPath()
}
decimalPlaces++
2017-01-15 16:50:31 +02:00
if value > uint64SafeToMultiple10 {
2017-01-15 11:30:02 +02:00
return iter.readFloat32SlowPath()
}
value = (value << 3) + (value << 1) + uint64(ind)
}
}
return iter.readFloat32SlowPath()
}
2017-05-31 06:40:50 +02:00
func (iter *Iterator) readNumberAsString() (ret string) {
2017-01-15 11:30:02 +02:00
strBuf := [16]byte{}
str := strBuf[0:0]
2017-06-06 17:27:00 +02:00
load_loop:
2017-01-15 12:08:49 +02:00
for {
2017-01-15 11:30:02 +02:00
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch c {
2017-07-10 08:06:37 +02:00
case '+', '-', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2017-01-15 11:30:02 +02:00
str = append(str, c)
continue
default:
iter.head = i
2017-01-15 12:08:49 +02:00
break load_loop
2017-01-15 11:30:02 +02:00
}
}
2017-01-15 12:08:49 +02:00
if !iter.loadMore() {
break
2017-01-15 11:30:02 +02:00
}
}
if iter.Error != nil && iter.Error != io.EOF {
return
}
2017-06-02 04:50:23 +02:00
if len(str) == 0 {
2017-06-20 09:11:01 +02:00
iter.ReportError("readNumberAsString", "invalid number")
2017-06-02 04:50:23 +02:00
}
2017-05-31 06:40:50 +02:00
return *(*string)(unsafe.Pointer(&str))
}
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
str := iter.readNumberAsString()
if iter.Error != nil && iter.Error != io.EOF {
return
}
2017-07-18 05:05:39 +02:00
if len(str) == 0 {
iter.ReportError("readFloat32SlowPath", "empty number")
return
}
if str[0] == '-' {
iter.ReportError("readFloat32SlowPath", "-- is not valid")
return
}
2017-05-31 06:40:50 +02:00
val, err := strconv.ParseFloat(str, 32)
2017-01-15 11:30:02 +02:00
if err != nil {
iter.Error = err
return
}
return float32(val)
}
2017-07-09 10:09:23 +02:00
// ReadFloat64 read float64
2017-01-15 11:30:02 +02:00
func (iter *Iterator) ReadFloat64() (ret float64) {
2017-01-15 12:08:49 +02:00
c := iter.nextToken()
if c == '-' {
return -iter.readPositiveFloat64()
}
2017-07-09 10:09:23 +02:00
iter.unreadByte()
return iter.readPositiveFloat64()
2017-01-15 12:08:49 +02:00
}
func (iter *Iterator) readPositiveFloat64() (ret float64) {
value := uint64(0)
c := byte(' ')
i := iter.head
2017-07-18 05:23:29 +02:00
// first char
if i == iter.tail {
return iter.readFloat64SlowPath()
}
c = iter.buf[i]
i++
ind := floatDigits[c]
switch ind {
case invalidCharForNumber:
return iter.readFloat64SlowPath()
case endOfNumber:
iter.ReportError("readFloat64", "empty number")
return
case dotInNumber:
iter.ReportError("readFloat64", "leading dot is invalid")
return
case 0:
if i == iter.tail {
return iter.readFloat64SlowPath()
}
c = iter.buf[i]
switch c {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
iter.ReportError("readFloat64", "leading zero is invalid")
return
}
}
value = uint64(ind)
// chars before dot
2017-06-06 17:27:00 +02:00
non_decimal_loop:
2017-01-15 12:08:49 +02:00
for ; i < iter.tail; i++ {
c = iter.buf[i]
2017-01-15 16:50:31 +02:00
ind := floatDigits[c]
2017-01-15 12:08:49 +02:00
switch ind {
case invalidCharForNumber:
return iter.readFloat64SlowPath()
case endOfNumber:
iter.head = i
return float64(value)
case dotInNumber:
break non_decimal_loop
}
2017-01-15 16:50:31 +02:00
if value > uint64SafeToMultiple10 {
2017-01-15 12:08:49 +02:00
return iter.readFloat64SlowPath()
}
2017-06-06 17:27:00 +02:00
value = (value << 3) + (value << 1) + uint64(ind) // value = value * 10 + ind;
2017-01-15 12:08:49 +02:00
}
2017-07-18 05:23:29 +02:00
// chars after dot
2017-01-15 12:08:49 +02:00
if c == '.' {
i++
2017-06-06 17:27:00 +02:00
decimalPlaces := 0
2017-01-15 12:08:49 +02:00
for ; i < iter.tail; i++ {
c = iter.buf[i]
2017-06-06 17:27:00 +02:00
ind := floatDigits[c]
2017-01-15 12:08:49 +02:00
switch ind {
case endOfNumber:
if decimalPlaces > 0 && decimalPlaces < len(pow10) {
2017-01-15 12:08:49 +02:00
iter.head = i
return float64(value) / float64(pow10[decimalPlaces])
2017-01-15 12:08:49 +02:00
}
// too many decimal places
return iter.readFloat64SlowPath()
case invalidCharForNumber:
fallthrough
case dotInNumber:
return iter.readFloat64SlowPath()
}
decimalPlaces++
2017-01-15 16:50:31 +02:00
if value > uint64SafeToMultiple10 {
2017-01-15 12:08:49 +02:00
return iter.readFloat64SlowPath()
}
value = (value << 3) + (value << 1) + uint64(ind)
}
}
return iter.readFloat64SlowPath()
}
func (iter *Iterator) readFloat64SlowPath() (ret float64) {
2017-05-31 06:40:50 +02:00
str := iter.readNumberAsString()
2017-01-15 11:30:02 +02:00
if iter.Error != nil && iter.Error != io.EOF {
return
}
2017-07-18 05:05:39 +02:00
if len(str) == 0 {
iter.ReportError("readFloat64SlowPath", "empty number")
return
}
if str[0] == '-' {
iter.ReportError("readFloat64SlowPath", "-- is not valid")
return
}
2017-05-31 06:40:50 +02:00
val, err := strconv.ParseFloat(str, 64)
2017-01-15 11:30:02 +02:00
if err != nil {
iter.Error = err
return
}
return val
}