mirror of
https://github.com/golang/go
synced 2024-11-26 00:57:56 -07:00
path: Windows support for Split
Make Split work on backslashes as well as on slashes under Windows and support the "C:filename" special case. Also add corresponding tests. R=r, rsc, PeterGo, r2, brainman CC=golang-dev https://golang.org/cl/3008041
This commit is contained in:
parent
555feea117
commit
b06dc26a58
@ -9,4 +9,21 @@ GOFILES=\
|
|||||||
match.go\
|
match.go\
|
||||||
path.go\
|
path.go\
|
||||||
|
|
||||||
|
GOFILES_freebsd=\
|
||||||
|
path_unix.go
|
||||||
|
|
||||||
|
GOFILES_darwin=\
|
||||||
|
path_unix.go
|
||||||
|
|
||||||
|
GOFILES_linux=\
|
||||||
|
path_unix.go
|
||||||
|
|
||||||
|
GOFILES_nacl=\
|
||||||
|
path_unix.go
|
||||||
|
|
||||||
|
GOFILES_windows=\
|
||||||
|
path_windows.go
|
||||||
|
|
||||||
|
GOFILES+=$(GOFILES_$(GOOS))
|
||||||
|
|
||||||
include ../../Make.pkg
|
include ../../Make.pkg
|
||||||
|
@ -102,17 +102,13 @@ func Clean(path string) string {
|
|||||||
return string(buf[0:w])
|
return string(buf[0:w])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Split splits path immediately following the final slash,
|
// Split splits path immediately following the final path separator,
|
||||||
// separating it into a directory and file name component.
|
// separating it into a directory and file name component.
|
||||||
// If there is no slash in path, Split returns an empty dir and
|
// If there is no separator in path, Split returns an empty dir and
|
||||||
// file set to path.
|
// file set to path.
|
||||||
func Split(path string) (dir, file string) {
|
func Split(path string) (dir, file string) {
|
||||||
for i := len(path) - 1; i >= 0; i-- {
|
i := strings.LastIndexAny(path, PathSeps)
|
||||||
if path[i] == '/' {
|
return path[:i+1], path[i+1:]
|
||||||
return path[0 : i+1], path[i+1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return "", path
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Join joins any number of path elements into a single path, adding a
|
// Join joins any number of path elements into a single path, adding a
|
||||||
|
@ -6,6 +6,7 @@ package path
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -83,7 +84,18 @@ var splittests = []SplitTest{
|
|||||||
{"/", "/", ""},
|
{"/", "/", ""},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var winsplittests = []SplitTest{
|
||||||
|
{`C:\Windows\System32`, `C:\Windows\`, `System32`},
|
||||||
|
{`C:\Windows\`, `C:\Windows\`, ``},
|
||||||
|
{`C:\Windows`, `C:\`, `Windows`},
|
||||||
|
{`C:Windows`, `C:`, `Windows`},
|
||||||
|
{`\\?\c:\`, `\\?\c:\`, ``},
|
||||||
|
}
|
||||||
|
|
||||||
func TestSplit(t *testing.T) {
|
func TestSplit(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
splittests = append(splittests, winsplittests...)
|
||||||
|
}
|
||||||
for _, test := range splittests {
|
for _, test := range splittests {
|
||||||
if d, f := Split(test.path); d != test.dir || f != test.file {
|
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)
|
t.Errorf("Split(%q) = %q, %q, want %q, %q", test.path, d, f, test.dir, test.file)
|
||||||
|
11
src/pkg/path/path_unix.go
Normal file
11
src/pkg/path/path_unix.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Copyright 2010 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
|
||||||
|
|
||||||
|
const (
|
||||||
|
DirSeps = `/` // directory separators
|
||||||
|
VolumeSeps = `` // volume separators
|
||||||
|
PathSeps = DirSeps + VolumeSeps // all path separators
|
||||||
|
)
|
11
src/pkg/path/path_windows.go
Normal file
11
src/pkg/path/path_windows.go
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
// Copyright 2010 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
|
||||||
|
|
||||||
|
const (
|
||||||
|
DirSeps = `\/` // directory separators
|
||||||
|
VolumeSeps = `:` // volume separators
|
||||||
|
PathSeps = DirSeps + VolumeSeps // all path separators
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user