diff options
author | Rob Pike <r@golang.org> | 2010-06-09 19:59:22 -0700 |
---|---|---|
committer | Rob Pike <r@golang.org> | 2010-06-09 19:59:22 -0700 |
commit | 4d4d8a8cbabbe5983a0bbe87fe224e655056e099 (patch) | |
tree | 3d7044f6b10b8baf9b7b91d678fcb66403251831 | |
parent | bd4d4eb075595c965a442b7149220011db30e97d (diff) | |
download | golang-4d4d8a8cbabbe5983a0bbe87fe224e655056e099.tar.gz |
add path.Base, analogous to Unix basename
R=rsc
CC=golang-dev
http://codereview.appspot.com/1633042
-rw-r--r-- | src/pkg/path/path.go | 22 | ||||
-rw-r--r-- | src/pkg/path/path_test.go | 23 |
2 files changed, 45 insertions, 0 deletions
diff --git a/src/pkg/path/path.go b/src/pkg/path/path.go index 86bfe6455..9c1d09374 100644 --- a/src/pkg/path/path.go +++ b/src/pkg/path/path.go @@ -186,3 +186,25 @@ func Walk(root string, v Visitor, errors chan<- os.Error) { } 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 == "" { + return "." + } + // Strip trailing slashes. + for len(name) > 0 && name[len(name)-1] == '/' { + name = name[0 : len(name)-1] + } + // Find the last element + if i := strings.LastIndex(name, "/"); i >= 0 { + name = name[i+1:] + } + // If empty now, it had only slashes. + if name == "" { + return "/" + } + return name +} diff --git a/src/pkg/path/path_test.go b/src/pkg/path/path_test.go index e2458f20c..6915b48bb 100644 --- a/src/pkg/path/path_test.go +++ b/src/pkg/path/path_test.go @@ -284,3 +284,26 @@ func TestWalk(t *testing.T) { t.Errorf("removeTree: %v", err) } } + +var basetests = []CleanTest{ + // Already clean + CleanTest{"", "."}, + CleanTest{".", "."}, + CleanTest{"/.", "."}, + CleanTest{"/", "/"}, + CleanTest{"////", "/"}, + CleanTest{"x/", "x"}, + CleanTest{"abc", "abc"}, + CleanTest{"abc/def", "def"}, + CleanTest{"a/b/.x", ".x"}, + CleanTest{"a/b/c.", "c."}, + CleanTest{"a/b/c.x", "c.x"}, +} + +func TestBase(t *testing.T) { + for _, test := range basetests { + if s := Base(test.path); s != test.clean { + t.Errorf("Base(%q) = %q, want %q", test.path, s, test.clean) + } + } +} |