commit bcc3095
Hubert Hirtz
·
2021-05-20 19:48:11 +0000 UTC
parent 9e679e2
Handle consecutive spaces on arg split
1 files changed,
+46,
-1
+46,
-1
1@@ -358,6 +358,51 @@ func commandDoTopic(app *App, buffer string, args []string) (err error) {
2 return
3 }
4
5+// implemented from https://golang.org/src/strings/strings.go?s=8055:8085#L310
6+func fieldsN(s string, n int) []string {
7+ s = strings.TrimSpace(s)
8+ if s == "" || n == 0 {
9+ return nil
10+ }
11+ if n == 1 {
12+ return []string{s}
13+ }
14+ n--
15+ // Start of the ASCII fast path.
16+ var a []string
17+ na := 0
18+ fieldStart := 0
19+ i := 0
20+ // Skip spaces in front of the input.
21+ for i < len(s) && s[i] == ' ' {
22+ i++
23+ }
24+ fieldStart = i
25+ for i < len(s) {
26+ if s[i] != ' ' {
27+ i++
28+ continue
29+ }
30+ a = append(a, s[fieldStart:i])
31+ na++
32+ i++
33+ // Skip spaces in between fields.
34+ for i < len(s) && s[i] == ' ' {
35+ i++
36+ }
37+ fieldStart = i
38+ if n <= na {
39+ a = append(a, s[fieldStart:])
40+ return a
41+ }
42+ }
43+ if fieldStart < len(s) {
44+ // Last field ends at EOF.
45+ a = append(a, s[fieldStart:])
46+ }
47+ return a
48+}
49+
50 func parseCommand(s string) (command, args string, isCommand bool) {
51 if s == "" {
52 return "", "", false
53@@ -416,7 +461,7 @@ func (app *App) handleInput(buffer, content string) error {
54
55 var args []string
56 if rawArgs != "" && cmd.MaxArgs != 0 {
57- args = strings.SplitN(rawArgs, " ", cmd.MaxArgs)
58+ args = fieldsN(rawArgs, cmd.MaxArgs)
59 }
60
61 if len(args) < cmd.MinArgs {