mirror of
https://github.com/golang/go
synced 2024-11-22 02:54:39 -07:00
path: make Join variadic
R=rsc, r CC=golang-dev https://golang.org/cl/198049
This commit is contained in:
parent
0e47c75f60
commit
bc43cc3db0
@ -115,13 +115,15 @@ func Split(path string) (dir, file string) {
|
||||
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
|
||||
// Join joins any number of path elemets into a single path, adding a
|
||||
// separating slash if necessary. All empty strings are ignored.
|
||||
func Join(elem ...string) string {
|
||||
for i, e := range elem {
|
||||
if e != "" {
|
||||
return Clean(strings.Join(elem[i:], "/"))
|
||||
}
|
||||
return Clean(dir + "/" + file)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// Ext returns the file name extension used by path.
|
||||
|
@ -92,23 +92,39 @@ func TestSplit(t *testing.T) {
|
||||
}
|
||||
|
||||
type JoinTest struct {
|
||||
dir, file, path string
|
||||
elem []string
|
||||
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"},
|
||||
// zero parameters
|
||||
JoinTest{[]string{}, ""},
|
||||
|
||||
// one parameter
|
||||
JoinTest{[]string{""}, ""},
|
||||
JoinTest{[]string{"a"}, "a"},
|
||||
|
||||
// two parameters
|
||||
JoinTest{[]string{"a", "b"}, "a/b"},
|
||||
JoinTest{[]string{"a", ""}, "a"},
|
||||
JoinTest{[]string{"", "b"}, "b"},
|
||||
JoinTest{[]string{"/", "a"}, "/a"},
|
||||
JoinTest{[]string{"/", ""}, "/"},
|
||||
JoinTest{[]string{"a/", "b"}, "a/b"},
|
||||
JoinTest{[]string{"a/", ""}, "a"},
|
||||
JoinTest{[]string{"", ""}, ""},
|
||||
}
|
||||
|
||||
// join takes a []string and passes it to Join.
|
||||
func join(elem []string, args ...string) string {
|
||||
args = elem
|
||||
return Join(args)
|
||||
}
|
||||
|
||||
func TestJoin(t *testing.T) {
|
||||
for _, 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)
|
||||
if p := join(test.elem); p != test.path {
|
||||
t.Errorf("join(%q) = %q, want %q", test.elem, p, test.path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user