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>
This commit is contained in:
apocelipes 2025-02-05 00:37:18 +00:00 committed by Gopher Robot
parent 0e35fb2f99
commit a1ea78c470
8 changed files with 11 additions and 15 deletions

View File

@ -1014,7 +1014,7 @@ func parseRange(s string, size int64) ([]httpRange, error) {
} }
var ranges []httpRange var ranges []httpRange
noOverlap := false noOverlap := false
for _, ra := range strings.Split(s[len(b):], ",") { for ra := range strings.SplitSeq(s[len(b):], ",") {
ra = textproto.TrimString(ra) ra = textproto.TrimString(ra)
if ra == "" { if ra == "" {
continue continue

View File

@ -207,7 +207,7 @@ func (rw *ResponseRecorder) Result() *http.Response {
if trailers, ok := rw.snapHeader["Trailer"]; ok { if trailers, ok := rw.snapHeader["Trailer"]; ok {
res.Trailer = make(http.Header, len(trailers)) res.Trailer = make(http.Header, len(trailers))
for _, k := range trailers { for _, k := range trailers {
for _, k := range strings.Split(k, ",") { for k := range strings.SplitSeq(k, ",") {
k = http.CanonicalHeaderKey(textproto.TrimString(k)) k = http.CanonicalHeaderKey(textproto.TrimString(k))
if !httpguts.ValidTrailerHeader(k) { if !httpguts.ValidTrailerHeader(k) {
// Ignore since forbidden by RFC 7230, section 4.1.2. // Ignore since forbidden by RFC 7230, section 4.1.2.

View File

@ -577,7 +577,7 @@ func shouldPanicOnCopyError(req *http.Request) bool {
func removeHopByHopHeaders(h http.Header) { func removeHopByHopHeaders(h http.Header) {
// RFC 7230, section 6.1: Remove headers listed in the "Connection" header. // RFC 7230, section 6.1: Remove headers listed in the "Connection" header.
for _, f := range h["Connection"] { for _, f := range h["Connection"] {
for _, sf := range strings.Split(f, ",") { for sf := range strings.SplitSeq(f, ",") {
if sf = textproto.TrimString(sf); sf != "" { if sf = textproto.TrimString(sf); sf != "" {
h.Del(sf) h.Del(sf)
} }

View File

@ -197,7 +197,7 @@ func TestReverseProxyStripHeadersPresentInConnection(t *testing.T) {
c := r.Header["Connection"] c := r.Header["Connection"]
var cf []string var cf []string
for _, f := range c { for _, f := range c {
for _, sf := range strings.Split(f, ",") { for sf := range strings.SplitSeq(f, ",") {
if sf = strings.TrimSpace(sf); sf != "" { if sf = strings.TrimSpace(sf); sf != "" {
cf = append(cf, sf) cf = append(cf, sf)
} }

View File

@ -31,7 +31,7 @@ func TestMain(m *testing.M) {
func interestingGoroutines() (gs []string) { func interestingGoroutines() (gs []string) {
buf := make([]byte, 2<<20) buf := make([]byte, 2<<20)
buf = buf[:runtime.Stack(buf, true)] 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.Cut(g, "\n")
stack = strings.TrimSpace(stack) stack = strings.TrimSpace(stack)
if stack == "" || if stack == "" ||

View File

@ -1590,7 +1590,7 @@ func foreachHeaderElement(v string, fn func(string)) {
fn(v) fn(v)
return return
} }
for _, f := range strings.Split(v, ",") { for f := range strings.SplitSeq(v, ",") {
if f = textproto.TrimString(f); f != "" { if f = textproto.TrimString(f); f != "" {
fn(f) fn(f)
} }

View File

@ -185,7 +185,7 @@ func runningGoroutines() []string {
var gss []string var gss []string
b := make([]byte, 2<<20) b := make([]byte, 2<<20)
b = b[:runtime.Stack(b, true)] 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.Cut(s, "\n")
stack = strings.TrimSpace(stack) stack = strings.TrimSpace(stack)
if !strings.Contains(stack, "created by net") { if !strings.Contains(stack, "created by net") {

View File

@ -240,8 +240,7 @@ func netshInterfaceIPShowInterface(ipver string, ifaces map[string]bool) error {
//Metric : 10 //Metric : 10
//... //...
var name string var name string
lines := bytes.Split(out, []byte{'\r', '\n'}) for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) {
for _, line := range lines {
if bytes.HasPrefix(line, []byte("Interface ")) && bytes.HasSuffix(line, []byte(" Parameters")) { if bytes.HasPrefix(line, []byte("Interface ")) && bytes.HasSuffix(line, []byte(" Parameters")) {
f := line[len("Interface "):] f := line[len("Interface "):]
f = f[:len(f)-len(" Parameters")] f = f[:len(f)-len(" Parameters")]
@ -330,8 +329,7 @@ func netshInterfaceIPv4ShowAddress(name string, netshOutput []byte) []string {
addrs := make([]string, 0) addrs := make([]string, 0)
var addr, subnetprefix string var addr, subnetprefix string
var processingOurInterface bool var processingOurInterface bool
lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) {
for _, line := range lines {
if !processingOurInterface { if !processingOurInterface {
if !bytes.HasPrefix(line, []byte("Configuration for interface")) { if !bytes.HasPrefix(line, []byte("Configuration for interface")) {
continue 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 // TODO: need to test ipv6 netmask too, but netsh does not outputs it
var addr string var addr string
addrs := make([]string, 0) addrs := make([]string, 0)
lines := bytes.Split(netshOutput, []byte{'\r', '\n'}) for line := range bytes.SplitSeq(netshOutput, []byte{'\r', '\n'}) {
for _, line := range lines {
if addr != "" { if addr != "" {
if len(line) == 0 { if len(line) == 0 {
addr = "" addr = ""
@ -584,8 +581,7 @@ func TestInterfaceHardwareAddrWithGetmac(t *testing.T) {
want[cname] = addr want[cname] = addr
group = make(map[string]string) group = make(map[string]string)
} }
lines := bytes.Split(out, []byte{'\r', '\n'}) for line := range bytes.SplitSeq(out, []byte{'\r', '\n'}) {
for _, line := range lines {
if len(line) == 0 { if len(line) == 0 {
processGroup() processGroup()
continue continue