diff options
author | Russ Cox <rsc@golang.org> | 2021-09-22 10:46:32 -0400 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2021-10-06 15:53:04 +0000 |
commit | 4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee (patch) | |
tree | 1e850efb295d4c5f0589e46bd8d9f1930d4af0b5 /src/crypto | |
parent | 8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff) | |
download | go-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.tar.gz go-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.zip |
all: use bytes.Cut, strings.Cut
Many uses of Index/IndexByte/IndexRune/Split/SplitN
can be written more clearly using the new Cut functions.
Do that. Also rewrite to other functions if that's clearer.
For #46336.
Change-Id: I68d024716ace41a57a8bf74455c62279bde0f448
Reviewed-on: https://go-review.googlesource.com/c/go/+/351711
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/crypto')
-rw-r--r-- | src/crypto/ecdsa/ecdsa_test.go | 6 | ||||
-rw-r--r-- | src/crypto/tls/handshake_client_test.go | 10 | ||||
-rw-r--r-- | src/crypto/tls/handshake_test.go | 13 | ||||
-rw-r--r-- | src/crypto/x509/pem_decrypt.go | 5 |
4 files changed, 16 insertions, 18 deletions
diff --git a/src/crypto/ecdsa/ecdsa_test.go b/src/crypto/ecdsa/ecdsa_test.go index 556818acf4..c8390b2cc9 100644 --- a/src/crypto/ecdsa/ecdsa_test.go +++ b/src/crypto/ecdsa/ecdsa_test.go @@ -219,9 +219,9 @@ func TestVectors(t *testing.T) { if line[0] == '[' { line = line[1 : len(line)-1] - parts := strings.SplitN(line, ",", 2) + curve, hash, _ := strings.Cut(line, ",") - switch parts[0] { + switch curve { case "P-224": pub.Curve = elliptic.P224() case "P-256": @@ -234,7 +234,7 @@ func TestVectors(t *testing.T) { pub.Curve = nil } - switch parts[1] { + switch hash { case "SHA-1": h = sha1.New() case "SHA-224": diff --git a/src/crypto/tls/handshake_client_test.go b/src/crypto/tls/handshake_client_test.go index b6eb488a4d..2158f3247b 100644 --- a/src/crypto/tls/handshake_client_test.go +++ b/src/crypto/tls/handshake_client_test.go @@ -97,18 +97,18 @@ func (o *opensslOutputSink) Write(data []byte) (n int, err error) { o.all = append(o.all, data...) for { - i := bytes.IndexByte(o.line, '\n') - if i < 0 { + line, next, ok := bytes.Cut(o.line, []byte("\n")) + if !ok { break } - if bytes.Equal([]byte(opensslEndOfHandshake), o.line[:i]) { + if bytes.Equal([]byte(opensslEndOfHandshake), line) { o.handshakeComplete <- struct{}{} } - if bytes.Equal([]byte(opensslReadKeyUpdate), o.line[:i]) { + if bytes.Equal([]byte(opensslReadKeyUpdate), line) { o.readKeyUpdate <- struct{}{} } - o.line = o.line[i+1:] + o.line = next } return len(data), nil diff --git a/src/crypto/tls/handshake_test.go b/src/crypto/tls/handshake_test.go index 9bfb1177f2..90ac9bd11e 100644 --- a/src/crypto/tls/handshake_test.go +++ b/src/crypto/tls/handshake_test.go @@ -191,18 +191,17 @@ func parseTestData(r io.Reader) (flows [][]byte, err error) { // Otherwise the line is a line of hex dump that looks like: // 00000170 fc f5 06 bf (...) |.....X{&?......!| // (Some bytes have been omitted from the middle section.) - - if i := strings.IndexByte(line, ' '); i >= 0 { - line = line[i:] - } else { + _, after, ok := strings.Cut(line, " ") + if !ok { return nil, errors.New("invalid test data") } + line = after - if i := strings.IndexByte(line, '|'); i >= 0 { - line = line[:i] - } else { + before, _, ok := strings.Cut(line, "|") + if !ok { return nil, errors.New("invalid test data") } + line = before hexBytes := strings.Fields(line) for _, hexByte := range hexBytes { diff --git a/src/crypto/x509/pem_decrypt.go b/src/crypto/x509/pem_decrypt.go index 781cb3de83..682923ac53 100644 --- a/src/crypto/x509/pem_decrypt.go +++ b/src/crypto/x509/pem_decrypt.go @@ -127,12 +127,11 @@ func DecryptPEMBlock(b *pem.Block, password []byte) ([]byte, error) { return nil, errors.New("x509: no DEK-Info header in block") } - idx := strings.Index(dek, ",") - if idx == -1 { + mode, hexIV, ok := strings.Cut(dek, ",") + if !ok { return nil, errors.New("x509: malformed DEK-Info header") } - mode, hexIV := dek[:idx], dek[idx+1:] ciph := cipherByName(mode) if ciph == nil { return nil, errors.New("x509: unknown encryption mode") |