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

This commit is contained in:
Hugo 2022-05-14 22:10:36 +02:00 committed by GitHub
parent 9e88759b19
commit 25f5bb5f97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 20 additions and 20 deletions

View File

@ -387,7 +387,7 @@ __%[1]s_handle_word()
} }
func writePostscript(buf io.StringWriter, name string) { 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("__start_%s()\n", name))
WriteStringAndCheck(buf, fmt.Sprintf(`{ WriteStringAndCheck(buf, fmt.Sprintf(`{
local cur prev words cword split local cur prev words cword split
@ -645,8 +645,8 @@ func gen(buf io.StringWriter, cmd *Command) {
gen(buf, c) gen(buf, c)
} }
commandName := cmd.CommandPath() commandName := cmd.CommandPath()
commandName = strings.Replace(commandName, " ", "_", -1) commandName = strings.ReplaceAll(commandName, " ", "_")
commandName = strings.Replace(commandName, ":", "__", -1) commandName = strings.ReplaceAll(commandName, ":", "__")
if cmd.Root() == cmd { if cmd.Root() == cmd {
WriteStringAndCheck(buf, fmt.Sprintf("_%s_root_command()\n{\n", commandName)) WriteStringAndCheck(buf, fmt.Sprintf("_%s_root_command()\n{\n", commandName))

View File

@ -66,7 +66,7 @@ func GenManTreeFromOpts(cmd *cobra.Command, opts GenManTreeOptions) error {
if opts.CommandSeparator != "" { if opts.CommandSeparator != "" {
separator = 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) filename := filepath.Join(opts.Path, basename+"."+section)
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { 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 { func fillHeader(header *GenManHeader, name string, disableAutoGen bool) error {
if header.Title == "" { if header.Title == "" {
header.Title = strings.ToUpper(strings.Replace(name, " ", "\\-", -1)) header.Title = strings.ToUpper(strings.ReplaceAll(name, " ", "\\-"))
} }
if header.Section == "" { if header.Section == "" {
header.Section = "1" header.Section = "1"
@ -203,7 +203,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
cmd.InitDefaultHelpFlag() cmd.InitDefaultHelpFlag()
// something like `rootcmd-subcmd1-subcmd2` // something like `rootcmd-subcmd1-subcmd2`
dashCommandName := strings.Replace(cmd.CommandPath(), " ", "-", -1) dashCommandName := strings.ReplaceAll(cmd.CommandPath(), " ", "-")
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
@ -218,7 +218,7 @@ func genMan(cmd *cobra.Command, header *GenManHeader) []byte {
seealsos := make([]string, 0) seealsos := make([]string, 0)
if cmd.HasParent() { if cmd.HasParent() {
parentPath := cmd.Parent().CommandPath() parentPath := cmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1) dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section) seealso := fmt.Sprintf("**%s(%s)**", dashParentPath, header.Section)
seealsos = append(seealsos, seealso) seealsos = append(seealsos, seealso)
cmd.VisitParents(func(c *cobra.Command) { cmd.VisitParents(func(c *cobra.Command) {

View File

@ -20,7 +20,7 @@ func assertNoErr(t *testing.T, e error) {
} }
func translate(in string) string { func translate(in string) string {
return strings.Replace(in, "-", "\\-", -1) return strings.ReplaceAll(in, "-", "\\-")
} }
func TestGenManDoc(t *testing.T) { func TestGenManDoc(t *testing.T) {
@ -38,7 +38,7 @@ func TestGenManDoc(t *testing.T) {
// Make sure parent has - in CommandPath() in SEE ALSO: // Make sure parent has - in CommandPath() in SEE ALSO:
parentPath := echoCmd.Parent().CommandPath() parentPath := echoCmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1) dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
expected := translate(dashParentPath) expected := translate(dashParentPath)
expected = expected + "(" + header.Section + ")" expected = expected + "(" + header.Section + ")"
checkStringContains(t, output, expected) checkStringContains(t, output, expected)
@ -73,7 +73,7 @@ func TestGenManNoHiddenParents(t *testing.T) {
// Make sure parent has - in CommandPath() in SEE ALSO: // Make sure parent has - in CommandPath() in SEE ALSO:
parentPath := echoCmd.Parent().CommandPath() parentPath := echoCmd.Parent().CommandPath()
dashParentPath := strings.Replace(parentPath, " ", "-", -1) dashParentPath := strings.ReplaceAll(parentPath, " ", "-")
expected := translate(dashParentPath) expected := translate(dashParentPath)
expected = expected + "(" + header.Section + ")" expected = expected + "(" + header.Section + ")"
checkStringContains(t, output, expected) checkStringContains(t, output, expected)

View File

@ -83,7 +83,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
parent := cmd.Parent() parent := cmd.Parent()
pname := parent.CommandPath() pname := parent.CommandPath()
link := pname + ".md" 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)) buf.WriteString(fmt.Sprintf("* [%s](%s)\t - %s\n", pname, linkHandler(link), parent.Short))
cmd.VisitParents(func(c *cobra.Command) { cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag { if c.DisableAutoGenTag {
@ -101,7 +101,7 @@ func GenMarkdownCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string)
} }
cname := name + " " + child.Name() cname := name + " " + child.Name()
link := cname + ".md" 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(fmt.Sprintf("* [%s](%s)\t - %s\n", cname, linkHandler(link), child.Short))
} }
buf.WriteString("\n") 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) filename := filepath.Join(dir, basename)
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {

View File

@ -70,7 +70,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
if len(long) == 0 { if len(long) == 0 {
long = short long = short
} }
ref := strings.Replace(name, " ", "_", -1) ref := strings.ReplaceAll(name, " ", "_")
buf.WriteString(".. _" + ref + ":\n\n") buf.WriteString(".. _" + ref + ":\n\n")
buf.WriteString(name + "\n") buf.WriteString(name + "\n")
@ -99,7 +99,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
if cmd.HasParent() { if cmd.HasParent() {
parent := cmd.Parent() parent := cmd.Parent()
pname := parent.CommandPath() 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)) buf.WriteString(fmt.Sprintf("* %s \t - %s\n", linkHandler(pname, ref), parent.Short))
cmd.VisitParents(func(c *cobra.Command) { cmd.VisitParents(func(c *cobra.Command) {
if c.DisableAutoGenTag { if c.DisableAutoGenTag {
@ -116,7 +116,7 @@ func GenReSTCustom(cmd *cobra.Command, w io.Writer, linkHandler func(string, str
continue continue
} }
cname := name + " " + child.Name() 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(fmt.Sprintf("* %s \t - %s\n", linkHandler(cname, ref), child.Short))
} }
buf.WriteString("\n") 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) filename := filepath.Join(dir, basename)
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {

View File

@ -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) filename := filepath.Join(dir, basename)
f, err := os.Create(filename) f, err := os.Create(filename)
if err != nil { if err != nil {

View File

@ -11,8 +11,8 @@ import (
func genFishComp(buf io.StringWriter, name string, includeDesc bool) { func genFishComp(buf io.StringWriter, name string, includeDesc bool) {
// Variables should not contain a '-' or ':' character // Variables should not contain a '-' or ':' character
nameForVar := name nameForVar := name
nameForVar = strings.Replace(nameForVar, "-", "_", -1) nameForVar = strings.ReplaceAll(nameForVar, "-", "_")
nameForVar = strings.Replace(nameForVar, ":", "_", -1) nameForVar = strings.ReplaceAll(nameForVar, ":", "_")
compCmd := ShellCompRequestCmd compCmd := ShellCompRequestCmd
if !includeDesc { if !includeDesc {