1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-04 21:34:16 +02:00

support WhatIsNext

This commit is contained in:
Tao Wen 2016-12-10 14:34:36 +08:00
parent e66687e863
commit f98c2a4150
3 changed files with 54 additions and 6 deletions

View File

@ -9,7 +9,20 @@ import (
"encoding/base64" "encoding/base64"
) )
type ValueType int
const (
Invalid ValueType = iota
String
Number
Null
Boolean
Array
Object
)
var digits []byte var digits []byte
var valueTypes []ValueType
func init() { func init() {
digits = make([]byte, 256) digits = make([]byte, 256)
@ -25,6 +38,27 @@ func init() {
for i := 'A'; i <= 'F'; i++ { for i := 'A'; i <= 'F'; i++ {
digits[i] = byte((i - 'A') + 10); digits[i] = byte((i - 'A') + 10);
} }
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;
} }
type Iterator struct { type Iterator struct {
@ -57,7 +91,19 @@ func ParseBytes(input []byte) *Iterator {
return iter return iter
} }
func (iter *Iterator) Reuse(input []byte) *Iterator { 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 {
// only for benchmarking // only for benchmarking
iter.reader = nil iter.reader = nil
iter.Error = nil iter.Error = nil
@ -68,8 +114,10 @@ func (iter *Iterator) Reuse(input []byte) *Iterator {
return iter return iter
} }
func ParseString(input string) *Iterator { func (iter *Iterator) WhatIsNext() ValueType {
return ParseBytes([]byte(input)) valueType := valueTypes[iter.readByte()];
iter.unreadByte();
return valueType;
} }
func (iter *Iterator) skipWhitespaces() { func (iter *Iterator) skipWhitespaces() {

View File

@ -123,7 +123,7 @@ func Benchmark_jsoniter_array(b *testing.B) {
iter := ParseBytes(input) iter := ParseBytes(input)
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
iter.Reuse(input) iter.ResetBytes(input)
for iter.ReadArray() { for iter.ReadArray() {
iter.ReadUint64() iter.ReadUint64()
} }

View File

@ -95,7 +95,7 @@ func Benchmark_jsoniter_ascii(b *testing.B) {
iter := ParseString(`"hello, world!"`) iter := ParseString(`"hello, world!"`)
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
iter.Reuse(iter.buf) iter.ResetBytes(iter.buf)
iter.ReadString() iter.ReadString()
} }
} }
@ -104,7 +104,7 @@ func Benchmark_jsoniter_string_as_bytes(b *testing.B) {
iter := ParseString(`"hello, world!"`) iter := ParseString(`"hello, world!"`)
b.ResetTimer() b.ResetTimer()
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
iter.Reuse(iter.buf) iter.ResetBytes(iter.buf)
iter.ReadStringAsBytes() iter.ReadStringAsBytes()
} }
} }