Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer ReplaceAll instead of Replace(..., -1) #1530

Merged
merged 1 commit into from
May 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bash_completions.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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