]> Cypherpunks.ru repositories - gostls13.git/commitdiff
path: avoid import of strings
authorRuss Cox <rsc@golang.org>
Tue, 14 Jul 2020 02:24:52 +0000 (22:24 -0400)
committerRuss Cox <rsc@golang.org>
Tue, 13 Oct 2020 01:12:09 +0000 (01:12 +0000)
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 <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
src/go/build/deps_test.go
src/path/match.go
src/path/match_test.go
src/path/path.go
src/path/path_test.go

index 1edd96c5e37dded4c588c4879e34877c182c4d15..79836c0d674f673a884c2a3f204bd4e4cd5b6803 100644 (file)
@@ -101,6 +101,11 @@ var depsRules = `
 
        reflect !< sort;
 
+       RUNTIME, unicode/utf8
+       < path;
+
+       unicode !< path;
+
        # SYSCALL is RUNTIME plus the packages necessary for basic system calls.
        RUNTIME, unicode/utf8, unicode/utf16
        < internal/syscall/windows/sysdll, syscall/js
@@ -137,7 +142,7 @@ var depsRules = `
        # STR is basic string and buffer manipulation.
        RUNTIME, io, unicode/utf8, unicode/utf16, unicode
        < bytes, strings
-       < bufio, path;
+       < bufio;
 
        bufio, path, strconv
        < STR;
index d39d24450a9b5fdc94d6845f1886d483e705e899..837eb8bb8b724777695caf0e067cf9f9518c0d57 100644 (file)
@@ -6,7 +6,7 @@ package path
 
 import (
        "errors"
-       "strings"
+       "internal/bytealg"
        "unicode/utf8"
 )
 
@@ -43,7 +43,7 @@ Pattern:
                star, chunk, pattern = scanChunk(pattern)
                if star && chunk == "" {
                        // Trailing * matches rest of string unless it has a /.
-                       return !strings.Contains(name, "/"), nil
+                       return bytealg.IndexByteString(name, '/') < 0, nil
                }
                // Look for match at current position.
                t, ok, err := matchChunk(chunk, name)
index 127180e5700d65690f4e87743844dc3ecbc5d1a5..3e027e1f682a481eaef2a0d5c537768f690e4a72 100644 (file)
@@ -2,9 +2,12 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package path
+package path_test
 
-import "testing"
+import (
+       . "path"
+       "testing"
+)
 
 type MatchTest struct {
        pattern, s string
index c513114b4d7df3c692a286382e0ac54f0868a484..f1f3499f63e00cc524ff11fec6cc148176a341d4 100644 (file)
 // 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.
index 2a3635300ecbb66ebabc9774ee82842a7241f885..a57286f6b8468b17de80e4ca3ffa78d90820dfac 100644 (file)
@@ -2,9 +2,10 @@
 // Use of this source code is governed by a BSD-style
 // license that can be found in the LICENSE file.
 
-package path
+package path_test
 
 import (
+       . "path"
        "runtime"
        "testing"
 )