2017-01-15 16:50:31 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-02-07 03:24:36 +02:00
|
|
|
import (
|
|
|
|
"strconv"
|
|
|
|
)
|
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-01-16 03:01:44 +02:00
|
|
|
const uint32SafeToMultiply10 = uint32(0xffffffff) / 10 - 1
|
|
|
|
const uint64SafeToMultiple10 = uint64(0xffffffffffffffff) / 10 - 1
|
|
|
|
const int64Max = uint64(0x7fffffffffffffff)
|
|
|
|
const int32Max = uint32(0x7fffffff)
|
|
|
|
const int16Max = uint32(0x7fff)
|
|
|
|
const uint16Max = uint32(0xffff)
|
|
|
|
const int8Max = uint32(0x7fff)
|
|
|
|
const uint8Max = uint32(0xffff)
|
2017-01-15 16:50:31 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
intDigits = make([]int8, 256)
|
|
|
|
for i := 0; i < len(floatDigits); i++ {
|
|
|
|
intDigits[i] = invalidCharForNumber
|
|
|
|
}
|
|
|
|
for i := int8('0'); i <= int8('9'); i++ {
|
|
|
|
intDigits[i] = i - int8('0')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-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 > int8Max + 1 {
|
|
|
|
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return -int8(val)
|
2017-01-15 17:17:17 +02:00
|
|
|
} else {
|
2017-01-16 03:01:44 +02:00
|
|
|
val := iter.readUint32(c)
|
|
|
|
if val > int8Max {
|
|
|
|
iter.reportError("ReadInt8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return int8(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 > uint8Max {
|
|
|
|
iter.reportError("ReadUint8", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return uint8(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
|
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 > int16Max + 1 {
|
|
|
|
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return -int16(val)
|
2017-01-15 17:17:17 +02:00
|
|
|
} else {
|
2017-01-16 03:01:44 +02:00
|
|
|
val := iter.readUint32(c)
|
|
|
|
if val > int16Max {
|
|
|
|
iter.reportError("ReadInt16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return int16(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 03:01:44 +02:00
|
|
|
func (iter *Iterator) ReadUint16() (ret uint16) {
|
|
|
|
val := iter.readUint32(iter.nextToken())
|
|
|
|
if val > uint16Max {
|
|
|
|
iter.reportError("ReadUint16", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return uint16(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
|
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 > int32Max + 1 {
|
|
|
|
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return -int32(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
} else {
|
2017-01-16 03:01:44 +02:00
|
|
|
val := iter.readUint32(c)
|
|
|
|
if val > int32Max {
|
|
|
|
iter.reportError("ReadInt32", "overflow: " + strconv.FormatInt(int64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return int32(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-01-15 16:50:31 +02:00
|
|
|
ind := intDigits[c]
|
|
|
|
if ind == 0 {
|
|
|
|
return 0 // single zero
|
|
|
|
}
|
|
|
|
if ind == invalidCharForNumber {
|
|
|
|
iter.reportError("readUint32", "unexpected character: " + string([]byte{byte(ind)}))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
value := uint32(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 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-01-15 17:17:17 +02:00
|
|
|
iter.reportError("readUint32", "overflow")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
value = value2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint32(ind)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
if (!iter.loadMore()) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 > int64Max + 1 {
|
|
|
|
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return -int64(val)
|
2017-01-15 17:17:17 +02:00
|
|
|
} else {
|
2017-01-16 03:01:44 +02:00
|
|
|
val := iter.readUint64(c)
|
|
|
|
if val > int64Max {
|
|
|
|
iter.reportError("ReadInt64", "overflow: " + strconv.FormatUint(uint64(val), 10))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return int64(val)
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
2017-01-15 17:17:17 +02:00
|
|
|
}
|
2017-01-15 16:50:31 +02:00
|
|
|
|
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) {
|
2017-01-15 17:17:17 +02:00
|
|
|
ind := intDigits[c]
|
|
|
|
if ind == 0 {
|
|
|
|
return 0 // single zero
|
|
|
|
}
|
|
|
|
if ind == invalidCharForNumber {
|
|
|
|
iter.reportError("readUint64", "unexpected character: " + string([]byte{byte(ind)}))
|
|
|
|
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-01-15 17:17:17 +02:00
|
|
|
iter.reportError("readUint64", "overflow")
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
value = value2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
value = (value << 3) + (value << 1) + uint64(ind)
|
|
|
|
}
|
|
|
|
if (!iter.loadMore()) {
|
|
|
|
return value
|
|
|
|
}
|
2017-01-15 16:50:31 +02:00
|
|
|
}
|
|
|
|
}
|