-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathhelper.go
78 lines (65 loc) · 1.27 KB
/
helper.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
package pongo
import (
"strings"
)
func splitArgs(in *string, sep string) *[]string {
if in == nil {
panic("Implementation error; parseArgs got a nil string as input. Please report this issue.")
}
if len(sep) != 1 {
panic("Separator must be exactly one char (string of length 1).")
}
res := make([]string, 0, strings.Count(*in, sep)+1) // approx count(sep)+1 args
escaped := false
in_string := false
pos := 0
buf := *in
argbuf := ""
pc := ""
for pos < len(buf) {
c := buf[pos : pos+1]
if pos > 0 {
pc = buf[pos-1 : pos]
}
// TODO: Handle string escape correctly (e. g. "this is \"nice\""), still too lazy to do
if pc == "\\" {
escaped = true
} else {
escaped = false
}
if c == "\"" && !escaped {
if in_string {
// String end
in_string = false
// We go a string, now add it to res
argbuf += buf[:pos+1]
buf = buf[pos+1:]
pos = 0
} else {
// String found
in_string = true
pos++
}
continue
}
if in_string {
pos++
continue
}
if c == sep {
// seperator found, add new arg
res = append(res, argbuf)
argbuf = ""
buf = buf[pos+1:]
pos = 0
continue
}
argbuf += c
pos++
}
// Is there a last argument?
if len(argbuf) > 0 {
res = append(res, argbuf)
}
return &res
}