-
Notifications
You must be signed in to change notification settings - Fork 2
/
iterator_skip.go
127 lines (123 loc) · 2.52 KB
/
iterator_skip.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package jzon
var (
skipFunctions [charNum]func(it *Iterator, c byte) error
)
func init() {
skipFunctions['"'] = skipString
skipFunctions['n'] = func(it *Iterator, _ byte) error {
return it.expectBytes("ull")
}
skipFunctions['t'] = func(it *Iterator, _ byte) error {
return it.expectBytes("rue")
}
skipFunctions['f'] = func(it *Iterator, _ byte) error {
return it.expectBytes("alse")
}
skipFunctions['['] = skipArrayWithStack
skipFunctions['{'] = skipObjectWithStack
for _, c := range []byte("-0123456789") {
skipFunctions[c] = skipNumber
}
errFunc := func(it *Iterator, c byte) error {
return UnexpectedByteError{got: c}
}
for i := 0; i < charNum; i++ {
if skipFunctions[i] == nil {
skipFunctions[i] = errFunc
}
}
}
func skipWithStack(it *Iterator, top stackElement, s *stack) (err error) {
var c byte
for {
c, err = it.nextToken()
if err != nil {
return err
}
it.head++
if top&1 == 0 {
// stackElementObjectBegin
// stackElementObject
if c == '}' {
if top = s.pop(); top == stackElementNone {
return nil
}
continue
}
if top == stackElementObject {
if c != ',' {
return UnexpectedByteError{got: c, exp: ','}
}
c, err = it.nextToken()
if err != nil {
return err
}
it.head++
}
if c != '"' {
return UnexpectedByteError{got: c, exp: '"'}
}
if err = it.skipObjectField(); err != nil {
return err
}
if c, err = it.nextToken(); err != nil {
return err
}
it.head++
switch c {
case '[':
s.pushObject()
top = stackElementArrayBegin
case '{':
s.pushObject()
top = stackElementObjectBegin
default:
if err = skipFunctions[c](it, c); err != nil {
return err
}
top = stackElementObject
}
} else {
// stackElementArrayBegin
// stackElementArray
if c == ']' {
if top = s.pop(); top == stackElementNone {
return nil
}
continue
}
if top == stackElementArray {
if c != ',' {
return UnexpectedByteError{got: c, exp: ','}
}
c, err = it.nextToken()
if err != nil {
return err
}
it.head++
}
switch c {
case '[':
s.pushArray()
top = stackElementArrayBegin
case '{':
s.pushArray()
top = stackElementObjectBegin
default:
if err = skipFunctions[c](it, c); err != nil {
return err
}
top = stackElementArray
}
}
}
}
// Skip skips a json object
func (it *Iterator) Skip() error {
c, err := it.nextToken()
if err != nil {
return err
}
it.head++
return skipFunctions[c](it, c)
}