diff options
author | Ian Lance Taylor <iant@golang.org> | 2020-12-23 09:57:37 -0800 |
---|---|---|
committer | Ian Lance Taylor <iant@golang.org> | 2020-12-30 15:13:24 -0800 |
commit | cfcbb4227fb20191e04eb8d7766ae6202f526afd (patch) | |
tree | e2effea96f6f204451779f044415c2385e45042b /libgo/go/path/path.go | |
parent | 0696141107d61483f38482b941549959a0d7f613 (diff) | |
download | gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.zip gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.gz gcc-cfcbb4227fb20191e04eb8d7766ae6202f526afd.tar.bz2 |
libgo: update to Go1.16beta1 release
This does not yet include support for the //go:embed directive added
in this release.
* Makefile.am (check-runtime): Don't create check-runtime-dir.
(mostlyclean-local): Don't remove check-runtime-dir.
(check-go-tool, check-vet): Copy in go.mod and modules.txt.
(check-cgo-test, check-carchive-test): Add go.mod file.
* Makefile.in: Regenerate.
Reviewed-on: https://go-review.googlesource.com/c/gofrontend/+/280172
Diffstat (limited to 'libgo/go/path/path.go')
-rw-r--r-- | libgo/go/path/path.go | 36 |
1 files changed, 26 insertions, 10 deletions
diff --git a/libgo/go/path/path.go b/libgo/go/path/path.go index c513114..f1f3499 100644 --- a/libgo/go/path/path.go +++ b/libgo/go/path/path.go @@ -11,10 +11,6 @@ // operating system paths, use the path/filepath package. package path -import ( - "strings" -) - // A lazybuf is a lazily constructed path buffer. // It supports append, reading previously appended bytes, // and retrieving the final string. It does not allocate a buffer @@ -139,13 +135,22 @@ func Clean(path string) string { return out.string() } +// lastSlash(s) is strings.LastIndex(s, "/") but we can't import strings. +func lastSlash(s string) int { + i := len(s) - 1 + for i >= 0 && s[i] != '/' { + i-- + } + return i +} + // Split splits path immediately following the final slash, // separating it into a directory and file name component. // If there is no slash in path, Split returns an empty dir and // file set to path. // The returned values have the property that path = dir+file. func Split(path string) (dir, file string) { - i := strings.LastIndex(path, "/") + i := lastSlash(path) return path[:i+1], path[i+1:] } @@ -155,12 +160,23 @@ func Split(path string) (dir, file string) { // empty or all its elements are empty, Join returns // an empty string. func Join(elem ...string) string { - for i, e := range elem { - if e != "" { - return Clean(strings.Join(elem[i:], "/")) + size := 0 + for _, e := range elem { + size += len(e) + } + if size == 0 { + return "" + } + buf := make([]byte, 0, size+len(elem)-1) + for _, e := range elem { + if len(buf) > 0 || e != "" { + if len(buf) > 0 { + buf = append(buf, '/') + } + buf = append(buf, e...) } } - return "" + return Clean(string(buf)) } // Ext returns the file name extension used by path. @@ -189,7 +205,7 @@ func Base(path string) string { path = path[0 : len(path)-1] } // Find the last element - if i := strings.LastIndex(path, "/"); i >= 0 { + if i := lastSlash(path); i >= 0 { path = path[i+1:] } // If empty now, it had only slashes. |