-
Notifications
You must be signed in to change notification settings - Fork 453
/
quote.go
304 lines (277 loc) · 7.63 KB
/
quote.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package strconv
import (
"strconv"
"unicode/utf8"
)
// NB: predefined strconv constants
const (
tx = 0x80 // 1000 0000
t2 = 0xC0 // 1100 0000
t3 = 0xE0 // 1110 0000
t4 = 0xF0 // 1111 0000
maskx = 0x3F // 0011 1111
rune1Max = 1<<7 - 1
rune2Max = 1<<11 - 1
rune3Max = 1<<16 - 1
runeError = '\uFFFD' // the "error" Rune or "Unicode replacement character"
maxRune = '\U0010FFFF' // Maximum valid Unicode code point.
// NB: Code points in the surrogate range are not valid for UTF-8.
surrogateMin = 0xD800
surrogateMax = 0xDFFF
lowerhex = "0123456789abcdef"
quote = byte('"')
)
// EncodeRune writes into src (which must be large enough) the UTF-8 encoding
// of the rune at the given index. It returns the number of bytes written.
//
// NB: based on utf8.encodeRune method, but instead uses indexed insertion
// into a predefined buffer.
func encodeRune(dst []byte, r rune, idx int) int {
// Negative values are erroneous. Making it unsigned addresses the problem.
switch i := uint32(r); {
case i <= rune1Max:
dst[idx] = byte(r)
return idx + 1
case i <= rune2Max:
dst[idx] = t2 | byte(r>>6)
dst[idx+1] = tx | byte(r)&maskx
return idx + 2
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
r = runeError
fallthrough
case i <= rune3Max:
dst[idx] = t3 | byte(r>>12)
dst[idx+2] = tx | byte(r)&maskx
dst[idx+1] = tx | byte(r>>6)&maskx
return idx + 3
default:
dst[idx] = t4 | byte(r>>18)
dst[idx+1] = tx | byte(r>>12)&maskx
dst[idx+2] = tx | byte(r>>6)&maskx
dst[idx+3] = tx | byte(r)&maskx
return idx + 4
}
}
// It returns the number of bytes written.
func insertEscapedRune(dst []byte, r rune, idx int) int {
if r == rune(quote) || r == '\\' { // always backslashed
dst[idx] = '\\'
dst[idx+1] = byte(r)
return idx + 2
}
if strconv.IsPrint(r) {
return encodeRune(dst, r, idx)
}
switch r {
case '\a':
dst[idx] = '\\'
dst[idx+1] = 'a'
return idx + 2
case '\b':
dst[idx] = '\\'
dst[idx+1] = 'b'
return idx + 2
case '\f':
dst[idx] = '\\'
dst[idx+1] = 'f'
return idx + 2
case '\n':
dst[idx] = '\\'
dst[idx+1] = 'n'
return idx + 2
case '\r':
dst[idx] = '\\'
dst[idx+1] = 'r'
return idx + 2
case '\t':
dst[idx] = '\\'
dst[idx+1] = 't'
return idx + 2
case '\v':
dst[idx] = '\\'
dst[idx+1] = 'v'
return idx + 2
default:
switch {
case r < ' ':
dst[idx] = '\\'
dst[idx+1] = 'x'
dst[idx+2] = lowerhex[byte(r)>>4]
dst[idx+3] = lowerhex[byte(r)&0xF]
return idx + 4
case r > utf8.MaxRune:
r = 0xFFFD
fallthrough
case r < 0x10000:
dst[idx] = '\\'
dst[idx+1] = 'u'
dst[idx+2] = lowerhex[r>>uint(12)&0xF]
dst[idx+3] = lowerhex[r>>uint(8)&0xF]
dst[idx+4] = lowerhex[r>>uint(4)&0xF]
dst[idx+5] = lowerhex[r>>uint(0)&0xF]
return idx + 6
default:
dst[idx] = '\\'
dst[idx+1] = 'U'
dst[idx+2] = lowerhex[r>>uint(28)&0xF]
dst[idx+3] = lowerhex[r>>uint(24)&0xF]
dst[idx+4] = lowerhex[r>>uint(20)&0xF]
dst[idx+5] = lowerhex[r>>uint(16)&0xF]
dst[idx+6] = lowerhex[r>>uint(12)&0xF]
dst[idx+7] = lowerhex[r>>uint(8)&0xF]
dst[idx+8] = lowerhex[r>>uint(4)&0xF]
dst[idx+9] = lowerhex[r>>uint(0)&0xF]
return idx + 10
}
}
}
// Escape copies byte slice src to dst at a given index, adding escaping any
// quote or control characters. It returns the index at which the copy finished.
//
// NB: ensure that dst is large enough to store src, additional
// quotation runes, and any additional escape characters.
// as generated by Quote, to dst and returns the extended buffer.
func Escape(dst, src []byte, idx int) int {
// nolint
for width := 0; len(src) > 0; src = src[width:] {
r := rune(src[0])
width = 1
if r >= utf8.RuneSelf {
r, width = utf8.DecodeRune(src)
}
if width == 1 && r == utf8.RuneError {
dst[idx] = '\\'
dst[idx+1] = 'x'
dst[idx+2] = lowerhex[src[0]>>4]
dst[idx+3] = lowerhex[src[0]&0xF]
idx += 4
continue
}
idx = insertEscapedRune(dst, r, idx)
}
return idx
}
// Quote copies byte slice src to dst at a given index, adding
// quotation runes around the src slice and escaping any quote or control
// characters. It returns the index at which the copy finished.
//
// NB: ensure that dst is large enough to store src, additional
// quotation runes, and any additional escape characters.
// as generated by Quote, to dst and returns the extended buffer.
//
// NB: based on stconv.Quote method, but instead uses indexed insertion
// into a predefined buffer.
func Quote(dst, src []byte, idx int) int {
dst[idx] = quote
idx++
idx = Escape(dst, src, idx)
dst[idx] = quote
return idx + 1
}
// QuoteSimple copies byte slice src to dst at a given index, adding
// quotation runes around the src slice, but does not escape any
// characters. It returns the index at which the copy finished.
//
// NB: ensure that dst is large enough to store src and two other characters.
func QuoteSimple(dst, src []byte, idx int) int {
dst[idx] = quote
idx++
idx += copy(dst[idx:], src)
dst[idx] = quote
return idx + 1
}
// EscapedLength computes the length required for a byte slice to hold
// a quoted byte slice.
//
// NB: essentially a dry-run of `Escape` that does not write characters, but
// instead counts total character counts for the destination byte slice.
func EscapedLength(src []byte) int {
length := 0
// nolint
for width := 0; len(src) > 0; src = src[width:] {
r := rune(src[0])
width = 1
if r >= utf8.RuneSelf {
r, width = utf8.DecodeRune(src)
}
if width == 1 && r == utf8.RuneError {
length += 4
continue
}
length += escapedRuneLength(r)
}
return length
}
// QuotedLength computes the length required for a byte slice to hold
// a quoted byte slice.
//
// NB: essentially a dry-run of `Quote` that does not write characters, but
// instead counts total character counts for the destination byte slice.
func QuotedLength(src []byte) int {
return 2 + EscapedLength(src) // account for opening and closing quotes
}
func escapedRuneLength(r rune) int {
if r == rune(quote) || r == '\\' { // always backslashed
return 2
}
if strconv.IsPrint(r) {
switch i := uint32(r); {
case i <= rune1Max:
return 1
case i <= rune2Max:
return 2
case i > maxRune, surrogateMin <= i && i <= surrogateMax:
fallthrough
case i <= rune3Max:
return 3
default:
return 4
}
}
switch r {
case '\a':
return 2
case '\b':
return 2
case '\f':
return 2
case '\n':
return 2
case '\r':
return 2
case '\t':
return 2
case '\v':
return 2
default:
switch {
case r < ' ':
return 4
case r > utf8.MaxRune:
fallthrough
case r < 0x10000:
return 6
default:
return 10
}
}
}