diff options
| author | Russ Cox <rsc@golang.org> | 2021-09-22 10:46:32 -0400 |
|---|---|---|
| committer | Russ Cox <rsc@golang.org> | 2021-10-06 15:53:04 +0000 |
| commit | 4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee (patch) | |
| tree | 1e850efb295d4c5f0589e46bd8d9f1930d4af0b5 /src/net/http/cgi | |
| parent | 8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff) | |
| download | go-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.tar.xz | |
all: use bytes.Cut, strings.Cut
Many uses of Index/IndexByte/IndexRune/Split/SplitN
can be written more clearly using the new Cut functions.
Do that. Also rewrite to other functions if that's clearer.
For #46336.
Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448
Reviewed-on: https://go-review.googlesource.com/c/go/+/351711
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>
Diffstat (limited to 'src/net/http/cgi')
| -rw-r--r-- | src/net/http/cgi/child.go | 4 | ||||
| -rw-r--r-- | src/net/http/cgi/host.go | 5 | ||||
| -rw-r--r-- | src/net/http/cgi/host_test.go | 10 |
3 files changed, 9 insertions, 10 deletions
diff --git a/src/net/http/cgi/child.go b/src/net/http/cgi/child.go index 0114da377b..bdb35a64e5 100644 --- a/src/net/http/cgi/child.go +++ b/src/net/http/cgi/child.go @@ -39,8 +39,8 @@ func Request() (*http.Request, error) { func envMap(env []string) map[string]string { m := make(map[string]string) for _, kv := range env { - if idx := strings.Index(kv, "="); idx != -1 { - m[kv[:idx]] = kv[idx+1:] + if k, v, ok := strings.Cut(kv, "="); ok { + m[k] = v } } return m diff --git a/src/net/http/cgi/host.go b/src/net/http/cgi/host.go index eff67caf4e..e7124a2ab0 100644 --- a/src/net/http/cgi/host.go +++ b/src/net/http/cgi/host.go @@ -273,12 +273,11 @@ func (h *Handler) ServeHTTP(rw http.ResponseWriter, req *http.Request) { break } headerLines++ - parts := strings.SplitN(string(line), ":", 2) - if len(parts) < 2 { + header, val, ok := strings.Cut(string(line), ":") + if !ok { h.printf("cgi: bogus header line: %s", string(line)) continue } - header, val := parts[0], parts[1] if !httpguts.ValidHeaderFieldName(header) { h.printf("cgi: invalid header name: %q", header) continue diff --git a/src/net/http/cgi/host_test.go b/src/net/http/cgi/host_test.go index fb869a6728..f8abc88c89 100644 --- a/src/net/http/cgi/host_test.go +++ b/src/net/http/cgi/host_test.go @@ -62,12 +62,12 @@ readlines: } linesRead++ trimmedLine := strings.TrimRight(line, "\r\n") - split := strings.SplitN(trimmedLine, "=", 2) - if len(split) != 2 { - t.Fatalf("Unexpected %d parts from invalid line number %v: %q; existing map=%v", - len(split), linesRead, line, m) + k, v, ok := strings.Cut(trimmedLine, "=") + if !ok { + t.Fatalf("Unexpected response from invalid line number %v: %q; existing map=%v", + linesRead, line, m) } - m[split[0]] = split[1] + m[k] = v } for key, expected := range expectedMap { |
