mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
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:
parent
0e35fb2f99
commit
a1ea78c470
@ -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
|
||||
|
@ -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.
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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 == "" ||
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -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") {
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user