-
-
Notifications
You must be signed in to change notification settings - Fork 148
/
decode_stream.go
198 lines (185 loc) · 3.33 KB
/
decode_stream.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package json
import (
"bytes"
"io"
)
const (
readChunkSize = 512
)
type stream struct {
buf []byte
length int64
r io.Reader
offset int64
cursor int64
allRead bool
}
func (s *stream) buffered() io.Reader {
return bytes.NewReader(s.buf[s.cursor:])
}
func (s *stream) totalOffset() int64 {
return s.offset + s.cursor
}
func (s *stream) prevChar() byte {
return s.buf[s.cursor-1]
}
func (s *stream) char() byte {
return s.buf[s.cursor]
}
func (s *stream) end() bool {
return s.allRead && s.length <= s.cursor
}
func (s *stream) progressN(n int64) bool {
if s.cursor+n < s.length-1 || s.read() {
s.cursor += n
return true
}
s.cursor = s.length
return false
}
func (s *stream) reset() {
s.buf = s.buf[s.cursor:]
s.length -= s.cursor
s.cursor = 0
}
func (s *stream) read() bool {
if s.allRead {
return false
}
buf := make([]byte, readChunkSize)
n, err := s.r.Read(buf)
if err != nil && err != io.EOF {
return false
}
if n < readChunkSize || err == io.EOF {
s.allRead = true
}
totalSize := s.length + int64(n) + 1
if totalSize > readChunkSize {
newBuf := make([]byte, totalSize)
copy(newBuf, s.buf)
copy(newBuf[s.length:], buf)
s.buf = newBuf
s.length = totalSize - 1
} else if s.length > 0 {
copy(buf[s.length:], buf)
copy(buf, s.buf[:s.length])
s.buf = buf
s.length = totalSize - 1
} else {
s.buf = buf
s.length = totalSize - 1
}
s.offset += s.cursor
if n == 0 {
return false
}
return true
}
func (s *stream) skipWhiteSpace() {
LOOP:
c := s.char()
if isWhiteSpace[c] {
s.cursor++
goto LOOP
} else if c == nul {
if s.read() {
goto LOOP
}
}
}
func (s *stream) skipValue() error {
s.skipWhiteSpace()
braceCount := 0
bracketCount := 0
for {
switch s.char() {
case nul:
if s.read() {
continue
}
return errUnexpectedEndOfJSON("value of object", s.totalOffset())
case '{':
braceCount++
case '[':
bracketCount++
case '}':
braceCount--
if braceCount == -1 && bracketCount == 0 {
return nil
}
case ']':
bracketCount--
case ',':
if bracketCount == 0 && braceCount == 0 {
return nil
}
case '"':
for {
s.cursor++
c := s.char()
if c == nul {
if !s.read() {
return errUnexpectedEndOfJSON("value of string", s.totalOffset())
}
c = s.char()
}
if c != '"' {
continue
}
if s.prevChar() == '\\' {
continue
}
if bracketCount == 0 && braceCount == 0 {
s.cursor++
return nil
}
break
}
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
for {
s.cursor++
c := s.char()
if floatTable[c] {
continue
} else if c == nul {
if s.read() {
s.cursor-- // for retry current character
continue
}
}
break
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
case 't':
if err := trueBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
case 'f':
if err := falseBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
case 'n':
if err := nullBytes(s); err != nil {
return err
}
if bracketCount == 0 && braceCount == 0 {
return nil
}
continue
}
s.cursor++
}
return errUnexpectedEndOfJSON("value of object", s.offset)
}