]> Cypherpunks repositories - gostls13.git/commitdiff
net: use strings.SplitSeq and bytes.SplitSeq
authorapocelipes <seve3r@outlook.com>
Wed, 5 Feb 2025 00:37:18 +0000 (00:37 +0000)
committerGopher Robot <gobot@golang.org>
Wed, 5 Feb 2025 01:20:47 +0000 (17:20 -0800)
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>

src/net/http/fs.go
src/net/http/httptest/recorder.go
src/net/http/httputil/reverseproxy.go
src/net/http/httputil/reverseproxy_test.go
src/net/http/main_test.go
src/net/http/server.go
src/net/main_test.go
src/net/net_windows_test.go

index e990f196d6d904f84bc8f1eb9322fa9cc0384ebb..48ba05a664dbafaefc385fecf5455e4d95268560 100644 (file)
@@ -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
index dd51901b0d3b948c5fc0519127a4cdb0009748f2..17aa70f06760a2e7adf34e879bdbfa465de9fc8b 100644 (file)
@@ -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.
index d64d2fc3a18ee3f86b7775f3bfdd9913dddd9f26..15e968470815d475f309675e1f3c6af0c8e9f463 100644 (file)
@@ -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)
                        }
index 2f9a5eec5cce4ce65c5508c6b0425e8a68754ffa..c618f6f19e37065c30e9d079b1ecaf8a0218fa12 100644 (file)
@@ -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)
                                }
index 4c1832071704e243c7155243a1842c58ed0136a4..0c58a94f20fc24c55fe0544146154bebc50e389f 100644 (file)
@@ -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 == "" ||
index 1e8e1437d26832c07ecacba7a598132ee8945ed3..cbdc9dd0e3af60e6ed452f57336ec9381b11c801 100644 (file)
@@ -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)
                }
index e5767f7c7c12f6482b552d196b0951db75c1a901..66735962f103737846f6460cb8c89a9b3951f51c 100644 (file)
@@ -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") {
index 50554c05c5eb2a7d237489200ef36d80a89d5423..480e89dfd74945b5246d2a092af0c361e722ead4 100644 (file)
@@ -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