-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathsplit.go
515 lines (443 loc) · 11 KB
/
split.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
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package parser
import (
"fmt"
"strings"
"unicode"
)
/* Functions to split/join, unescape/escape strings similar to Exec=... lines in unit files */
type SplitFlags = uint64
const (
SplitRelax SplitFlags = 1 << iota // Allow unbalanced quote and eat up trailing backslash.
SplitCUnescape // Unescape known escape sequences.
SplitUnescapeRelax // Allow and keep unknown escape sequences, allow and keep trailing backslash.
SplitUnescapeSeparators // Unescape separators (those specified, or whitespace by default).
SplitKeepQuote // Ignore separators in quoting with "" and ''.
SplitUnquote // Ignore separators in quoting with "" and '', and remove the quotes.
SplitDontCoalesceSeparators // Don't treat multiple adjacent separators as one
SplitRetainEscape // Treat escape character '\' as any other character without special meaning
SplitRetainSeparators // Do not advance the original string pointer past the separator(s) */
)
const WhitespaceSeparators = " \t\n\r"
func unoctchar(v byte) int {
if v >= '0' && v <= '7' {
return int(v - '0')
}
return -1
}
func unhexchar(v byte) int {
if v >= '0' && v <= '9' {
return int(v - '0')
}
if v >= 'a' && v <= 'f' {
return int(v - 'a' + 10)
}
if v >= 'A' && v <= 'F' {
return int(v - 'A' + 10)
}
return -1
}
func isValidUnicode(c uint32) bool {
return c <= unicode.MaxRune
}
/* This is based on code from systemd (src/basic/escape.c), marked LGPL-2.1-or-later and is copyrighted by the systemd developers */
func cUnescapeOne(p string, acceptNul bool) (int, rune, bool) {
var count = 1
var eightBit = false
var ret rune
// Unescapes C style. Returns the unescaped character in ret.
// Returns eightBit as true if the escaped sequence either fits in
// one byte in UTF-8 or is a non-unicode literal byte and should
// instead be copied directly.
if len(p) < 1 {
return -1, 0, false
}
switch p[0] {
case 'a':
ret = '\a'
case 'b':
ret = '\b'
case 'f':
ret = '\f'
case 'n':
ret = '\n'
case 'r':
ret = '\r'
case 't':
ret = '\t'
case 'v':
ret = '\v'
case '\\':
ret = '\\'
case '"':
ret = '"'
case '\'':
ret = '\''
case 's':
/* This is an extension of the XDG syntax files */
ret = ' '
case 'x':
/* hexadecimal encoding */
if len(p) < 3 {
return -1, 0, false
}
a := unhexchar(p[1])
if a < 0 {
return -1, 0, false
}
b := unhexchar(p[2])
if b < 0 {
return -1, 0, false
}
/* Don't allow NUL bytes */
if a == 0 && b == 0 && !acceptNul {
return -1, 0, false
}
ret = rune((a << 4) | b)
eightBit = true
count = 3
case 'u':
/* C++11 style 16bit unicode */
if len(p) < 5 {
return -1, 0, false
}
var a [4]int
for i := 0; i < 4; i++ {
a[i] = unhexchar(p[1+i])
if a[i] < 0 {
return -1, 0, false
}
}
c := (uint32(a[0]) << 12) | (uint32(a[1]) << 8) | (uint32(a[2]) << 4) | uint32(a[3])
/* Don't allow 0 chars */
if c == 0 && !acceptNul {
return -1, 0, false
}
ret = rune(c)
count = 5
case 'U':
/* C++11 style 32bit unicode */
if len(p) < 9 {
return -1, 0, false
}
var a [8]int
for i := 0; i < 8; i++ {
a[i] = unhexchar(p[1+i])
if a[i] < 0 {
return -10, 0, false
}
}
c := (uint32(a[0]) << 28) | (uint32(a[1]) << 24) | (uint32(a[2]) << 20) | (uint32(a[3]) << 16) |
(uint32(a[4]) << 12) | (uint32(a[5]) << 8) | (uint32(a[6]) << 4) | uint32(a[7])
/* Don't allow 0 chars */
if c == 0 && !acceptNul {
return -1, 0, false
}
/* Don't allow invalid code points */
if !isValidUnicode(c) {
return -1, 0, false
}
ret = rune(c)
count = 9
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
/* octal encoding */
if len(p) < 3 {
return -1, 0, false
}
a := unoctchar(p[0])
if a < 0 {
return -1, 0, false
}
b := unoctchar(p[0])
if b < 0 {
return -1, 0, false
}
c := unoctchar(p[0])
if c < 0 {
return -1, 0, false
}
/* don't allow NUL bytes */
if a == 0 && b == 0 && c == 0 && !acceptNul {
return -1, 0, false
}
/* Don't allow bytes above 255 */
m := (uint32(a) << 6) | (uint32(b) << 3) | uint32(c)
if m > 255 {
return -1, 0, false
}
ret = rune(m)
eightBit = true
count = 3
default:
return -1, 0, false
}
return count, ret, eightBit
}
/* This is based on code from systemd (src/basic/extract-workd.c), marked LGPL-2.1-or-later and is copyrighted by the systemd developers */
// Returns: word, remaining, more-words, error
func extractFirstWord(in string, separators string, flags SplitFlags) (string, string, bool, error) {
var s strings.Builder
var quote byte // 0 or ' or "
backslash := false // whether we've just seen a backslash
// The string handling in this function is a bit weird, using
// 0 bytes to mark end-of-string. This is because it is a direct
// conversion of the C in systemd, and w want to ensure
// exactly the same behaviour of some complex code
p := 0
end := len(in)
var c byte
nextChar := func() byte {
p++
if p >= end {
return 0
}
return in[p]
}
/* Bail early if called after last value or with no input */
if len(in) == 0 {
goto finish
}
// Parses the first word of a string, and returns it and the
// remainder. Removes all quotes in the process. When parsing
// fails (because of an uneven number of quotes or similar),
// the rest is at the first invalid character. */
loop1:
for c = in[0]; ; c = nextChar() {
switch {
case c == 0:
goto finishForceTerminate
case strings.ContainsRune(separators, rune(c)):
if flags&SplitDontCoalesceSeparators != 0 {
if !(flags&SplitRetainSeparators != 0) {
p++
}
goto finishForceNext
}
default:
// We found a non-blank character, so we will always
// want to return a string (even if it is empty),
// allocate it here.
break loop1
}
}
for ; ; c = nextChar() {
switch {
case backslash:
if c == 0 {
if flags&SplitUnescapeRelax != 0 &&
(quote == 0 || flags&SplitRelax != 0) {
// If we find an unquoted trailing backslash and we're in
// SplitUnescapeRelax mode, keep it verbatim in the
// output.
//
// Unbalanced quotes will only be allowed in SplitRelax
// mode, SplitUnescapeRelax mode does not allow them.
s.WriteString("\\")
goto finishForceTerminate
}
if flags&SplitRelax != 0 {
goto finishForceTerminate
}
return "", "", false, fmt.Errorf("unbalanced escape")
}
if flags&(SplitCUnescape|SplitUnescapeSeparators) != 0 {
var r = -1
var u rune
if flags&SplitCUnescape != 0 {
r, u, _ = cUnescapeOne(in[p:], false)
}
switch {
case r > 0:
p += r - 1
s.WriteRune(u)
case (flags&SplitUnescapeSeparators != 0) &&
(strings.ContainsRune(separators, rune(c)) || c == '\\'):
/* An escaped separator char or the escape char itself */
s.WriteByte(c)
case flags&SplitUnescapeRelax != 0:
s.WriteByte('\\')
s.WriteByte(c)
default:
return "", "", false, fmt.Errorf("unsupported escape char")
}
} else {
s.WriteByte(c)
}
backslash = false
case quote != 0:
/* inside either single or double quotes */
quoteloop:
for ; ; c = nextChar() {
switch {
case c == 0:
if flags&SplitRelax != 0 {
goto finishForceTerminate
}
return "", "", false, fmt.Errorf("unbalanced quotes")
case c == quote:
/* found the end quote */
quote = 0
if flags&SplitUnquote != 0 {
break quoteloop
}
case c == '\\' && !(flags&SplitRetainEscape != 0):
backslash = true
break quoteloop
}
s.WriteByte(c)
if quote == 0 {
break quoteloop
}
}
default:
nonquoteloop:
for ; ; c = nextChar() {
switch {
case c == 0:
goto finishForceTerminate
case (c == '\'' || c == '"') && (flags&(SplitKeepQuote|SplitUnquote) != 0):
quote = c
if flags&SplitUnquote != 0 {
break nonquoteloop
}
case c == '\\' && !(flags&SplitRetainEscape != 0):
backslash = true
break nonquoteloop
case strings.ContainsRune(separators, rune(c)):
if flags&SplitDontCoalesceSeparators != 0 {
if !(flags&SplitRetainSeparators != 0) {
p++
}
goto finishForceNext
}
if !(flags&SplitRetainSeparators != 0) {
/* Skip additional coalesced separators. */
for ; ; c = nextChar() {
if c == 0 {
goto finishForceTerminate
}
if !strings.ContainsRune(separators, rune(c)) {
break
}
}
}
goto finish
}
s.WriteByte(c)
if quote != 0 {
break nonquoteloop
}
}
}
}
finishForceTerminate:
p = end
finish:
if s.Len() == 0 {
return "", "", false, nil
}
finishForceNext:
return s.String(), in[p:], true, nil
}
func splitStringAppend(appendTo []string, s string, separators string, flags SplitFlags) ([]string, error) {
orig := appendTo
for {
word, remaining, moreWords, err := extractFirstWord(s, separators, flags)
if err != nil {
return orig, err
}
if !moreWords {
break
}
appendTo = append(appendTo, word)
s = remaining
}
return appendTo, nil
}
func splitString(s string, separators string, flags SplitFlags) ([]string, error) {
return splitStringAppend(make([]string, 0), s, separators, flags)
}
func charNeedEscape(c rune, isPath bool) bool {
if c > 128 {
return false /* unicode is ok */
}
pathRune := (isPath && c == '-') ||
(isPath && c == '/')
return unicode.IsSpace(c) ||
unicode.IsControl(c) ||
pathRune ||
c == '"' ||
c == '\'' ||
c == '\\'
}
func wordNeedEscape(word string) bool {
for _, c := range word {
if charNeedEscape(c, false) {
return true
}
}
return false
}
func appendEscapeWord(escaped *strings.Builder, word string) {
escaped.WriteRune('"')
escapeString(escaped, word, false)
escaped.WriteRune('"')
}
func escapeString(escaped *strings.Builder, word string, isPath bool) {
for i, c := range word {
if charNeedEscape(c, isPath) {
switch c {
case '\a':
escaped.WriteString("\\a")
case '\b':
escaped.WriteString("\\b")
case '\n':
escaped.WriteString("\\n")
case '\r':
escaped.WriteString("\\r")
case '\t':
escaped.WriteString("\\t")
case '\v':
escaped.WriteString("\\v")
case '\f':
escaped.WriteString("\\f")
case '\\':
escaped.WriteString("\\\\")
case ' ':
escaped.WriteString(" ")
case '"':
escaped.WriteString("\\\"")
case '\'':
escaped.WriteString("'")
case '/':
if isPath && i != 0 {
escaped.WriteString("-")
}
default:
escaped.WriteString(fmt.Sprintf("\\x%.2x", c))
}
} else {
escaped.WriteRune(c)
}
}
}
func escapeWords(words []string) string {
var escaped strings.Builder
for i, word := range words {
if i != 0 {
escaped.WriteString(" ")
}
if wordNeedEscape(word) {
appendEscapeWord(&escaped, word)
} else {
escaped.WriteString(word)
}
}
return escaped.String()
}