cmd/go: using strings.CutPrefix replace strings.HasPrefix and strings.TrimPrefix

Change-Id: I143d05c24a3e897d0f3ee78dd16954c32ceae091
Reviewed-on: https://go-review.googlesource.com/c/go/+/435137
Run-TryBot: xie cui <523516579@qq.com>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
This commit is contained in:
cuiweixie 2022-09-27 19:03:54 +08:00 committed by Gopher Robot
parent 2b45aebe14
commit 32a52e0399
8 changed files with 22 additions and 22 deletions

View file

@ -453,8 +453,8 @@ func printEnvAsJSON(env []cfg.EnvVar) {
func getOrigEnv(key string) string { func getOrigEnv(key string) string {
for _, v := range cfg.OrigEnv { for _, v := range cfg.OrigEnv {
if strings.HasPrefix(v, key+"=") { if v, found := strings.CutPrefix(v, key+"="); found {
return strings.TrimPrefix(v, key+"=") return v
} }
} }
return "" return ""

View file

@ -486,9 +486,9 @@ func (r *gitRepo) stat(rev string) (info *RevInfo, err error) {
// Either way, try a local stat before falling back to network I/O. // Either way, try a local stat before falling back to network I/O.
if !didStatLocal { if !didStatLocal {
if info, err := r.statLocal(rev, hash); err == nil { if info, err := r.statLocal(rev, hash); err == nil {
if strings.HasPrefix(ref, "refs/tags/") { if after, found := strings.CutPrefix(ref, "refs/tags/"); found {
// Make sure tag exists, so it will be in localTags next time the go command is run. // Make sure tag exists, so it will be in localTags next time the go command is run.
Run(r.dir, "git", "tag", strings.TrimPrefix(ref, "refs/tags/"), hash) Run(r.dir, "git", "tag", after, hash)
} }
return info, nil return info, nil
} }

View file

@ -1095,14 +1095,16 @@ func (r *codeRepo) Zip(dst io.Writer, version string) error {
} }
topPrefix = zf.Name[:i+1] topPrefix = zf.Name[:i+1]
} }
if !strings.HasPrefix(zf.Name, topPrefix) { var name string
var found bool
if name, found = strings.CutPrefix(zf.Name, topPrefix); !found {
return fmt.Errorf("zip file contains more than one top-level directory") return fmt.Errorf("zip file contains more than one top-level directory")
} }
name := strings.TrimPrefix(zf.Name, topPrefix)
if !strings.HasPrefix(name, subdir) { if name, found = strings.CutPrefix(name, subdir); !found {
continue continue
} }
name = strings.TrimPrefix(name, subdir)
if name == "" || strings.HasSuffix(name, "/") { if name == "" || strings.HasSuffix(name, "/") {
continue continue
} }

View file

@ -548,13 +548,12 @@ func resolveLocalPackage(ctx context.Context, dir string, rs *Requirements) (str
modRoot := MainModules.ModRoot(mainModule) modRoot := MainModules.ModRoot(mainModule)
if modRoot != "" && strings.HasPrefix(absDir, modRoot+string(filepath.Separator)) && !strings.Contains(absDir[len(modRoot):], "@") { if modRoot != "" && strings.HasPrefix(absDir, modRoot+string(filepath.Separator)) && !strings.Contains(absDir[len(modRoot):], "@") {
suffix := filepath.ToSlash(absDir[len(modRoot):]) suffix := filepath.ToSlash(absDir[len(modRoot):])
if strings.HasPrefix(suffix, "/vendor/") { if pkg, found := strings.CutPrefix(suffix, "/vendor/"); found {
if cfg.BuildMod != "vendor" { if cfg.BuildMod != "vendor" {
return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir) return "", fmt.Errorf("without -mod=vendor, directory %s has no package path", absDir)
} }
readVendorList(mainModule) readVendorList(mainModule)
pkg := strings.TrimPrefix(suffix, "/vendor/")
if _, ok := vendorPkgModule[pkg]; !ok { if _, ok := vendorPkgModule[pkg]; !ok {
return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir) return "", fmt.Errorf("directory %s is not a package listed in vendor/modules.txt", absDir)
} }

View file

@ -98,16 +98,16 @@ func readVendorList(mainModule module.Version) {
continue continue
} }
if strings.HasPrefix(line, "## ") { if annonations, ok := strings.CutPrefix(line, "## "); ok {
// Metadata. Take the union of annotations across multiple lines, if present. // Metadata. Take the union of annotations across multiple lines, if present.
meta := vendorMeta[mod] meta := vendorMeta[mod]
for _, entry := range strings.Split(strings.TrimPrefix(line, "## "), ";") { for _, entry := range strings.Split(annonations, ";") {
entry = strings.TrimSpace(entry) entry = strings.TrimSpace(entry)
if entry == "explicit" { if entry == "explicit" {
meta.Explicit = true meta.Explicit = true
} }
if strings.HasPrefix(entry, "go ") { if goVersion, ok := strings.CutPrefix(entry, "go "); ok {
meta.GoVersion = strings.TrimPrefix(entry, "go ") meta.GoVersion = goVersion
rawGoVersion.Store(mod, meta.GoVersion) rawGoVersion.Store(mod, meta.GoVersion)
} }
// All other tokens are reserved for future use. // All other tokens are reserved for future use.

View file

@ -68,10 +68,11 @@ func testFlags() []string {
var names []string var names []string
flag.VisitAll(func(f *flag.Flag) { flag.VisitAll(func(f *flag.Flag) {
if !strings.HasPrefix(f.Name, "test.") { var name string
var found bool
if name, found = strings.CutPrefix(f.Name, "test."); !found {
return return
} }
name := strings.TrimPrefix(f.Name, "test.")
switch name { switch name {
case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker": case "testlogfile", "paniconexit0", "fuzzcachedir", "fuzzworker":

View file

@ -372,8 +372,8 @@ func readpkglist(shlibpath string) (pkgs []*load.Package) {
scanner := bufio.NewScanner(bytes.NewBuffer(data)) scanner := bufio.NewScanner(bytes.NewBuffer(data))
for scanner.Scan() { for scanner.Scan() {
t := scanner.Text() t := scanner.Text()
if strings.HasPrefix(t, "pkgpath ") { var found bool
t = strings.TrimPrefix(t, "pkgpath ") if t, found = strings.CutPrefix(t, "pkgpath "); found {
t = strings.TrimSuffix(t, ";") t = strings.TrimSuffix(t, ";")
pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd(), nil, &stk, nil, 0)) pkgs = append(pkgs, load.LoadImportWithFlags(t, base.Cwd(), nil, &stk, nil, 0))
} }

View file

@ -448,8 +448,7 @@ Script:
}).(bool) }).(bool)
break break
} }
if strings.HasPrefix(cond.tag, "GODEBUG:") { if value, found := strings.CutPrefix(cond.tag, "GODEBUG:"); found {
value := strings.TrimPrefix(cond.tag, "GODEBUG:")
parts := strings.Split(os.Getenv("GODEBUG"), ",") parts := strings.Split(os.Getenv("GODEBUG"), ",")
for _, p := range parts { for _, p := range parts {
if strings.TrimSpace(p) == value { if strings.TrimSpace(p) == value {
@ -459,8 +458,7 @@ Script:
} }
break break
} }
if strings.HasPrefix(cond.tag, "buildmode:") { if value, found := strings.CutPrefix(cond.tag, "buildmode:"); found {
value := strings.TrimPrefix(cond.tag, "buildmode:")
ok = sys.BuildModeSupported(runtime.Compiler, value, runtime.GOOS, runtime.GOARCH) ok = sys.BuildModeSupported(runtime.Compiler, value, runtime.GOOS, runtime.GOARCH)
break break
} }