all: use strs.CutPrefix and strs.CutSuffix more

Updates places where we use HasPrefix + TrimPrefix to use the combined
function.

Updates #5309

Signed-off-by: Mihai Parparita <mihai@tailscale.com>
This commit is contained in:
Mihai Parparita
2022-11-18 10:13:14 -08:00
committed by Mihai Parparita
parent 41e1d336cc
commit 33520920c3
14 changed files with 46 additions and 34 deletions

View File

@@ -21,6 +21,7 @@ import (
"strings"
"tailscale.com/util/dnsname"
"tailscale.com/util/strs"
)
// Path is the canonical location of resolv.conf.
@@ -68,8 +69,7 @@ func Parse(r io.Reader) (*Config, error) {
line, _, _ = strings.Cut(line, "#") // remove any comments
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "nameserver") {
s := strings.TrimPrefix(line, "nameserver")
if s, ok := strs.CutPrefix(line, "nameserver"); ok {
nameserver := strings.TrimSpace(s)
if len(nameserver) == len(s) {
return nil, fmt.Errorf("missing space after \"nameserver\" in %q", line)
@@ -82,8 +82,7 @@ func Parse(r io.Reader) (*Config, error) {
continue
}
if strings.HasPrefix(line, "search") {
s := strings.TrimPrefix(line, "search")
if s, ok := strs.CutPrefix(line, "search"); ok {
domain := strings.TrimSpace(s)
if len(domain) == len(s) {
// No leading space?!

View File

@@ -34,6 +34,7 @@ import (
"tailscale.com/util/clientmetric"
"tailscale.com/util/cloudenv"
"tailscale.com/util/dnsname"
"tailscale.com/util/strs"
"tailscale.com/wgengine/monitor"
)
@@ -1215,8 +1216,7 @@ func (r *Resolver) respond(query []byte) ([]byte, error) {
// unARPA maps from "4.4.8.8.in-addr.arpa." to "8.8.4.4", etc.
func unARPA(a string) (ipStr string, ok bool) {
const suf4 = ".in-addr.arpa."
if strings.HasSuffix(a, suf4) {
s := strings.TrimSuffix(a, suf4)
if s, ok := strs.CutSuffix(a, suf4); ok {
// Parse and reverse octets.
ip, err := netip.ParseAddr(s)
if err != nil || !ip.Is4() {