From 8ef504f745e686cadeac14c61d9ae3f55279fd7a Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Wed, 17 Mar 2021 23:12:26 +0100 Subject: [PATCH] all: Simplify some method calls (#7499) strings.Replace(a, b, c, -1) -> strings.ReplaceAll(a, b, c) (Go 1.12) and who knows what was up with that dialQueue.Sort() thing. --- cmd/stvanity/main.go | 2 +- cmd/ursrv/main.go | 4 ++-- lib/connections/quic_listen.go | 4 ++-- lib/connections/service.go | 2 +- lib/fs/filesystem.go | 3 +-- lib/locations/locations.go | 4 ++-- lib/model/model.go | 2 +- lib/protocol/deviceid.go | 10 +++++----- lib/upnp/upnp.go | 2 +- lib/versioner/external.go | 4 ++-- 10 files changed, 18 insertions(+), 19 deletions(-) diff --git a/cmd/stvanity/main.go b/cmd/stvanity/main.go index 1ab5cd008..128f35300 100644 --- a/cmd/stvanity/main.go +++ b/cmd/stvanity/main.go @@ -37,7 +37,7 @@ type result struct { func main() { flag.Parse() - prefix := strings.ToUpper(strings.Replace(flag.Arg(0), "-", "", -1)) + prefix := strings.ToUpper(strings.ReplaceAll(flag.Arg(0), "-", "")) if len(prefix) > 7 { prefix = prefix[:7] + "-" + prefix[7:] } diff --git a/cmd/ursrv/main.go b/cmd/ursrv/main.go index 9d126077c..6cbf44299 100644 --- a/cmd/ursrv/main.go +++ b/cmd/ursrv/main.go @@ -725,8 +725,8 @@ func getReport(db *sql.DB) map[string]interface{} { if rep.NATType != "" { natType := rep.NATType - natType = strings.Replace(natType, "unknown", "Unknown", -1) - natType = strings.Replace(natType, "Symetric", "Symmetric", -1) + natType = strings.ReplaceAll(natType, "unknown", "Unknown") + natType = strings.ReplaceAll(natType, "Symetric", "Symmetric") add(featureGroups["Various"]["v3"], "NAT Type", natType, 1) } diff --git a/lib/connections/quic_listen.go b/lib/connections/quic_listen.go index ada6429ca..e8bd8d111 100644 --- a/lib/connections/quic_listen.go +++ b/lib/connections/quic_listen.go @@ -79,7 +79,7 @@ func (t *quicListener) OnExternalAddressChanged(address *stun.Host, via string) } func (t *quicListener) serve(ctx context.Context) error { - network := strings.Replace(t.uri.Scheme, "quic", "udp", -1) + network := strings.ReplaceAll(t.uri.Scheme, "quic", "udp") packetConn, err := net.ListenPacket(network, t.uri.Host) if err != nil { @@ -174,7 +174,7 @@ func (t *quicListener) WANAddresses() []*url.URL { func (t *quicListener) LANAddresses() []*url.URL { addrs := []*url.URL{t.uri} - network := strings.Replace(t.uri.Scheme, "quic", "udp", -1) + network := strings.ReplaceAll(t.uri.Scheme, "quic", "udp") addrs = append(addrs, getURLsForAllAdaptersIfUnspecified(network, t.uri)...) return addrs } diff --git a/lib/connections/service.go b/lib/connections/service.go index 95187fa5c..1f036f58f 100644 --- a/lib/connections/service.go +++ b/lib/connections/service.go @@ -478,7 +478,7 @@ func (s *service) dialDevices(ctx context.Context, now time.Time, cfg config.Con // doesn't have much effect, but it may result in getting up and running // quicker if only a subset of configured devices are actually reachable // (by prioritizing those that were reachable recently). - dialQueue.Sort(queue) + queue.Sort() // Perform dials according to the queue, stopping when we've reached the // allowed additional number of connections (if limited). diff --git a/lib/fs/filesystem.go b/lib/fs/filesystem.go index fb8d51d70..506ed71b5 100644 --- a/lib/fs/filesystem.go +++ b/lib/fs/filesystem.go @@ -252,8 +252,7 @@ func Canonicalize(file string) (string, error) { file = filepath.Clean(file) // It is not acceptable to attempt to traverse upwards. - switch file { - case "..": + if file == ".." { return "", errNotRelative } if strings.HasPrefix(file, ".."+pathSep) { diff --git a/lib/locations/locations.go b/lib/locations/locations.go index f6c497812..3df0b877f 100644 --- a/lib/locations/locations.go +++ b/lib/locations/locations.go @@ -115,7 +115,7 @@ func expandLocations() error { newLocations := make(map[LocationEnum]string) for key, dir := range locationTemplates { for varName, value := range baseDirs { - dir = strings.Replace(dir, "${"+string(varName)+"}", value, -1) + dir = strings.ReplaceAll(dir, "${"+string(varName)+"}", value) } var err error dir, err = fs.ExpandTilde(dir) @@ -197,5 +197,5 @@ func GetTimestamped(key LocationEnum) string { // 2006 replaced by 2015... tpl := locations[key] now := time.Now().Format("20060102-150405") - return strings.Replace(tpl, "${timestamp}", now, -1) + return strings.ReplaceAll(tpl, "${timestamp}", now) } diff --git a/lib/model/model.go b/lib/model/model.go index b5ef20016..8f65ae373 100644 --- a/lib/model/model.go +++ b/lib/model/model.go @@ -692,7 +692,7 @@ func (m *model) UsageReportingStats(report *contract.Report, version int, previe if strings.Contains(line, "**") { report.IgnoreStats.DoubleStars++ // Remove not to trip up star checks. - line = strings.Replace(line, "**", "", -1) + line = strings.ReplaceAll(line, "**", "") } if strings.Contains(line, "*") { diff --git a/lib/protocol/deviceid.go b/lib/protocol/deviceid.go index 00f805967..7dd6efaf3 100644 --- a/lib/protocol/deviceid.go +++ b/lib/protocol/deviceid.go @@ -203,15 +203,15 @@ func chunkify(s string) string { } func unchunkify(s string) string { - s = strings.Replace(s, "-", "", -1) - s = strings.Replace(s, " ", "", -1) + s = strings.ReplaceAll(s, "-", "") + s = strings.ReplaceAll(s, " ", "") return s } func untypeoify(s string) string { - s = strings.Replace(s, "0", "O", -1) - s = strings.Replace(s, "1", "I", -1) - s = strings.Replace(s, "8", "B", -1) + s = strings.ReplaceAll(s, "0", "O") + s = strings.ReplaceAll(s, "1", "I") + s = strings.ReplaceAll(s, "8", "B") return s } diff --git a/lib/upnp/upnp.go b/lib/upnp/upnp.go index b18be59ac..72fc22fed 100644 --- a/lib/upnp/upnp.go +++ b/lib/upnp/upnp.go @@ -156,7 +156,7 @@ USER-AGENT: syncthing/1.0 ` searchStr := fmt.Sprintf(tpl, deviceType, timeout/time.Second) - search := []byte(strings.Replace(searchStr, "\n", "\r\n", -1) + "\r\n") + search := []byte(strings.ReplaceAll(searchStr, "\n", "\r\n") + "\r\n") l.Debugln("Starting discovery of device type", deviceType, "on", intf.Name) diff --git a/lib/versioner/external.go b/lib/versioner/external.go index 58a7ecbf9..b39d4044b 100644 --- a/lib/versioner/external.go +++ b/lib/versioner/external.go @@ -36,7 +36,7 @@ func newExternal(cfg config.FolderConfiguration) Versioner { command := cfg.Versioning.Params["command"] if runtime.GOOS == "windows" { - command = strings.Replace(command, `\`, `\\`, -1) + command = strings.ReplaceAll(command, `\`, `\\`) } s := external{ @@ -81,7 +81,7 @@ func (v external) Archive(filePath string) error { for i, word := range words { for key, val := range context { - word = strings.Replace(word, key, val, -1) + word = strings.ReplaceAll(word, key, val) } words[i] = word