Skip to content

Commit 6083fe4

Browse files
apocelipespull[bot]
authored andcommitted
cmd,log,net,runtime: simplify string prefix and suffix processing
Use the TrimPrefix, TrimSuffix and CutPrefix to simplify the code. Change-Id: I3e2b271ec0d3f9ce664b830e2b0c21ab47337ed0 GitHub-Last-Rev: 4bd1577 GitHub-Pull-Request: #68629 Reviewed-on: https://go-review.googlesource.com/c/go/+/601675 LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Ian Lance Taylor <[email protected]> Reviewed-by: Michael Knyszek <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 625722d commit 6083fe4

File tree

8 files changed

+11
-29
lines changed

8 files changed

+11
-29
lines changed

src/cmd/cgo/out.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,9 +378,7 @@ func dynimport(obj string) {
378378
defer f.Close()
379379
sym, _ := f.ImportedSymbols()
380380
for _, s := range sym {
381-
if len(s) > 0 && s[0] == '_' {
382-
s = s[1:]
383-
}
381+
s = strings.TrimPrefix(s, "_")
384382
checkImportSymName(s)
385383
fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "")
386384
}

src/cmd/compile/internal/types2/resolver.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,7 @@ func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
167167
if imp == nil {
168168
// create a new fake package
169169
// come up with a sensible package name (heuristic)
170-
name := path
171-
if i := len(name); i > 0 && name[i-1] == '/' {
172-
name = name[:i-1]
173-
}
170+
name := strings.TrimSuffix(path, "/")
174171
if i := strings.LastIndex(name, "/"); i >= 0 {
175172
name = name[i+1:]
176173
}

src/cmd/go/internal/load/pkg.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2076,11 +2076,7 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
20762076
for _, pattern = range patterns {
20772077
pid++
20782078

2079-
glob := pattern
2080-
all := strings.HasPrefix(pattern, "all:")
2081-
if all {
2082-
glob = pattern[len("all:"):]
2083-
}
2079+
glob, all := strings.CutPrefix(pattern, "all:")
20842080
// Check pattern is valid for //go:embed.
20852081
if _, err := pathpkg.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
20862082
return nil, nil, fmt.Errorf("invalid pattern syntax")

src/cmd/go/internal/work/gc.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,10 +248,7 @@ func (a *Action) trimpath() string {
248248
// same situations.
249249

250250
// Strip the object directory entirely.
251-
objdir := a.Objdir
252-
if len(objdir) > 1 && objdir[len(objdir)-1] == filepath.Separator {
253-
objdir = objdir[:len(objdir)-1]
254-
}
251+
objdir := strings.TrimSuffix(a.Objdir, string(filepath.Separator))
255252
rewrite := ""
256253

257254
rewriteDir := a.Package.Dir

src/log/slog/logger.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package slog
66

77
import (
8+
"bytes"
89
"context"
910
"log"
1011
loginternal "log/internal"
@@ -96,9 +97,7 @@ func (w *handlerWriter) Write(buf []byte) (int, error) {
9697

9798
// Remove final newline.
9899
origLen := len(buf) // Report that the entire buf was written.
99-
if len(buf) > 0 && buf[len(buf)-1] == '\n' {
100-
buf = buf[:len(buf)-1]
101-
}
100+
buf = bytes.TrimSuffix(buf, []byte{'\n'})
102101
r := NewRecord(time.Now(), level, string(buf), pc)
103102
return origLen, w.h.Handle(context.Background(), r)
104103
}

src/net/dnsclient_unix.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"internal/bytealg"
1919
"internal/godebug"
2020
"internal/itoa"
21+
"internal/stringslite"
2122
"io"
2223
"os"
2324
"runtime"
@@ -487,9 +488,7 @@ func avoidDNS(name string) bool {
487488
if name == "" {
488489
return true
489490
}
490-
if name[len(name)-1] == '.' {
491-
name = name[:len(name)-1]
492-
}
491+
name = stringslite.TrimSuffix(name, ".")
493492
return stringsHasSuffixFold(name, ".onion")
494493
}
495494

src/net/http/cookiejar/jar.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -500,9 +500,7 @@ func (j *Jar) domainAndType(host, domain string) (string, bool, error) {
500500
// From here on: If the cookie is valid, it is a domain cookie (with
501501
// the one exception of a public suffix below).
502502
// See RFC 6265 section 5.2.3.
503-
if domain[0] == '.' {
504-
domain = domain[1:]
505-
}
503+
domain = strings.TrimPrefix(domain, ".")
506504

507505
if len(domain) == 0 || domain[0] == '.' {
508506
// Received either "Domain=." or "Domain=..some.thing",

src/runtime/os_darwin.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime
66

77
import (
88
"internal/abi"
9+
"internal/stringslite"
910
"unsafe"
1011
)
1112

@@ -465,10 +466,7 @@ func sysargs(argc int32, argv **byte) {
465466
executablePath = gostringnocopy(argv_index(argv, n+1))
466467

467468
// strip "executable_path=" prefix if available, it's added after OS X 10.11.
468-
const prefix = "executable_path="
469-
if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
470-
executablePath = executablePath[len(prefix):]
471-
}
469+
executablePath = stringslite.TrimPrefix(executablePath, "executable_path=")
472470
}
473471

474472
func signalM(mp *m, sig int) {

0 commit comments

Comments
 (0)