1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

speed up skip

This commit is contained in:
Tao Wen 2016-12-06 21:28:39 +08:00
parent e5e9a13f2e
commit 4863661c51
2 changed files with 86 additions and 77 deletions

View File

@ -721,43 +721,36 @@ func (iter *Iterator) ReadBool() (ret bool) {
} }
func (iter *Iterator) skipTrue() { func (iter *Iterator) skipTrue() {
c := iter.readByte() for {
if c != 'r' { for i := iter.head; i < iter.tail; i++ {
iter.ReportError("skipTrue", "expect r of true") c := iter.buf[i]
return switch c {
} case 'r', 'u', 'e':
c = iter.readByte() continue
if c != 'u' { }
iter.ReportError("skipTrue", "expect u of true") iter.head = i
return return
} }
c = iter.readByte() if !iter.loadMore() {
if c != 'e' { return
iter.ReportError("skipTrue", "expect e of true") }
return
} }
} }
func (iter *Iterator) skipFalse() { func (iter *Iterator) skipFalse() {
c := iter.readByte() for {
if c != 'a' { for i := iter.head; i < iter.tail; i++ {
iter.ReportError("skipFalse", "expect a of false") c := iter.buf[i]
return switch c {
} case 'a', 'l', 's', 'e':
c = iter.readByte() continue
if c != 'l' { }
iter.ReportError("skipFalse", "expect l of false") iter.head = i
return return
} }
c = iter.readByte() if !iter.loadMore() {
if c != 's' { return
iter.ReportError("skipFalse", "expect s of false") }
return
}
c = iter.readByte()
if c != 'e' {
iter.ReportError("skipFalse", "expect e of false")
return
} }
} }
@ -772,28 +765,24 @@ func (iter *Iterator) ReadNull() (ret bool) {
} }
func (iter *Iterator) skipNull() { func (iter *Iterator) skipNull() {
c := iter.readByte() for {
if c != 'u' { for i := iter.head; i < iter.tail; i++ {
iter.ReportError("skipNull", "expect u of null") c := iter.buf[i]
return switch c {
} case 'u', 'l':
c = iter.readByte() continue
if c != 'l' { }
iter.ReportError("skipNull", "expect l of null") iter.head = i
return return
} }
c = iter.readByte() if !iter.loadMore() {
if c != 'l' { return
iter.ReportError("skipNull", "expect l of null") }
return
} }
} }
func (iter *Iterator) Skip() { func (iter *Iterator) Skip() {
c := iter.readByte() c := iter.readByte()
if iter.Error != nil {
return
}
switch c { switch c {
case '"': case '"':
iter.skipString() iter.skipString()
@ -816,38 +805,58 @@ func (iter *Iterator) Skip() {
} }
func (iter *Iterator) skipString() { func (iter *Iterator) skipString() {
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() { escaped := false
switch c { for {
case '"': for i := iter.head; i < iter.tail; i++ {
return // end of string found c := iter.buf[i]
case '\\': switch c {
iter.readByte() // " after \\ does not count case '"':
if escaped {
escaped = false
} else {
iter.head = i+1
return
}
case '\\':
escaped = !escaped
default:
escaped= false
}
}
if !iter.loadMore() {
return
} }
} }
} }
func (iter *Iterator) skipNumber() { func (iter *Iterator) skipNumber() {
for c := iter.readByte(); iter.Error == nil; c = iter.readByte() { for {
switch c { for i := iter.head; i < iter.tail; i++ {
case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9': c := iter.buf[i]
continue switch c {
default: case '-', '+', '.', 'e', 'E', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
iter.unreadByte() continue
}
iter.head = i
return
}
if !iter.loadMore() {
return return
} }
} }
} }
func (iter *Iterator) skipArray() { func (iter *Iterator) skipArray() {
c := iter.nextToken()
if c == ']' {
return
} else {
iter.unreadByte()
}
for { for {
c := iter.nextToken()
if iter.Error != nil { if iter.Error != nil {
return return
} }
if c == ']' {
return
}
iter.unreadByte()
iter.Skip() iter.Skip()
c = iter.nextToken() c = iter.nextToken()
switch c { switch c {
@ -865,9 +874,6 @@ func (iter *Iterator) skipArray() {
func (iter *Iterator) skipObject() { func (iter *Iterator) skipObject() {
c := iter.nextToken() c := iter.nextToken()
if iter.Error != nil {
return
}
if c == '}' { if c == '}' {
return // end of object return // end of object
} else { } else {
@ -886,6 +892,9 @@ func (iter *Iterator) skipObject() {
return return
} }
iter.skipWhitespaces() iter.skipWhitespaces()
if iter.Error != nil {
return
}
iter.Skip() iter.Skip()
c = iter.nextToken() c = iter.nextToken()
switch c { switch c {

View File

@ -76,13 +76,13 @@ func Test_skip_empty_object(t *testing.T) {
} }
func Test_skip_nested(t *testing.T) { func Test_skip_nested(t *testing.T) {
iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`) iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`)
iter.ReadArray() iter.ReadArray()
iter.Skip() iter.Skip()
iter.ReadArray() iter.ReadArray()
if iter.ReadString() != "b" { if iter.ReadString() != "b" {
t.FailNow() t.FailNow()
} }
} }
type TestResp struct { type TestResp struct {