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/encoding/pem | |
| parent | 8e36ab055162efa6f67f3b9ee62f625ac8874901 (diff) | |
| download | go-4d8db00641cc9ff4f44de7df9b8c4f4a4f9416ee.tar.xz | |
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/encoding/pem')
| -rw-r--r-- | src/encoding/pem/pem.go | 10 |
1 files changed, 5 insertions, 5 deletions
diff --git a/src/encoding/pem/pem.go b/src/encoding/pem/pem.go index a7272da5ad..e7adf88382 100644 --- a/src/encoding/pem/pem.go +++ b/src/encoding/pem/pem.go @@ -78,6 +78,7 @@ func removeSpacesAndTabs(data []byte) []byte { var pemStart = []byte("\n-----BEGIN ") var pemEnd = []byte("\n-----END ") var pemEndOfLine = []byte("-----") +var colon = []byte(":") // Decode will find the next PEM formatted block (certificate, private key // etc) in the input. It returns that block and the remainder of the input. If @@ -89,8 +90,8 @@ func Decode(data []byte) (p *Block, rest []byte) { rest = data if bytes.HasPrefix(data, pemStart[1:]) { rest = rest[len(pemStart)-1 : len(data)] - } else if i := bytes.Index(data, pemStart); i >= 0 { - rest = rest[i+len(pemStart) : len(data)] + } else if _, after, ok := bytes.Cut(data, pemStart); ok { + rest = after } else { return nil, data } @@ -114,13 +115,12 @@ func Decode(data []byte) (p *Block, rest []byte) { } line, next := getLine(rest) - i := bytes.IndexByte(line, ':') - if i == -1 { + key, val, ok := bytes.Cut(line, colon) + if !ok { break } // TODO(agl): need to cope with values that spread across lines. - key, val := line[:i], line[i+1:] key = bytes.TrimSpace(key) val = bytes.TrimSpace(val) p.Headers[string(key)] = string(val) |
