Skip to content

Commit ec095f1

Browse files
committed
path: avoid import of strings
Pushing path lower in the hierarchy, to allow path < io/fs < os in the io/fs prototype. But this change is worth doing even if io/fs is not accepted. Change-Id: Id51b3a638167ca005dadfb9b730287e518ec12a8 Reviewed-on: https://go-review.googlesource.com/c/go/+/243904 Trust: Russ Cox <[email protected]> Run-TryBot: Russ Cox <[email protected]> TryBot-Result: Go Bot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent 84f3b33 commit ec095f1

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

src/go/build/deps_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,11 @@ var depsRules = `
101101
102102
reflect !< sort;
103103
104+
RUNTIME, unicode/utf8
105+
< path;
106+
107+
unicode !< path;
108+
104109
# SYSCALL is RUNTIME plus the packages necessary for basic system calls.
105110
RUNTIME, unicode/utf8, unicode/utf16
106111
< internal/syscall/windows/sysdll, syscall/js
@@ -137,7 +142,7 @@ var depsRules = `
137142
# STR is basic string and buffer manipulation.
138143
RUNTIME, io, unicode/utf8, unicode/utf16, unicode
139144
< bytes, strings
140-
< bufio, path;
145+
< bufio;
141146
142147
bufio, path, strconv
143148
< STR;

src/path/match.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ package path
66

77
import (
88
"errors"
9-
"strings"
9+
"internal/bytealg"
1010
"unicode/utf8"
1111
)
1212

@@ -43,7 +43,7 @@ Pattern:
4343
star, chunk, pattern = scanChunk(pattern)
4444
if star && chunk == "" {
4545
// Trailing * matches rest of string unless it has a /.
46-
return !strings.Contains(name, "/"), nil
46+
return bytealg.IndexByteString(name, '/') < 0, nil
4747
}
4848
// Look for match at current position.
4949
t, ok, err := matchChunk(chunk, name)

src/path/match_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package path
5+
package path_test
66

7-
import "testing"
7+
import (
8+
. "path"
9+
"testing"
10+
)
811

912
type MatchTest struct {
1013
pattern, s string

src/path/path.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
// operating system paths, use the path/filepath package.
1212
package path
1313

14-
import (
15-
"strings"
16-
)
17-
1814
// A lazybuf is a lazily constructed path buffer.
1915
// It supports append, reading previously appended bytes,
2016
// and retrieving the final string. It does not allocate a buffer
@@ -139,13 +135,22 @@ func Clean(path string) string {
139135
return out.string()
140136
}
141137

138+
// lastSlash(s) is strings.LastIndex(s, "/") but we can't import strings.
139+
func lastSlash(s string) int {
140+
i := len(s) - 1
141+
for i >= 0 && s[i] != '/' {
142+
i--
143+
}
144+
return i
145+
}
146+
142147
// Split splits path immediately following the final slash,
143148
// separating it into a directory and file name component.
144149
// If there is no slash in path, Split returns an empty dir and
145150
// file set to path.
146151
// The returned values have the property that path = dir+file.
147152
func Split(path string) (dir, file string) {
148-
i := strings.LastIndex(path, "/")
153+
i := lastSlash(path)
149154
return path[:i+1], path[i+1:]
150155
}
151156

@@ -155,12 +160,23 @@ func Split(path string) (dir, file string) {
155160
// empty or all its elements are empty, Join returns
156161
// an empty string.
157162
func Join(elem ...string) string {
158-
for i, e := range elem {
159-
if e != "" {
160-
return Clean(strings.Join(elem[i:], "/"))
163+
size := 0
164+
for _, e := range elem {
165+
size += len(e)
166+
}
167+
if size == 0 {
168+
return ""
169+
}
170+
buf := make([]byte, 0, size+len(elem)-1)
171+
for _, e := range elem {
172+
if len(buf) > 0 || e != "" {
173+
if len(buf) > 0 {
174+
buf = append(buf, '/')
175+
}
176+
buf = append(buf, e...)
161177
}
162178
}
163-
return ""
179+
return Clean(string(buf))
164180
}
165181

166182
// Ext returns the file name extension used by path.
@@ -189,7 +205,7 @@ func Base(path string) string {
189205
path = path[0 : len(path)-1]
190206
}
191207
// Find the last element
192-
if i := strings.LastIndex(path, "/"); i >= 0 {
208+
if i := lastSlash(path); i >= 0 {
193209
path = path[i+1:]
194210
}
195211
// If empty now, it had only slashes.

src/path/path_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
package path
5+
package path_test
66

77
import (
8+
. "path"
89
"runtime"
910
"testing"
1011
)

0 commit comments

Comments
 (0)