-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
67 lines (56 loc) · 1.16 KB
/
Copy pathutils.go
File metadata and controls
67 lines (56 loc) · 1.16 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
package main
import (
"errors"
"strings"
"unicode"
)
// splitArgs splits a command line string into separate arguments,
// respecting single and double quotes and backslash escaping.
func splitArgs(input string) ([]string, error) {
var args []string
var current strings.Builder
inDoubleQuotes := false
inSingleQuotes := false
escaped := false
for _, r := range input {
if escaped {
current.WriteRune(r)
escaped = false
continue
}
if r == '\\' {
if inSingleQuotes {
current.WriteRune(r)
} else {
escaped = true
}
continue
}
if r == '"' && !inSingleQuotes {
inDoubleQuotes = !inDoubleQuotes
continue
}
if r == '\'' && !inDoubleQuotes {
inSingleQuotes = !inSingleQuotes
continue
}
if unicode.IsSpace(r) && !inDoubleQuotes && !inSingleQuotes {
if current.Len() > 0 {
args = append(args, current.String())
current.Reset()
}
continue
}
current.WriteRune(r)
}
if inDoubleQuotes || inSingleQuotes {
return nil, errors.New("unclosed quotes")
}
if escaped {
return nil, errors.New("trailing backslash")
}
if current.Len() > 0 {
args = append(args, current.String())
}
return args, nil
}