forked from mhr3/jsoniter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiter_str.go
350 lines (321 loc) · 7.1 KB
/
iter_str.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package jsoniter
import (
"bytes"
"strconv"
"strings"
"unicode/utf16"
)
// ReadString read string from iterator
func (iter *Iterator) ReadString() string {
c := iter.nextToken()
switch c {
case '"':
case 'n':
iter.skipThreeBytes('u', 'l', 'l')
return ""
default:
iter.ReportError("ReadString", `expects " or n, but found `+string([]byte{c}))
return ""
}
return iter.readStringInner()
}
// ReadStringAsSlice read string from iterator without copying into string form.
// The []byte can not be kept, as it will change after next iterator call.
// DEPRECATED: Use ReadRawString instead
func (iter *Iterator) ReadStringAsSlice() (ret []byte) {
return iter.ReadRawString().buf
}
func (iter *Iterator) readStringInner() string {
sb := strings.Builder{}
outerLoop:
for iter.Error == nil {
// eliminate bounds check inside the loop
if iter.head < 0 || iter.tail > len(iter.buf) {
return ""
}
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch {
case c == '"':
if sb.Len() == 0 {
// super fast path
res := iter.buf[iter.head:i]
iter.head = i + 1
return string(res)
}
sb.Write(iter.buf[iter.head:i])
iter.head = i + 1
return sb.String()
case c == '\\':
sb.Write(iter.buf[iter.head:i])
iter.head = i + 1
iter.readEscapedChar(&sb)
continue outerLoop
case c < ' ':
iter.ReportError("ReadString",
"invalid control character found: "+strconv.Itoa(int(c)))
return ""
}
}
// copy buffer and load more
sb.Write(iter.buf[iter.head:iter.tail])
iter.head = iter.tail
// load next chunk
if iter.readByte() == 0 {
break
}
iter.unreadByte()
}
iter.ReportError("ReadString", "unexpected end of input")
return ""
}
// ReadRawString reads string from iterator without decoding escape sequences.
// Note that the returned RawString is only valid until the next read from the iterator.
func (iter *Iterator) ReadRawString() RawString {
c := iter.nextToken()
switch c {
case '"':
case 'n':
iter.skipThreeBytes('u', 'l', 'l')
return RawString{}
default:
iter.ReportError("ReadRawString", `expects " or n, but found `+string([]byte{c}))
return RawString{}
}
return iter.readRawStringInner()
}
func (iter *Iterator) readRawStringInner() RawString {
var (
copied bytes.Buffer
readingEscape bool
hasEscapes bool
)
outerLoop:
for iter.Error == nil {
// eliminate bounds check inside the loop
if iter.head < 0 || iter.tail > len(iter.buf) {
return RawString{}
}
for i := iter.head; i < iter.tail; i++ {
c := iter.buf[i]
switch {
case c == '"':
if readingEscape {
readingEscape = false
continue
}
// careful, we're copying the ending double quote into the buffer
if copied.Len() == 0 {
// super fast path
prevHead := iter.head
iter.head = i + 1
return RawString{buf: iter.buf[prevHead:iter.head], isRaw: true, hasEscapes: hasEscapes}
}
prevHead := iter.head
iter.head = i + 1
copied.Write(iter.buf[prevHead:iter.head])
return RawString{buf: copied.Bytes(), hasEscapes: hasEscapes}
case c == '\\':
// toggle readingEscape
readingEscape = readingEscape != true
hasEscapes = true
continue
case c < ' ':
iter.ReportError("ReadRawString",
"invalid control character found: "+strconv.Itoa(int(c)))
return RawString{}
default:
if !readingEscape {
continue
}
}
readingEscape = false
switch c {
case '"', '\\', '/', 'b', 'f', 'n', 'r', 't':
case 'u':
prevHead := iter.head
iter.head = i + 1
// are we about to change iter.buf?
if i+4 >= iter.tail {
copied.Write(iter.buf[prevHead:iter.head])
var buf [4]byte
iter.readAndFillU4(buf[:])
copied.Write(buf[:])
continue outerLoop
}
if iter.parseU4() == -1 {
iter.ReportError("ReadRawString", "invalid unicode escape sequence")
return RawString{}
}
// it shouldn't be necessary to break out of the loop, but
// for some reason the compiler doesn't like this branch
// and inserts slice bounds check around the iter.buf[i] read
// without the "continue outerLoop"
copied.Write(iter.buf[prevHead:iter.head])
continue outerLoop
default:
iter.ReportError("ReadRawString", `invalid escape char after \`)
return RawString{}
}
}
// copy buffer and load more
copied.Write(iter.buf[iter.head:iter.tail])
iter.head = iter.tail
// load next chunk
if iter.readByte() == 0 {
break
}
iter.unreadByte()
}
iter.ReportError("ReadRawString", "unexpected end of input")
return RawString{}
}
func (iter *Iterator) readEscapedChar(sb *strings.Builder) {
c := iter.readByte()
start:
switch c {
case 'u':
r := iter.readU4()
if utf16.IsSurrogate(r) {
c = iter.readByte()
if iter.Error != nil {
return
}
if c != '\\' {
iter.unreadByte()
sb.WriteRune(r)
return
}
c = iter.readByte()
if iter.Error != nil {
return
}
if c != 'u' {
sb.WriteRune(r)
goto start
}
r2 := iter.readU4()
if iter.Error != nil {
return
}
combined := utf16.DecodeRune(r, r2)
if combined == '\uFFFD' {
sb.WriteRune(r)
sb.WriteRune(r2)
} else {
sb.WriteRune(combined)
}
} else if iter.Error == nil {
sb.WriteRune(r)
}
case '"':
sb.WriteByte('"')
case '\\':
sb.WriteByte('\\')
case '/':
sb.WriteByte('/')
case 'b':
sb.WriteByte('\b')
case 'f':
sb.WriteByte('\f')
case 'n':
sb.WriteByte('\n')
case 'r':
sb.WriteByte('\r')
case 't':
sb.WriteByte('\t')
default:
iter.ReportError("readEscapedChar", `invalid escape char after \`)
}
}
func (iter *Iterator) parseU4() (ret rune) {
// eliminate bounds check inside the loop
end := iter.head + 4
if iter.head < 0 || end > len(iter.buf) {
return -1
}
for i := iter.head; i < end; i++ {
c := iter.buf[i]
c -= '0'
if c <= 9 {
ret = ret*16 + rune(c)
continue
}
c -= 'A' - '0'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
c -= 'a' - 'A'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
return -1
}
iter.head = end
return ret
}
func (iter *Iterator) readU4() (ret rune) {
if iter.tail-iter.head >= 4 {
if ret = iter.parseU4(); ret < 0 {
iter.ReportError("readU4", "invalid hex char")
return 0
}
return ret
}
for i := 0; i < 4; i++ {
c := iter.readByte()
if iter.Error != nil {
return
}
c -= '0'
if c <= 9 {
ret = ret*16 + rune(c)
continue
}
c -= 'A' - '0'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
c -= 'a' - 'A'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
iter.ReportError("readU4", "invalid hex char")
return
}
return ret
}
func (iter *Iterator) readAndFillU4(buf []byte) (ret rune) {
if len(buf) < 4 {
panic("buffer too small")
}
for i := 0; i < 4; i++ {
c := iter.readByte()
if iter.Error != nil {
return
}
buf[i] = c
c -= '0'
if c <= 9 {
ret = ret*16 + rune(c)
continue
}
c -= 'A' - '0'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
c -= 'a' - 'A'
if c <= 5 {
ret = ret*16 + rune(c+10)
continue
}
iter.ReportError("readU4", "invalid hex char")
return
}
return ret
}