all: replace strings.Replace with strings.ReplaceAll

This was an addition to Go 1.13 and results in slightly easier to read
code.
Этот коммит содержится в:
Ayke van Laethem 2021-03-04 21:39:09 +01:00 коммит произвёл Ron Evans
родитель b0366743cd
коммит e3aa13c2a6
4 изменённых файлов: 16 добавлений и 16 удалений

Просмотреть файл

@ -24,7 +24,7 @@ var flagUpdate = flag.Bool("update", false, "Update images based on test output.
// normalizeResult normalizes Go source code that comes out of tests across // normalizeResult normalizes Go source code that comes out of tests across
// platforms and Go versions. // platforms and Go versions.
func normalizeResult(result string) string { func normalizeResult(result string) string {
actual := strings.Replace(result, "\r\n", "\n", -1) actual := strings.ReplaceAll(result, "\r\n", "\n")
// Make sure all functions are wrapped, even those that would otherwise be // Make sure all functions are wrapped, even those that would otherwise be
// single-line functions. This is necessary because Go 1.14 changed the way // single-line functions. This is necessary because Go 1.14 changed the way
@ -113,7 +113,7 @@ func TestCGo(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("could not read expected output: %v", err) t.Fatalf("could not read expected output: %v", err)
} }
expected := strings.Replace(string(expectedBytes), "\r\n", "\n", -1) expected := strings.ReplaceAll(string(expectedBytes), "\r\n", "\n")
// Check whether the output is as expected. // Check whether the output is as expected.
if expected != actual { if expected != actual {
@ -154,7 +154,7 @@ func formatDiagnostic(err error) string {
msg := err.Error() msg := err.Error()
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
// Fix Windows path slashes. // Fix Windows path slashes.
msg = strings.Replace(msg, "testdata\\", "testdata/", -1) msg = strings.ReplaceAll(msg, "testdata\\", "testdata/")
} }
return "// " + msg + "\n" return "// " + msg + "\n"
} }

Просмотреть файл

@ -165,7 +165,7 @@ func (c *Config) AutomaticStackSize() bool {
func (c *Config) CFlags() []string { func (c *Config) CFlags() []string {
cflags := append([]string{}, c.Options.CFlags...) cflags := append([]string{}, c.Options.CFlags...)
for _, flag := range c.Target.CFlags { for _, flag := range c.Target.CFlags {
cflags = append(cflags, strings.Replace(flag, "{root}", goenv.Get("TINYGOROOT"), -1)) cflags = append(cflags, strings.ReplaceAll(flag, "{root}", goenv.Get("TINYGOROOT")))
} }
if c.Target.Libc == "picolibc" { if c.Target.Libc == "picolibc" {
root := goenv.Get("TINYGOROOT") root := goenv.Get("TINYGOROOT")
@ -186,7 +186,7 @@ func (c *Config) LDFlags() []string {
// Merge and adjust LDFlags. // Merge and adjust LDFlags.
ldflags := append([]string{}, c.Options.LDFlags...) ldflags := append([]string{}, c.Options.LDFlags...)
for _, flag := range c.Target.LDFlags { for _, flag := range c.Target.LDFlags {
ldflags = append(ldflags, strings.Replace(flag, "{root}", root, -1)) ldflags = append(ldflags, strings.ReplaceAll(flag, "{root}", root))
} }
ldflags = append(ldflags, "-L", root) ldflags = append(ldflags, "-L", root)
if c.Target.LinkerScript != "" { if c.Target.LinkerScript != "" {

Просмотреть файл

@ -266,7 +266,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
// Create the command. // Create the command.
flashCmd := config.Target.FlashCommand flashCmd := config.Target.FlashCommand
fileToken := "{" + fileExt[1:] + "}" fileToken := "{" + fileExt[1:] + "}"
flashCmd = strings.Replace(flashCmd, fileToken, result.Binary, -1) flashCmd = strings.ReplaceAll(flashCmd, fileToken, result.Binary)
if port == "" && strings.Contains(flashCmd, "{port}") { if port == "" && strings.Contains(flashCmd, "{port}") {
var err error var err error
@ -276,7 +276,7 @@ func Flash(pkgName, port string, options *compileopts.Options) error {
} }
} }
flashCmd = strings.Replace(flashCmd, "{port}", port, -1) flashCmd = strings.ReplaceAll(flashCmd, "{port}", port)
// Execute the command. // Execute the command.
var cmd *exec.Cmd var cmd *exec.Cmd

Просмотреть файл

@ -135,7 +135,7 @@ type Bitfield struct {
func formatText(text string) string { func formatText(text string) string {
text = regexp.MustCompile(`[ \t\n]+`).ReplaceAllString(text, " ") // Collapse whitespace (like in HTML) text = regexp.MustCompile(`[ \t\n]+`).ReplaceAllString(text, " ") // Collapse whitespace (like in HTML)
text = strings.Replace(text, "\\n ", "\n", -1) text = strings.ReplaceAll(text, "\\n ", "\n")
text = strings.TrimSpace(text) text = strings.TrimSpace(text)
return text return text
} }
@ -273,9 +273,9 @@ func readSVD(path, sourceURL string) (*Device, error) {
p.Registers = append(p.Registers, parseRegister(regName, register, baseAddress, "")...) p.Registers = append(p.Registers, parseRegister(regName, register, baseAddress, "")...)
} }
for _, cluster := range periphEl.Clusters { for _, cluster := range periphEl.Clusters {
clusterName := strings.Replace(cluster.Name, "[%s]", "", -1) clusterName := strings.ReplaceAll(cluster.Name, "[%s]", "")
if cluster.DimIndex != nil { if cluster.DimIndex != nil {
clusterName = strings.Replace(clusterName, "%s", "", -1) clusterName = strings.ReplaceAll(clusterName, "%s", "")
} }
clusterPrefix := clusterName + "_" clusterPrefix := clusterName + "_"
clusterOffset, err := strconv.ParseUint(cluster.AddressOffset, 0, 32) clusterOffset, err := strconv.ParseUint(cluster.AddressOffset, 0, 32)
@ -292,7 +292,7 @@ func readSVD(path, sourceURL string) (*Device, error) {
} }
// handle sub-clusters of registers // handle sub-clusters of registers
for _, subClusterEl := range cluster.Clusters { for _, subClusterEl := range cluster.Clusters {
subclusterName := strings.Replace(subClusterEl.Name, "[%s]", "", -1) subclusterName := strings.ReplaceAll(subClusterEl.Name, "[%s]", "")
subclusterPrefix := subclusterName + "_" subclusterPrefix := subclusterName + "_"
subclusterOffset, err := strconv.ParseUint(subClusterEl.AddressOffset, 0, 32) subclusterOffset, err := strconv.ParseUint(subClusterEl.AddressOffset, 0, 32)
if err != nil { if err != nil {
@ -414,7 +414,7 @@ func readSVD(path, sourceURL string) (*Device, error) {
// Properly format the license block, with comments. // Properly format the license block, with comments.
licenseBlock := "" licenseBlock := ""
if text := formatText(device.LicenseText); text != "" { if text := formatText(device.LicenseText); text != "" {
licenseBlock = "// " + strings.Replace(text, "\n", "\n// ", -1) licenseBlock = "// " + strings.ReplaceAll(text, "\n", "\n// ")
licenseBlock = regexp.MustCompile(`\s+\n`).ReplaceAllString(licenseBlock, "\n") licenseBlock = regexp.MustCompile(`\s+\n`).ReplaceAllString(licenseBlock, "\n")
} }
@ -595,7 +595,7 @@ func parseBitfields(groupName, regName string, fieldEls []*SVDField, bitfieldPre
if err != nil { if err != nil {
if enumBitSpecifier.MatchString(enumEl.Value) { if enumBitSpecifier.MatchString(enumEl.Value) {
// NXP SVDs use the form #xx1x, #x0xx, etc for values // NXP SVDs use the form #xx1x, #x0xx, etc for values
enumValue, err = strconv.ParseUint(strings.Replace(enumEl.Value[1:], "x", "0", -1), 2, 32) enumValue, err = strconv.ParseUint(strings.ReplaceAll(enumEl.Value[1:], "x", "0"), 2, 32)
if err != nil { if err != nil {
panic(err) panic(err)
} }
@ -656,7 +656,7 @@ func NewRegister(element *SVDRegister, baseAddress uint64) *Register {
} }
func (r *Register) name() string { func (r *Register) name() string {
return strings.Replace(r.element.Name, "[%s]", "", -1) return strings.ReplaceAll(r.element.Name, "[%s]", "")
} }
func (r *Register) description() string { func (r *Register) description() string {
@ -765,7 +765,7 @@ func parseRegister(groupName string, regEl *SVDRegister, baseAddress uint64, bit
for i, j := range reg.dimIndex() { for i, j := range reg.dimIndex() {
regAddress := reg.address() + (uint64(i) * dimIncrement) regAddress := reg.address() + (uint64(i) * dimIncrement)
results = append(results, &PeripheralField{ results = append(results, &PeripheralField{
Name: strings.ToUpper(strings.Replace(reg.name(), "%s", j, -1)), Name: strings.ToUpper(strings.ReplaceAll(reg.name(), "%s", j)),
Address: regAddress, Address: regAddress,
Description: reg.description(), Description: reg.description(),
Array: -1, Array: -1,
@ -773,7 +773,7 @@ func parseRegister(groupName string, regEl *SVDRegister, baseAddress uint64, bit
}) })
} }
// set first result bitfield // set first result bitfield
shortName := strings.ToUpper(strings.Replace(strings.Replace(reg.name(), "_%s", "", -1), "%s", "", -1)) shortName := strings.ToUpper(strings.ReplaceAll(strings.ReplaceAll(reg.name(), "_%s", ""), "%s", ""))
results[0].Bitfields = parseBitfields(groupName, shortName, regEl.Fields, bitfieldPrefix) results[0].Bitfields = parseBitfields(groupName, shortName, regEl.Fields, bitfieldPrefix)
return results return results
} }