From 1f495bb3c893f449f67453eba8f2ab17d61eafd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 9 May 2023 23:55:29 +0300 Subject: [PATCH] Avoid redundant string splits There likely isn't actually more than once to split in the source strings in these cases, but avoid doing so anyway as we're only interested in the first. --- args.go | 2 +- bash_completions.go | 2 +- completions.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/args.go b/args.go index e79ec33a..b7674c88 100644 --- a/args.go +++ b/args.go @@ -54,7 +54,7 @@ func OnlyValidArgs(cmd *Command, args []string) error { // A description is following a tab character. var validArgs []string for _, v := range cmd.ValidArgs { - validArgs = append(validArgs, strings.Split(v, "\t")[0]) + validArgs = append(validArgs, strings.SplitN(v, "\t", 2)[0]) } for _, v := range args { if !stringInSlice(v, validArgs) { diff --git a/bash_completions.go b/bash_completions.go index 8a531518..be835704 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -621,7 +621,7 @@ func writeRequiredNouns(buf io.StringWriter, cmd *Command) { for _, value := range cmd.ValidArgs { // Remove any description that may be included following a tab character. // Descriptions are not supported by bash completion. - value = strings.Split(value, "\t")[0] + value = strings.SplitN(value, "\t", 2)[0] WriteStringAndCheck(buf, fmt.Sprintf(" must_have_one_noun+=(%q)\n", value)) } if cmd.ValidArgsFunction != nil { diff --git a/completions.go b/completions.go index 55a9f62c..b76e54ad 100644 --- a/completions.go +++ b/completions.go @@ -226,14 +226,14 @@ func (c *Command) initCompleteCmd(args []string) { } if noDescriptions { // Remove any description that may be included following a tab character. - comp = strings.Split(comp, "\t")[0] + comp = strings.SplitN(comp, "\t", 2)[0] } // Make sure we only write the first line to the output. // This is needed if a description contains a linebreak. // Otherwise the shell scripts will interpret the other lines as new flags // and could therefore provide a wrong completion. - comp = strings.Split(comp, "\n")[0] + comp = strings.SplitN(comp, "\n", 2)[0] // Finally trim the completion. This is especially important to get rid // of a trailing tab when there are no description following it.