diff options
Diffstat (limited to 'libgo/go/cmd/go/internal/str/path.go')
-rw-r--r-- | libgo/go/cmd/go/internal/str/path.go | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/libgo/go/cmd/go/internal/str/path.go b/libgo/go/cmd/go/internal/str/path.go index 84ca9d581e9..a9b4d759a6b 100644 --- a/libgo/go/cmd/go/internal/str/path.go +++ b/libgo/go/cmd/go/internal/str/path.go @@ -9,8 +9,25 @@ import ( "strings" ) -// HasFilePathPrefix reports whether the filesystem path s begins with the -// elements in prefix. +// HasPath reports whether the slash-separated path s +// begins with the elements in prefix. +func HasPathPrefix(s, prefix string) bool { + if len(s) == len(prefix) { + return s == prefix + } + if prefix == "" { + return true + } + if len(s) > len(prefix) { + if prefix[len(prefix)-1] == '/' || s[len(prefix)] == '/' { + return s[:len(prefix)] == prefix + } + } + return false +} + +// HasFilePathPrefix reports whether the filesystem path s +// begins with the elements in prefix. func HasFilePathPrefix(s, prefix string) bool { sv := strings.ToUpper(filepath.VolumeName(s)) pv := strings.ToUpper(filepath.VolumeName(prefix)) @@ -23,8 +40,10 @@ func HasFilePathPrefix(s, prefix string) bool { return false case len(s) == len(prefix): return s == prefix + case prefix == "": + return true case len(s) > len(prefix): - if prefix != "" && prefix[len(prefix)-1] == filepath.Separator { + if prefix[len(prefix)-1] == filepath.Separator { return strings.HasPrefix(s, prefix) } return s[len(prefix)] == filepath.Separator && s[:len(prefix)] == prefix |