all: make use of stringslite.{HasPrefix, HasSuffix}

Just a code cleanup.

Change-Id: Ie887ab2c71de11b4844c4e6fd4e5aca3265ac3aa
Reviewed-on: https://go-review.googlesource.com/c/go/+/583216
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Michael Pratt <mpratt@google.com>
This commit is contained in:
Jes Cok 2024-05-04 20:13:15 +08:00 committed by Gopher Robot
parent ac174400f4
commit eabf59bc47
8 changed files with 22 additions and 46 deletions

View File

@ -16,6 +16,7 @@ import "C"
import (
"crypto/internal/boring/sig"
_ "crypto/internal/boring/syso"
"internal/stringslite"
"math/bits"
"unsafe"
)
@ -39,16 +40,12 @@ func Unreachable() {
// provided by runtime to avoid os import.
func runtime_arg0() string
func hasSuffix(s, t string) bool {
return len(s) > len(t) && s[len(s)-len(t):] == t
}
// UnreachableExceptTests marks code that should be unreachable
// when BoringCrypto is in use. It panics.
func UnreachableExceptTests() {
name := runtime_arg0()
// If BoringCrypto ran on Windows we'd need to allow _test.exe and .test.exe as well.
if !hasSuffix(name, "_test") && !hasSuffix(name, ".test") {
if !stringslite.HasSuffix(name, "_test") && !stringslite.HasSuffix(name, ".test") {
println("boringcrypto: unexpected code execution in", name)
panic("boringcrypto: invalid code execution")
}

View File

@ -9,7 +9,10 @@
// of the use of BoringCrypto.
package fipstls
import "sync/atomic"
import (
"internal/stringslite"
"sync/atomic"
)
var required atomic.Bool
@ -33,7 +36,7 @@ func Abandon() {
// and empty string for Windows (where runtime_arg0 can't easily find the name).
// Since this is an internal package, testing that this isn't used on the
// other operating systems should suffice to catch any mistakes.
if !hasSuffix(name, "_test") && !hasSuffix(name, ".test") && name != "NaClMain" && name != "" {
if !stringslite.HasSuffix(name, "_test") && !stringslite.HasSuffix(name, ".test") && name != "NaClMain" && name != "" {
panic("fipstls: invalid use of Abandon in " + name)
}
required.Store(false)
@ -42,10 +45,6 @@ func Abandon() {
// provided by runtime
func runtime_arg0() string
func hasSuffix(s, t string) bool {
return len(s) > len(t) && s[len(s)-len(t):] == t
}
// Required reports whether FIPS-approved settings are required.
func Required() bool {
return required.Load()

View File

@ -6,6 +6,7 @@ package poll
import (
"errors"
"internal/stringslite"
"io"
"sync"
"syscall"
@ -203,11 +204,11 @@ func (fd *FD) ReadUnlock() {
}
func isHangup(err error) bool {
return err != nil && stringsHasSuffix(err.Error(), "Hangup")
return err != nil && stringslite.HasSuffix(err.Error(), "Hangup")
}
func isInterrupted(err error) bool {
return err != nil && stringsHasSuffix(err.Error(), "interrupted")
return err != nil && stringslite.HasSuffix(err.Error(), "interrupted")
}
// IsPollDescriptor reports whether fd is the descriptor being used by the poller.

View File

@ -1,13 +0,0 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build plan9
package poll
// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
// suffix.
func stringsHasSuffix(s, suffix string) bool {
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}

View File

@ -8,6 +8,7 @@ import (
"errors"
"internal/bytealg"
"internal/godebug"
"internal/stringslite"
"io/fs"
"os"
"runtime"
@ -335,7 +336,7 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
}
// Canonicalize the hostname by removing any trailing dot.
if stringsHasSuffix(hostname, ".") {
if stringslite.HasSuffix(hostname, ".") {
hostname = hostname[:len(hostname)-1]
}
@ -396,7 +397,7 @@ func (c *conf) lookupOrder(r *Resolver, hostname string) (ret hostLookupOrder, d
return hostLookupCgo, dnsConf
}
continue
case hostname != "" && stringsHasPrefix(src.source, "mdns"):
case hostname != "" && stringslite.HasPrefix(src.source, "mdns"):
if stringsHasSuffixFold(hostname, ".local") {
// Per RFC 6762, the ".local" TLD is special. And
// because Go's native resolver doesn't do mDNS or

View File

@ -7,6 +7,7 @@ package net
import (
"errors"
"internal/itoa"
"internal/stringslite"
"os"
)
@ -70,7 +71,7 @@ func readInterface(i int) (*Interface, error) {
ifc.MTU = mtu
// Not a loopback device ("/dev/null") or packet interface (e.g. "pkt2")
if stringsHasPrefix(device, netdir+"/") {
if stringslite.HasPrefix(device, netdir+"/") {
deviceaddrf, err := open(device + "/addr")
if err != nil {
return nil, err

View File

@ -9,6 +9,7 @@ import (
"errors"
"internal/bytealg"
"internal/itoa"
"internal/stringslite"
"io"
"os"
)
@ -107,10 +108,10 @@ func queryDNS(ctx context.Context, addr string, typ string) (res []string, err e
}
func handlePlan9DNSError(err error, name string) error {
if stringsHasSuffix(err.Error(), "dns: name does not exist") ||
stringsHasSuffix(err.Error(), "dns: resource does not exist; negrcode 0") ||
stringsHasSuffix(err.Error(), "dns: resource does not exist; negrcode") ||
stringsHasSuffix(err.Error(), "dns failure") {
if stringslite.HasSuffix(err.Error(), "dns: name does not exist") ||
stringslite.HasSuffix(err.Error(), "dns: resource does not exist; negrcode 0") ||
stringslite.HasSuffix(err.Error(), "dns: resource does not exist; negrcode") ||
stringslite.HasSuffix(err.Error(), "dns failure") {
err = errNoSuchHost
}
return newDNSError(err, name, "")
@ -227,7 +228,7 @@ func (r *Resolver) lookupPort(ctx context.Context, network, service string) (por
func (*Resolver) lookupPortWithNetwork(ctx context.Context, network, errNetwork, service string) (port int, err error) {
lines, err := queryCS(ctx, network, "127.0.0.1", toLower(service))
if err != nil {
if stringsHasSuffix(err.Error(), "can't translate service") {
if stringslite.HasSuffix(err.Error(), "can't translate service") {
return 0, &DNSError{Err: "unknown port", Name: errNetwork + "/" + service, IsNotFound: true}
}
return
@ -256,7 +257,7 @@ func (r *Resolver) lookupCNAME(ctx context.Context, name string) (cname string,
lines, err := queryDNS(ctx, name, "cname")
if err != nil {
if stringsHasSuffix(err.Error(), "dns failure") || stringsHasSuffix(err.Error(), "resource does not exist; negrcode 0") {
if stringslite.HasSuffix(err.Error(), "dns failure") || stringslite.HasSuffix(err.Error(), "resource does not exist; negrcode 0") {
return absDomainName(name), nil
}
return "", handlePlan9DNSError(err, cname)

View File

@ -251,23 +251,12 @@ func foreachField(x string, fn func(field string) error) error {
return nil
}
// stringsHasSuffix is strings.HasSuffix. It reports whether s ends in
// suffix.
func stringsHasSuffix(s, suffix string) bool {
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
}
// stringsHasSuffixFold reports whether s ends in suffix,
// ASCII-case-insensitively.
func stringsHasSuffixFold(s, suffix string) bool {
return len(s) >= len(suffix) && stringsEqualFold(s[len(s)-len(suffix):], suffix)
}
// stringsHasPrefix is strings.HasPrefix. It reports whether s begins with prefix.
func stringsHasPrefix(s, prefix string) bool {
return len(s) >= len(prefix) && s[:len(prefix)] == prefix
}
// stringsEqualFold is strings.EqualFold, ASCII only. It reports whether s and t
// are equal, ASCII-case-insensitively.
func stringsEqualFold(s, t string) bool {