mirror of https://github.com/golang/go.git
cmd/go/internal/get: more efficient path prefix checking code
Splitting the string is unnecessary. Change-Id: I02796cb91602c1b9bf22721b985cd41b18cc92f2 Reviewed-on: https://go-review.googlesource.com/119936 Run-TryBot: David Symonds <dsymonds@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rob Pike <r@golang.org>
This commit is contained in:
parent
a1addf15df
commit
f76eaeb2c8
|
|
@ -903,16 +903,16 @@ type metaImport struct {
|
||||||
Prefix, VCS, RepoRoot string
|
Prefix, VCS, RepoRoot string
|
||||||
}
|
}
|
||||||
|
|
||||||
func splitPathHasPrefix(path, prefix []string) bool {
|
// pathPrefix reports whether sub is a prefix of s,
|
||||||
if len(path) < len(prefix) {
|
// only considering entire path components.
|
||||||
|
func pathPrefix(s, sub string) bool {
|
||||||
|
// strings.HasPrefix is necessary but not sufficient.
|
||||||
|
if !strings.HasPrefix(s, sub) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
for i, p := range prefix {
|
// The remainder after the prefix must either be empty or start with a slash.
|
||||||
if path[i] != p {
|
rem := s[len(sub):]
|
||||||
return false
|
return rem == "" || rem[0] == '/'
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// A ImportMismatchError is returned where metaImport/s are present
|
// A ImportMismatchError is returned where metaImport/s are present
|
||||||
|
|
@ -935,13 +935,10 @@ func (m ImportMismatchError) Error() string {
|
||||||
// errNoMatch is returned if none match.
|
// errNoMatch is returned if none match.
|
||||||
func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
|
func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
|
||||||
match := -1
|
match := -1
|
||||||
imp := strings.Split(importPath, "/")
|
|
||||||
|
|
||||||
errImportMismatch := ImportMismatchError{importPath: importPath}
|
errImportMismatch := ImportMismatchError{importPath: importPath}
|
||||||
for i, im := range imports {
|
for i, im := range imports {
|
||||||
pre := strings.Split(im.Prefix, "/")
|
if !pathPrefix(importPath, im.Prefix) {
|
||||||
|
|
||||||
if !splitPathHasPrefix(imp, pre) {
|
|
||||||
errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
|
errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue