-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcompact.go
337 lines (312 loc) · 6.52 KB
/
compact.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
package chkjson
import (
"unsafe"
)
// AppendCompact appends the compact form of src to dst if src is entirely
// JSON, returning the updated dst and whether all of src was valid.
//
// This function assumes and returns ownership of dst. If src is invalid, this
// will return nil.
//
// It is valid to pass (src[:0], src) to this function; src will be overwritten
// with any valid JSON. This function is guaranteed not to add bytes to the
// input JSON, meaning this function will not reallocate the input slice if it
// is used as both src and dst.
//
// This function does not escape line-separator or paragraph-separator
// characters.
//
// If simply compacting a slice in place, it is recommended to use Compact.
func AppendCompact(dst, src []byte) ([]byte, bool) {
return AppendCompactString(dst, *(*string)(unsafe.Pointer(&src)))
}
// AppendCompactString is exactly like AppendCompact but for compacting
// strings.
func AppendCompactString(dst []byte, src string) ([]byte, bool) {
dst, at, ok := packAny(dst, src, 0)
if !ok {
return nil, false
}
for ; at < len(src); at++ {
switch src[at] {
case '\t', '\n', '\r', ' ':
default:
return nil, false
}
}
return dst, true
}
// As in chkjson.go, this function is super ugly. It is mostly a copy of
// chkjson's any but we keep bytes as appropriate.
func packAny(dst []byte, src string, at int) ([]byte, int, bool) {
start := at
var c byte
var ok bool
whitespace:
if at == len(src) {
return nil, 0, false
}
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
start++
goto whitespace
case '{':
start++
dst = append(dst, '{')
goto finObj
case '[':
start++
dst = append(dst, '[')
goto finArr
case '"':
goto finStr
case 't':
end := at + len("rue")
if end <= len(src) &&
src[at+2] == 'e' &&
src[at] == 'r' &&
src[at+1] == 'u' {
dst = append(dst, 't', 'r', 'u', 'e')
return dst, end, true
}
return nil, 0, false
case 'f':
end := at + len("alse")
if end <= len(src) && src[at:end] == "alse" {
dst = append(dst, 'f', 'a', 'l', 's', 'e')
return dst, end, true
}
return nil, 0, false
case 'n':
end := at + len("ull")
if end <= len(src) &&
src[at] == 'u' &&
src[at+1] == 'l' &&
src[at+2] == 'l' {
dst = append(dst, 'n', 'u', 'l', 'l')
return dst, end, true
}
return nil, 0, false
case '-':
goto finNeg
case '0':
goto fin0
case '1', '2', '3', '4', '5', '6', '7', '8', '9':
goto fin1
default:
return nil, 0, false
}
finStr:
for ; at < len(src); at++ {
switch src[at] {
case 0, 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:
return nil, 0, false
case '"':
at++
dst = append(dst, src[start:at]...)
return dst, at, true
case '\\':
at++
if at == len(src) {
return nil, 0, false
}
switch src[at] {
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
case 'u':
if len(src[at:]) > 5 &&
isHex(src[at+1]) &&
isHex(src[at+2]) &&
isHex(src[at+3]) &&
isHex(src[at+4]) {
at += 5
goto finStr
}
return nil, 0, false
default:
return nil, 0, false
}
}
}
return nil, 0, false
finObj:
for at < len(src) { // finish obj immediately or begin a key
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
start++
case '"':
goto finObjKey
case '}':
dst = append(dst, '}')
return dst, at, true
default:
return dst, at, false
}
}
finObjKey:
for ; at < len(src); at++ { // duplicated above for better jumps
switch src[at] {
case 0, 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:
return nil, 0, false
case '"':
at++
dst, start = append(dst, src[start:at]...), at
goto finObjSep
case '\\':
at++
if at == len(src) {
return nil, 0, false
}
switch src[at] {
case 'b', 'f', 'n', 'r', 't', '\\', '/', '"':
case 'u':
if len(src[at:]) > 5 &&
isHex(src[at+1]) &&
isHex(src[at+2]) &&
isHex(src[at+3]) &&
isHex(src[at+4]) {
at += 5
goto finObjKey
}
return nil, 0, false
default:
return nil, 0, false
}
}
}
return nil, 0, false
finObjSep:
for at < len(src) {
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
case ':':
dst = append(dst, ':')
goto objAny
default:
return nil, 0, false
}
}
objAny:
if dst, at, ok = packAny(dst, src, at); !ok {
return nil, 0, false
}
for at < len(src) {
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
case ',':
dst, start = append(dst, ','), at-1
goto beginStr
case '}':
dst = append(dst, '}')
return dst, at, true
default:
return nil, 0, false
}
}
beginStr:
for at < len(src) {
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
case '"':
start = at - 1
goto finObjKey
default:
return nil, 0, false
}
}
return nil, 0, false
finArr:
for at < len(src) {
switch c = src[at]; c {
case ' ', '\r', '\t', '\n':
at++
case ']':
dst = append(dst, ']')
return dst, at + 1, true
default:
goto arrAny
}
}
arrAny:
if dst, at, ok = packAny(dst, src, at); !ok {
return nil, 0, false
}
for at < len(src) {
switch c, at = src[at], at+1; c {
case ' ', '\r', '\t', '\n':
case ',':
dst = append(dst, ',')
goto arrAny
case ']':
dst = append(dst, ']')
return dst, at, true
default:
return nil, 0, false
}
}
return nil, 0, false
finNeg:
if at == len(src) {
return nil, 0, false
}
if c, at = src[at], at+1; c == '0' {
goto fin0
}
if !isNat(c) {
return nil, 0, false
}
fin1:
for ; at < len(src) && isNum(src[at]); at++ {
}
fin0:
if at == len(src) {
dst = append(dst, src[start:at]...)
return dst, at, true
}
c = src[at]
if isE(c) {
at++
goto finE
}
if c != '.' {
dst = append(dst, src[start:at]...)
return dst, at, true
}
at++
// finDot
if at == len(src) {
return nil, 0, false
}
if c, at = src[at], at+1; !isNum(c) { // first char after dot must be num
return nil, 0, false
}
for ; at < len(src) && isNum(src[at]); at++ {
}
if at == len(src) || !isE(src[at]) {
dst = append(dst, src[start:at]...)
return dst, at, true
}
at++
finE:
if at == len(src) {
return nil, 0, false
}
if c, at = src[at], at+1; c == '+' || c == '-' {
if at == len(src) {
return nil, 0, false
}
c, at = src[at], at+1
}
if !isNum(c) { // first after e (and +/-) must be num
return nil, 0, false
}
for ; at < len(src) && isNum(src[at]); at++ {
}
dst = append(dst, src[start:at]...)
return dst, at, true
}