Prefix, VCS, RepoRoot string
}
-func splitPathHasPrefix(path, prefix []string) bool {
- if len(path) < len(prefix) {
+// pathPrefix reports whether sub is a prefix of s,
+// 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
}
- for i, p := range prefix {
- if path[i] != p {
- return false
- }
- }
- return true
+ // The remainder after the prefix must either be empty or start with a slash.
+ rem := s[len(sub):]
+ return rem == "" || rem[0] == '/'
}
// A ImportMismatchError is returned where metaImport/s are present
// errNoMatch is returned if none match.
func matchGoImport(imports []metaImport, importPath string) (metaImport, error) {
match := -1
- imp := strings.Split(importPath, "/")
errImportMismatch := ImportMismatchError{importPath: importPath}
for i, im := range imports {
- pre := strings.Split(im.Prefix, "/")
-
- if !splitPathHasPrefix(imp, pre) {
+ if !pathPrefix(importPath, im.Prefix) {
errImportMismatch.mismatches = append(errImportMismatch.mismatches, im.Prefix)
continue
}