1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-09 08:55:35 +02:00
json-iterator/feature_iter_int.go

269 lines
6.5 KiB
Go
Raw Normal View History

2017-01-15 16:50:31 +02:00
package jsoniter
2017-02-07 03:24:36 +02:00
import (
"math"
2017-06-20 18:26:18 +02:00
"strconv"
2017-02-07 03:24:36 +02:00
)
2017-01-16 03:01:44 +02:00
2017-01-15 16:50:31 +02:00
var intDigits []int8
2017-01-15 17:17:17 +02:00
2017-06-06 17:27:00 +02:00
const uint32SafeToMultiply10 = uint32(0xffffffff)/10 - 1
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff)/10 - 1
2017-01-15 16:50:31 +02:00
func init() {
intDigits = make([]int8, 256)
2017-06-06 15:08:04 +02:00
for i := 0; i < len(intDigits); i++ {
2017-01-15 16:50:31 +02:00
intDigits[i] = invalidCharForNumber
}
for i := int8('0'); i <= int8('9'); i++ {
intDigits[i] = i - int8('0')
}
}
2017-07-09 10:09:23 +02:00
// ReadUint read uint
2017-01-15 17:17:17 +02:00
func (iter *Iterator) ReadUint() uint {
return uint(iter.ReadUint64())
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadInt read int
2017-01-15 17:17:17 +02:00
func (iter *Iterator) ReadInt() int {
return int(iter.ReadInt64())
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadInt8 read int8
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadInt8() (ret int8) {
2017-01-15 16:50:31 +02:00
c := iter.nextToken()
2017-01-15 17:17:17 +02:00
if c == '-' {
2017-01-16 03:01:44 +02:00
val := iter.readUint32(iter.readByte())
if val > math.MaxInt8+1 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return -int8(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
val := iter.readUint32(c)
if val > math.MaxInt8 {
iter.ReportError("ReadInt8", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int8(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadUint8 read uint8
2017-01-15 17:17:17 +02:00
func (iter *Iterator) ReadUint8() (ret uint8) {
2017-01-16 03:01:44 +02:00
val := iter.readUint32(iter.nextToken())
if val > math.MaxUint8 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadUint8", "overflow: "+strconv.FormatInt(int64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return uint8(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadInt16 read int16
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadInt16() (ret int16) {
2017-01-15 17:17:17 +02:00
c := iter.nextToken()
if c == '-' {
2017-01-16 03:01:44 +02:00
val := iter.readUint32(iter.readByte())
if val > math.MaxInt16+1 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return -int16(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
val := iter.readUint32(c)
if val > math.MaxInt16 {
iter.ReportError("ReadInt16", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int16(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadUint16 read uint16
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadUint16() (ret uint16) {
val := iter.readUint32(iter.nextToken())
if val > math.MaxUint16 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadUint16", "overflow: "+strconv.FormatInt(int64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return uint16(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadInt32 read int32
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadInt32() (ret int32) {
2017-01-15 16:50:31 +02:00
c := iter.nextToken()
if c == '-' {
2017-01-16 03:01:44 +02:00
val := iter.readUint32(iter.readByte())
if val > math.MaxInt32+1 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return -int32(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
val := iter.readUint32(c)
if val > math.MaxInt32 {
iter.ReportError("ReadInt32", "overflow: "+strconv.FormatInt(int64(val), 10))
return
}
return int32(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
// ReadUint32 read uint32
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadUint32() (ret uint32) {
return iter.readUint32(iter.nextToken())
2017-01-15 16:50:31 +02:00
}
2017-01-16 03:01:44 +02:00
func (iter *Iterator) readUint32(c byte) (ret uint32) {
defer func() {
2017-11-15 17:34:21 +02:00
if iter.head < len(iter.buf) && iter.buf[iter.head] == '.' {
iter.ReportError("readUint32", "can not decode float as int")
}
}()
2017-01-15 16:50:31 +02:00
ind := intDigits[c]
if ind == 0 {
return 0 // single zero
}
if ind == invalidCharForNumber {
2017-06-20 09:11:01 +02:00
iter.ReportError("readUint32", "unexpected character: "+string([]byte{byte(ind)}))
2017-01-15 16:50:31 +02:00
return
}
value := uint32(ind)
2017-06-06 17:27:00 +02:00
if iter.tail-iter.head > 10 {
2017-02-08 07:46:28 +02:00
i := iter.head
ind2 := intDigits[iter.buf[i]]
if ind2 == invalidCharForNumber {
iter.head = i
return value
}
i++
ind3 := intDigits[iter.buf[i]]
if ind3 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*10 + uint32(ind2)
2017-02-08 07:46:28 +02:00
}
//iter.head = i + 1
//value = value * 100 + uint32(ind2) * 10 + uint32(ind3)
i++
ind4 := intDigits[iter.buf[i]]
if ind4 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*100 + uint32(ind2)*10 + uint32(ind3)
2017-02-08 07:46:28 +02:00
}
i++
ind5 := intDigits[iter.buf[i]]
if ind5 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*1000 + uint32(ind2)*100 + uint32(ind3)*10 + uint32(ind4)
2017-02-08 07:46:28 +02:00
}
i++
ind6 := intDigits[iter.buf[i]]
if ind6 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*10000 + uint32(ind2)*1000 + uint32(ind3)*100 + uint32(ind4)*10 + uint32(ind5)
2017-02-08 07:46:28 +02:00
}
i++
ind7 := intDigits[iter.buf[i]]
if ind7 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*100000 + uint32(ind2)*10000 + uint32(ind3)*1000 + uint32(ind4)*100 + uint32(ind5)*10 + uint32(ind6)
2017-02-08 07:46:28 +02:00
}
i++
ind8 := intDigits[iter.buf[i]]
if ind8 == invalidCharForNumber {
iter.head = i
2017-06-06 17:27:00 +02:00
return value*1000000 + uint32(ind2)*100000 + uint32(ind3)*10000 + uint32(ind4)*1000 + uint32(ind5)*100 + uint32(ind6)*10 + uint32(ind7)
2017-02-08 07:46:28 +02:00
}
i++
ind9 := intDigits[iter.buf[i]]
2017-06-06 17:27:00 +02:00
value = value*10000000 + uint32(ind2)*1000000 + uint32(ind3)*100000 + uint32(ind4)*10000 + uint32(ind5)*1000 + uint32(ind6)*100 + uint32(ind7)*10 + uint32(ind8)
2017-02-08 07:46:28 +02:00
iter.head = i
if ind9 == invalidCharForNumber {
return value
}
}
2017-01-15 16:50:31 +02:00
for {
for i := iter.head; i < iter.tail; i++ {
ind = intDigits[iter.buf[i]]
if ind == invalidCharForNumber {
2017-02-07 03:24:36 +02:00
iter.head = i
2017-01-15 16:50:31 +02:00
return value
}
2017-01-16 03:01:44 +02:00
if value > uint32SafeToMultiply10 {
2017-01-15 17:17:17 +02:00
value2 := (value << 3) + (value << 1) + uint32(ind)
2017-01-16 03:01:44 +02:00
if value2 < value {
2017-06-20 09:11:01 +02:00
iter.ReportError("readUint32", "overflow")
2017-01-15 17:17:17 +02:00
return
}
2017-07-09 10:09:23 +02:00
value = value2
continue
2017-01-15 17:17:17 +02:00
}
value = (value << 3) + (value << 1) + uint32(ind)
2017-01-15 16:50:31 +02:00
}
2017-06-06 17:27:00 +02:00
if !iter.loadMore() {
2017-01-15 16:50:31 +02:00
return value
}
}
}
2017-07-09 10:09:23 +02:00
// ReadInt64 read int64
2017-01-16 03:01:44 +02:00
func (iter *Iterator) ReadInt64() (ret int64) {
2017-01-15 16:50:31 +02:00
c := iter.nextToken()
2017-01-15 17:17:17 +02:00
if c == '-' {
2017-01-16 03:01:44 +02:00
val := iter.readUint64(iter.readByte())
if val > math.MaxInt64+1 {
2017-06-20 09:11:01 +02:00
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
2017-01-16 03:01:44 +02:00
return
}
return -int64(val)
2017-01-15 16:50:31 +02:00
}
2017-07-09 10:09:23 +02:00
val := iter.readUint64(c)
if val > math.MaxInt64 {
iter.ReportError("ReadInt64", "overflow: "+strconv.FormatUint(uint64(val), 10))
return
}
return int64(val)
2017-01-15 17:17:17 +02:00
}
2017-01-15 16:50:31 +02:00
2017-07-09 10:09:23 +02:00
// ReadUint64 read uint64
2017-01-15 17:17:17 +02:00
func (iter *Iterator) ReadUint64() uint64 {
2017-01-16 03:01:44 +02:00
return iter.readUint64(iter.nextToken())
2017-01-15 17:17:17 +02:00
}
2017-01-16 03:01:44 +02:00
func (iter *Iterator) readUint64(c byte) (ret uint64) {
defer func() {
2017-11-15 17:34:21 +02:00
if iter.head < len(iter.buf) && iter.buf[iter.head] == '.' {
iter.ReportError("readUint64", "can not decode float as int")
}
}()
2017-01-15 17:17:17 +02:00
ind := intDigits[c]
if ind == 0 {
return 0 // single zero
}
if ind == invalidCharForNumber {
2017-06-20 09:11:01 +02:00
iter.ReportError("readUint64", "unexpected character: "+string([]byte{byte(ind)}))
2017-01-15 17:17:17 +02:00
return
}
value := uint64(ind)
for {
for i := iter.head; i < iter.tail; i++ {
ind = intDigits[iter.buf[i]]
if ind == invalidCharForNumber {
2017-02-07 03:24:36 +02:00
iter.head = i
2017-01-15 17:17:17 +02:00
return value
}
2017-01-16 03:01:44 +02:00
if value > uint64SafeToMultiple10 {
2017-01-15 17:17:17 +02:00
value2 := (value << 3) + (value << 1) + uint64(ind)
2017-01-16 03:01:44 +02:00
if value2 < value {
2017-06-20 09:11:01 +02:00
iter.ReportError("readUint64", "overflow")
2017-01-15 17:17:17 +02:00
return
}
2017-07-09 10:09:23 +02:00
value = value2
continue
2017-01-15 17:17:17 +02:00
}
value = (value << 3) + (value << 1) + uint64(ind)
}
2017-06-06 17:27:00 +02:00
if !iter.loadMore() {
2017-01-15 17:17:17 +02:00
return value
}
2017-01-15 16:50:31 +02:00
}
}