diff options
author | Rob Pike <r@golang.org> | 2011-12-22 13:58:58 -0800 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2011-12-22 13:58:58 -0800 |
commit | 302649cfec21038acab1fe9e321a9a434ad9bf32 (patch) | |
tree | 98b1738255eb8765cde782fd33b0a12defdd2266 | |
parent | db9f3ca5b6016b317b177a241fae6d94d5abbd40 (diff) | |
download | go-302649cfec21038acab1fe9e321a9a434ad9bf32.tar.gz |
path/filepath: Dir
There was Base but not Dir, so fill in the gap.
R=n13m3y3r, r, rsc, gustavo
CC=golang-dev
http://codereview.appspot.com/5503067
-rw-r--r-- | src/pkg/path/filepath/path.go | 19 | ||||
-rw-r--r-- | src/pkg/path/filepath/path_test.go | 23 |
2 files changed, 42 insertions, 0 deletions
diff --git a/src/pkg/path/filepath/path.go b/src/pkg/path/filepath/path.go index 68cbae664..f1cda7c53 100644 --- a/src/pkg/path/filepath/path.go +++ b/src/pkg/path/filepath/path.go @@ -147,6 +147,7 @@ func SplitList(path string) []string { // separating it into a directory and file name component. // If there is no Separator in path, Split returns an empty dir // and file set to path. +// The returned values have the property that path = dir+file. func Split(path string) (dir, file string) { vol := VolumeName(path) i := len(path) - 1 @@ -439,3 +440,21 @@ func Base(path string) string { } return path } + +// Dir returns the all but the last element of path, typically the path's directory. +// Trailing path separators are removed before processing. +// If the path is empty, Dir returns ".". +// If the path consists entirely of separators, Dir returns a single separator. +// The returned path does not end in a separator unless it is the root directory. +func Dir(path string) string { + dir, _ := Split(path) + dir = Clean(dir) + last := len(dir) - 1 + if last > 0 && os.IsPathSeparator(dir[last]) { + dir = dir[:last] + } + if dir == "" { + dir = "." + } + return dir +} diff --git a/src/pkg/path/filepath/path_test.go b/src/pkg/path/filepath/path_test.go index 67d8858fc..49a7135b4 100644 --- a/src/pkg/path/filepath/path_test.go +++ b/src/pkg/path/filepath/path_test.go @@ -431,6 +431,29 @@ func TestBase(t *testing.T) { } } +var dirtests = []PathTest{ + {"", "."}, + {".", "."}, + {"/.", "/"}, + {"/", "/"}, + {"////", "/"}, + {"/foo", "/"}, + {"x/", "x"}, + {"abc", "."}, + {"abc/def", "abc"}, + {"a/b/.x", "a/b"}, + {"a/b/c.", "a/b"}, + {"a/b/c.x", "a/b"}, +} + +func TestDir(t *testing.T) { + for _, test := range dirtests { + if s := filepath.ToSlash(filepath.Dir(test.path)); s != test.result { + t.Errorf("Dir(%q) = %q, want %q", test.path, s, test.result) + } + } +} + type IsAbsTest struct { path string isAbs bool |