-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathiterator_read_array.go
60 lines (59 loc) · 1.34 KB
/
iterator_read_array.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
package jzon
func readArrayWithStack(it *Iterator, _ byte) (interface{}, error) {
c, err := it.nextToken()
if err != nil {
return nil, err
}
it.head++
topObj := make([]interface{}, 0)
if c == ']' {
return topObj, nil
}
for {
// We disabled the following switch to test benchmark
// comparing to using builtin stack
// the result is using our own stack will improve the
// performance by about 25%
switch c {
case '{':
s := stackPool.Get().(*stack).initArray()
ns := nodeStackPool.Get().(*nodeStack).
initArray(topObj).
pushObject("")
ret, err := readWithStack(it, stackElementObjectBegin, s, ns)
releaseNodeStack(ns)
stackPool.Put(s)
return ret, err
case '[':
s := stackPool.Get().(*stack).initArray()
ns := nodeStackPool.Get().(*nodeStack).
initArray(topObj).
pushArray("")
ret, err := readWithStack(it, stackElementArrayBegin, s, ns)
releaseNodeStack(ns)
stackPool.Put(s)
return ret, err
}
o, err := readFunctions[c](it, c)
if err != nil {
return nil, err
}
topObj = append(topObj, o)
c, err = it.nextToken()
if err != nil {
return nil, err
}
it.head++
if c == ']' {
return topObj, nil
}
if c != ',' {
return nil, UnexpectedByteError{got: c, exp: ',', exp2: ']'}
}
c, err = it.nextToken()
if err != nil {
return nil, err
}
it.head++
}
}