-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_string.go
More file actions
208 lines (202 loc) · 5.73 KB
/
Copy pathencoder_string.go
File metadata and controls
208 lines (202 loc) · 5.73 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
package vibejson
import (
"slices"
"unicode/utf8"
"unsafe"
"github.com/thesyncim/vibejson/x/byteview"
"github.com/thesyncim/vibejson/x/scanner"
)
const encodeHexDigits = "0123456789abcdef"
// appendShortCleanJSONString quotes strings shorter than one vector when no
// byte needs escaping, testing and copying word-at-a-time instead of
// byte-at-a-time. The caller must guarantee 1 <= len(s) <= 16: an empty
// string has no data pointer to load and a longer one would skip its middle
// bytes. ok reports whether it emitted; any flagged byte, or a dst too full
// for its unconditional word stores, defers to the general path.
func appendShortCleanJSONString(dst []byte, s string, escapeHTML bool) ([]byte, bool) {
n := uint(len(s))
if cap(dst)-len(dst) < int(n)+10 {
return dst, false
}
p := unsafe.Pointer(unsafe.StringData(s))
if n >= 8 {
w0 := loadUint64LE(p)
w1 := loadUint64LE(unsafe.Add(p, n-8))
var mask uint64
if escapeHTML {
mask = scanner.HTMLStringSpecialMask64(w0) | scanner.HTMLStringSpecialMask64(w1)
} else {
mask = scanner.StringSpecialMask64(w0) | scanner.StringSpecialMask64(w1)
}
if mask != 0 {
return dst, false
}
start := len(dst)
dst = dst[:start+int(n)+2]
dst[start] = '"'
base := sliceBase(dst)
storeUint64LE(unsafe.Add(base, start+1), w0)
storeUint64LE(unsafe.Add(base, start+1+int(n)-8), w1)
dst[start+1+int(n)] = '"'
return dst, true
}
// Overlapped halves build the exact zero-padded word image of s, so one
// mask probe and one store cover any length; the padding lanes are
// discarded from the mask and overwritten by the closing quote.
var w uint64
switch {
case n >= 4:
w = uint64(loadUint32LE(p)) | uint64(loadUint32LE(unsafe.Add(p, n-4)))<<((n-4)*8)
case n >= 2:
w = uint64(loadUint16LE(p)) | uint64(loadUint16LE(unsafe.Add(p, n-2)))<<((n-2)*8)
default:
w = uint64(*(*byte)(p))
}
var mask uint64
if escapeHTML {
mask = scanner.HTMLStringSpecialMask64(w)
} else {
mask = scanner.StringSpecialMask64(w)
}
if mask&(uint64(1)<<(8*n)-1) != 0 {
return dst, false
}
start := len(dst)
dst = dst[:start+int(n)+2]
dst[start] = '"'
storeUint64LE(unsafe.Add(sliceBase(dst), start+1), w)
dst[start+1+int(n)] = '"'
return dst, true
}
// Provenance: GO-STRING-001.
// The scalar escape behavior and control-flow shape are conservatively treated
// as adapted from Go commit d468ad3648be469ffc4090e4586c29709182d6b6,
// src/encoding/json/encode.go appendString; BSD-3-Clause, see LICENSE-GO.
// SIMD prefix scanning, fused copies, and buffer integration are local changes.
//
// appendEncodedJSONString appends s as a quoted JSON string with
// encoding/json's spelling: control bytes, quotes, and backslashes are
// escaped, invalid UTF-8 uses the active encoding/json release's spelling,
// U+2028/U+2029 are escaped, and
// escapeHTML additionally escapes '<', '>', and '&'.
func appendEncodedJSONString(dst []byte, s string, escapeHTML bool) []byte {
const fusedCopyMinBytes = 16
if len(s) == 0 {
return append(dst, '"', '"')
}
if len(s) < fusedCopyMinBytes {
if out, ok := appendShortCleanJSONString(dst, s, escapeHTML); ok {
return out
}
}
if len(s) >= fusedCopyMinBytes {
dst = slices.Grow(dst, len(s)+2)
}
dst = append(dst, '"')
src := byteview.Bytes(s)
first := 0
copiedPrefix := false
if len(s) >= fusedCopyMinBytes {
start := len(dst)
dst = dst[:start+len(s)]
if escapeHTML {
first = scanner.CopyHTMLStringPrefix(dst[start:], src)
} else {
first = scanner.CopyStringPrefix(dst[start:], src)
}
if first >= 0 {
if first == len(src) {
return append(dst, '"')
}
dst = dst[:start+first]
copiedPrefix = true
} else {
dst = dst[:start]
}
}
if !copiedPrefix {
if escapeHTML {
first = scanEncodedHTMLSpecialFast(src, 0)
} else {
first = scanStringSpecial(src, 0)
}
}
if first == len(src) {
dst = append(dst, s...)
return append(dst, '"')
}
unicodeClean := false
if src[first] >= 0x80 {
// The prefix scanner proved every earlier byte ASCII, so validating
// only the non-ASCII tail preserves the complete UTF-8 certificate.
unicodeClean = validUTF8NoLineSeparatorFast(src[first:])
}
start := 0
if copiedPrefix {
start = first
}
for i := first; i < len(s); {
// The scanners stop at exactly the escape-relevant set: quotes,
// backslashes, control bytes, non-ASCII, and in HTML mode the
// angle brackets and ampersand encoding/json escapes by default.
if unicodeClean && escapeHTML {
i = scanEncodedHTMLSyntaxFast(src, i)
} else if unicodeClean {
i = scanStringSyntax(src, i)
} else if escapeHTML {
i = scanEncodedHTMLSpecialFast(src, i)
} else {
i = scanStringSpecial(src, i)
}
if i >= len(s) {
break
}
c := s[i]
if c < 0x80 {
dst = append(dst, s[start:i]...)
switch c {
case '"':
dst = append(dst, '\\', '"')
case '\\':
dst = append(dst, '\\', '\\')
case '\b':
dst = append(dst, '\\', 'b')
case '\f':
dst = append(dst, '\\', 'f')
case '\n':
dst = append(dst, '\\', 'n')
case '\r':
dst = append(dst, '\\', 'r')
case '\t':
dst = append(dst, '\\', 't')
default:
dst = append(dst, '\\', 'u', '0', '0', encodeHexDigits[c>>4], encodeHexDigits[c&0xF])
}
i++
start = i
continue
}
r, size := utf8.DecodeRuneInString(s[i:])
if r == utf8.RuneError && size == 1 {
dst = append(dst, s[start:i]...)
if escapeInvalidUTF8 {
dst = append(dst, '\\', 'u', 'f', 'f', 'f', 'd')
} else {
dst = utf8.AppendRune(dst, utf8.RuneError)
}
i++
start = i
continue
}
if r == '\u2028' || r == '\u2029' {
dst = append(dst, s[start:i]...)
dst = append(dst, '\\', 'u', '2', '0', '2', encodeHexDigits[r&0xF])
i += size
start = i
continue
}
i += size
}
dst = append(dst, s[start:]...)
return append(dst, '"')
}