"os"
"path/filepath"
"runtime"
- "strings"
)
// Path is a validated list of Trees derived from $GOROOT and $GOPATH at init.
}
for _, t := range Path {
tpath := t.SrcDir() + string(filepath.Separator)
- if !strings.HasPrefix(path, tpath) {
+ if !filepath.HasPrefix(path, tpath) {
continue
}
tree = t
}
// isLocalPath returns whether the given path is local (/foo ./foo ../foo . ..)
+// Windows paths that starts with drive letter (c:\foo c:foo) are considered local.
func isLocalPath(s string) bool {
const sep = string(filepath.Separator)
- return strings.HasPrefix(s, sep) || strings.HasPrefix(s, "."+sep) || strings.HasPrefix(s, ".."+sep) || s == "." || s == ".."
+ return s == "." || s == ".." ||
+ filepath.HasPrefix(s, sep) ||
+ filepath.HasPrefix(s, "."+sep) || filepath.HasPrefix(s, ".."+sep) ||
+ filepath.VolumeName(s) != ""
}
var (
// Getting Dot-Dot right,''
// http://plan9.bell-labs.com/sys/doc/lexnames.html
func Clean(path string) string {
- vol := volumeName(path)
+ vol := VolumeName(path)
path = path[len(vol):]
if path == "" {
return vol + "."
return strings.HasPrefix(path, "/") || strings.HasPrefix(path, "#")
}
-// volumeName returns the leading volume name on Windows.
+// VolumeName returns the leading volume name on Windows.
// It returns "" elsewhere
-func volumeName(path string) string {
+func VolumeName(path string) string {
return ""
}
+
+// HasPrefix tests whether the path p begins with prefix.
+func HasPrefix(p, prefix string) bool {
+ return strings.HasPrefix(p, prefix)
+}
return strings.HasPrefix(path, "/")
}
-// volumeName returns the leading volume name on Windows.
+// VolumeName returns the leading volume name on Windows.
// It returns "" elsewhere.
-func volumeName(path string) string {
+func VolumeName(path string) string {
return ""
}
+
+// HasPrefix tests whether the path p begins with prefix.
+func HasPrefix(p, prefix string) bool {
+ return strings.HasPrefix(p, prefix)
+}
package filepath
+import "strings"
+
// IsAbs returns true if the path is absolute.
func IsAbs(path string) (b bool) {
- v := volumeName(path)
+ v := VolumeName(path)
if v == "" {
return false
}
return path[0] == '/' || path[0] == '\\'
}
-// volumeName return leading volume name.
-// If given "C:\foo\bar", return "C:" on windows.
-func volumeName(path string) (v string) {
+// VolumeName returns leading volume name.
+// Given "C:\foo\bar" it returns "C:" under windows.
+// On other platforms it returns "".
+func VolumeName(path string) (v string) {
if len(path) < 2 {
return ""
}
}
return ""
}
+
+// HasPrefix tests whether the path p begins with prefix.
+// It ignores case while comparing.
+func HasPrefix(p, prefix string) bool {
+ if strings.HasPrefix(p, prefix) {
+ return true
+ }
+ return strings.HasPrefix(strings.ToLower(p), strings.ToLower(prefix))
+}