Skip to content

Commit ca9b351

Browse files
tklauserpull[bot]
authored andcommitted
net: use internal/bytealg.CountString
On platforms that provide a native implementation this might be slightly faster. On other platforms it is equivalent to the count func. Change-Id: If46cc65598993e64084cc98533cb8c1e9679a6fd Reviewed-on: https://go-review.googlesource.com/c/go/+/522136 TryBot-Result: Gopher Robot <[email protected]> Auto-Submit: Tobias Klauser <[email protected]> Reviewed-by: Damien Neil <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Tobias Klauser <[email protected]>
1 parent 922e124 commit ca9b351

File tree

3 files changed

+4
-14
lines changed

3 files changed

+4
-14
lines changed

src/net/dnsclient_unix.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package net
1717
import (
1818
"context"
1919
"errors"
20+
"internal/bytealg"
2021
"internal/itoa"
2122
"io"
2223
"os"
@@ -513,7 +514,7 @@ func (conf *dnsConfig) nameList(name string) []string {
513514
return []string{name}
514515
}
515516

516-
hasNdots := count(name, '.') >= conf.ndots
517+
hasNdots := bytealg.CountString(name, '.') >= conf.ndots
517518
name += "."
518519
l++
519520

src/net/ipsock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,10 @@ func (addrs addrList) forResolve(network, addr string) Addr {
8383
switch network {
8484
case "ip":
8585
// IPv6 literal (addr does NOT contain a port)
86-
want6 = count(addr, ':') > 0
86+
want6 = bytealg.CountString(addr, ':') > 0
8787
case "tcp", "udp":
8888
// IPv6 literal. (addr contains a port, so look for '[')
89-
want6 = count(addr, '[') > 0
89+
want6 = bytealg.CountString(addr, '[') > 0
9090
}
9191
if want6 {
9292
return addrs.first(isNotIPv4)

src/net/parse.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -180,17 +180,6 @@ func xtoi2(s string, e byte) (byte, bool) {
180180
return byte(n), ok && ei == 2
181181
}
182182

183-
// Number of occurrences of b in s.
184-
func count(s string, b byte) int {
185-
n := 0
186-
for i := 0; i < len(s); i++ {
187-
if s[i] == b {
188-
n++
189-
}
190-
}
191-
return n
192-
}
193-
194183
// Index of rightmost occurrence of b in s.
195184
func last(s string, b byte) int {
196185
i := len(s)

0 commit comments

Comments
 (0)