blob: 08863416a34a1b5ae05397f75d0ad21816551f4e (
plain)
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 tokener
type Tokener struct {
quotes []byte
escaped bool
fixed string
space bool
}
// Visit visit a byte and update the state of the quotes.
// It returns true if the byte was quotes or escape character.
func (t *Tokener) Visit(b byte) {
// Check space.
if b == ' ' {
if !t.escaped && !t.Quoted() {
t.space = true
}
} else {
t.space = false
}
// Check escaping
if b == '\\' {
t.escaped = !t.escaped
} else {
defer func() { t.escaped = false }()
}
// Check quotes.
if !t.escaped && (b == '"' || b == '\'') {
if t.Quoted() && t.quotes[len(t.quotes)-1] == b {
t.quotes = t.quotes[:len(t.quotes)-1]
} else {
t.quotes = append(t.quotes, b)
}
}
// If not quoted, insert escape before inserting space.
if t.LastSpace() {
t.fixed += "\\"
}
t.fixed += string(b)
}
func (t *Tokener) Escaped() bool {
return t.escaped
}
func (t *Tokener) Quoted() bool {
return len(t.quotes) > 0
}
func (t *Tokener) Fixed() string {
return t.fixed
}
func (t *Tokener) Closed() string {
fixed := t.fixed
for i := len(t.quotes) - 1; i >= 0; i-- {
fixed += string(t.quotes[i])
}
return fixed
}
func (t *Tokener) LastSpace() bool {
return t.space
}
|