Skip to content

Commit 2c74018

Browse files
tklausergopherbot
authored andcommitted
path: use bytealg.LastIndexByteString
While strings.LastIndex{,Byte} cannot be used in package path, the respective internal/bytealg function can be used. Change-Id: If0ecc36484308221f50875c8609913f6f2887fba Reviewed-on: https://go-review.googlesource.com/c/go/+/527855 Auto-Submit: Tobias Klauser <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent 3e1db32 commit 2c74018

File tree

1 file changed

+4
-11
lines changed

1 file changed

+4
-11
lines changed

src/path/path.go

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

14+
import "internal/bytealg"
15+
1416
// A lazybuf is a lazily constructed path buffer.
1517
// It supports append, reading previously appended bytes,
1618
// and retrieving the final string. It does not allocate a buffer
@@ -135,22 +137,13 @@ func Clean(path string) string {
135137
return out.string()
136138
}
137139

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-
147140
// Split splits path immediately following the final slash,
148141
// separating it into a directory and file name component.
149142
// If there is no slash in path, Split returns an empty dir and
150143
// file set to path.
151144
// The returned values have the property that path = dir+file.
152145
func Split(path string) (dir, file string) {
153-
i := lastSlash(path)
146+
i := bytealg.LastIndexByteString(path, '/')
154147
return path[:i+1], path[i+1:]
155148
}
156149

@@ -205,7 +198,7 @@ func Base(path string) string {
205198
path = path[0 : len(path)-1]
206199
}
207200
// Find the last element
208-
if i := lastSlash(path); i >= 0 {
201+
if i := bytealg.LastIndexByteString(path, '/'); i >= 0 {
209202
path = path[i+1:]
210203
}
211204
// If empty now, it had only slashes.

0 commit comments

Comments
 (0)