diff options
Diffstat (limited to 'src/pkg/path/path.go')
-rw-r--r-- | src/pkg/path/path.go | 78 |
1 files changed, 14 insertions, 64 deletions
diff --git a/src/pkg/path/path.go b/src/pkg/path/path.go index 61eea8858..658eec093 100644 --- a/src/pkg/path/path.go +++ b/src/pkg/path/path.go @@ -7,8 +7,6 @@ package path import ( - "io/ioutil" - "os" "strings" ) @@ -107,7 +105,7 @@ func Clean(path string) string { // If there is no separator in path, Split returns an empty dir and // file set to path. func Split(path string) (dir, file string) { - i := strings.LastIndexAny(path, PathSeps) + i := strings.LastIndex(path, "/") return path[:i+1], path[i+1:] } @@ -135,78 +133,30 @@ func Ext(path string) string { return "" } -// Visitor methods are invoked for corresponding file tree entries -// visited by Walk. The parameter path is the full path of f relative -// to root. -type Visitor interface { - VisitDir(path string, f *os.FileInfo) bool - VisitFile(path string, f *os.FileInfo) -} - -func walk(path string, f *os.FileInfo, v Visitor, errors chan<- os.Error) { - if !f.IsDirectory() { - v.VisitFile(path, f) - return - } - - if !v.VisitDir(path, f) { - return // skip directory entries - } - - list, err := ioutil.ReadDir(path) - if err != nil { - if errors != nil { - errors <- err - } - } - - for _, e := range list { - walk(Join(path, e.Name), e, v, errors) - } -} - -// Walk walks the file tree rooted at root, calling v.VisitDir or -// v.VisitFile for each directory or file in the tree, including root. -// If v.VisitDir returns false, Walk skips the directory's entries; -// otherwise it invokes itself for each directory entry in sorted order. -// An error reading a directory does not abort the Walk. -// If errors != nil, Walk sends each directory read error -// to the channel. Otherwise Walk discards the error. -func Walk(root string, v Visitor, errors chan<- os.Error) { - f, err := os.Lstat(root) - if err != nil { - if errors != nil { - errors <- err - } - return // can't progress - } - walk(root, f, v, errors) -} - -// Base returns the last path element of the slash-separated name. -// Trailing slashes are removed before extracting the last element. If the name is -// empty, "." is returned. If it consists entirely of slashes, "/" is returned. -func Base(name string) string { - if name == "" { +// Base returns the last element of path. +// Trailing slashes are removed before extracting the last element. +// If the path is empty, Base returns ".". +// If the path consists entirely of slashes, Base returns "/". +func Base(path string) string { + if path == "" { return "." } // Strip trailing slashes. - for len(name) > 0 && name[len(name)-1] == '/' { - name = name[0 : len(name)-1] + for len(path) > 0 && path[len(path)-1] == '/' { + path = path[0 : len(path)-1] } // Find the last element - if i := strings.LastIndex(name, "/"); i >= 0 { - name = name[i+1:] + if i := strings.LastIndex(path, "/"); i >= 0 { + path = path[i+1:] } // If empty now, it had only slashes. - if name == "" { + if path == "" { return "/" } - return name + return path } // IsAbs returns true if the path is absolute. func IsAbs(path string) bool { - // TODO: Add Windows support - return strings.HasPrefix(path, "/") + return len(path) > 0 && path[0] == '/' } |