Skip to content

Commit e3aa13c

Browse files
aykevldeadprogram
authored andcommitted
all: replace strings.Replace with strings.ReplaceAll
This was an addition to Go 1.13 and results in slightly easier to read code.
1 parent b036674 commit e3aa13c

File tree

4 files changed

+16
-16
lines changed

4 files changed

+16
-16
lines changed

cgo/cgo_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var flagUpdate = flag.Bool("update", false, "Update images based on test output.
2424
// normalizeResult normalizes Go source code that comes out of tests across
2525
// platforms and Go versions.
2626
func normalizeResult(result string) string {
27-
actual := strings.Replace(result, "\r\n", "\n", -1)
27+
actual := strings.ReplaceAll(result, "\r\n", "\n")
2828

2929
// Make sure all functions are wrapped, even those that would otherwise be
3030
// single-line functions. This is necessary because Go 1.14 changed the way
@@ -113,7 +113,7 @@ func TestCGo(t *testing.T) {
113113
if err != nil {
114114
t.Fatalf("could not read expected output: %v", err)
115115
}
116-
expected := strings.Replace(string(expectedBytes), "\r\n", "\n", -1)
116+
expected := strings.ReplaceAll(string(expectedBytes), "\r\n", "\n")
117117

118118
// Check whether the output is as expected.
119119
if expected != actual {
@@ -154,7 +154,7 @@ func formatDiagnostic(err error) string {
154154
msg := err.Error()
155155
if runtime.GOOS == "windows" {
156156
// Fix Windows path slashes.
157-
msg = strings.Replace(msg, "testdata\\", "testdata/", -1)
157+
msg = strings.ReplaceAll(msg, "testdata\\", "testdata/")
158158
}
159159
return "// " + msg + "\n"
160160
}

compileopts/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (c *Config) AutomaticStackSize() bool {
165165
func (c *Config) CFlags() []string {
166166
cflags := append([]string{}, c.Options.CFlags...)
167167
for _, flag := range c.Target.CFlags {
168-
cflags = append(cflags, strings.Replace(flag, "{root}", goenv.Get("TINYGOROOT"), -1))
168+
cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
169169
}
170170
if c.Target.Libc == "picolibc" {
171171
root := goenv.Get("TINYGOROOT")
@@ -186,7 +186,7 @@ func (c *Config) LDFlags() []string {
186186
// Merge and adjust LDFlags.
187187
ldflags := append([]string{}, c.Options.LDFlags...)
188188
for _, flag := range c.Target.LDFlags {
189-
ldflags = append(ldflags, strings.Replace(flag, "{root}", root, -1))
189+
ldflags = append(ldflags, strings.ReplaceAll(flag, "{root}", root))
190190
}
191191
ldflags = append(ldflags, "-L", root)
192192
if c.Target.LinkerScript != "" {

main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
266266
// Create the command.
267267
flashCmd := config.Target.FlashCommand
268268
fileToken := "{" + fileExt[1:] + "}"
269-
flashCmd = strings.Replace(flashCmd, fileToken, result.Binary, -1)
269+
flashCmd = strings.ReplaceAll(flashCmd, fileToken, result.Binary)
270270

271271
if port == "" && strings.Contains(flashCmd, "{port}") {
272272
var err error
@@ -276,7 +276,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
276276
}
277277
}
278278

279-
flashCmd = strings.Replace(flashCmd, "{port}", port, -1)
279+
flashCmd = strings.ReplaceAll(flashCmd, "{port}", port)
280280

281281
// Execute the command.
282282
var cmd *exec.Cmd

tools/gen-device-svd/gen-device-svd.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ type Bitfield struct {
135135

136136
func formatText(text string) string {
137137
text = regexp.MustCompile(`[ \t\n]+`).ReplaceAllString(text, " ") // Collapse whitespace (like in HTML)
138-
text = strings.Replace(text, "\\n ", "\n", -1)
138+
text = strings.ReplaceAll(text, "\\n ", "\n")
139139
text = strings.TrimSpace(text)
140140
return text
141141
}
@@ -273,9 +273,9 @@ func readSVD(path, sourceURL string) (*Device, error) {
273273
p.Registers = append(p.Registers, parseRegister(regName, register, baseAddress, "")...)
274274
}
275275
for _, cluster := range periphEl.Clusters {
276-
clusterName := strings.Replace(cluster.Name, "[%s]", "", -1)
276+
clusterName := strings.ReplaceAll(cluster.Name, "[%s]", "")
277277
if cluster.DimIndex != nil {
278-
clusterName = strings.Replace(clusterName, "%s", "", -1)
278+
clusterName = strings.ReplaceAll(clusterName, "%s", "")
279279
}
280280
clusterPrefix := clusterName + "_"
281281
clusterOffset, err := strconv.ParseUint(cluster.AddressOffset, 0, 32)
@@ -292,7 +292,7 @@ func readSVD(path, sourceURL string) (*Device, error) {
292292
}
293293
// handle sub-clusters of registers
294294
for _, subClusterEl := range cluster.Clusters {
295-
subclusterName := strings.Replace(subClusterEl.Name, "[%s]", "", -1)
295+
subclusterName := strings.ReplaceAll(subClusterEl.Name, "[%s]", "")
296296
subclusterPrefix := subclusterName + "_"
297297
subclusterOffset, err := strconv.ParseUint(subClusterEl.AddressOffset, 0, 32)
298298
if err != nil {
@@ -414,7 +414,7 @@ func readSVD(path, sourceURL string) (*Device, error) {
414414
// Properly format the license block, with comments.
415415
licenseBlock := ""
416416
if text := formatText(device.LicenseText); text != "" {
417-
licenseBlock = "// " + strings.Replace(text, "\n", "\n// ", -1)
417+
licenseBlock = "// " + strings.ReplaceAll(text, "\n", "\n// ")
418418
licenseBlock = regexp.MustCompile(`\s+\n`).ReplaceAllString(licenseBlock, "\n")
419419
}
420420

@@ -595,7 +595,7 @@ func parseBitfields(groupName, regName string, fieldEls []*SVDField, bitfieldPre
595595
if err != nil {
596596
if enumBitSpecifier.MatchString(enumEl.Value) {
597597
// NXP SVDs use the form #xx1x, #x0xx, etc for values
598-
enumValue, err = strconv.ParseUint(strings.Replace(enumEl.Value[1:], "x", "0", -1), 2, 32)
598+
enumValue, err = strconv.ParseUint(strings.ReplaceAll(enumEl.Value[1:], "x", "0"), 2, 32)
599599
if err != nil {
600600
panic(err)
601601
}
@@ -656,7 +656,7 @@ func NewRegister(element *SVDRegister, baseAddress uint64) *Register {
656656
}
657657

658658
func (r *Register) name() string {
659-
return strings.Replace(r.element.Name, "[%s]", "", -1)
659+
return strings.ReplaceAll(r.element.Name, "[%s]", "")
660660
}
661661

662662
func (r *Register) description() string {
@@ -765,15 +765,15 @@ func parseRegister(groupName string, regEl *SVDRegister, baseAddress uint64, bit
765765
for i, j := range reg.dimIndex() {
766766
regAddress := reg.address() + (uint64(i) * dimIncrement)
767767
results = append(results, &PeripheralField{
768-
Name: strings.ToUpper(strings.Replace(reg.name(), "%s", j, -1)),
768+
Name: strings.ToUpper(strings.ReplaceAll(reg.name(), "%s", j)),
769769
Address: regAddress,
770770
Description: reg.description(),
771771
Array: -1,
772772
ElementSize: reg.size(),
773773
})
774774
}
775775
// set first result bitfield
776-
shortName := strings.ToUpper(strings.Replace(strings.Replace(reg.name(), "_%s", "", -1), "%s", "", -1))
776+
shortName := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(reg.name(), "_%s", ""), "%s", ""))
777777
results[0].Bitfields = parseBitfields(groupName, shortName, regEl.Fields, bitfieldPrefix)
778778
return results
779779
}

0 commit comments

Comments
 (0)