]> Cypherpunks repositories - gostls13.git/commitdiff
add path.Base, analogous to Unix basename
authorRob Pike <r@golang.org>
Thu, 10 Jun 2010 02:59:22 +0000 (19:59 -0700)
committerRob Pike <r@golang.org>
Thu, 10 Jun 2010 02:59:22 +0000 (19:59 -0700)
R=rsc
CC=golang-dev
https://golang.org/cl/1633042

src/pkg/path/path.go
src/pkg/path/path_test.go

index 86bfe64555046b7b310421e9b1201bd0968f6917..9c1d09374cca39ceff09f5c6f52319fa6f9790f1 100644 (file)
@@ -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
+}
index e2458f20c477fa99af3d2ed75acd13183d77954f..6915b48bbb089aaf4254e697ad7f4f38d9e0959c 100644 (file)
@@ -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)
+               }
+       }
+}