2016-11-30 18:56:25 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2016-12-01 04:35:38 +02:00
|
|
|
"io"
|
2016-11-30 18:56:25 +02:00
|
|
|
"fmt"
|
2016-12-01 04:35:38 +02:00
|
|
|
"unicode/utf16"
|
2016-12-01 17:25:29 +02:00
|
|
|
"strconv"
|
2016-12-06 04:21:47 +02:00
|
|
|
"unsafe"
|
2016-12-08 07:29:54 +02:00
|
|
|
"encoding/base64"
|
2016-11-30 18:56:25 +02:00
|
|
|
)
|
|
|
|
|
2016-12-10 08:34:36 +02:00
|
|
|
type ValueType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Invalid ValueType = iota
|
|
|
|
String
|
|
|
|
Number
|
|
|
|
Null
|
|
|
|
Boolean
|
|
|
|
Array
|
|
|
|
Object
|
|
|
|
)
|
|
|
|
|
2016-12-06 04:41:05 +02:00
|
|
|
var digits []byte
|
2016-12-10 08:34:36 +02:00
|
|
|
var valueTypes []ValueType
|
2016-12-06 04:41:05 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
digits = make([]byte, 256)
|
|
|
|
for i := 0; i < len(digits); i++ {
|
|
|
|
digits[i] = 255
|
|
|
|
}
|
|
|
|
for i := '0'; i <= '9'; i++ {
|
|
|
|
digits[i] = byte(i - '0');
|
|
|
|
}
|
|
|
|
for i := 'a'; i <= 'f'; i++ {
|
|
|
|
digits[i] = byte((i - 'a') + 10);
|
|
|
|
}
|
|
|
|
for i := 'A'; i <= 'F'; i++ {
|
|
|
|
digits[i] = byte((i - 'A') + 10);
|
|
|
|
}
|
2016-12-10 08:34:36 +02:00
|
|
|
valueTypes = make([]ValueType, 256)
|
|
|
|
for i := 0; i < len(valueTypes); i++ {
|
|
|
|
valueTypes[i] = Invalid
|
|
|
|
}
|
|
|
|
valueTypes['"'] = String;
|
|
|
|
valueTypes['-'] = Number;
|
|
|
|
valueTypes['0'] = Number;
|
|
|
|
valueTypes['1'] = Number;
|
|
|
|
valueTypes['2'] = Number;
|
|
|
|
valueTypes['3'] = Number;
|
|
|
|
valueTypes['4'] = Number;
|
|
|
|
valueTypes['5'] = Number;
|
|
|
|
valueTypes['6'] = Number;
|
|
|
|
valueTypes['7'] = Number;
|
|
|
|
valueTypes['8'] = Number;
|
|
|
|
valueTypes['9'] = Number;
|
|
|
|
valueTypes['t'] = Boolean;
|
|
|
|
valueTypes['f'] = Boolean;
|
|
|
|
valueTypes['n'] = Null;
|
|
|
|
valueTypes['['] = Array;
|
|
|
|
valueTypes['{'] = Object;
|
2016-12-06 04:41:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 18:56:25 +02:00
|
|
|
type Iterator struct {
|
2016-12-01 04:35:38 +02:00
|
|
|
reader io.Reader
|
|
|
|
buf []byte
|
|
|
|
head int
|
|
|
|
tail int
|
|
|
|
Error error
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func Parse(reader io.Reader, bufSize int) *Iterator {
|
2016-12-01 07:53:36 +02:00
|
|
|
iter := &Iterator{
|
2016-12-01 04:35:38 +02:00
|
|
|
reader: reader,
|
|
|
|
buf: make([]byte, bufSize),
|
|
|
|
head: 0,
|
|
|
|
tail: 0,
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-01 07:53:36 +02:00
|
|
|
iter.skipWhitespaces()
|
|
|
|
return iter
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func ParseBytes(input []byte) *Iterator {
|
2016-12-01 07:53:36 +02:00
|
|
|
iter := &Iterator{
|
2016-12-01 04:35:38 +02:00
|
|
|
reader: nil,
|
|
|
|
buf: input,
|
|
|
|
head: 0,
|
|
|
|
tail: len(input),
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-01 07:53:36 +02:00
|
|
|
iter.skipWhitespaces()
|
|
|
|
return iter
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-10 08:34:36 +02:00
|
|
|
func ParseString(input string) *Iterator {
|
|
|
|
return ParseBytes([]byte(input))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) Reset(reader io.Reader) *Iterator {
|
|
|
|
iter.reader = reader
|
|
|
|
iter.head = 0
|
|
|
|
iter.tail = 0
|
|
|
|
iter.skipWhitespaces()
|
|
|
|
return iter
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ResetBytes(input []byte) *Iterator {
|
2016-12-06 07:48:03 +02:00
|
|
|
// only for benchmarking
|
|
|
|
iter.reader = nil
|
|
|
|
iter.Error = nil
|
|
|
|
iter.buf = input
|
|
|
|
iter.head = 0
|
|
|
|
iter.tail = len(input)
|
|
|
|
iter.skipWhitespaces()
|
|
|
|
return iter
|
|
|
|
}
|
|
|
|
|
2016-12-10 08:34:36 +02:00
|
|
|
func (iter *Iterator) WhatIsNext() ValueType {
|
|
|
|
valueType := valueTypes[iter.readByte()];
|
|
|
|
iter.unreadByte();
|
|
|
|
return valueType;
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 07:53:36 +02:00
|
|
|
func (iter *Iterator) skipWhitespaces() {
|
2016-12-06 07:48:03 +02:00
|
|
|
for {
|
2016-12-06 14:01:22 +02:00
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
2016-12-08 03:25:24 +02:00
|
|
|
case ' ', '\n', '\t', '\r':
|
2016-12-06 14:01:22 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
iter.head = i
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
2016-12-06 07:48:03 +02:00
|
|
|
}
|
2016-12-01 07:53:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-09 07:08:14 +02:00
|
|
|
func (iter *Iterator) skipWhitespacesWithoutLoadMore() bool {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case ' ', '\n', '\t', '\r':
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iter.head = i
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-12-06 14:09:19 +02:00
|
|
|
func (iter *Iterator) nextToken() byte {
|
2016-12-06 17:51:29 +02:00
|
|
|
// a variation of skip whitespaces, returning the next non-whitespace token
|
2016-12-06 14:09:19 +02:00
|
|
|
for {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case ' ', '\n', '\t', 'r':
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
iter.head = i+1
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) ReportError(operation string, msg string) {
|
2016-12-06 05:08:36 +02:00
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-12-02 05:22:20 +02:00
|
|
|
peekStart := iter.head - 10
|
|
|
|
if peekStart < 0 {
|
|
|
|
peekStart = 0
|
|
|
|
}
|
|
|
|
iter.Error = fmt.Errorf("%s: %s, parsing %v ...%s... at %s", operation, msg, iter.head,
|
|
|
|
string(iter.buf[peekStart: iter.head]), string(iter.buf[0:iter.tail]))
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-05 04:43:42 +02:00
|
|
|
func (iter *Iterator) CurrentBuffer() string {
|
|
|
|
peekStart := iter.head - 10
|
|
|
|
if peekStart < 0 {
|
|
|
|
peekStart = 0
|
|
|
|
}
|
2016-12-09 07:08:14 +02:00
|
|
|
return fmt.Sprintf("parsing %v ...|%s|... at %s", iter.head,
|
2016-12-05 04:43:42 +02:00
|
|
|
string(iter.buf[peekStart: iter.head]), string(iter.buf[0:iter.tail]))
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) readByte() (ret byte) {
|
|
|
|
if iter.head == iter.tail {
|
2016-12-06 14:01:22 +02:00
|
|
|
if iter.loadMore() {
|
|
|
|
ret = iter.buf[iter.head]
|
|
|
|
iter.head++
|
|
|
|
return ret
|
2016-12-08 04:02:18 +02:00
|
|
|
} else {
|
|
|
|
return 0
|
2016-12-01 04:35:38 +02:00
|
|
|
}
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-01 04:35:38 +02:00
|
|
|
ret = iter.buf[iter.head]
|
2016-12-05 08:18:16 +02:00
|
|
|
iter.head++
|
2016-12-01 04:35:38 +02:00
|
|
|
return ret
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 14:01:22 +02:00
|
|
|
func (iter *Iterator) loadMore() bool {
|
|
|
|
if iter.reader == nil {
|
|
|
|
iter.Error = io.EOF
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
n, err := iter.reader.Read(iter.buf)
|
|
|
|
if n == 0 {
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
// n == 0, err == nil is not EOF
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iter.head = 0
|
|
|
|
iter.tail = n
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) unreadByte() {
|
|
|
|
if iter.head == 0 {
|
|
|
|
iter.ReportError("unreadByte", "unread too many bytes")
|
2016-11-30 18:56:25 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-01 04:35:38 +02:00
|
|
|
iter.head -= 1
|
|
|
|
return
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
const maxUint64 = (1 << 64 - 1)
|
|
|
|
const cutoffUint64 = maxUint64 / 10 + 1
|
|
|
|
const maxUint32 = (1 << 32 - 1)
|
|
|
|
const cutoffUint32 = maxUint32 / 10 + 1
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-04 15:32:43 +02:00
|
|
|
func (iter *Iterator) ReadUint() (ret uint) {
|
|
|
|
val := iter.ReadUint64()
|
|
|
|
converted := uint(val)
|
|
|
|
if uint64(converted) != val {
|
|
|
|
iter.ReportError("ReadUint", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadUint8() (ret uint8) {
|
|
|
|
val := iter.ReadUint64()
|
|
|
|
converted := uint8(val)
|
|
|
|
if uint64(converted) != val {
|
|
|
|
iter.ReportError("ReadUint8", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadUint16() (ret uint16) {
|
|
|
|
val := iter.ReadUint64()
|
|
|
|
converted := uint16(val)
|
|
|
|
if uint64(converted) != val {
|
|
|
|
iter.ReportError("ReadUint16", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadUint32() (ret uint32) {
|
|
|
|
val := iter.ReadUint64()
|
|
|
|
converted := uint32(val)
|
|
|
|
if uint64(converted) != val {
|
|
|
|
iter.ReportError("ReadUint32", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) ReadUint64() (ret uint64) {
|
|
|
|
c := iter.readByte()
|
2016-12-06 04:41:05 +02:00
|
|
|
v := digits[c]
|
|
|
|
if v == 0 {
|
|
|
|
return 0 // single zero
|
|
|
|
}
|
|
|
|
if v == 255 {
|
|
|
|
iter.ReportError("ReadUint64", "unexpected character")
|
2016-11-30 18:56:25 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-06 04:41:05 +02:00
|
|
|
for {
|
|
|
|
if ret >= cutoffUint64 {
|
|
|
|
iter.ReportError("ReadUint64", "overflow")
|
|
|
|
return
|
2016-12-01 04:35:38 +02:00
|
|
|
}
|
2016-12-06 04:41:05 +02:00
|
|
|
ret = ret * 10 + uint64(v)
|
|
|
|
c = iter.readByte()
|
|
|
|
v = digits[c]
|
|
|
|
if v == 255 {
|
2016-12-01 04:35:38 +02:00
|
|
|
iter.unreadByte()
|
2016-12-06 04:41:05 +02:00
|
|
|
break
|
2016-12-01 04:35:38 +02:00
|
|
|
}
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-01 04:35:38 +02:00
|
|
|
return ret
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 15:19:54 +02:00
|
|
|
func (iter *Iterator) ReadInt() (ret int) {
|
|
|
|
val := iter.ReadInt64()
|
|
|
|
converted := int(val)
|
|
|
|
if int64(converted) != val {
|
|
|
|
iter.ReportError("ReadInt", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2016-12-04 15:32:43 +02:00
|
|
|
func (iter *Iterator) ReadInt8() (ret int8) {
|
|
|
|
val := iter.ReadInt64()
|
|
|
|
converted := int8(val)
|
|
|
|
if int64(converted) != val {
|
|
|
|
iter.ReportError("ReadInt8", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadInt16() (ret int16) {
|
|
|
|
val := iter.ReadInt64()
|
|
|
|
converted := int16(val)
|
|
|
|
if int64(converted) != val {
|
|
|
|
iter.ReportError("ReadInt16", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadInt32() (ret int32) {
|
|
|
|
val := iter.ReadInt64()
|
|
|
|
converted := int32(val)
|
|
|
|
if int64(converted) != val {
|
|
|
|
iter.ReportError("ReadInt32", "int overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return converted
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) ReadInt64() (ret int64) {
|
|
|
|
c := iter.readByte()
|
|
|
|
if iter.Error != nil {
|
2016-11-30 18:56:25 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
/* optional leading minus */
|
|
|
|
if c == '-' {
|
|
|
|
n := iter.ReadUint64()
|
|
|
|
return -int64(n)
|
|
|
|
} else {
|
|
|
|
iter.unreadByte()
|
|
|
|
n := iter.ReadUint64()
|
|
|
|
return int64(n)
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) ReadString() (ret string) {
|
2016-12-10 17:59:40 +02:00
|
|
|
return string(iter.readStringAsBytes())
|
2016-12-06 08:23:59 +02:00
|
|
|
}
|
|
|
|
|
2016-12-06 17:31:11 +02:00
|
|
|
|
2016-12-10 17:59:40 +02:00
|
|
|
func (iter *Iterator) readStringAsBytes() (ret []byte) {
|
2016-12-01 04:35:38 +02:00
|
|
|
c := iter.readByte()
|
2016-12-06 07:48:03 +02:00
|
|
|
if c == 'n' {
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-11-30 18:56:25 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-06 07:48:03 +02:00
|
|
|
if c != '"' {
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReportError("ReadString", `expects " or n`)
|
2016-11-30 18:56:25 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-07 08:21:25 +02:00
|
|
|
end := iter.findStringEndWithoutEscape()
|
|
|
|
if end != -1 {
|
2016-12-06 17:31:11 +02:00
|
|
|
// fast path: reuse the underlying buffer
|
|
|
|
ret = iter.buf[iter.head:end-1]
|
|
|
|
iter.head = end
|
2016-12-06 08:23:59 +02:00
|
|
|
return ret
|
|
|
|
}
|
|
|
|
str := make([]byte, 0, 8)
|
2016-12-06 07:48:03 +02:00
|
|
|
for iter.Error == nil {
|
2016-12-01 04:35:38 +02:00
|
|
|
c = iter.readByte()
|
2016-12-06 07:48:03 +02:00
|
|
|
if c == '"' {
|
2016-12-06 08:23:59 +02:00
|
|
|
return str
|
2016-12-01 04:35:38 +02:00
|
|
|
}
|
2016-12-06 07:48:03 +02:00
|
|
|
if c == '\\' {
|
2016-12-01 04:35:38 +02:00
|
|
|
c = iter.readByte()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 'u':
|
|
|
|
r := iter.readU4()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if utf16.IsSurrogate(r) {
|
|
|
|
c = iter.readByte()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c != '\\' {
|
|
|
|
iter.ReportError("ReadString",
|
|
|
|
`expects \u after utf16 surrogate, but \ not found`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c = iter.readByte()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if c != 'u' {
|
|
|
|
iter.ReportError("ReadString",
|
|
|
|
`expects \u after utf16 surrogate, but \u not found`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
r2 := iter.readU4()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
combined := utf16.DecodeRune(r, r2)
|
|
|
|
str = appendRune(str, combined)
|
|
|
|
} else {
|
|
|
|
str = appendRune(str, r)
|
|
|
|
}
|
|
|
|
case '"':
|
|
|
|
str = append(str, '"')
|
|
|
|
case '\\':
|
|
|
|
str = append(str, '\\')
|
|
|
|
case '/':
|
|
|
|
str = append(str, '/')
|
|
|
|
case 'b':
|
|
|
|
str = append(str, '\b')
|
|
|
|
case 'f':
|
|
|
|
str = append(str, '\f')
|
|
|
|
case 'n':
|
|
|
|
str = append(str, '\n')
|
|
|
|
case 'r':
|
|
|
|
str = append(str, '\r')
|
|
|
|
case 't':
|
|
|
|
str = append(str, '\t')
|
|
|
|
default:
|
|
|
|
iter.ReportError("ReadString",
|
|
|
|
`invalid escape char after \`)
|
|
|
|
return
|
|
|
|
}
|
2016-12-06 07:48:03 +02:00
|
|
|
} else {
|
2016-12-01 04:35:38 +02:00
|
|
|
str = append(str, c)
|
|
|
|
}
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-06 07:48:03 +02:00
|
|
|
return
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func (iter *Iterator) readU4() (ret rune) {
|
|
|
|
for i := 0; i < 4; i++ {
|
|
|
|
c := iter.readByte()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (c >= '0' && c <= '9') {
|
|
|
|
if ret >= cutoffUint32 {
|
|
|
|
iter.ReportError("readU4", "overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret = ret * 16 + rune(c - '0')
|
|
|
|
} else if ((c >= 'a' && c <= 'f') ) {
|
|
|
|
if ret >= cutoffUint32 {
|
|
|
|
iter.ReportError("readU4", "overflow")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ret = ret * 16 + rune(c - 'a' + 10)
|
|
|
|
} else {
|
|
|
|
iter.ReportError("readU4", "expects 0~9 or a~f")
|
|
|
|
return
|
|
|
|
}
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
2016-12-01 04:35:38 +02:00
|
|
|
return ret
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
const (
|
|
|
|
t1 = 0x00 // 0000 0000
|
|
|
|
tx = 0x80 // 1000 0000
|
|
|
|
t2 = 0xC0 // 1100 0000
|
|
|
|
t3 = 0xE0 // 1110 0000
|
|
|
|
t4 = 0xF0 // 1111 0000
|
|
|
|
t5 = 0xF8 // 1111 1000
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
maskx = 0x3F // 0011 1111
|
|
|
|
mask2 = 0x1F // 0001 1111
|
|
|
|
mask3 = 0x0F // 0000 1111
|
|
|
|
mask4 = 0x07 // 0000 0111
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
rune1Max = 1 << 7 - 1
|
|
|
|
rune2Max = 1 << 11 - 1
|
|
|
|
rune3Max = 1 << 16 - 1
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
surrogateMin = 0xD800
|
|
|
|
surrogateMax = 0xDFFF
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
MaxRune = '\U0010FFFF' // Maximum valid Unicode code point.
|
|
|
|
RuneError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
|
|
|
|
)
|
2016-11-30 18:56:25 +02:00
|
|
|
|
2016-12-01 04:35:38 +02:00
|
|
|
func appendRune(p []byte, r rune) []byte {
|
|
|
|
// Negative values are erroneous. Making it unsigned addresses the problem.
|
|
|
|
switch i := uint32(r); {
|
|
|
|
case i <= rune1Max:
|
|
|
|
p = append(p, byte(r))
|
|
|
|
return p
|
|
|
|
case i <= rune2Max:
|
|
|
|
p = append(p, t2 | byte(r >> 6))
|
|
|
|
p = append(p, tx | byte(r) & maskx)
|
|
|
|
return p
|
|
|
|
case i > MaxRune, surrogateMin <= i && i <= surrogateMax:
|
|
|
|
r = RuneError
|
|
|
|
fallthrough
|
|
|
|
case i <= rune3Max:
|
|
|
|
p = append(p, t3 | byte(r >> 12))
|
|
|
|
p = append(p, tx | byte(r >> 6) & maskx)
|
|
|
|
p = append(p, tx | byte(r) & maskx)
|
|
|
|
return p
|
|
|
|
default:
|
|
|
|
p = append(p, t4 | byte(r >> 18))
|
|
|
|
p = append(p, tx | byte(r >> 12) & maskx)
|
|
|
|
p = append(p, tx | byte(r >> 6) & maskx)
|
|
|
|
p = append(p, tx | byte(r) & maskx)
|
|
|
|
return p
|
|
|
|
}
|
2016-11-30 18:56:25 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 10:11:07 +02:00
|
|
|
func (iter *Iterator) ReadArray() (ret bool) {
|
2016-12-06 14:09:19 +02:00
|
|
|
c := iter.nextToken()
|
2016-12-01 07:53:36 +02:00
|
|
|
if iter.Error != nil {
|
2016-12-01 10:11:07 +02:00
|
|
|
return
|
2016-12-01 07:53:36 +02:00
|
|
|
}
|
2016-12-01 10:11:07 +02:00
|
|
|
switch c {
|
2016-12-02 05:22:20 +02:00
|
|
|
case 'n': {
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-12-02 05:22:20 +02:00
|
|
|
return false // null
|
|
|
|
}
|
2016-12-01 10:11:07 +02:00
|
|
|
case '[': {
|
2016-12-06 14:09:19 +02:00
|
|
|
c = iter.nextToken()
|
2016-12-01 07:53:36 +02:00
|
|
|
if iter.Error != nil {
|
2016-12-01 10:11:07 +02:00
|
|
|
return
|
2016-12-01 07:53:36 +02:00
|
|
|
}
|
|
|
|
if c == ']' {
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
iter.unreadByte()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 10:11:07 +02:00
|
|
|
case ']': return false
|
2016-12-01 18:13:13 +02:00
|
|
|
case ',':
|
|
|
|
iter.skipWhitespaces()
|
|
|
|
return true
|
2016-12-01 10:11:07 +02:00
|
|
|
default:
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReportError("ReadArray", "expect [ or , or ] or n")
|
2016-12-01 10:11:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) ReadObject() (ret string) {
|
2016-12-06 14:09:19 +02:00
|
|
|
c := iter.nextToken()
|
2016-12-01 10:11:07 +02:00
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
2016-12-01 07:53:36 +02:00
|
|
|
}
|
2016-12-01 10:11:07 +02:00
|
|
|
switch c {
|
2016-12-02 05:22:20 +02:00
|
|
|
case 'n': {
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-12-02 05:22:20 +02:00
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return "" // null
|
|
|
|
}
|
2016-12-01 10:11:07 +02:00
|
|
|
case '{': {
|
2016-12-06 14:09:19 +02:00
|
|
|
c = iter.nextToken()
|
2016-12-01 10:11:07 +02:00
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case '}':
|
|
|
|
return "" // end of object
|
|
|
|
case '"':
|
|
|
|
iter.unreadByte()
|
2016-12-06 04:53:50 +02:00
|
|
|
return iter.readObjectField()
|
2016-12-01 10:11:07 +02:00
|
|
|
default:
|
|
|
|
iter.ReportError("ReadObject", `expect " after {`)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ',':
|
|
|
|
iter.skipWhitespaces()
|
2016-12-06 04:53:50 +02:00
|
|
|
return iter.readObjectField()
|
2016-12-01 10:11:07 +02:00
|
|
|
case '}':
|
|
|
|
return "" // end of object
|
|
|
|
default:
|
2016-12-02 05:22:20 +02:00
|
|
|
iter.ReportError("ReadObject", `expect { or , or } or n`)
|
2016-12-01 10:11:07 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) readObjectField() (ret string) {
|
2016-12-10 17:59:40 +02:00
|
|
|
str := iter.readStringAsBytes()
|
2016-12-09 07:08:14 +02:00
|
|
|
if iter.skipWhitespacesWithoutLoadMore() {
|
|
|
|
if ret == "" {
|
|
|
|
ret = string(str);
|
|
|
|
}
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if iter.buf[iter.head] != ':' {
|
2016-12-01 10:11:07 +02:00
|
|
|
iter.ReportError("ReadObject", "expect : after object field")
|
|
|
|
return
|
|
|
|
}
|
2016-12-09 07:08:14 +02:00
|
|
|
iter.head++
|
|
|
|
if iter.skipWhitespacesWithoutLoadMore() {
|
|
|
|
if ret == "" {
|
|
|
|
ret = string(str);
|
2016-12-08 13:22:24 +02:00
|
|
|
}
|
2016-12-09 07:08:14 +02:00
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
2016-12-08 13:22:24 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-09 07:08:14 +02:00
|
|
|
if ret == "" {
|
|
|
|
return *(*string)(unsafe.Pointer(&str))
|
|
|
|
}
|
|
|
|
return ret
|
2016-12-01 17:25:29 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 16:15:12 +02:00
|
|
|
func (iter *Iterator) ReadFloat32() (ret float32) {
|
2016-12-08 04:02:18 +02:00
|
|
|
strBuf := [8]byte{}
|
|
|
|
str := strBuf[0:0]
|
|
|
|
hasMore := true
|
|
|
|
for(hasMore) {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
str = append(str, c)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
hasMore = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasMore {
|
|
|
|
if !iter.loadMore() {
|
|
|
|
break
|
|
|
|
}
|
2016-12-04 16:15:12 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 04:21:47 +02:00
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
2016-12-04 16:15:12 +02:00
|
|
|
}
|
2016-12-06 04:21:47 +02:00
|
|
|
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 32)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return float32(val)
|
2016-12-04 16:15:12 +02:00
|
|
|
}
|
|
|
|
|
2016-12-10 17:59:40 +02:00
|
|
|
func (iter *Iterator) ReadNumber() (ret string) {
|
|
|
|
strBuf := [8]byte{}
|
|
|
|
str := strBuf[0:0]
|
|
|
|
hasMore := true
|
|
|
|
for(hasMore) {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
str = append(str, c)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
hasMore = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasMore {
|
|
|
|
if !iter.loadMore() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return string(str)
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:25:29 +02:00
|
|
|
func (iter *Iterator) ReadFloat64() (ret float64) {
|
2016-12-08 04:02:18 +02:00
|
|
|
strBuf := [8]byte{}
|
|
|
|
str := strBuf[0:0]
|
|
|
|
hasMore := true
|
|
|
|
for(hasMore) {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
|
|
|
|
str = append(str, c)
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
hasMore = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if hasMore {
|
|
|
|
if !iter.loadMore() {
|
|
|
|
break
|
|
|
|
}
|
2016-12-01 17:25:29 +02:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 04:21:47 +02:00
|
|
|
if iter.Error != nil && iter.Error != io.EOF {
|
|
|
|
return
|
2016-12-01 17:25:29 +02:00
|
|
|
}
|
2016-12-06 04:21:47 +02:00
|
|
|
val, err := strconv.ParseFloat(*(*string)(unsafe.Pointer(&str)), 64)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return val
|
2016-12-01 17:25:29 +02:00
|
|
|
}
|
2016-12-01 18:13:13 +02:00
|
|
|
|
2016-12-02 05:22:20 +02:00
|
|
|
func (iter *Iterator) ReadBool() (ret bool) {
|
|
|
|
c := iter.readByte()
|
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch c {
|
|
|
|
case 't':
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-12-02 05:22:20 +02:00
|
|
|
return true
|
|
|
|
case 'f':
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-12-02 05:22:20 +02:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
iter.ReportError("ReadBool", "expect t or f")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-08 07:29:54 +02:00
|
|
|
func (iter *Iterator) ReadBase64() (ret []byte) {
|
2016-12-10 17:59:40 +02:00
|
|
|
src := iter.readStringAsBytes()
|
2016-12-08 07:29:54 +02:00
|
|
|
if iter.Error != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b64 := base64.StdEncoding
|
|
|
|
ret = make([]byte, b64.DecodedLen(len(src)))
|
|
|
|
n, err := b64.Decode(ret, src)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return ret[:n]
|
|
|
|
}
|
|
|
|
|
2016-12-02 05:22:20 +02:00
|
|
|
func (iter *Iterator) ReadNull() (ret bool) {
|
|
|
|
c := iter.readByte()
|
|
|
|
if c == 'n' {
|
2016-12-06 17:31:11 +02:00
|
|
|
iter.skipUntilBreak()
|
2016-12-02 05:22:20 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
iter.unreadByte()
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:13:13 +02:00
|
|
|
func (iter *Iterator) Skip() {
|
|
|
|
c := iter.readByte()
|
|
|
|
switch c {
|
|
|
|
case '"':
|
|
|
|
iter.skipString()
|
2016-12-06 17:31:11 +02:00
|
|
|
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 't', 'f', 'n':
|
|
|
|
iter.skipUntilBreak()
|
2016-12-01 18:13:13 +02:00
|
|
|
case '[':
|
|
|
|
iter.skipArray()
|
|
|
|
case '{':
|
|
|
|
iter.skipObject()
|
|
|
|
default:
|
|
|
|
iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) skipString() {
|
2016-12-06 15:28:39 +02:00
|
|
|
for {
|
2016-12-06 17:31:11 +02:00
|
|
|
end, escaped := iter.findStringEnd()
|
|
|
|
if end == -1 {
|
|
|
|
if !iter.loadMore() {
|
|
|
|
return
|
2016-12-06 15:28:39 +02:00
|
|
|
}
|
2016-12-06 17:31:11 +02:00
|
|
|
if escaped {
|
|
|
|
iter.head = 1 // skip the first char as last char read is \
|
2016-12-06 15:28:39 +02:00
|
|
|
}
|
2016-12-06 17:31:11 +02:00
|
|
|
} else {
|
|
|
|
iter.head = end
|
2016-12-01 18:13:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 08:21:25 +02:00
|
|
|
|
|
|
|
// adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
|
|
|
|
// Tries to find the end of string
|
|
|
|
// Support if string contains escaped quote symbols.
|
|
|
|
func (iter *Iterator) findStringEnd() (int, bool) {
|
|
|
|
escaped := false
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
if c == '"' {
|
|
|
|
if !escaped {
|
|
|
|
return i + 1, false
|
|
|
|
} else {
|
|
|
|
j := i - 1
|
|
|
|
for {
|
|
|
|
if j < iter.head || iter.buf[j] != '\\' {
|
|
|
|
// even number of backslashes
|
|
|
|
// either end of buffer, or " found
|
|
|
|
return i + 1, true
|
|
|
|
}
|
|
|
|
j--
|
|
|
|
if j < iter.head || iter.buf[j] != '\\' {
|
|
|
|
// odd number of backslashes
|
|
|
|
// it is \" or \\\"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
j--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if c == '\\' {
|
|
|
|
escaped = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
j := iter.tail - 1
|
|
|
|
for {
|
|
|
|
if j < iter.head || iter.buf[j] != '\\' {
|
|
|
|
// even number of backslashes
|
|
|
|
// either end of buffer, or " found
|
|
|
|
return -1, false // do not end with \
|
|
|
|
}
|
|
|
|
j--
|
|
|
|
if j < iter.head || iter.buf[j] != '\\' {
|
|
|
|
// odd number of backslashes
|
|
|
|
// it is \" or \\\"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
j--
|
|
|
|
|
|
|
|
}
|
|
|
|
return -1, true // end with \
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (iter *Iterator) findStringEndWithoutEscape() int {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
if c == '"' {
|
|
|
|
return i + 1
|
|
|
|
} else if c == '\\' {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
2016-12-01 18:13:13 +02:00
|
|
|
func (iter *Iterator) skipArray() {
|
2016-12-06 17:51:29 +02:00
|
|
|
level := 1
|
2016-12-01 18:13:13 +02:00
|
|
|
for {
|
2016-12-06 17:51:29 +02:00
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
switch iter.buf[i] {
|
|
|
|
case '"': // If inside string, skip it
|
|
|
|
iter.head = i + 1
|
|
|
|
iter.skipString()
|
|
|
|
i = iter.head - 1 // it will be i++ soon
|
|
|
|
case '[': // If open symbol, increase level
|
|
|
|
level++
|
|
|
|
case ']': // If close symbol, increase level
|
|
|
|
level--
|
|
|
|
|
|
|
|
// If we have returned to the original level, we're done
|
|
|
|
if level == 0 {
|
|
|
|
iter.head = i + 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 18:13:13 +02:00
|
|
|
}
|
2016-12-06 17:51:29 +02:00
|
|
|
if (!iter.loadMore()) {
|
2016-12-01 18:13:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) skipObject() {
|
2016-12-06 17:51:29 +02:00
|
|
|
level := 1
|
2016-12-01 18:13:13 +02:00
|
|
|
for {
|
2016-12-06 17:51:29 +02:00
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
switch iter.buf[i] {
|
|
|
|
case '"': // If inside string, skip it
|
|
|
|
iter.head = i + 1
|
|
|
|
iter.skipString()
|
|
|
|
i = iter.head - 1 // it will be i++ soon
|
|
|
|
case '{': // If open symbol, increase level
|
|
|
|
level++
|
|
|
|
case '}': // If close symbol, increase level
|
|
|
|
level--
|
|
|
|
|
|
|
|
// If we have returned to the original level, we're done
|
|
|
|
if level == 0 {
|
|
|
|
iter.head = i + 1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-12-01 18:13:13 +02:00
|
|
|
}
|
2016-12-06 17:51:29 +02:00
|
|
|
if (!iter.loadMore()) {
|
2016-12-01 18:13:13 +02:00
|
|
|
return
|
|
|
|
}
|
2016-12-06 17:51:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (iter *Iterator) skipUntilBreak() {
|
|
|
|
// true, false, null, number
|
|
|
|
for {
|
|
|
|
for i := iter.head; i < iter.tail; i++ {
|
|
|
|
c := iter.buf[i]
|
|
|
|
switch c {
|
|
|
|
case ' ', '\n', '\r', '\t', ',', '}', ']':
|
|
|
|
iter.head = i
|
|
|
|
return
|
|
|
|
}
|
2016-12-06 15:28:39 +02:00
|
|
|
}
|
2016-12-06 17:51:29 +02:00
|
|
|
if (!iter.loadMore()) {
|
2016-12-01 18:13:13 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|