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: 4bd1577d24
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>
This commit is contained in:
apocelipes 2024-07-29 10:23:14 +00:00 committed by Gopher Robot
parent fad6390f38
commit ac51262592
8 changed files with 11 additions and 29 deletions

View file

@ -378,9 +378,7 @@ func dynimport(obj string) {
defer f.Close() defer f.Close()
sym, _ := f.ImportedSymbols() sym, _ := f.ImportedSymbols()
for _, s := range sym { for _, s := range sym {
if len(s) > 0 && s[0] == '_' { s = strings.TrimPrefix(s, "_")
s = s[1:]
}
checkImportSymName(s) checkImportSymName(s)
fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "") fmt.Fprintf(stdout, "//go:cgo_import_dynamic %s %s %q\n", s, s, "")
} }

View file

@ -167,10 +167,7 @@ func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
if imp == nil { if imp == nil {
// create a new fake package // create a new fake package
// come up with a sensible package name (heuristic) // come up with a sensible package name (heuristic)
name := path name := strings.TrimSuffix(path, "/")
if i := len(name); i > 0 && name[i-1] == '/' {
name = name[:i-1]
}
if i := strings.LastIndex(name, "/"); i >= 0 { if i := strings.LastIndex(name, "/"); i >= 0 {
name = name[i+1:] name = name[i+1:]
} }

View file

@ -2076,11 +2076,7 @@ func resolveEmbed(pkgdir string, patterns []string) (files []string, pmap map[st
for _, pattern = range patterns { for _, pattern = range patterns {
pid++ pid++
glob := pattern glob, all := strings.CutPrefix(pattern, "all:")
all := strings.HasPrefix(pattern, "all:")
if all {
glob = pattern[len("all:"):]
}
// Check pattern is valid for //go:embed. // Check pattern is valid for //go:embed.
if _, err := pathpkg.Match(glob, ""); err != nil || !validEmbedPattern(glob) { if _, err := pathpkg.Match(glob, ""); err != nil || !validEmbedPattern(glob) {
return nil, nil, fmt.Errorf("invalid pattern syntax") return nil, nil, fmt.Errorf("invalid pattern syntax")

View file

@ -248,10 +248,7 @@ func (a *Action) trimpath() string {
// same situations. // same situations.
// Strip the object directory entirely. // Strip the object directory entirely.
objdir := a.Objdir objdir := strings.TrimSuffix(a.Objdir, string(filepath.Separator))
if len(objdir) > 1 && objdir[len(objdir)-1] == filepath.Separator {
objdir = objdir[:len(objdir)-1]
}
rewrite := "" rewrite := ""
rewriteDir := a.Package.Dir rewriteDir := a.Package.Dir

View file

@ -5,6 +5,7 @@
package slog package slog
import ( import (
"bytes"
"context" "context"
"log" "log"
loginternal "log/internal" loginternal "log/internal"
@ -96,9 +97,7 @@ func (w *handlerWriter) Write(buf []byte) (int, error) {
// Remove final newline. // Remove final newline.
origLen := len(buf) // Report that the entire buf was written. origLen := len(buf) // Report that the entire buf was written.
if len(buf) > 0 && buf[len(buf)-1] == '\n' { buf = bytes.TrimSuffix(buf, []byte{'\n'})
buf = buf[:len(buf)-1]
}
r := NewRecord(time.Now(), level, string(buf), pc) r := NewRecord(time.Now(), level, string(buf), pc)
return origLen, w.h.Handle(context.Background(), r) return origLen, w.h.Handle(context.Background(), r)
} }

View file

@ -18,6 +18,7 @@ import (
"internal/bytealg" "internal/bytealg"
"internal/godebug" "internal/godebug"
"internal/itoa" "internal/itoa"
"internal/stringslite"
"io" "io"
"os" "os"
"runtime" "runtime"
@ -487,9 +488,7 @@ func avoidDNS(name string) bool {
if name == "" { if name == "" {
return true return true
} }
if name[len(name)-1] == '.' { name = stringslite.TrimSuffix(name, ".")
name = name[:len(name)-1]
}
return stringsHasSuffixFold(name, ".onion") return stringsHasSuffixFold(name, ".onion")
} }

View 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 // From here on: If the cookie is valid, it is a domain cookie (with
// the one exception of a public suffix below). // the one exception of a public suffix below).
// See RFC 6265 section 5.2.3. // See RFC 6265 section 5.2.3.
if domain[0] == '.' { domain = strings.TrimPrefix(domain, ".")
domain = domain[1:]
}
if len(domain) == 0 || domain[0] == '.' { if len(domain) == 0 || domain[0] == '.' {
// Received either "Domain=." or "Domain=..some.thing", // Received either "Domain=." or "Domain=..some.thing",

View file

@ -6,6 +6,7 @@ package runtime
import ( import (
"internal/abi" "internal/abi"
"internal/stringslite"
"unsafe" "unsafe"
) )
@ -465,10 +466,7 @@ func sysargs(argc int32, argv **byte) {
executablePath = gostringnocopy(argv_index(argv, n+1)) executablePath = gostringnocopy(argv_index(argv, n+1))
// strip "executable_path=" prefix if available, it's added after OS X 10.11. // strip "executable_path=" prefix if available, it's added after OS X 10.11.
const prefix = "executable_path=" executablePath = stringslite.TrimPrefix(executablePath, "executable_path=")
if len(executablePath) > len(prefix) && executablePath[:len(prefix)] == prefix {
executablePath = executablePath[len(prefix):]
}
} }
func signalM(mp *m, sig int) { func signalM(mp *m, sig int) {