summaryrefslogtreecommitdiff
path: root/src/lib/path
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/path')
-rw-r--r--src/lib/path/Makefile68
-rw-r--r--src/lib/path/path.go136
-rw-r--r--src/lib/path/path_test.go136
3 files changed, 340 insertions, 0 deletions
diff --git a/src/lib/path/Makefile b/src/lib/path/Makefile
new file mode 100644
index 000000000..020a66035
--- /dev/null
+++ b/src/lib/path/Makefile
@@ -0,0 +1,68 @@
+# Copyright 2009 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+# DO NOT EDIT. Automatically generated by gobuild.
+# gobuild -m >Makefile
+
+D=
+
+O_arm=5
+O_amd64=6
+O_386=8
+OS=568vq
+
+O=$(O_$(GOARCH))
+GC=$(O)g -I_obj
+CC=$(O)c -FVw
+AS=$(O)a
+AR=6ar
+
+default: packages
+
+clean:
+ rm -rf *.[$(OS)] *.a [$(OS)].out _obj
+
+test: packages
+ gotest
+
+coverage: packages
+ gotest
+ 6cov -g `pwd` | grep -v '_test\.go:'
+
+%.$O: %.go
+ $(GC) $*.go
+
+%.$O: %.c
+ $(CC) $*.c
+
+%.$O: %.s
+ $(AS) $*.s
+
+O1=\
+ path.$O\
+
+
+phases: a1
+_obj$D/path.a: phases
+
+a1: $(O1)
+ $(AR) grc _obj$D/path.a path.$O
+ rm -f $(O1)
+
+
+newpkg: clean
+ mkdir -p _obj$D
+ $(AR) grc _obj$D/path.a
+
+$(O1): newpkg
+$(O2): a1
+
+nuke: clean
+ rm -f $(GOROOT)/pkg$D/path.a
+
+packages: _obj$D/path.a
+
+install: packages
+ test -d $(GOROOT)/pkg && mkdir -p $(GOROOT)/pkg$D
+ cp _obj$D/path.a $(GOROOT)/pkg$D/path.a
diff --git a/src/lib/path/path.go b/src/lib/path/path.go
new file mode 100644
index 000000000..a7e2c26c3
--- /dev/null
+++ b/src/lib/path/path.go
@@ -0,0 +1,136 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// The path package implements utility routines for manipulating
+// slash-separated filename paths.
+package path
+
+import "io"
+
+// Clean returns the shortest path name equivalent to path
+// by purely lexical processing. It applies the following rules
+// iteratively until no further processing can be done:
+//
+// 1. Replace multiple slashes with a single slash.
+// 2. Eliminate each . path name element (the current directory).
+// 3. Eliminate each inner .. path name element (the parent directory)
+// along with the non-.. element that precedes it.
+// 4. Eliminate .. elements that begin a rooted path:
+// that is, replace "/.." by "/" at the beginning of a path.
+//
+// If the result of this process is an empty string, Clean
+// returns the string ".".
+//
+// See also Rob Pike, ``Lexical File Names in Plan 9 or
+// Getting Dot-Dot right,''
+// http://plan9.bell-labs.com/sys/doc/lexnames.html
+func Clean(path string) string {
+ if path == "" {
+ return "."
+ }
+
+ rooted := path[0] == '/';
+ n := len(path);
+
+ // Invariants:
+ // reading from path; r is index of next byte to process.
+ // writing to buf; w is index of next byte to write.
+ // dotdot is index in buf where .. must stop, either because
+ // it is the leading slash or it is a leading ../../.. prefix.
+ buf := io.StringBytes(path);
+ r, w, dotdot := 0, 0, 0;
+ if rooted {
+ r, w, dotdot = 1, 1, 1;
+ }
+
+ for r < n {
+ switch {
+ case path[r] == '/':
+ // empty path element
+ r++;
+ case path[r] == '.' && (r+1 == n || path[r+1] == '/'):
+ // . element
+ r++;
+ case path[r] == '.' && path[r+1] == '.' && (r+2 == n || path[r+2] == '/'):
+ // .. element: remove to last /
+ r += 2;
+ switch {
+ case w > dotdot:
+ // can backtrack
+ w--;
+ for w > dotdot && buf[w] != '/' {
+ w--;
+ }
+ case !rooted:
+ // cannot backtrack, but not rooted, so append .. element.
+ if w > 0 {
+ buf[w] = '/';
+ w++;
+ }
+ buf[w] = '.';
+ w++;
+ buf[w] = '.';
+ w++;
+ dotdot = w;
+ }
+ default:
+ // real path element.
+ // add slash if needed
+ if rooted && w != 1 || !rooted && w != 0 {
+ buf[w] = '/';
+ w++;
+ }
+ // copy element
+ for ; r < n && path[r] != '/'; r++ {
+ buf[w] = path[r];
+ w++;
+ }
+ }
+ }
+
+ // Turn empty string into "."
+ if w == 0 {
+ buf[w] = '.';
+ w++;
+ }
+
+ return string(buf[0:w]);
+}
+
+// Split splits path immediately following the final slash,
+// separating it into a directory and file name component.
+// If there is no slash in path, DirFile returns an empty dir and
+// file set to path.
+func Split(path string) (dir, file string) {
+ for i := len(path)-1; i >= 0; i-- {
+ if path[i] == '/' {
+ return path[0:i+1], path[i+1:len(path)];
+ }
+ }
+ return "", path
+}
+
+// Join joins dir and file into a single path, adding a separating
+// slash if necessary. If dir is empty, it returns file.
+func Join(dir, file string) string {
+ if dir == "" {
+ return file;
+ }
+ return Clean(dir + "/" + file);
+}
+
+// Ext returns the file name extension used by path.
+// The extension is the suffix beginning at the final dot
+// in the final slash-separated element of path;
+// it is empty if there is no dot.
+func Ext(path string) string {
+ dot := -1;
+ for i := len(path)-1; i >= 0 && path[i] != '/'; i-- {
+ if path[i] == '.' {
+ return path[i:len(path)];
+ }
+ }
+ return ""
+}
+
diff --git a/src/lib/path/path_test.go b/src/lib/path/path_test.go
new file mode 100644
index 000000000..1238ac1cd
--- /dev/null
+++ b/src/lib/path/path_test.go
@@ -0,0 +1,136 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package path
+
+import (
+ "path";
+ "testing"
+)
+
+type CleanTest struct {
+ path, clean string
+}
+
+var cleantests = []CleanTest {
+ // Already clean
+ CleanTest{"", "."},
+ CleanTest{"abc", "abc"},
+ CleanTest{"abc/def", "abc/def"},
+ CleanTest{"a/b/c", "a/b/c"},
+ CleanTest{".", "."},
+ CleanTest{"..", ".."},
+ CleanTest{"../..", "../.."},
+ CleanTest{"../../abc", "../../abc"},
+ CleanTest{"/abc", "/abc"},
+ CleanTest{"/", "/"},
+
+ // Remove trailing slash
+ CleanTest{"abc/", "abc"},
+ CleanTest{"abc/def/", "abc/def"},
+ CleanTest{"a/b/c/", "a/b/c"},
+ CleanTest{"./", "."},
+ CleanTest{"../", ".."},
+ CleanTest{"../../", "../.."},
+ CleanTest{"/abc/", "/abc"},
+
+ // Remove doubled slash
+ CleanTest{"abc//def//ghi", "abc/def/ghi"},
+ CleanTest{"//abc", "/abc"},
+ CleanTest{"///abc", "/abc"},
+ CleanTest{"//abc//", "/abc"},
+ CleanTest{"abc//", "abc"},
+
+ // Remove . elements
+ CleanTest{"abc/./def", "abc/def"},
+ CleanTest{"/./abc/def", "/abc/def"},
+ CleanTest{"abc/.", "abc"},
+
+ // Remove .. elements
+ CleanTest{"abc/def/ghi/../jkl", "abc/def/jkl"},
+ CleanTest{"abc/def/../ghi/../jkl", "abc/jkl"},
+ CleanTest{"abc/def/..", "abc"},
+ CleanTest{"abc/def/../..", "."},
+ CleanTest{"/abc/def/../..", "/"},
+ CleanTest{"abc/def/../../..", ".."},
+ CleanTest{"/abc/def/../../..", "/"},
+ CleanTest{"abc/def/../../../ghi/jkl/../../../mno", "../../mno"},
+
+ // Combinations
+ CleanTest{"abc/./../def", "def"},
+ CleanTest{"abc//./../def", "def"},
+ CleanTest{"abc/../../././../def", "../../def"},
+}
+
+func TestClean(t *testing.T) {
+ for i, test := range cleantests {
+ if s := Clean(test.path); s != test.clean {
+ t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.clean);
+ }
+ }
+}
+
+type SplitTest struct {
+ path, dir, file string
+}
+
+var splittests = []SplitTest {
+ SplitTest{"a/b", "a/", "b"},
+ SplitTest{"a/b/", "a/b/", ""},
+ SplitTest{"a/", "a/", ""},
+ SplitTest{"a", "", "a"},
+ SplitTest{"/", "/", ""},
+}
+
+func TestSplit(t *testing.T) {
+ for i, test := range splittests {
+ if d, f := Split(test.path); d != test.dir || f != test.file {
+ t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file);
+ }
+ }
+}
+
+type JoinTest struct {
+ dir, file, path string
+}
+
+var jointests = []JoinTest {
+ JoinTest{"a", "b", "a/b"},
+ JoinTest{"a", "", "a"},
+ JoinTest{"", "b", "b"},
+ JoinTest{"/", "a", "/a"},
+ JoinTest{"/", "", "/"},
+ JoinTest{"a/", "b", "a/b"},
+ JoinTest{"a/", "", "a"},
+}
+
+func TestJoin(t *testing.T) {
+ for i, test := range jointests {
+ if p := Join(test.dir, test.file); p != test.path {
+ t.Errorf("Join(%q, %q) = %q, want %q", test.dir, test.file, p, test.path);
+ }
+ }
+}
+
+type ExtTest struct {
+ path, ext string
+}
+
+var exttests = []ExtTest {
+ ExtTest{"path.go", ".go"},
+ ExtTest{"path.pb.go", ".go"},
+ ExtTest{"path", ""},
+ ExtTest{"a.dir/b", ""},
+ ExtTest{"a.dir/b.go", ".go"},
+ ExtTest{"a.dir/", ""},
+}
+
+func TestExt(t *testing.T) {
+ for i, test := range exttests {
+ if x := Ext(test.path); x != test.ext {
+ t.Errorf("Ext(%q) = %q, want %q", test.path, x, test.ext);
+ }
+ }
+}
+