Skip to content

Commit

Permalink
Prefer ReplaceAll instead of Replace(..., -1) (#1530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugo committed May 14, 2022
1 parent 9e88759 commit 25f5bb5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
6 changes: 3 additions & 3 deletions bash_completions.go
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
8 changes: 4 additions & 4 deletions doc/man_docs.go
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions doc/man_docs_test.go
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions doc/md_docs.go
Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions doc/rest_docs.go
Expand Up @@ -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")
Expand Down Expand Up @@ -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 {
Expand All @@ -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")
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion doc/yaml_docs.go
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions fish_completions.go
Expand Up @@ -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 {
Expand Down

0 comments on commit 25f5bb5

Please sign in to comment.