diff options
| author | apocelipes <seve3r@outlook.com> | 2025-02-05 00:37:18 +0000 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2025-02-04 17:20:47 -0800 |
| commit | a1ea78c470d3136b7aed42a4d8b94497563f98ea (patch) | |
| tree | eeff0948e18d8ac6087b9120f0be65ab64fd13af /src | |
| parent | 0e35fb2f99ce4c249c0a42ad93a597835ae742b5 (diff) | |
| download | go-a1ea78c470d3136b7aed42a4d8b94497563f98ea.tar.xz | |
net: use strings.SplitSeq and bytes.SplitSeq
Replace `for _, s := range {strings, bytes}.Split(v, sep)` with
`for s := range {strings, bytes}.SplitSeq(v, sep)`, to simplify
the code and reduce some memory allocations.
Change-Id: Idead4de1e3928fc75cc5ba8caeff85542f1243d5
GitHub-Last-Rev: 5fb196a073e7583b23b1ebb446d6c067580ed63a
GitHub-Pull-Request: golang/go#71554
Reviewed-on: https://go-review.googlesource.com/c/go/+/646216
Reviewed-by: Damien Neil <dneil@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Auto-Submit: Ian Lance Taylor <iant@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/net/http/fs.go | 2 | ||||
| -rw-r--r-- | src/net/http/httptest/recorder.go | 2 | ||||
| -rw-r--r-- | src/net/http/httputil/reverseproxy.go | 2 | ||||
| -rw-r--r-- | src/net/http/httputil/reverseproxy_test.go | 2 | ||||
| -rw-r--r-- | src/net/http/main_test.go | 2 | ||||
| -rw-r--r-- | src/net/http/server.go | 2 | ||||
| -rw-r--r-- | src/net/main_test.go | 2 | ||||
| -rw-r--r-- | src/net/net_windows_test.go | 12 |
8 files changed, 11 insertions, 15 deletions
diff --git a/src/net/http/fs.go b/src/net/http/fs.go index e990f196d6..48ba05a664 100644 --- a/src/net/http/fs.go +++ b/src/net/http/fs.go @@ -1014,7 +1014,7 @@ func parseRange(s string, size int64) ([]httpRange, error) { } var ranges []httpRange noOverlap := false - for _, ra := range strings.Split(s[len(b):], ",") { + for ra := range strings.SplitSeq(s[len(b):], ",") { ra = textproto.TrimString(ra) if ra == "" { continue diff --git a/src/net/http/httptest/recorder.go b/src/net/http/httptest/recorder.go index dd51901b0d..17aa70f067 100644 --- a/src/net/http/httptest/recorder.go +++ b/src/net/http/httptest/recorder.go @@ -207,7 +207,7 @@ func (rw *ResponseRecorder) Result() *http.Response { if trailers, ok := rw.snapHeader["Trailer"]; ok { res.Trailer = make(http.Header, len(trailers)) for _, k := range trailers { - for _, k := range strings.Split(k, ",") { + for k := range strings.SplitSeq(k, ",") { k = http.CanonicalHeaderKey(textproto.TrimString(k)) if !httpguts.ValidTrailerHeader(k) { // Ignore since forbidden by RFC 7230, section 4.1.2. diff --git a/src/net/http/httputil/reverseproxy.go b/src/net/http/httputil/reverseproxy.go index d64d2fc3a1..15e9684708 100644 --- a/src/net/http/httputil/reverseproxy.go +++ b/src/net/http/httputil/reverseproxy.go @@ -577,7 +577,7 @@ func shouldPanicOnCopyError(req *http.Request) bool { func removeHopByHopHeaders(h http.Header) { // RFC 7230, section 6.1: Remove headers listed in the "Connection" header. for _, f := range h["Connection"] { - for _, sf := range strings.Split(f, ",") { + for sf := range strings.SplitSeq(f, ",") { if sf = textproto.TrimString(sf); sf != "" { h.Del(sf) } diff --git a/src/net/http/httputil/reverseproxy_test.go b/src/net/http/httputil/reverseproxy_test.go index 2f9a5eec5c..c618f6f19e 100644 --- a/src/net/http/httputil/reverseproxy_test.go +++ b/src/net/http/httputil/reverseproxy_test.go @@ -197,7 +197,7 @@ func TestReverseProxyStripHeadersPresentInConnection(t *testing.T) { c := r.Header["Connection"] var cf []string for _, f := range c { - for _, sf := range strings.Split(f, ",") { + for sf := range strings.SplitSeq(f, ",") { if sf = strings.TrimSpace(sf); sf != "" { cf = append(cf, sf) } diff --git a/src/net/http/main_test.go b/src/net/http/main_test.go index 4c18320717..0c58a94f20 100644 --- a/src/net/http/main_test.go +++ b/src/net/http/main_test.go @@ -31,7 +31,7 @@ func TestMain(m *testing.M) { func interestingGoroutines() (gs []string) { buf := make([]byte, 2<<20) buf = buf[:runtime.Stack(buf, true)] - for _, g := range strings.Split(string(buf), "\n\n") { + for g := range strings.SplitSeq(string(buf), "\n\n") { _, stack, _ := strings.Cut(g, "\n") stack = strings.TrimSpace(stack) if stack == "" || diff --git a/src/net/http/server.go b/src/net/http/server.go index 1e8e1437d2..cbdc9dd0e3 100644 --- a/src/net/http/server.go +++ b/src/net/http/server.go @@ -1590,7 +1590,7 @@ func foreachHeaderElement(v string, fn func(string)) { fn(v) return } - for _, f := range strings.Split(v, ",") { + for f := range strings.SplitSeq(v, ",") { if f = textproto.TrimString(f); f != "" { fn(f) } diff --git a/src/net/main_test.go b/src/net/main_test.go index e5767f7c7c..66735962f1 100644 --- a/src/net/main_test.go +++ b/src/net/main_test.go @@ -185,7 +185,7 @@ func runningGoroutines() []string { var gss []string b := make([]byte, 2<<20) b = b[:runtime.Stack(b, true)] - for _, s := range strings.Split(string(b), "\n\n") { + for s := range strings.SplitSeq(string(b), "\n\n") { _, stack, _ := strings.Cut(s, "\n") stack = strings.TrimSpace(stack) if !strings.Contains(stack, "created by net") { diff --git a/src/net/net_windows_test.go b/src/net/net_windows_test.go index 50554c05c5..480e89dfd7 100644 --- a/src/net/net_windows_test.go +++ b/src/net/net_windows_test.go @@ -240,8 +240,7 @@ func netshInterfaceIPShowInterface(ipver string, ifaces map[string]bool) error { //Metric : 10 //... var name string - lines := bytes.Split(out, []byte{'\r', '\n'}) - for _, line := range lines { + for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) { if bytes.HasPrefix(line, []byte("Interface ")) && bytes.HasSuffix(line, []byte(" Parameters")) { f := line[len("Interface "):] f = f[:len(f)-len(" Parameters")] @@ -330,8 +329,7 @@ func netshInterfaceIPv4ShowAddress(name string, netshOutput []byte) []string { addrs := make([]string, 0) var addr, subnetprefix string var processingOurInterface bool - lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) - for _, line := range lines { + for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) { if !processingOurInterface { if !bytes.HasPrefix(line, []byte("Configuration for interface")) { continue @@ -398,8 +396,7 @@ func netshInterfaceIPv6ShowAddress(name string, netshOutput []byte) []string { // TODO: need to test ipv6 netmask too, but netsh does not outputs it var addr string addrs := make([]string, 0) - lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) - for _, line := range lines { + for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) { if addr != "" { if len(line) == 0 { addr = "" @@ -584,8 +581,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) { want[cname] = addr group = make(map[string]string) } - lines := bytes.Split(out, []byte{'\r', '\n'}) - for _, line := range lines { + for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) { if len(line) == 0 { processGroup() continue |
