diff --git a/client/commands.go b/client/commands.go index e33ee42..de46aa1 100644 --- a/client/commands.go +++ b/client/commands.go @@ -85,25 +85,21 @@ func splitMessage(msg string, splitLen int) (msgs []string) { return append(msgs, msg) } -// splitCommand takes a command of the form -// and returns a list of commands of the same form -// where the len of each command doesn't exceed splitLen -func splitCommand(cmdPrefix string, args []string, maxLen int) []string { - cmds := make([]string, 0) +func splitArgs(args []string, maxLen int) []string { + res := make([]string, 0) i := 0 for i < len(args) { - currCmd := cmdPrefix + args[i] + currArg := args[i] i++ - for i < len(args) && len(currCmd)+len(args[i])+1 < maxLen { - currCmd += " " + args[i] + for i < len(args) && len(currArg)+len(args[i])+1 < maxLen { + currArg += " " + args[i] i++ } - cmds = append(cmds, currCmd) - currCmd = cmdPrefix + res = append(res, currArg) } - return cmds + return res } // Raw sends a raw line to the server, should really only be used for @@ -324,8 +320,9 @@ func (conn *Conn) Cap(subcommmand string, capabilities ...string) { // make output predictable for testing sort.Strings(capabilities) - for _, cmd := range splitCommand(CAP+" "+subcommmand+" :", capabilities, defaultSplit) { - conn.Raw(cmd) + cmdPrefix := CAP + " " + subcommmand + " :" + for _, args := range splitArgs(capabilities, defaultSplit-len(cmdPrefix)) { + conn.Raw(cmdPrefix + args) } } } diff --git a/client/commands_test.go b/client/commands_test.go index f23b0ab..25af371 100644 --- a/client/commands_test.go +++ b/client/commands_test.go @@ -3,6 +3,7 @@ package client import ( "reflect" "strconv" + "strings" "testing" ) @@ -214,12 +215,9 @@ func TestSplitCommand(t *testing.T) { } for maxLen := 1; maxLen <= defaultSplit; maxLen *= 2 { - for _, cmd := range splitCommand("TEST :", args, maxLen) { - if len(cmd) > maxLen { - line := ParseLine(cmd) - if len(line.Args) > 1 { // len(cmd) can exceed maxLen only if cmd includes a single argument. - t.Fatalf("maxLen = %d, but len(cmd) = %d", maxLen, len(cmd)) - } + for _, argStr := range splitArgs(args, maxLen) { + if len(argStr) > maxLen && len(strings.Split(argStr, " ")) > 1 { + t.Errorf("maxLen = %d, but len(cmd) = %d", maxLen, len(argStr)) } } }