many: use strings.Cut
Change-Id: I63aff7182ffba765bbd5b104089562b04bff0960
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/388956
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
diff --git a/internal/config/config.go b/internal/config/config.go
index 8faa521..bda15bf 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -27,7 +27,6 @@
"cloud.google.com/go/storage"
"github.com/ghodss/yaml"
"golang.org/x/net/context/ctxhttp"
- "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/derrors"
"golang.org/x/pkgsite/internal/log"
"golang.org/x/pkgsite/internal/secrets"
@@ -296,7 +295,7 @@
if c.ServiceID == "" {
return "local"
}
- before, _, found := internal.Cut(c.ServiceID, "-")
+ before, _, found := strings.Cut(c.ServiceID, "-")
if !found {
return "prod"
}
@@ -312,7 +311,7 @@
if c.ServiceID == "" {
return "unknownApp"
}
- before, after, found := internal.Cut(c.ServiceID, "-")
+ before, after, found := strings.Cut(c.ServiceID, "-")
var svc string
if !found {
svc = before
diff --git a/internal/config/dynconfig/dynconfig.go b/internal/config/dynconfig/dynconfig.go
index 29e65d1..5708730 100644
--- a/internal/config/dynconfig/dynconfig.go
+++ b/internal/config/dynconfig/dynconfig.go
@@ -40,7 +40,7 @@
log.Debugf(ctx, "reading dynamic config from %s", location)
var r io.ReadCloser
if strings.HasPrefix(location, "gs://") {
- bucket, object, found := internal.Cut(location[5:], "/")
+ bucket, object, found := strings.Cut(location[5:], "/")
if !found {
return nil, errors.New("bad GCS URL")
}
diff --git a/internal/frontend/directory.go b/internal/frontend/directory.go
index bda7d9a..5a1a421 100644
--- a/internal/frontend/directory.go
+++ b/internal/frontend/directory.go
@@ -59,7 +59,7 @@
// subdirectories are grouped.
mappedDirs := make(map[string]*Directory)
for _, d := range directories {
- prefix, _, _ := internal.Cut(d.Suffix, "/")
+ prefix, _, _ := strings.Cut(d.Suffix, "/")
// Skip internal directories that are not in the top level internal
// directory. For example, foo/internal and foo/internal/bar should
diff --git a/internal/frontend/symbol.go b/internal/frontend/symbol.go
index f21f0c4..0d52af9 100644
--- a/internal/frontend/symbol.go
+++ b/internal/frontend/symbol.go
@@ -326,7 +326,7 @@
return
}
for _, b := range builds {
- goos, _, _ := internal.Cut(b, "/")
+ goos, _, _ := strings.Cut(b, "/")
var build internal.BuildContext
switch goos {
case "linux":
diff --git a/internal/frontend/urlinfo.go b/internal/frontend/urlinfo.go
index 7565070..cadac79 100644
--- a/internal/frontend/urlinfo.go
+++ b/internal/frontend/urlinfo.go
@@ -52,7 +52,7 @@
func extractURLPathInfo(urlPath string) (_ *urlPathInfo, err error) {
defer derrors.Wrap(&err, "extractURLPathInfo(%q)", urlPath)
- if m, _, _ := internal.Cut(strings.TrimPrefix(urlPath, "/"), "@"); stdlib.Contains(m) {
+ if m, _, _ := strings.Cut(strings.TrimPrefix(urlPath, "/"), "@"); stdlib.Contains(m) {
return parseStdLibURLPath(urlPath)
}
return parseDetailsURLPath(urlPath)
@@ -91,7 +91,7 @@
// /<module-path>, @<version>/<suffix>
// or
// /<module-path>/<suffix>, @<version>
- modulePath, rest, found := internal.Cut(urlPath, "@")
+ modulePath, rest, found := strings.Cut(urlPath, "@")
info := &urlPathInfo{
fullPath: strings.TrimSuffix(strings.TrimPrefix(modulePath, "/"), "/"),
modulePath: internal.UnknownModulePath,
@@ -136,7 +136,7 @@
// This splits urlPath into either:
// /<path>@<tag> or /<path>
- fullPath, tag, found := internal.Cut(urlPath, "@")
+ fullPath, tag, found := strings.Cut(urlPath, "@")
fullPath = strings.TrimSuffix(strings.TrimPrefix(fullPath, "/"), "/")
if !isValidPath(fullPath) {
return nil, &userError{
diff --git a/internal/godoc/dochtml/internal/render/linkify.go b/internal/godoc/dochtml/internal/render/linkify.go
index 347c5b0..681a64b 100644
--- a/internal/godoc/dochtml/internal/render/linkify.go
+++ b/internal/godoc/dochtml/internal/render/linkify.go
@@ -21,7 +21,6 @@
"github.com/google/safehtml"
"github.com/google/safehtml/legacyconversions"
"github.com/google/safehtml/template"
- "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/godoc/internal/doc"
"golang.org/x/pkgsite/internal/log"
)
@@ -150,7 +149,7 @@
if !strings.HasPrefix(line, "- ") && !strings.HasPrefix(line, "-\t") {
return nil
}
- text, href, found := internal.Cut(line[2:], ",")
+ text, href, found := strings.Cut(line[2:], ",")
if !found {
return nil
}
diff --git a/internal/middleware/quota.go b/internal/middleware/quota.go
index 8fd6cd4..70bde53 100644
--- a/internal/middleware/quota.go
+++ b/internal/middleware/quota.go
@@ -20,7 +20,6 @@
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
- "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/log"
)
@@ -49,7 +48,7 @@
}
func ipKey(s string) string {
- addr, _, _ := internal.Cut(s, ",")
+ addr, _, _ := strings.Cut(s, ",")
// First field is the originating IP address.
origin := strings.TrimSpace(addr)
ip := net.ParseIP(origin)
diff --git a/internal/postgres/search.go b/internal/postgres/search.go
index faeb72c..71d63c4 100644
--- a/internal/postgres/search.go
+++ b/internal/postgres/search.go
@@ -562,7 +562,7 @@
// Packages in the standard library are grouped by their top-level
// directory, and we can consider them all part of the same major version.
if r.ModulePath == stdlib.ModulePath {
- before, _, _ := internal.Cut(r.PackagePath, "/")
+ before, _, _ := strings.Cut(r.PackagePath, "/")
return before, 1
}
series, major := internal.SeriesPathAndMajorVersion(r.ModulePath)
diff --git a/internal/util.go b/internal/util.go
index eb7f9bc..a803139 100644
--- a/internal/util.go
+++ b/internal/util.go
@@ -37,18 +37,3 @@
}
return lines, nil
}
-
-// Cut cuts s around the first instance of sep,
-// returning the text before and after sep.
-// The found result reports whether sep appears in s.
-// If sep does not appear in s, cut returns s, "", false.
-//
-// https://golang.org/issue/46336 is an accepted proposal to add this to the
-// standard library. It will presumably land in Go 1.18, so this can be removed
-// when pkgsite moves to that version.
-func Cut(s, sep string) (before, after string, found bool) {
- if i := strings.Index(s, sep); i >= 0 {
- return s[:i], s[i+len(sep):], true
- }
- return s, "", false
-}
diff --git a/tests/search/main.go b/tests/search/main.go
index b82b105..508f4a3 100755
--- a/tests/search/main.go
+++ b/tests/search/main.go
@@ -18,7 +18,6 @@
"strings"
_ "github.com/jackc/pgx/v4/stdlib" // for pgx driver
- "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/database"
"golang.org/x/pkgsite/internal/derrors"
@@ -261,7 +260,7 @@
if line == "" || line[0] == '#' {
continue
}
- path, count, found := internal.Cut(line, ", ")
+ path, count, found := strings.Cut(line, ", ")
if !found {
return nil, errors.New("missing comma")
}