From 25f5bb5f9702a947581857f29b1deab5a6dee921 Mon Sep 17 00:00:00 2001 From: Hugo Date: Sat, 14 May 2022 22:10:36 +0200 Subject: [PATCH] Prefer ReplaceAll instead of Replace(..., -1) (#1530) --- bash_completions.go | 6 +++--- doc/man_docs.go | 8 ++++---- doc/man_docs_test.go | 6 +++--- doc/md_docs.go | 6 +++--- doc/rest_docs.go | 8 ++++---- doc/yaml_docs.go | 2 +- fish_completions.go | 4 ++-- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index ff3f8f5..3e1e528 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -387,7 +387,7 @@ __%[1]s_handle_word() } func writePostscript(buf io.StringWriter, name string) { - name = strings.Replace(name, ":", "__", -1) + name = strings.ReplaceAll(name, ":", "__") WriteStringAndCheck(buf, fmt.Sprintf("__start_%s()\n", name)) WriteStringAndCheck(buf, fmt.Sprintf(`{ local cur prev words cword split @@ -645,8 +645,8 @@ func gen(buf io.StringWriter, cmd *Command) { gen(buf, c) } commandName := cmd.CommandPath() - commandName = strings.Replace(commandName, " ", "_", -1) - commandName = strings.Replace(commandName, ":", "__", -1) + commandName = strings.ReplaceAll(commandName, " ", "_") + commandName = strings.ReplaceAll(commandName, ":", "__") if cmd.Root() == cmd { WriteStringAndCheck(buf, fmt.Sprintf("_%s_root_command()\n{\n", commandName)) diff --git a/doc/man_docs.go b/doc/man_docs.go index 916e361..8dbce35 100644 --- a/doc/man_docs.go +++ b/doc/man_docs.go @@ -66,7 +66,7 @@ func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error { if opts.CommandSeparator != "" { separator = opts.CommandSeparator } - basename := strings.Replace(cmd.CommandPath(), " ", separator, -1) + basename := strings.ReplaceAll(cmd.CommandPath(), " ", separator) filename := filepath.Join(opts.Path, basename+"."+section) f, err := os.Create(filename) if err != nil { @@ -116,7 +116,7 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error { func fillHeader(header *GenManHeader, name string, disableAutoGen bool) error { if header.Title == "" { - header.Title = strings.ToUpper(strings.Replace(name, " ", "\\-", -1)) + header.Title = strings.ToUpper(strings.ReplaceAll(name, " ", "\\-")) } if header.Section == "" { header.Section = "1" @@ -203,7 +203,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { cmd.InitDefaultHelpFlag() // something like `rootcmd-subcmd1-subcmd2` - dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1) + dashCommandName := strings.ReplaceAll(cmd.CommandPath(), " ", "-") buf := new(bytes.Buffer) @@ -218,7 +218,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte { seealsos := make([]string, 0) if cmd.HasParent() { parentPath := cmd.Parent().CommandPath() - dashParentPath := strings.Replace(parentPath, " ", "-", -1) + dashParentPath := strings.ReplaceAll(parentPath, " ", "-") seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section) seealsos = append(seealsos, seealso) cmd.VisitParents(func(c *cobra.Command) { diff --git a/doc/man_docs_test.go b/doc/man_docs_test.go index f3fe3d6..f7994f8 100644 --- a/doc/man_docs_test.go +++ b/doc/man_docs_test.go @@ -20,7 +20,7 @@ func assertNoErr(t *testing.T, e error) { } func translate(in string) string { - return strings.Replace(in, "-", "\\-", -1) + return strings.ReplaceAll(in, "-", "\\-") } func TestGenManDoc(t *testing.T) { @@ -38,7 +38,7 @@ func TestGenManDoc(t *testing.T) { // Make sure parent has - in CommandPath() in SEE ALSO: parentPath := echoCmd.Parent().CommandPath() - dashParentPath := strings.Replace(parentPath, " ", "-", -1) + dashParentPath := strings.ReplaceAll(parentPath, " ", "-") expected := translate(dashParentPath) expected = expected + "(" + header.Section + ")" checkStringContains(t, output, expected) @@ -73,7 +73,7 @@ func TestGenManNoHiddenParents(t *testing.T) { // Make sure parent has - in CommandPath() in SEE ALSO: parentPath := echoCmd.Parent().CommandPath() - dashParentPath := strings.Replace(parentPath, " ", "-", -1) + dashParentPath := strings.ReplaceAll(parentPath, " ", "-") expected := translate(dashParentPath) expected = expected + "(" + header.Section + ")" checkStringContains(t, output, expected) diff --git a/doc/md_docs.go b/doc/md_docs.go index 5181af8..19d7e93 100644 --- a/doc/md_docs.go +++ b/doc/md_docs.go @@ -83,7 +83,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) parent := cmd.Parent() pname := parent.CommandPath() link := pname + ".md" - link = strings.Replace(link, " ", "_", -1) + link = strings.ReplaceAll(link, " ", "_") buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short)) cmd.VisitParents(func(c *cobra.Command) { if c.DisableAutoGenTag { @@ -101,7 +101,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string) } cname := name + " " + child.Name() link := cname + ".md" - link = strings.Replace(link, " ", "_", -1) + link = strings.ReplaceAll(link, " ", "_") buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short)) } buf.WriteString("\n") @@ -137,7 +137,7 @@ func GenMarkdownTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHa } } - basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".md" + basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".md" filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { diff --git a/doc/rest_docs.go b/doc/rest_docs.go index 051d8dc..23dca16 100644 --- a/doc/rest_docs.go +++ b/doc/rest_docs.go @@ -70,7 +70,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str if len(long) == 0 { long = short } - ref := strings.Replace(name, " ", "_", -1) + ref := strings.ReplaceAll(name, " ", "_") buf.WriteString(".. _" + ref + ":\n\n") buf.WriteString(name + "\n") @@ -99,7 +99,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str if cmd.HasParent() { parent := cmd.Parent() pname := parent.CommandPath() - ref = strings.Replace(pname, " ", "_", -1) + ref = strings.ReplaceAll(pname, " ", "_") buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(pname, ref), parent.Short)) cmd.VisitParents(func(c *cobra.Command) { if c.DisableAutoGenTag { @@ -116,7 +116,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str continue } cname := name + " " + child.Name() - ref = strings.Replace(cname, " ", "_", -1) + ref = strings.ReplaceAll(cname, " ", "_") buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(cname, ref), child.Short)) } buf.WriteString("\n") @@ -151,7 +151,7 @@ func GenReSTTreeCustom(cmd *cobra.Command, dir string, filePrepender func(string } } - basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".rst" + basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".rst" filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { diff --git a/doc/yaml_docs.go b/doc/yaml_docs.go index 96e6ad7..a79fa40 100644 --- a/doc/yaml_docs.go +++ b/doc/yaml_docs.go @@ -66,7 +66,7 @@ func GenYamlTreeCustom(cmd *cobra.Command, dir string, filePrepender, linkHandle } } - basename := strings.Replace(cmd.CommandPath(), " ", "_", -1) + ".yaml" + basename := strings.ReplaceAll(cmd.CommandPath(), " ", "_") + ".yaml" filename := filepath.Join(dir, basename) f, err := os.Create(filename) if err != nil { diff --git a/fish_completions.go b/fish_completions.go index bb57fd5..e14a06e 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -11,8 +11,8 @@ import ( func genFishComp(buf io.StringWriter, name string, includeDesc bool) { // Variables should not contain a '-' or ':' character nameForVar := name - nameForVar = strings.Replace(nameForVar, "-", "_", -1) - nameForVar = strings.Replace(nameForVar, ":", "_", -1) + nameForVar = strings.ReplaceAll(nameForVar, "-", "_") + nameForVar = strings.ReplaceAll(nameForVar, ":", "_") compCmd := ShellCompRequestCmd if !includeDesc {