2017-01-15 11:30:02 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"strconv"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2017-01-15 16:50:31 +02:00
|
|
|
var floatDigits []int8
|
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
|
|
|
}
|
2017-01-15 16:50:31 +02:00
|
|
|
floatDigits[','] = endOfNumber;
|
|
|
|
floatDigits[']'] = endOfNumber;
|
|
|
|
floatDigits['}'] = endOfNumber;
|
|
|
|
floatDigits[' '] = endOfNumber;
|
|
|
|
floatDigits['.'] = dotInNumber;
|
2017-01-15 11:30:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
|
|
|
c := iter.nextToken()
|
|
|
|
if c == '-' {
|
|
|
|
return -iter.readPositiveFloat32()
|
|
|
|
} else {
|
|
|
|
iter.unreadByte()
|
|
|
|
return iter.readPositiveFloat32()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readPositiveFloat32() (ret float32) {
|
|
|
|
value := uint64(0)
|
|
|
|
c := byte(' ')
|
|
|
|
i := iter.head
|
|
|
|
non_decimal_loop:
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
|
|
|
}
|
|
|
|
if c == '.' {
|
|
|
|
i++
|
|
|
|
decimalPlaces := 0;
|
|
|
|
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 endOfNumber:
|
|
|
|
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
|
|
|
iter.head = i
|
|
|
|
return float32(float64(value) / float64(POW10[decimalPlaces]))
|
|
|
|
}
|
|
|
|
// too many decimal places
|
|
|
|
return iter.readFloat32SlowPath()
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readFloat32SlowPath() (ret float32) {
|
|
|
|
strBuf := [16]byte{}
|
|
|
|
str := strBuf[0:0]
|
2017-01-15 12:08:49 +02:00
|
|
|
load_loop:
|
|
|
|
for {
|
2017-01-15 11:30:02 +02:00
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
2017-01-15 12:08:49 +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:
|
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
|
|
|
|
}
|
|
|
|
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 32)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return float32(val)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadFloat64() (ret float64) {
|
2017-01-15 12:08:49 +02:00
|
|
|
c := iter.nextToken()
|
|
|
|
if c == '-' {
|
|
|
|
return -iter.readPositiveFloat64()
|
|
|
|
} else {
|
|
|
|
iter.unreadByte()
|
|
|
|
return iter.readPositiveFloat64()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readPositiveFloat64() (ret float64) {
|
|
|
|
value := uint64(0)
|
|
|
|
c := byte(' ')
|
|
|
|
i := iter.head
|
|
|
|
non_decimal_loop:
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind); // value = value * 10 + ind;
|
|
|
|
}
|
|
|
|
if c == '.' {
|
|
|
|
i++
|
|
|
|
decimalPlaces := 0;
|
|
|
|
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 endOfNumber:
|
|
|
|
if decimalPlaces > 0 && decimalPlaces < len(POW10) {
|
|
|
|
iter.head = i
|
|
|
|
return float64(value) / float64(POW10[decimalPlaces])
|
|
|
|
}
|
|
|
|
// 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) {
|
|
|
|
strBuf := [16]byte{}
|
2017-01-15 11:30:02 +02:00
|
|
|
str := strBuf[0:0]
|
2017-01-15 12:08:49 +02:00
|
|
|
load_loop:
|
|
|
|
for {
|
2017-01-15 11:30:02 +02:00
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
2017-01-15 12:08:49 +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:
|
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
|
|
|
|
}
|
|
|
|
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 64)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return val
|
|
|
|
}
|