all: use strings.Cut even more

Change-Id: I943ce72c6f339589235bddbe10d07799c4e37979
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
This commit is contained in:
Brad Fitzpatrick 2022-03-19 12:42:46 -07:00 committed by Brad Fitzpatrick
parent f30473211b
commit f2041c9088
8 changed files with 14 additions and 25 deletions

View File

@ -116,10 +116,7 @@ func doLocalRequestNiceError(req *http.Request) (*http.Response, error) {
if ue, ok := err.(*url.Error); ok {
if oe, ok := ue.Err.(*net.OpError); ok && oe.Op == "dial" {
path := req.URL.Path
pathPrefix := path
if i := strings.Index(path, "?"); i != -1 {
pathPrefix = path[:i]
}
pathPrefix, _, _ := strings.Cut(path, "?")
return nil, fmt.Errorf("Failed to connect to local Tailscale daemon for %s; %s Error: %w", pathPrefix, tailscaledConnectHint(), oe)
}
}
@ -520,8 +517,8 @@ func tailscaledConnectHint() string {
// SubState=dead
st := map[string]string{}
for _, line := range strings.Split(string(out), "\n") {
if i := strings.Index(line, "="); i != -1 {
st[line[:i]] = strings.TrimSpace(line[i+1:])
if k, v, ok := strings.Cut(line, "="); ok {
st[k] = strings.TrimSpace(v)
}
}
if st["LoadState"] == "loaded" &&

View File

@ -206,8 +206,6 @@ func root(w http.ResponseWriter, r *http.Request) {
// firstLabel s up until the first period, if any.
func firstLabel(s string) string {
if i := strings.Index(s, "."); i != -1 {
return s[:i]
}
s, _, _ = strings.Cut(s, ".")
return s
}

View File

@ -65,10 +65,7 @@ func Parse(r io.Reader) (*Config, error) {
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
i := strings.IndexByte(line, '#')
if i >= 0 {
line = line[:i]
}
line, _, _ = strings.Cut(line, "#") // remove any comments
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "nameserver") {

View File

@ -196,12 +196,11 @@ func maxDoHInFlight(goos string) int {
// Unknown iOS version, be cautious.
return 10
}
idx := strings.Index(ver, ".")
if idx == -1 {
major, _, ok := strings.Cut(ver, ".")
if !ok {
// Unknown iOS version, be cautious.
return 10
}
major := ver[:idx]
if m, err := strconv.Atoi(major); err != nil || m < 15 {
return 10
}

View File

@ -37,13 +37,13 @@ func TestResolversWithDelays(t *testing.T) {
o := func(ss ...string) (rr []resolverAndDelay) {
for _, s := range ss {
var d time.Duration
if i := strings.Index(s, "+"); i != -1 {
s, durStr, hasPlus := strings.Cut(s, "+")
if hasPlus {
var err error
d, err = time.ParseDuration(s[i+1:])
d, err = time.ParseDuration(durStr)
if err != nil {
panic(fmt.Sprintf("parsing duration in %q: %v", s, err))
}
s = s[:i]
}
host, _, err := net.SplitHostPort(s)
if err != nil {

View File

@ -676,7 +676,7 @@ func pfx(strs ...string) (ret []netaddr.IPPrefix) {
func nets(nets ...string) (ret []netaddr.IPPrefix) {
for _, s := range nets {
if i := strings.IndexByte(s, '/'); i == -1 {
if !strings.Contains(s, "/") {
ip, err := netaddr.ParseIP(s)
if err != nil {
panic(err)

View File

@ -188,8 +188,8 @@ func peerDebugName(p *tailcfg.Node) string {
return ""
}
n := p.Name
if i := strings.Index(n, "."); i != -1 {
return n[:i]
if base, _, ok := strings.Cut(n, "."); ok {
return base
}
return p.Hostinfo.Hostname()
}

View File

@ -905,9 +905,7 @@ func (c *Conn) Ping(peer *tailcfg.Node, res *ipnstate.PingResult, cb func(*ipnst
if res.NodeName == "" {
res.NodeName = peer.Hostinfo.Hostname() // else hostname
} else {
if i := strings.Index(res.NodeName, "."); i != -1 {
res.NodeName = res.NodeName[:i]
}
res.NodeName, _, _ = strings.Cut(res.NodeName, ".")
}
ep, ok := c.peerMap.endpointForNodeKey(peer.Key)