-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.go
More file actions
464 lines (440 loc) · 10.4 KB
/
Copy pathencode.go
File metadata and controls
464 lines (440 loc) · 10.4 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package jsonx
import (
"math"
"reflect"
"strconv"
"sync"
"unicode/utf8"
"unsafe"
)
type encoder struct {
buf []byte
}
var encoderPool = sync.Pool{New: func() interface{} { return &encoder{buf: make([]byte, 0, 1024)} }}
func (e *encoder) reset() { e.buf = e.buf[:0] }
func (e *encoder) encode(v interface{}) error {
if v == nil {
e.buf = append(e.buf, 'n', 'u', 'l', 'l')
return nil
}
// Fast path for common scalar interface values.
switch x := v.(type) {
case string:
e.writeString(x)
return nil
case bool:
if x {
e.buf = append(e.buf, 't', 'r', 'u', 'e')
} else {
e.buf = append(e.buf, 'f', 'a', 'l', 's', 'e')
}
return nil
case float64:
return e.writeFloat(x, 64)
case int:
e.buf = strconv.AppendInt(e.buf, int64(x), 10)
return nil
case int64:
e.buf = strconv.AppendInt(e.buf, x, 10)
return nil
case map[string]interface{}:
return e.writeMapInterface(x)
case []interface{}:
return e.writeSliceInterface(x)
case []byte:
if x == nil {
e.buf = append(e.buf, 'n', 'u', 'l', 'l')
return nil
}
e.buf = append(e.buf, '"')
e.buf = base64Encode(e.buf, x)
e.buf = append(e.buf, '"')
return nil
}
rv := reflect.ValueOf(v)
enc := cachedEncoder(rv.Type())
return enc(e, unsafePointerOf(rv))
}
func unsafePointerOf(rv reflect.Value) unsafe.Pointer {
// For most kinds the reflect.Value holds the value itself (not a pointer).
// We need a pointer to that value. Use Addr() if addressable; otherwise
// copy to a heap slot.
if rv.CanAddr() {
return unsafe.Pointer(rv.UnsafeAddr())
}
// Make addressable via a new pointer of the same type.
p := reflect.New(rv.Type())
p.Elem().Set(rv)
return unsafe.Pointer(p.Pointer())
}
// -------- string writing --------
var htmlEscape = [256]bool{'<': true, '>': true, '&': true}
var stringSafeSet = func() [256]bool {
var safe [256]bool
for i := 0x20; i < utf8.RuneSelf; i++ {
c := byte(i)
safe[c] = c != '"' && c != '\\' && !htmlEscape[c]
}
return safe
}()
func (e *encoder) writeString(s string) {
e.buf = appendJSONString(e.buf, s)
}
func appendJSONString(dst []byte, s string) []byte {
n := len(s)
if n == 0 {
return append(dst, '"', '"')
}
i := scanString(unsafe.Pointer(unsafe.StringData(s)), n)
if i == n {
// Fast path: no escapes. One combined grow check + copy instead
// of three separate appends (each one does its own grow check +
// slice-header write, the latter provoking GC write barriers).
L := len(dst)
need := L + n + 2
if need <= cap(dst) {
buf := dst[:need]
buf[L] = '"'
copy(buf[L+1:], s)
buf[need-1] = '"'
return buf
}
// slow path via append (grows)
dst = append(dst, '"')
dst = append(dst, s...)
dst = append(dst, '"')
return dst
}
dst = append(dst, '"')
return appendJSONStringSlow(dst, s, i)
}
func stringEncodeBreakMask(w uint64) uint64 {
const lo = 0x0101010101010101
const hi = 0x8080808080808080
return stringBreakMask(w) |
byteEqMask(w, '<') |
byteEqMask(w, '>') |
byteEqMask(w, '&') |
(w & hi)
}
func byteEqMask(w uint64, c byte) uint64 {
const lo = 0x0101010101010101
const hi = 0x8080808080808080
x := w ^ (lo * uint64(c))
return (x - lo) & ^x & hi
}
func appendJSONStringSlow(dst []byte, s string, start int) []byte {
i := start
chunkStart := 0
for i < len(s) {
c := s[i]
if c < utf8.RuneSelf {
if stringSafeSet[c] {
i++
continue
}
dst = append(dst, s[chunkStart:i]...)
switch {
case c == '"' || c == '\\':
dst = append(dst, '\\', c)
case c == '\n':
dst = append(dst, '\\', 'n')
case c == '\r':
dst = append(dst, '\\', 'r')
case c == '\t':
dst = append(dst, '\\', 't')
case c == '\b':
dst = append(dst, '\\', 'b')
case c == '\f':
dst = append(dst, '\\', 'f')
default:
dst = append(dst, '\\', 'u', '0', '0', hexChar[c>>4], hexChar[c&0xf])
}
i++
chunkStart = i
continue
}
switch utf8SeqLen(s, i) {
case 2:
i += 2
continue
case 3:
if s[i] == 0xe2 && s[i+1] == 0x80 && s[i+2]&^1 == 0xa8 {
dst = append(dst, s[chunkStart:i]...)
dst = append(dst, '\\', 'u', '2', '0', '2', hexChar[s[i+2]&0xf])
i += 3
chunkStart = i
continue
}
i += 3
continue
case 4:
i += 4
continue
}
dst = append(dst, s[chunkStart:i]...)
dst = append(dst, '\\', 'u', 'f', 'f', 'f', 'd')
i++
chunkStart = i
}
dst = append(dst, s[chunkStart:]...)
return append(dst, '"')
}
func utf8SeqLen(s string, i int) int {
c := s[i]
if c < 0xc2 {
return 0
}
if c < 0xe0 {
if i+1 < len(s) && isUTF8Cont(s[i+1]) {
return 2
}
return 0
}
if c < 0xf0 {
if i+2 >= len(s) {
return 0
}
c1, c2 := s[i+1], s[i+2]
if !isUTF8Cont(c1) || !isUTF8Cont(c2) {
return 0
}
if c == 0xe0 && c1 < 0xa0 {
return 0
}
if c == 0xed && c1 >= 0xa0 {
return 0
}
return 3
}
if c < 0xf5 {
if i+3 >= len(s) {
return 0
}
c1, c2, c3 := s[i+1], s[i+2], s[i+3]
if !isUTF8Cont(c1) || !isUTF8Cont(c2) || !isUTF8Cont(c3) {
return 0
}
if c == 0xf0 && c1 < 0x90 {
return 0
}
if c == 0xf4 && c1 >= 0x90 {
return 0
}
return 4
}
return 0
}
func isUTF8Cont(c byte) bool {
return c&0xc0 == 0x80
}
var hexChar = "0123456789abcdef"
func appendCompactEscapedJSON(dst, src []byte) []byte {
inString := false
escape := false
for i := 0; i < len(src); i++ {
c := src[i]
if inString {
if escape {
dst = append(dst, c)
escape = false
continue
}
switch c {
case '\\':
dst = append(dst, c)
escape = true
case '"':
dst = append(dst, c)
inString = false
case '<', '>', '&':
dst = append(dst, '\\', 'u', '0', '0', hexChar[c>>4], hexChar[c&0xf])
case 0xe2:
if i+2 < len(src) && src[i+1] == 0x80 && src[i+2]&^1 == 0xa8 {
dst = append(dst, '\\', 'u', '2', '0', '2', hexChar[src[i+2]&0xf])
i += 2
} else {
dst = append(dst, c)
}
default:
dst = append(dst, c)
}
continue
}
switch c {
case '"':
inString = true
dst = append(dst, c)
case ' ', '\t', '\n', '\r':
// compact insignificant whitespace
default:
dst = append(dst, c)
}
}
return dst
}
// appendIndented reformats compact JSON from src into dst, inserting newlines,
// prefix, and per-level indent matching encoding/json.Indent semantics. src is
// assumed to be valid, compact JSON as produced by Marshal.
func appendIndented(dst, src []byte, prefix, indent string) []byte {
depth := 0
inString := false
escape := false
needIndent := false
writeIndent := func(dst []byte, d int) []byte {
dst = append(dst, '\n')
dst = append(dst, prefix...)
for i := 0; i < d; i++ {
dst = append(dst, indent...)
}
return dst
}
for i := 0; i < len(src); i++ {
c := src[i]
if inString {
dst = append(dst, c)
if escape {
escape = false
continue
}
switch c {
case '\\':
escape = true
case '"':
inString = false
}
continue
}
if needIndent && c != '}' && c != ']' {
dst = writeIndent(dst, depth)
}
needIndent = false
switch c {
case '"':
inString = true
dst = append(dst, c)
case '{', '[':
depth++
dst = append(dst, c)
needIndent = true
case '}', ']':
depth--
if n := len(dst); n > 0 && (dst[n-1] == '{' || dst[n-1] == '[') {
dst = append(dst, c)
} else {
dst = writeIndent(dst, depth)
dst = append(dst, c)
}
case ',':
dst = append(dst, c)
needIndent = true
case ':':
dst = append(dst, c, ' ')
case ' ', '\t', '\n', '\r':
// Marshal doesn't emit whitespace outside strings; ignore defensively.
default:
dst = append(dst, c)
}
}
return dst
}
// -------- number writing --------
func (e *encoder) writeFloat(f float64, bits int) error {
if math.IsNaN(f) || math.IsInf(f, 0) {
return &UnsupportedTypeError{Type: reflect.TypeOf(f)}
}
// float64 → pure-Go Schubfach (E21). ~41 % faster than strconv
// shortest in isolation microbench (458 ns vs 777 ns on canada
// samples). Round-trip identical to strconv (fuzzed against 1 M
// random + all 111 k canada.json floats, all bit-exact).
if bits == 64 {
e.buf = schubfachAppendFloat64(e.buf, f)
return nil
}
// float32: keep stdlib for now; doesn't affect our corpora.
abs := math.Abs(f)
fmt := byte('f')
if abs != 0 && (abs < 1e-6 || abs >= 1e21) {
fmt = 'e'
}
e.buf = strconv.AppendFloat(e.buf, f, fmt, -1, 32)
return nil
}
// -------- map[string]interface{} / []interface{} --------
func (e *encoder) writeMapInterface(m map[string]interface{}) error {
e.buf = append(e.buf, '{')
first := true
for k, v := range m {
if !first {
e.buf = append(e.buf, ',')
}
first = false
e.writeString(k)
e.buf = append(e.buf, ':')
if err := e.encodeAny(v); err != nil {
return err
}
}
e.buf = append(e.buf, '}')
return nil
}
func (e *encoder) writeSliceInterface(a []interface{}) error {
e.buf = append(e.buf, '[')
for i, v := range a {
if i > 0 {
e.buf = append(e.buf, ',')
}
if err := e.encodeAny(v); err != nil {
return err
}
}
e.buf = append(e.buf, ']')
return nil
}
// encodeAny dispatches an interface{} value using direct type-pointer
// comparison — faster than Go's type-switch assembly for a small, fixed
// set of "hot" types (strings, float64, map/slice of interface{}). On
// twitter.json this removed ≈ 18 % GC write-barrier overhead that the
// type-switch assembly was triggering via implicit iface copies.
func (e *encoder) encodeAny(v interface{}) error {
ef := (*eface)(unsafe.Pointer(&v))
tp := ef.typ
if tp == nil {
e.buf = append(e.buf, 'n', 'u', 'l', 'l')
return nil
}
switch tp {
case typeString:
// data points to a string header (len,data)
s := *(*string)(ef.data)
e.writeString(s)
return nil
case typeFloat64:
f := *(*float64)(ef.data)
return e.writeFloat(f, 64)
case typeBool:
if *(*bool)(ef.data) {
e.buf = append(e.buf, 't', 'r', 'u', 'e')
} else {
e.buf = append(e.buf, 'f', 'a', 'l', 's', 'e')
}
return nil
case typeMapStringInterface:
// A Go map is internally a pointer (hmap*), so the iface data
// field already IS the map pointer — reinterpret &ef.data, not
// the pointee.
return e.writeMapInterface(*(*map[string]interface{})(unsafe.Pointer(&ef.data)))
case typeSliceInterface:
// A slice header is 24 bytes, so Go boxes it — ef.data points
// at the header.
return e.writeSliceInterface(*(*[]interface{})(ef.data))
case typeInt:
e.buf = strconv.AppendInt(e.buf, int64(*(*int)(ef.data)), 10)
return nil
case typeInt64:
e.buf = strconv.AppendInt(e.buf, *(*int64)(ef.data), 10)
return nil
}
// Fallback for less-common types: reflect path.
return e.encode(v)
}