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 | |
| 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')
| -rw-r--r-- | src/encoding/asn1/common.go | 9 | ||||
| -rw-r--r-- | src/encoding/json/tags.go | 16 | ||||
| -rw-r--r-- | src/encoding/pem/pem.go | 10 | ||||
| -rw-r--r-- | src/encoding/xml/typeinfo.go | 4 | ||||
| -rw-r--r-- | src/encoding/xml/xml.go | 49 |
5 files changed, 36 insertions, 52 deletions
diff --git a/src/encoding/asn1/common.go b/src/encoding/asn1/common.go index 1c712e1eff..40115df8b4 100644 --- a/src/encoding/asn1/common.go +++ b/src/encoding/asn1/common.go @@ -94,14 +94,7 @@ type fieldParameters struct { func parseFieldParameters(str string) (ret fieldParameters) { var part string for len(str) > 0 { - // This loop uses IndexByte and explicit slicing - // instead of strings.Split(str, ",") to reduce allocations. - i := strings.IndexByte(str, ',') - if i < 0 { - part, str = str, "" - } else { - part, str = str[:i], str[i+1:] - } + part, str, _ = strings.Cut(str, ",") switch { case part == "optional": ret.optional = true diff --git a/src/encoding/json/tags.go b/src/encoding/json/tags.go index c38fd5102f..b490328f4c 100644 --- a/src/encoding/json/tags.go +++ b/src/encoding/json/tags.go @@ -15,10 +15,8 @@ type tagOptions string // parseTag splits a struct field's json tag into its name and // comma-separated options. func parseTag(tag string) (string, tagOptions) { - if idx := strings.Index(tag, ","); idx != -1 { - return tag[:idx], tagOptions(tag[idx+1:]) - } - return tag, tagOptions("") + tag, opt, _ := strings.Cut(tag, ",") + return tag, tagOptions(opt) } // Contains reports whether a comma-separated list of options @@ -30,15 +28,11 @@ func (o tagOptions) Contains(optionName string) bool { } s := string(o) for s != "" { - var next string - i := strings.Index(s, ",") - if i >= 0 { - s, next = s[:i], s[i+1:] - } - if s == optionName { + var name string + name, s, _ = strings.Cut(s, ",") + if name == optionName { return true } - s = next } return false } 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) diff --git a/src/encoding/xml/typeinfo.go b/src/encoding/xml/typeinfo.go index 162724ef1a..51e976cf01 100644 --- a/src/encoding/xml/typeinfo.go +++ b/src/encoding/xml/typeinfo.go @@ -115,8 +115,8 @@ func structFieldInfo(typ reflect.Type, f *reflect.StructField) (*fieldInfo, erro // Split the tag from the xml namespace if necessary. tag := f.Tag.Get("xml") - if i := strings.Index(tag, " "); i >= 0 { - finfo.xmlns, tag = tag[:i], tag[i+1:] + if ns, t, ok := strings.Cut(tag, " "); ok { + finfo.xmlns, tag = ns, t } // Parse flags. diff --git a/src/encoding/xml/xml.go b/src/encoding/xml/xml.go index c14954df15..33d0b417b9 100644 --- a/src/encoding/xml/xml.go +++ b/src/encoding/xml/xml.go @@ -1164,11 +1164,11 @@ func (d *Decoder) nsname() (name Name, ok bool) { } if strings.Count(s, ":") > 1 { name.Local = s - } else if i := strings.Index(s, ":"); i < 1 || i > len(s)-2 { + } else if space, local, ok := strings.Cut(s, ":"); !ok || space == "" || local == "" { name.Local = s } else { - name.Space = s[0:i] - name.Local = s[i+1:] + name.Space = space + name.Local = local } return name, true } @@ -2012,25 +2012,26 @@ func emitCDATA(w io.Writer, s []byte) error { if _, err := w.Write(cdataStart); err != nil { return err } + for { - i := bytes.Index(s, cdataEnd) - if i >= 0 && i+len(cdataEnd) <= len(s) { - // Found a nested CDATA directive end. - if _, err := w.Write(s[:i]); err != nil { - return err - } - if _, err := w.Write(cdataEscape); err != nil { - return err - } - i += len(cdataEnd) - } else { - if _, err := w.Write(s); err != nil { - return err - } + before, after, ok := bytes.Cut(s, cdataEnd) + if !ok { break } - s = s[i:] + // Found a nested CDATA directive end. + if _, err := w.Write(before); err != nil { + return err + } + if _, err := w.Write(cdataEscape); err != nil { + return err + } + s = after + } + + if _, err := w.Write(s); err != nil { + return err } + _, err := w.Write(cdataEnd) return err } @@ -2041,20 +2042,16 @@ func procInst(param, s string) string { // TODO: this parsing is somewhat lame and not exact. // It works for all actual cases, though. param = param + "=" - idx := strings.Index(s, param) - if idx == -1 { - return "" - } - v := s[idx+len(param):] + _, v, _ := strings.Cut(s, param) if v == "" { return "" } if v[0] != '\'' && v[0] != '"' { return "" } - idx = strings.IndexRune(v[1:], rune(v[0])) - if idx == -1 { + unquote, _, ok := strings.Cut(v[1:], v[:1]) + if !ok { return "" } - return v[1 : idx+1] + return unquote } |
