diff options
| author | Jonathan Amsterdam <jba@google.com> | 2021-09-06 12:04:47 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2021-09-07 15:14:39 +0000 |
| commit | 7624e4766ea785e034eef99e45d5df11778722d0 (patch) | |
| tree | 0c090aa37e709743ba829e6bfdbd70c84ec6c37f | |
| parent | a7412093f9cf2b3f8887269f53f0d6ece5e3ba2a (diff) | |
| download | go-x-pkgsite-7624e4766ea785e034eef99e45d5df11778722d0.tar.xz | |
many: replace most uses of strings.SplitN with Cut
In most cases Cut(s, sep) is clearer than strings.SplitN(s, sep, 2).
Change-Id: I375c15f778e43ff25b5b74c9ad0e96dac4615786
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/347889
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
TryBot-Result: kokoro <noreply+kokoro@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
| -rw-r--r-- | internal/config/config.go | 17 | ||||
| -rw-r--r-- | internal/config/dynconfig/dynconfig.go | 6 | ||||
| -rw-r--r-- | internal/frontend/directory.go | 2 | ||||
| -rw-r--r-- | internal/frontend/search.go | 14 | ||||
| -rw-r--r-- | internal/frontend/symbol.go | 4 | ||||
| -rw-r--r-- | internal/frontend/urlinfo.go | 19 | ||||
| -rw-r--r-- | internal/godoc/dochtml/internal/render/linkify.go | 11 | ||||
| -rw-r--r-- | internal/middleware/quota.go | 5 | ||||
| -rwxr-xr-x | tests/search/main.go | 10 |
9 files changed, 45 insertions, 43 deletions
diff --git a/internal/config/config.go b/internal/config/config.go index 7b1e9349..3f243db7 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -27,6 +27,7 @@ import ( "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" @@ -277,14 +278,14 @@ func (c *Config) DeploymentEnvironment() string { if c.ServiceID == "" { return "local" } - parts := strings.SplitN(c.ServiceID, "-", 2) - if len(parts) == 1 { + before, _, found := internal.Cut(c.ServiceID, "-") + if !found { return "prod" } - if parts[0] == "" { + if before == "" { return "unknownEnv" } - return parts[0] + return before } // Application returns the name of the running application: "worker", @@ -293,12 +294,12 @@ func (c *Config) Application() string { if c.ServiceID == "" { return "unknownApp" } - parts := strings.SplitN(c.ServiceID, "-", 2) + before, after, found := internal.Cut(c.ServiceID, "-") var svc string - if len(parts) == 1 { - svc = parts[0] + if !found { + svc = before } else { - svc = parts[1] + svc = after } switch svc { case "default": diff --git a/internal/config/dynconfig/dynconfig.go b/internal/config/dynconfig/dynconfig.go index 9c2e2342..29e65d17 100644 --- a/internal/config/dynconfig/dynconfig.go +++ b/internal/config/dynconfig/dynconfig.go @@ -40,12 +40,10 @@ func Read(ctx context.Context, location string) (_ *DynamicConfig, err error) { log.Debugf(ctx, "reading dynamic config from %s", location) var r io.ReadCloser if strings.HasPrefix(location, "gs://") { - parts := strings.SplitN(location[5:], "/", 2) - if len(parts) != 2 { + bucket, object, found := internal.Cut(location[5:], "/") + if !found { return nil, errors.New("bad GCS URL") } - bucket := parts[0] - object := parts[1] if err != nil { return nil, err } diff --git a/internal/frontend/directory.go b/internal/frontend/directory.go index 8dfa1934..bda7d9aa 100644 --- a/internal/frontend/directory.go +++ b/internal/frontend/directory.go @@ -59,7 +59,7 @@ func unitDirectories(directories []*DirectoryInfo) *Directories { // subdirectories are grouped. mappedDirs := make(map[string]*Directory) for _, d := range directories { - prefix := strings.SplitN(d.Suffix, "/", 2)[0] + prefix, _, _ := internal.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/search.go b/internal/frontend/search.go index 5745c86a..ae601d8d 100644 --- a/internal/frontend/search.go +++ b/internal/frontend/search.go @@ -398,17 +398,17 @@ func searchQuery(r *http.Request) (q string, searchSymbols bool) { if strings.HasPrefix(q, "#") { return strings.TrimPrefix(q, "#"), true } - if strings.Contains(q, ":") { - parts := strings.SplitN(q, ":", 2) - if searchModeSymbolKeyboardShortcuts[parts[0]] { - return parts[1], true + mode, query, found := internal.Cut(q, ":") + if found { + if searchModeSymbolKeyboardShortcuts[mode] { + return query, true } - if searchModePackageKeyboardShortcuts[parts[0]] { - return parts[1], false + if searchModePackageKeyboardShortcuts[mode] { + return query, false } return q, false } - mode := strings.TrimSpace(r.FormValue("m")) + mode = strings.TrimSpace(r.FormValue("m")) if mode == searchModePackage { return q, false } diff --git a/internal/frontend/symbol.go b/internal/frontend/symbol.go index 5284acfb..f21f0c46 100644 --- a/internal/frontend/symbol.go +++ b/internal/frontend/symbol.go @@ -326,9 +326,9 @@ func addSymbol(s *Symbol, v string, sh *internal.SymbolHistory, builds []string) return } for _, b := range builds { - parts := strings.SplitN(b, "/", 2) + goos, _, _ := internal.Cut(b, "/") var build internal.BuildContext - switch parts[0] { + switch goos { case "linux": build = internal.BuildContextLinux case "darwin": diff --git a/internal/frontend/urlinfo.go b/internal/frontend/urlinfo.go index 44559ba4..887b0994 100644 --- a/internal/frontend/urlinfo.go +++ b/internal/frontend/urlinfo.go @@ -52,8 +52,7 @@ func (e *userError) Unwrap() error { func extractURLPathInfo(urlPath string) (_ *urlPathInfo, err error) { defer derrors.Wrap(&err, "extractURLPathInfo(%q)", urlPath) - parts := strings.SplitN(strings.TrimPrefix(urlPath, "/"), "@", 2) - if stdlib.Contains(parts[0]) { + if m, _, _ := internal.Cut(strings.TrimPrefix(urlPath, "/"), "@"); stdlib.Contains(m) { return parseStdLibURLPath(urlPath) } return parseDetailsURLPath(urlPath) @@ -92,16 +91,16 @@ func parseDetailsURLPath(urlPath string) (_ *urlPathInfo, err error) { // /<module-path>, @<version>/<suffix> // or // /<module-path>/<suffix>, @<version> - parts := strings.SplitN(urlPath, "@", 2) + modulePath, rest, found := internal.Cut(urlPath, "@") info := &urlPathInfo{ - fullPath: strings.TrimSuffix(strings.TrimPrefix(parts[0], "/"), "/"), + fullPath: strings.TrimSuffix(strings.TrimPrefix(modulePath, "/"), "/"), modulePath: internal.UnknownModulePath, requestedVersion: version.Latest, } - if len(parts) != 1 { + if found { // The urlPath contains a "@". Parse the version and suffix from // parts[1], the string after the '@'. - endParts := strings.Split(parts[1], "/") + endParts := strings.Split(rest, "/") // Parse the requestedVersion from the urlPath. // The first path component after the '@' is the version. @@ -137,8 +136,8 @@ func parseStdLibURLPath(urlPath string) (_ *urlPathInfo, err error) { // This splits urlPath into either: // /<path>@<tag> or /<path> - parts := strings.SplitN(urlPath, "@", 2) - fullPath := strings.TrimSuffix(strings.TrimPrefix(parts[0], "/"), "/") + fullPath, tag, found := internal.Cut(urlPath, "@") + fullPath = strings.TrimSuffix(strings.TrimPrefix(fullPath, "/"), "/") if !isValidPath(fullPath) { return nil, &userError{ err: fmt.Errorf("isValidPath(%q) is false", fullPath), @@ -150,11 +149,11 @@ func parseStdLibURLPath(urlPath string) (_ *urlPathInfo, err error) { fullPath: fullPath, modulePath: stdlib.ModulePath, } - if len(parts) == 1 { + if !found { info.requestedVersion = version.Latest return info, nil } - tag := strings.TrimSuffix(parts[1], "/") + tag = strings.TrimSuffix(tag, "/") info.requestedVersion = stdlib.VersionForTag(tag) if info.requestedVersion == "" { return nil, &userError{ diff --git a/internal/godoc/dochtml/internal/render/linkify.go b/internal/godoc/dochtml/internal/render/linkify.go index ae2a3fc8..cdb61110 100644 --- a/internal/godoc/dochtml/internal/render/linkify.go +++ b/internal/godoc/dochtml/internal/render/linkify.go @@ -21,6 +21,7 @@ import ( "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" ) @@ -152,15 +153,13 @@ func parseLink(line string) *Link { if !strings.HasPrefix(line, "- ") && !strings.HasPrefix(line, "-\t") { return nil } - parts := strings.SplitN(line[2:], ",", 2) - if len(parts) != 2 { + text, href, found := internal.Cut(line[2:], ",") + if !found { return nil } - text := strings.TrimSpace(parts[0]) - href := strings.TrimSpace(parts[1]) return &Link{ - Text: text, - Href: href, + Text: strings.TrimSpace(text), + Href: strings.TrimSpace(href), } } diff --git a/internal/middleware/quota.go b/internal/middleware/quota.go index a2601824..8fd6cd45 100644 --- a/internal/middleware/quota.go +++ b/internal/middleware/quota.go @@ -20,6 +20,7 @@ import ( "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" ) @@ -48,9 +49,9 @@ func recordQuotaMetric(ctx context.Context, blocked string) { } func ipKey(s string) string { - fields := strings.SplitN(s, ",", 2) + addr, _, _ := internal.Cut(s, ",") // First field is the originating IP address. - origin := strings.TrimSpace(fields[0]) + origin := strings.TrimSpace(addr) ip := net.ParseIP(origin) if ip == nil { return "" diff --git a/tests/search/main.go b/tests/search/main.go index 2d1d5675..9acfa7ef 100755 --- a/tests/search/main.go +++ b/tests/search/main.go @@ -10,6 +10,7 @@ package main import ( "bufio" "context" + "errors" "flag" "fmt" "os" @@ -257,12 +258,15 @@ func readImportedByCounts(filename string) (map[string]int, error) { if line == "" || line[0] == '#' { continue } - parts := strings.SplitN(line, ", ", 2) - c, err := strconv.Atoi(parts[1]) + path, count, found := internal.Cut(line, ", ") + if !found { + return nil, errors.New("missing comma") + } + c, err := strconv.Atoi(count) if err != nil { return nil, err } - counts[parts[0]] = c + counts[path] = c } if err := scan.Err(); err != nil { return nil, err |
