diff options
author | Hubert Hirtz <hubert@hirtz.pm> | 2021-05-20 21:48:11 +0200 |
---|---|---|
committer | Hubert Hirtz <hubert@hirtz.pm> | 2021-05-20 22:45:48 +0200 |
commit | bcc309519e6ecd58cd29199fd1acf4321522eb25 (patch) | |
tree | 1c5cdc22c2d420211a3824e1b20e6175eb8b82df /commands.go | |
parent | Explicitly support keys in JOIN messages (diff) |
Handle consecutive spaces on arg split
Diffstat (limited to 'commands.go')
-rw-r--r-- | commands.go | 47 |
1 files changed, 46 insertions, 1 deletions
diff --git a/commands.go b/commands.go index a0cd411..6190ed8 100644 --- a/commands.go +++ b/commands.go @@ -358,6 +358,51 @@ func commandDoTopic(app *App, buffer string, args []string) (err error) { return } +// implemented from https://golang.org/src/strings/strings.go?s=8055:8085#L310 +func fieldsN(s string, n int) []string { + s = strings.TrimSpace(s) + if s == "" || n == 0 { + return nil + } + if n == 1 { + return []string{s} + } + n-- + // Start of the ASCII fast path. + var a []string + na := 0 + fieldStart := 0 + i := 0 + // Skip spaces in front of the input. + for i < len(s) && s[i] == ' ' { + i++ + } + fieldStart = i + for i < len(s) { + if s[i] != ' ' { + i++ + continue + } + a = append(a, s[fieldStart:i]) + na++ + i++ + // Skip spaces in between fields. + for i < len(s) && s[i] == ' ' { + i++ + } + fieldStart = i + if n <= na { + a = append(a, s[fieldStart:]) + return a + } + } + if fieldStart < len(s) { + // Last field ends at EOF. + a = append(a, s[fieldStart:]) + } + return a +} + func parseCommand(s string) (command, args string, isCommand bool) { if s == "" { return "", "", false @@ -416,7 +461,7 @@ func (app *App) handleInput(buffer, content string) error { var args []string if rawArgs != "" && cmd.MaxArgs != 0 { - args = strings.SplitN(rawArgs, " ", cmd.MaxArgs) + args = fieldsN(rawArgs, cmd.MaxArgs) } if len(args) < cmd.MinArgs { |