]> Cypherpunks repositories - gostls13.git/commitdiff
cmd,log,net,runtime: simplify string prefix and suffix processing
authorapocelipes <seve3r@outlook.com>
Mon, 29 Jul 2024 10:23:14 +0000 (10:23 +0000)
committerGopher Robot <gobot@golang.org>
Mon, 29 Jul 2024 21:29:17 +0000 (21:29 +0000)
Use the TrimPrefix, TrimSuffix and CutPrefix to simplify the code.

Change-Id: I3e2b271ec0d3f9ce664b830e2b0c21ab47337ed0
GitHub-Last-Rev: 4bd1577d2496173800d609da75b08f6de177aba9
GitHub-Pull-Request: golang/go#68629
Reviewed-on: https://go-review.googlesource.com/c/go/+/601675
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
src/cmd/cgo/out.go
src/cmd/compile/internal/types2/resolver.go
src/cmd/go/internal/load/pkg.go
src/cmd/go/internal/work/gc.go
src/log/slog/logger.go
src/net/dnsclient_unix.go
src/net/http/cookiejar/jar.go
src/runtime/os_darwin.go

index 5329cb3cd2d2248914e1ea15e982cfb08a1c969a..0a0ef88fbc06f6c604eccf00a2b651e0ef1254fb 100644 (file)
@@ -378,9 +378,7 @@ func dynimport(obj string) {
                defer f.Close()
                sym, _ := f.ImportedSymbols()
                for _, s := range sym {
-                       if len(s) > 0 && s[0] == '_' {
-                               s = s[1:]
-                       }
+                       s = strings.TrimPrefix(s, "_")
                        checkImportSymName(s)
                        fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "")
                }
index b381b541b75785a06588b21e133cd6ff9bb2db92..28e4a7faa6ce8df3396981e5f44af41a5eeb0156 100644 (file)
@@ -167,10 +167,7 @@ func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
                        if imp == nil {
                                // create a new fake package
                                // come up with a sensible package name (heuristic)
-                               name := path
-                               if i := len(name); i > 0 && name[i-1] == '/' {
-                                       name = name[:i-1]
-                               }
+                               name := strings.TrimSuffix(path, "/")
                                if i := strings.LastIndex(name, "/"); i >= 0 {
                                        name = name[i+1:]
                                }
index bf432f0bb78c6fd255a38a76bc0e42cf9f1ae484..238fb6efd222e2ce0d37dcdf79ba57a84ff0dce4 100644 (file)
@@ -2076,11 +2076,7 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
        for _, pattern = range patterns {
                pid++
 
-               glob := pattern
-               all := strings.HasPrefix(pattern, "all:")
-               if all {
-                       glob = pattern[len("all:"):]
-               }
+               glob, all := strings.CutPrefix(pattern, "all:")
                // Check pattern is valid for //go:embed.
                if _, err := pathpkg.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
                        return nil, nil, fmt.Errorf("invalid pattern syntax")
index 99bcaf9266028d950615c524f93a4b75969ce761..1fbd267e09571e6b6c20578d7191ad1fb99d712e 100644 (file)
@@ -248,10 +248,7 @@ func (a *Action) trimpath() string {
        // same situations.
 
        // Strip the object directory entirely.
-       objdir := a.Objdir
-       if len(objdir) > 1 && objdir[len(objdir)-1] == filepath.Separator {
-               objdir = objdir[:len(objdir)-1]
-       }
+       objdir := strings.TrimSuffix(a.Objdir, string(filepath.Separator))
        rewrite := ""
 
        rewriteDir := a.Package.Dir
index 10aa6a2b314feb49a1c5aaa0e28ae92fbe795ac2..961e0cd2cec67e244b5d78eb34ee27a1238816af 100644 (file)
@@ -5,6 +5,7 @@
 package slog
 
 import (
+       "bytes"
        "context"
        "log"
        loginternal "log/internal"
@@ -96,9 +97,7 @@ func (w *handlerWriter) Write(buf []byte) (int, error) {
 
        // Remove final newline.
        origLen := len(buf) // Report that the entire buf was written.
-       if len(buf) > 0 && buf[len(buf)-1] == '\n' {
-               buf = buf[:len(buf)-1]
-       }
+       buf = bytes.TrimSuffix(buf, []byte{'\n'})
        r := NewRecord(time.Now(), level, string(buf), pc)
        return origLen, w.h.Handle(context.Background(), r)
 }
index 54c7dc83bac6654844b4e5d5c03894b81a2bf652..5e060a6b489bf9caad5abb6b6d38bb7776e04aaa 100644 (file)
@@ -18,6 +18,7 @@ import (
        "internal/bytealg"
        "internal/godebug"
        "internal/itoa"
+       "internal/stringslite"
        "io"
        "os"
        "runtime"
@@ -487,9 +488,7 @@ func avoidDNS(name string) bool {
        if name == "" {
                return true
        }
-       if name[len(name)-1] == '.' {
-               name = name[:len(name)-1]
-       }
+       name = stringslite.TrimSuffix(name, ".")
        return stringsHasSuffixFold(name, ".onion")
 }
 
index 2eec1a3e745b42cded5a20bc2753b98c745e9e9a..edf14d03ad38091f8a40cbe1d0abd17e0bc1765d 100644 (file)
@@ -500,9 +500,7 @@ func (j *Jar) domainAndType(host, domain string) (string, bool, error) {
        // From here on: If the cookie is valid, it is a domain cookie (with
        // the one exception of a public suffix below).
        // See RFC 6265 section 5.2.3.
-       if domain[0] == '.' {
-               domain = domain[1:]
-       }
+       domain = strings.TrimPrefix(domain, ".")
 
        if len(domain) == 0 || domain[0] == '.' {
                // Received either "Domain=." or "Domain=..some.thing",
index 430d1865dffb410a34e89dc2578a37dd1709051f..c6e20188175489269d654bc9a712e94f4c34ee7e 100644 (file)
@@ -6,6 +6,7 @@ package runtime
 
 import (
        "internal/abi"
+       "internal/stringslite"
        "unsafe"
 )
 
@@ -465,10 +466,7 @@ func sysargs(argc int32, argv **byte) {
        executablePath = gostringnocopy(argv_index(argv, n+1))
 
        // strip "executable_path=" prefix if available, it's added after OS X 10.11.
-       const prefix = "executable_path="
-       if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
-               executablePath = executablePath[len(prefix):]
-       }
+       executablePath = stringslite.TrimPrefix(executablePath, "executable_path=")
 }
 
 func signalM(mp *m, sig int) {