|
|
Descriptionall: use strings.IndexByte instead of Index where possible
Patch Set 1 #Patch Set 2 : diff -r 816f25454af2 https://go.googlecode.com/hg/ #Patch Set 3 : diff -r 816f25454af2 https://go.googlecode.com/hg/ #Patch Set 4 : diff -r b2b6f8df031e https://go.googlecode.com/hg/ #
MessagesTotal messages: 10
Hello golang-dev@googlegroups.com, I'd like you to review this change to https://go.googlecode.com/hg/
Sign in to reply to this message.
On 2013/08/05 22:37:42, bradfitz wrote: > Hello mailto:golang-dev@googlegroups.com, > > I'd like you to review this change to > https://go.googlecode.com/hg/ LGTM.
Sign in to reply to this message.
*** Submitted as https://code.google.com/p/go/source/detail?r=ab644299d124 *** all: use strings.IndexByte instead of Index where possible R=golang-dev, khr CC=golang-dev https://codereview.appspot.com/12486043
Sign in to reply to this message.
Is there a vet rule for this, or did you just hit it with the grep hammer ? On 06/08/2013, at 8:37, bradfitz@golang.org wrote: > Reviewers: golang-dev1, > > Message: > Hello golang-dev@googlegroups.com, > > I'd like you to review this change to > https://go.googlecode.com/hg/ > > > Description: > all: use strings.IndexByte instead of Index where possible > > Please review this at https://codereview.appspot.com/12486043/ > > Affected files: > M src/pkg/crypto/x509/pem_decrypt.go > M src/pkg/debug/gosym/symtab.go > M src/pkg/encoding/json/tags.go > M src/pkg/encoding/xml/typeinfo.go > M src/pkg/encoding/xml/xml.go > M src/pkg/go/build/build.go > M src/pkg/go/printer/printer.go > M src/pkg/math/big/rat.go > M src/pkg/mime/mediatype.go > M src/pkg/net/http/cgi/child.go > M src/pkg/net/http/cookie.go > M src/pkg/net/http/fs.go > M src/pkg/net/http/request.go > M src/pkg/net/http/server.go > M src/pkg/net/url/url.go > M src/pkg/os/os_test.go > M src/pkg/os/user/lookup_unix.go > M src/pkg/path/match.go > M src/pkg/regexp/exec_test.go > M src/pkg/regexp/regexp.go > M src/pkg/unicode/maketables.go > > > Index: src/pkg/crypto/x509/pem_decrypt.go > =================================================================== > --- a/src/pkg/crypto/x509/pem_decrypt.go > +++ b/src/pkg/crypto/x509/pem_decrypt.go > @@ -115,7 +115,7 @@ > return nil, errors.New("x509: no DEK-Info header in block") > } > > - idx := strings.Index(dek, ",") > + idx := strings.IndexByte(dek, ',') > if idx == -1 { > return nil, errors.New("x509: malformed DEK-Info header") > } > Index: src/pkg/debug/gosym/symtab.go > =================================================================== > --- a/src/pkg/debug/gosym/symtab.go > +++ b/src/pkg/debug/gosym/symtab.go > @@ -40,7 +40,7 @@ > // PackageName returns the package part of the symbol name, > // or the empty string if there is none. > func (s *Sym) PackageName() string { > - if i := strings.Index(s.Name, "."); i != -1 { > + if i := strings.IndexByte(s.Name, '.'); i != -1 { > return s.Name[0:i] > } > return "" > @@ -49,7 +49,7 @@ > // ReceiverName returns the receiver type name of this symbol, > // or the empty string if there is none. > func (s *Sym) ReceiverName() string { > - l := strings.Index(s.Name, ".") > + l := strings.IndexByte(s.Name, '.') > r := strings.LastIndex(s.Name, ".") > if l == -1 || r == -1 || l == r { > return "" > Index: src/pkg/encoding/json/tags.go > =================================================================== > --- a/src/pkg/encoding/json/tags.go > +++ b/src/pkg/encoding/json/tags.go > @@ -15,7 +15,7 @@ > // 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 { > + if idx := strings.IndexByte(tag, ','); idx != -1 { > return tag[:idx], tagOptions(tag[idx+1:]) > } > return tag, tagOptions("") > @@ -31,7 +31,7 @@ > s := string(o) > for s != "" { > var next string > - i := strings.Index(s, ",") > + i := strings.IndexByte(s, ',') > if i >= 0 { > s, next = s[:i], s[i+1:] > } > Index: src/pkg/encoding/xml/typeinfo.go > =================================================================== > --- a/src/pkg/encoding/xml/typeinfo.go > +++ b/src/pkg/encoding/xml/typeinfo.go > @@ -113,7 +113,7 @@ > > // Split the tag from the xml namespace if necessary. > tag := f.Tag.Get("xml") > - if i := strings.Index(tag, " "); i >= 0 { > + if i := strings.IndexByte(tag, ' '); i >= 0 { > finfo.xmlns, tag = tag[:i], tag[i+1:] > } > > Index: src/pkg/encoding/xml/xml.go > =================================================================== > --- a/src/pkg/encoding/xml/xml.go > +++ b/src/pkg/encoding/xml/xml.go > @@ -1026,7 +1026,7 @@ > if !ok { > return > } > - i := strings.Index(s, ":") > + i := strings.IndexByte(s, ':') > if i < 0 { > name.Local = s > } else { > Index: src/pkg/go/build/build.go > =================================================================== > --- a/src/pkg/go/build/build.go > +++ b/src/pkg/go/build/build.go > @@ -877,7 +877,7 @@ > > // Split at colon. > line = strings.TrimSpace(line[4:]) > - i := strings.Index(line, ":") > + i := strings.IndexByte(line, ':') > if i < 0 { > return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) > } > @@ -1022,7 +1022,7 @@ > if name == "" { > return false > } > - if i := strings.Index(name, ","); i >= 0 { > + if i := strings.IndexByte(name, ','); i >= 0 { > // comma-separated list > return ctxt.match(name[:i]) && ctxt.match(name[i+1:]) > } > @@ -1076,7 +1076,7 @@ > // name_$(GOOS)_$(GOARCH)_test.* > // > func (ctxt *Context) goodOSArchFile(name string) bool { > - if dot := strings.Index(name, "."); dot != -1 { > + if dot := strings.IndexByte(name, '.'); dot != -1 { > name = name[:dot] > } > l := strings.Split(name, "_") > Index: src/pkg/go/printer/printer.go > =================================================================== > --- a/src/pkg/go/printer/printer.go > +++ b/src/pkg/go/printer/printer.go > @@ -474,7 +474,7 @@ > * Check for vertical "line of stars" and correct prefix accordingly. > */ > lineOfStars := false > - if i := strings.Index(prefix, "*"); i >= 0 { > + if i := strings.IndexByte(prefix, '*'); i >= 0 { > // Line of stars present. > if i > 0 && prefix[i-1] == ' ' { > i-- // remove trailing blank from prefix so stars remain aligned > Index: src/pkg/math/big/rat.go > =================================================================== > --- a/src/pkg/math/big/rat.go > +++ b/src/pkg/math/big/rat.go > @@ -429,7 +429,7 @@ > } > > // check for a quotient > - sep := strings.Index(s, "/") > + sep := strings.IndexByte(s, '/') > if sep >= 0 { > if _, ok := z.a.SetString(s[0:sep], 10); !ok { > return nil, false > @@ -443,7 +443,7 @@ > } > > // check for a decimal point > - sep = strings.Index(s, ".") > + sep = strings.IndexByte(s, '.') > // check for an exponent > e := strings.IndexAny(s, "eE") > var exp Int > Index: src/pkg/mime/mediatype.go > =================================================================== > --- a/src/pkg/mime/mediatype.go > +++ b/src/pkg/mime/mediatype.go > @@ -18,7 +18,7 @@ > // When any of the arguments result in a standard violation then > // FormatMediaType returns the empty string. > func FormatMediaType(t string, param map[string]string) string { > - slash := strings.Index(t, "/") > + slash := strings.IndexByte(t, '/') > if slash == -1 { > return "" > } > @@ -91,7 +91,7 @@ > // The returned map, params, maps from the lowercase > // attribute to the attribute value with its case preserved. > func ParseMediaType(v string) (mediatype string, params map[string]string, err error) { > - i := strings.Index(v, ";") > + i := strings.IndexByte(v, ';') > if i == -1 { > i = len(v) > } > @@ -127,7 +127,7 @@ > } > > pmap := params > - if idx := strings.Index(key, "*"); idx != -1 { > + if idx := strings.IndexByte(key, '*'); idx != -1 { > baseName := key[:idx] > if continuation == nil { > continuation = make(map[string]map[string]string) > Index: src/pkg/net/http/cgi/child.go > =================================================================== > --- a/src/pkg/net/http/cgi/child.go > +++ b/src/pkg/net/http/cgi/child.go > @@ -40,7 +40,7 @@ > func envMap(env []string) map[string]string { > m := make(map[string]string) > for _, kv := range env { > - if idx := strings.Index(kv, "="); idx != -1 { > + if idx := strings.IndexByte(kv, '='); idx != -1 { > m[kv[:idx]] = kv[idx+1:] > } > } > Index: src/pkg/net/http/cookie.go > =================================================================== > --- a/src/pkg/net/http/cookie.go > +++ b/src/pkg/net/http/cookie.go > @@ -47,7 +47,7 @@ > continue > } > parts[0] = strings.TrimSpace(parts[0]) > - j := strings.Index(parts[0], "=") > + j := strings.IndexByte(parts[0], '=') > if j < 0 { > continue > } > @@ -71,7 +71,7 @@ > } > > attr, val := parts[i], "" > - if j := strings.Index(attr, "="); j >= 0 { > + if j := strings.IndexByte(attr, '='); j >= 0 { > attr, val = attr[:j], attr[j+1:] > } > lowerAttr := strings.ToLower(attr) > @@ -188,7 +188,7 @@ > continue > } > name, val := parts[i], "" > - if j := strings.Index(name, "="); j >= 0 { > + if j := strings.IndexByte(name, '='); j >= 0 { > name, val = name[:j], name[j+1:] > } > if !isCookieNameValid(name) { > Index: src/pkg/net/http/fs.go > =================================================================== > --- a/src/pkg/net/http/fs.go > +++ b/src/pkg/net/http/fs.go > @@ -467,7 +467,7 @@ > if ra == "" { > continue > } > - i := strings.Index(ra, "-") > + i := strings.IndexByte(ra, '-') > if i < 0 { > return nil, errors.New("invalid range") > } > Index: src/pkg/net/http/request.go > =================================================================== > --- a/src/pkg/net/http/request.go > +++ b/src/pkg/net/http/request.go > @@ -408,7 +408,7 @@ > if !strings.HasPrefix(vers, "HTTP/") { > return 0, 0, false > } > - dot := strings.Index(vers, ".") > + dot := strings.IndexByte(vers, '.') > if dot < 0 { > return 0, 0, false > } > @@ -473,8 +473,8 @@ > > // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. > func parseRequestLine(line string) (method, requestURI, proto string, ok bool) { > - s1 := strings.Index(line, " ") > - s2 := strings.Index(line[s1+1:], " ") > + s1 := strings.IndexByte(line, ' ') > + s2 := strings.IndexByte(line[s1+1:], ' ') > if s1 < 0 || s2 < 0 { > return > } > Index: src/pkg/net/http/server.go > =================================================================== > --- a/src/pkg/net/http/server.go > +++ b/src/pkg/net/http/server.go > @@ -1262,7 +1262,7 @@ > } > > var query string > - if i := strings.Index(urlStr, "?"); i != -1 { > + if i := strings.IndexByte(urlStr, '?'); i != -1 { > urlStr, query = urlStr[:i], urlStr[i:] > } > > @@ -1494,7 +1494,7 @@ > if pattern[0] != '/' { > // In pattern, at least the last character is a '/', so > // strings.Index can't be -1. > - path = pattern[strings.Index(pattern, "/"):] > + path = pattern[strings.IndexByte(pattern, '/'):] > } > mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern} > } > Index: src/pkg/net/url/url.go > =================================================================== > --- a/src/pkg/net/url/url.go > +++ b/src/pkg/net/url/url.go > @@ -421,7 +421,7 @@ > return > } > userinfo, host := authority[:i], authority[i+1:] > - if strings.Index(userinfo, ":") < 0 { > + if strings.IndexByte(userinfo, ':') < 0 { > if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil { > return > } > @@ -536,7 +536,7 @@ > continue > } > value := "" > - if i := strings.Index(key, "="); i >= 0 { > + if i := strings.IndexByte(key, '='); i >= 0 { > key, value = key[:i], key[i+1:] > } > key, err1 := QueryUnescape(key) > Index: src/pkg/os/os_test.go > =================================================================== > --- a/src/pkg/os/os_test.go > +++ b/src/pkg/os/os_test.go > @@ -890,7 +890,7 @@ > } > want := run(t, []string{"/bin/hostname"}) > if hostname != want { > - i := strings.Index(hostname, ".") > + i := strings.IndexByte(hostname, '.') > if i < 0 || hostname[0:i] != want { > t.Errorf("Hostname() = %q, want %q", hostname, want) > } > Index: src/pkg/os/user/lookup_unix.go > =================================================================== > --- a/src/pkg/os/user/lookup_unix.go > +++ b/src/pkg/os/user/lookup_unix.go > @@ -105,7 +105,7 @@ > // say: "It is expected to be a comma separated list of > // personal data where the first item is the full name of the > // user." > - if i := strings.Index(u.Name, ","); i >= 0 { > + if i := strings.IndexByte(u.Name, ','); i >= 0 { > u.Name = u.Name[:i] > } > return u, nil > Index: src/pkg/path/match.go > =================================================================== > --- a/src/pkg/path/match.go > +++ b/src/pkg/path/match.go > @@ -43,7 +43,7 @@ > star, chunk, pattern = scanChunk(pattern) > if star && chunk == "" { > // Trailing * matches rest of string unless it has a /. > - return strings.Index(name, "/") < 0, nil > + return strings.IndexByte(name, '/') < 0, nil > } > // Look for match at current position. > t, ok, err := matchChunk(chunk, name) > Index: src/pkg/regexp/exec_test.go > =================================================================== > --- a/src/pkg/regexp/exec_test.go > +++ b/src/pkg/regexp/exec_test.go > @@ -293,7 +293,7 @@ > out[n] = -1 > out[n+1] = -1 > } else { > - k := strings.Index(pair, "-") > + k := strings.IndexByte(pair, '-') > if k < 0 { > t.Fatalf("%s:%d: invalid pair %s", file, lineno, pair) > } > @@ -456,7 +456,7 @@ > continue Reading > } > case ':': > - i := strings.Index(flag[1:], ":") > + i := strings.IndexByte(flag[1:], ':') > if i < 0 { > t.Logf("skip: %s", line) > continue Reading > Index: src/pkg/regexp/regexp.go > =================================================================== > --- a/src/pkg/regexp/regexp.go > +++ b/src/pkg/regexp/regexp.go > @@ -429,7 +429,7 @@ > // in Expand, so for instance $1 represents the text of the first submatch. > func (re *Regexp) ReplaceAllString(src, repl string) string { > n := 2 > - if strings.Index(repl, "$") >= 0 { > + if strings.IndexByte(repl, '$') >= 0 { > n = 2 * (re.numSubexp + 1) > } > b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte { > @@ -753,7 +753,7 @@ > > func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src string, match []int) []byte { > for len(template) > 0 { > - i := strings.Index(template, "$") > + i := strings.IndexByte(template, '$') > if i < 0 { > break > } > Index: src/pkg/unicode/maketables.go > =================================================================== > --- a/src/pkg/unicode/maketables.go > +++ b/src/pkg/unicode/maketables.go > @@ -616,7 +616,7 @@ > } > > func parseScript(line string, scripts map[string][]Script) { > - comment := strings.Index(line, "#") > + comment := strings.IndexByte(line, '#') > if comment >= 0 { > line = line[0:comment] > } > > > -- > > ---You received this message because you are subscribed to the Google Groups "golang-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > >
Sign in to reply to this message.
grep -r -E -l 'strings.Index.*"."' pkg | grep go$ | xargs perl -pi -e "s:strings.Index\((.+), \"(.)\":strings.IndexByte(\$1, '\$2':" On Mon, Aug 5, 2013 at 3:48 PM, Dave Cheney <dave@cheney.net> wrote: > Is there a vet rule for this, or did you just hit it with the grep hammer ? > > On 06/08/2013, at 8:37, bradfitz@golang.org wrote: > > > Reviewers: golang-dev1, > > > > Message: > > Hello golang-dev@googlegroups.com, > > > > I'd like you to review this change to > > https://go.googlecode.com/hg/ > > > > > > Description: > > all: use strings.IndexByte instead of Index where possible > > > > Please review this at https://codereview.appspot.com/12486043/ > > > > Affected files: > > M src/pkg/crypto/x509/pem_decrypt.go > > M src/pkg/debug/gosym/symtab.go > > M src/pkg/encoding/json/tags.go > > M src/pkg/encoding/xml/typeinfo.go > > M src/pkg/encoding/xml/xml.go > > M src/pkg/go/build/build.go > > M src/pkg/go/printer/printer.go > > M src/pkg/math/big/rat.go > > M src/pkg/mime/mediatype.go > > M src/pkg/net/http/cgi/child.go > > M src/pkg/net/http/cookie.go > > M src/pkg/net/http/fs.go > > M src/pkg/net/http/request.go > > M src/pkg/net/http/server.go > > M src/pkg/net/url/url.go > > M src/pkg/os/os_test.go > > M src/pkg/os/user/lookup_unix.go > > M src/pkg/path/match.go > > M src/pkg/regexp/exec_test.go > > M src/pkg/regexp/regexp.go > > M src/pkg/unicode/maketables.go > > > > > > Index: src/pkg/crypto/x509/pem_decrypt.go > > =================================================================== > > --- a/src/pkg/crypto/x509/pem_decrypt.go > > +++ b/src/pkg/crypto/x509/pem_decrypt.go > > @@ -115,7 +115,7 @@ > > return nil, errors.New("x509: no DEK-Info header in block") > > } > > > > - idx := strings.Index(dek, ",") > > + idx := strings.IndexByte(dek, ',') > > if idx == -1 { > > return nil, errors.New("x509: malformed DEK-Info header") > > } > > Index: src/pkg/debug/gosym/symtab.go > > =================================================================== > > --- a/src/pkg/debug/gosym/symtab.go > > +++ b/src/pkg/debug/gosym/symtab.go > > @@ -40,7 +40,7 @@ > > // PackageName returns the package part of the symbol name, > > // or the empty string if there is none. > > func (s *Sym) PackageName() string { > > - if i := strings.Index(s.Name, "."); i != -1 { > > + if i := strings.IndexByte(s.Name, '.'); i != -1 { > > return s.Name[0:i] > > } > > return "" > > @@ -49,7 +49,7 @@ > > // ReceiverName returns the receiver type name of this symbol, > > // or the empty string if there is none. > > func (s *Sym) ReceiverName() string { > > - l := strings.Index(s.Name, ".") > > + l := strings.IndexByte(s.Name, '.') > > r := strings.LastIndex(s.Name, ".") > > if l == -1 || r == -1 || l == r { > > return "" > > Index: src/pkg/encoding/json/tags.go > > =================================================================== > > --- a/src/pkg/encoding/json/tags.go > > +++ b/src/pkg/encoding/json/tags.go > > @@ -15,7 +15,7 @@ > > // 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 { > > + if idx := strings.IndexByte(tag, ','); idx != -1 { > > return tag[:idx], tagOptions(tag[idx+1:]) > > } > > return tag, tagOptions("") > > @@ -31,7 +31,7 @@ > > s := string(o) > > for s != "" { > > var next string > > - i := strings.Index(s, ",") > > + i := strings.IndexByte(s, ',') > > if i >= 0 { > > s, next = s[:i], s[i+1:] > > } > > Index: src/pkg/encoding/xml/typeinfo.go > > =================================================================== > > --- a/src/pkg/encoding/xml/typeinfo.go > > +++ b/src/pkg/encoding/xml/typeinfo.go > > @@ -113,7 +113,7 @@ > > > > // Split the tag from the xml namespace if necessary. > > tag := f.Tag.Get("xml") > > - if i := strings.Index(tag, " "); i >= 0 { > > + if i := strings.IndexByte(tag, ' '); i >= 0 { > > finfo.xmlns, tag = tag[:i], tag[i+1:] > > } > > > > Index: src/pkg/encoding/xml/xml.go > > =================================================================== > > --- a/src/pkg/encoding/xml/xml.go > > +++ b/src/pkg/encoding/xml/xml.go > > @@ -1026,7 +1026,7 @@ > > if !ok { > > return > > } > > - i := strings.Index(s, ":") > > + i := strings.IndexByte(s, ':') > > if i < 0 { > > name.Local = s > > } else { > > Index: src/pkg/go/build/build.go > > =================================================================== > > --- a/src/pkg/go/build/build.go > > +++ b/src/pkg/go/build/build.go > > @@ -877,7 +877,7 @@ > > > > // Split at colon. > > line = strings.TrimSpace(line[4:]) > > - i := strings.Index(line, ":") > > + i := strings.IndexByte(line, ':') > > if i < 0 { > > return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) > > } > > @@ -1022,7 +1022,7 @@ > > if name == "" { > > return false > > } > > - if i := strings.Index(name, ","); i >= 0 { > > + if i := strings.IndexByte(name, ','); i >= 0 { > > // comma-separated list > > return ctxt.match(name[:i]) && ctxt.match(name[i+1:]) > > } > > @@ -1076,7 +1076,7 @@ > > // name_$(GOOS)_$(GOARCH)_test.* > > // > > func (ctxt *Context) goodOSArchFile(name string) bool { > > - if dot := strings.Index(name, "."); dot != -1 { > > + if dot := strings.IndexByte(name, '.'); dot != -1 { > > name = name[:dot] > > } > > l := strings.Split(name, "_") > > Index: src/pkg/go/printer/printer.go > > =================================================================== > > --- a/src/pkg/go/printer/printer.go > > +++ b/src/pkg/go/printer/printer.go > > @@ -474,7 +474,7 @@ > > * Check for vertical "line of stars" and correct prefix accordingly. > > */ > > lineOfStars := false > > - if i := strings.Index(prefix, "*"); i >= 0 { > > + if i := strings.IndexByte(prefix, '*'); i >= 0 { > > // Line of stars present. > > if i > 0 && prefix[i-1] == ' ' { > > i-- // remove trailing blank from prefix so stars remain > aligned > > Index: src/pkg/math/big/rat.go > > =================================================================== > > --- a/src/pkg/math/big/rat.go > > +++ b/src/pkg/math/big/rat.go > > @@ -429,7 +429,7 @@ > > } > > > > // check for a quotient > > - sep := strings.Index(s, "/") > > + sep := strings.IndexByte(s, '/') > > if sep >= 0 { > > if _, ok := z.a.SetString(s[0:sep], 10); !ok { > > return nil, false > > @@ -443,7 +443,7 @@ > > } > > > > // check for a decimal point > > - sep = strings.Index(s, ".") > > + sep = strings.IndexByte(s, '.') > > // check for an exponent > > e := strings.IndexAny(s, "eE") > > var exp Int > > Index: src/pkg/mime/mediatype.go > > =================================================================== > > --- a/src/pkg/mime/mediatype.go > > +++ b/src/pkg/mime/mediatype.go > > @@ -18,7 +18,7 @@ > > // When any of the arguments result in a standard violation then > > // FormatMediaType returns the empty string. > > func FormatMediaType(t string, param map[string]string) string { > > - slash := strings.Index(t, "/") > > + slash := strings.IndexByte(t, '/') > > if slash == -1 { > > return "" > > } > > @@ -91,7 +91,7 @@ > > // The returned map, params, maps from the lowercase > > // attribute to the attribute value with its case preserved. > > func ParseMediaType(v string) (mediatype string, params > map[string]string, err error) { > > - i := strings.Index(v, ";") > > + i := strings.IndexByte(v, ';') > > if i == -1 { > > i = len(v) > > } > > @@ -127,7 +127,7 @@ > > } > > > > pmap := params > > - if idx := strings.Index(key, "*"); idx != -1 { > > + if idx := strings.IndexByte(key, '*'); idx != -1 { > > baseName := key[:idx] > > if continuation == nil { > > continuation = make(map[string]map[string]string) > > Index: src/pkg/net/http/cgi/child.go > > =================================================================== > > --- a/src/pkg/net/http/cgi/child.go > > +++ b/src/pkg/net/http/cgi/child.go > > @@ -40,7 +40,7 @@ > > func envMap(env []string) map[string]string { > > m := make(map[string]string) > > for _, kv := range env { > > - if idx := strings.Index(kv, "="); idx != -1 { > > + if idx := strings.IndexByte(kv, '='); idx != -1 { > > m[kv[:idx]] = kv[idx+1:] > > } > > } > > Index: src/pkg/net/http/cookie.go > > =================================================================== > > --- a/src/pkg/net/http/cookie.go > > +++ b/src/pkg/net/http/cookie.go > > @@ -47,7 +47,7 @@ > > continue > > } > > parts[0] = strings.TrimSpace(parts[0]) > > - j := strings.Index(parts[0], "=") > > + j := strings.IndexByte(parts[0], '=') > > if j < 0 { > > continue > > } > > @@ -71,7 +71,7 @@ > > } > > > > attr, val := parts[i], "" > > - if j := strings.Index(attr, "="); j >= 0 { > > + if j := strings.IndexByte(attr, '='); j >= 0 { > > attr, val = attr[:j], attr[j+1:] > > } > > lowerAttr := strings.ToLower(attr) > > @@ -188,7 +188,7 @@ > > continue > > } > > name, val := parts[i], "" > > - if j := strings.Index(name, "="); j >= 0 { > > + if j := strings.IndexByte(name, '='); j >= 0 { > > name, val = name[:j], name[j+1:] > > } > > if !isCookieNameValid(name) { > > Index: src/pkg/net/http/fs.go > > =================================================================== > > --- a/src/pkg/net/http/fs.go > > +++ b/src/pkg/net/http/fs.go > > @@ -467,7 +467,7 @@ > > if ra == "" { > > continue > > } > > - i := strings.Index(ra, "-") > > + i := strings.IndexByte(ra, '-') > > if i < 0 { > > return nil, errors.New("invalid range") > > } > > Index: src/pkg/net/http/request.go > > =================================================================== > > --- a/src/pkg/net/http/request.go > > +++ b/src/pkg/net/http/request.go > > @@ -408,7 +408,7 @@ > > if !strings.HasPrefix(vers, "HTTP/") { > > return 0, 0, false > > } > > - dot := strings.Index(vers, ".") > > + dot := strings.IndexByte(vers, '.') > > if dot < 0 { > > return 0, 0, false > > } > > @@ -473,8 +473,8 @@ > > > > // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. > > func parseRequestLine(line string) (method, requestURI, proto string, ok > bool) { > > - s1 := strings.Index(line, " ") > > - s2 := strings.Index(line[s1+1:], " ") > > + s1 := strings.IndexByte(line, ' ') > > + s2 := strings.IndexByte(line[s1+1:], ' ') > > if s1 < 0 || s2 < 0 { > > return > > } > > Index: src/pkg/net/http/server.go > > =================================================================== > > --- a/src/pkg/net/http/server.go > > +++ b/src/pkg/net/http/server.go > > @@ -1262,7 +1262,7 @@ > > } > > > > var query string > > - if i := strings.Index(urlStr, "?"); i != -1 { > > + if i := strings.IndexByte(urlStr, '?'); i != -1 { > > urlStr, query = urlStr[:i], urlStr[i:] > > } > > > > @@ -1494,7 +1494,7 @@ > > if pattern[0] != '/' { > > // In pattern, at least the last character is a '/', so > > // strings.Index can't be -1. > > - path = pattern[strings.Index(pattern, "/"):] > > + path = pattern[strings.IndexByte(pattern, '/'):] > > } > > mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, > StatusMovedPermanently), pattern: pattern} > > } > > Index: src/pkg/net/url/url.go > > =================================================================== > > --- a/src/pkg/net/url/url.go > > +++ b/src/pkg/net/url/url.go > > @@ -421,7 +421,7 @@ > > return > > } > > userinfo, host := authority[:i], authority[i+1:] > > - if strings.Index(userinfo, ":") < 0 { > > + if strings.IndexByte(userinfo, ':') < 0 { > > if userinfo, err = unescape(userinfo, encodeUserPassword); err != > nil { > > return > > } > > @@ -536,7 +536,7 @@ > > continue > > } > > value := "" > > - if i := strings.Index(key, "="); i >= 0 { > > + if i := strings.IndexByte(key, '='); i >= 0 { > > key, value = key[:i], key[i+1:] > > } > > key, err1 := QueryUnescape(key) > > Index: src/pkg/os/os_test.go > > =================================================================== > > --- a/src/pkg/os/os_test.go > > +++ b/src/pkg/os/os_test.go > > @@ -890,7 +890,7 @@ > > } > > want := run(t, []string{"/bin/hostname"}) > > if hostname != want { > > - i := strings.Index(hostname, ".") > > + i := strings.IndexByte(hostname, '.') > > if i < 0 || hostname[0:i] != want { > > t.Errorf("Hostname() = %q, want %q", hostname, want) > > } > > Index: src/pkg/os/user/lookup_unix.go > > =================================================================== > > --- a/src/pkg/os/user/lookup_unix.go > > +++ b/src/pkg/os/user/lookup_unix.go > > @@ -105,7 +105,7 @@ > > // say: "It is expected to be a comma separated list of > > // personal data where the first item is the full name of the > > // user." > > - if i := strings.Index(u.Name, ","); i >= 0 { > > + if i := strings.IndexByte(u.Name, ','); i >= 0 { > > u.Name = u.Name[:i] > > } > > return u, nil > > Index: src/pkg/path/match.go > > =================================================================== > > --- a/src/pkg/path/match.go > > +++ b/src/pkg/path/match.go > > @@ -43,7 +43,7 @@ > > star, chunk, pattern = scanChunk(pattern) > > if star && chunk == "" { > > // Trailing * matches rest of string unless it has a /. > > - return strings.Index(name, "/") < 0, nil > > + return strings.IndexByte(name, '/') < 0, nil > > } > > // Look for match at current position. > > t, ok, err := matchChunk(chunk, name) > > Index: src/pkg/regexp/exec_test.go > > =================================================================== > > --- a/src/pkg/regexp/exec_test.go > > +++ b/src/pkg/regexp/exec_test.go > > @@ -293,7 +293,7 @@ > > out[n] = -1 > > out[n+1] = -1 > > } else { > > - k := strings.Index(pair, "-") > > + k := strings.IndexByte(pair, '-') > > if k < 0 { > > t.Fatalf("%s:%d: invalid pair %s", file, lineno, pair) > > } > > @@ -456,7 +456,7 @@ > > continue Reading > > } > > case ':': > > - i := strings.Index(flag[1:], ":") > > + i := strings.IndexByte(flag[1:], ':') > > if i < 0 { > > t.Logf("skip: %s", line) > > continue Reading > > Index: src/pkg/regexp/regexp.go > > =================================================================== > > --- a/src/pkg/regexp/regexp.go > > +++ b/src/pkg/regexp/regexp.go > > @@ -429,7 +429,7 @@ > > // in Expand, so for instance $1 represents the text of the first > submatch. > > func (re *Regexp) ReplaceAllString(src, repl string) string { > > n := 2 > > - if strings.Index(repl, "$") >= 0 { > > + if strings.IndexByte(repl, '$') >= 0 { > > n = 2 * (re.numSubexp + 1) > > } > > b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte { > > @@ -753,7 +753,7 @@ > > > > func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src > string, match []int) []byte { > > for len(template) > 0 { > > - i := strings.Index(template, "$") > > + i := strings.IndexByte(template, '$') > > if i < 0 { > > break > > } > > Index: src/pkg/unicode/maketables.go > > =================================================================== > > --- a/src/pkg/unicode/maketables.go > > +++ b/src/pkg/unicode/maketables.go > > @@ -616,7 +616,7 @@ > > } > > > > func parseScript(line string, scripts map[string][]Script) { > > - comment := strings.Index(line, "#") > > + comment := strings.IndexByte(line, '#') > > if comment >= 0 { > > line = line[0:comment] > > } > > > > > > -- > > > > ---You received this message because you are subscribed to the Google > Groups "golang-dev" group. > > To unsubscribe from this group and stop receiving emails from it, send > an email to golang-dev+unsubscribe@googlegroups.com. > > For more options, visit https://groups.google.com/groups/opt_out. > > > > >
Sign in to reply to this message.
Do you think it is worthy of a vet check, or go fix -s? On 06/08/2013, at 8:50, Brad Fitzpatrick <bradfitz@golang.org> wrote: > grep -r -E -l 'strings.Index.*"."' pkg | grep go$ | xargs perl -pi -e "s:strings.Index\((.+), \"(.)\":strings.IndexByte(\$1, '\$2':" > > > On Mon, Aug 5, 2013 at 3:48 PM, Dave Cheney <dave@cheney.net> wrote: >> Is there a vet rule for this, or did you just hit it with the grep hammer ? >> >> On 06/08/2013, at 8:37, bradfitz@golang.org wrote: >> >> > Reviewers: golang-dev1, >> > >> > Message: >> > Hello golang-dev@googlegroups.com, >> > >> > I'd like you to review this change to >> > https://go.googlecode.com/hg/ >> > >> > >> > Description: >> > all: use strings.IndexByte instead of Index where possible >> > >> > Please review this at https://codereview.appspot.com/12486043/ >> > >> > Affected files: >> > M src/pkg/crypto/x509/pem_decrypt.go >> > M src/pkg/debug/gosym/symtab.go >> > M src/pkg/encoding/json/tags.go >> > M src/pkg/encoding/xml/typeinfo.go >> > M src/pkg/encoding/xml/xml.go >> > M src/pkg/go/build/build.go >> > M src/pkg/go/printer/printer.go >> > M src/pkg/math/big/rat.go >> > M src/pkg/mime/mediatype.go >> > M src/pkg/net/http/cgi/child.go >> > M src/pkg/net/http/cookie.go >> > M src/pkg/net/http/fs.go >> > M src/pkg/net/http/request.go >> > M src/pkg/net/http/server.go >> > M src/pkg/net/url/url.go >> > M src/pkg/os/os_test.go >> > M src/pkg/os/user/lookup_unix.go >> > M src/pkg/path/match.go >> > M src/pkg/regexp/exec_test.go >> > M src/pkg/regexp/regexp.go >> > M src/pkg/unicode/maketables.go >> > >> > >> > Index: src/pkg/crypto/x509/pem_decrypt.go >> > =================================================================== >> > --- a/src/pkg/crypto/x509/pem_decrypt.go >> > +++ b/src/pkg/crypto/x509/pem_decrypt.go >> > @@ -115,7 +115,7 @@ >> > return nil, errors.New("x509: no DEK-Info header in block") >> > } >> > >> > - idx := strings.Index(dek, ",") >> > + idx := strings.IndexByte(dek, ',') >> > if idx == -1 { >> > return nil, errors.New("x509: malformed DEK-Info header") >> > } >> > Index: src/pkg/debug/gosym/symtab.go >> > =================================================================== >> > --- a/src/pkg/debug/gosym/symtab.go >> > +++ b/src/pkg/debug/gosym/symtab.go >> > @@ -40,7 +40,7 @@ >> > // PackageName returns the package part of the symbol name, >> > // or the empty string if there is none. >> > func (s *Sym) PackageName() string { >> > - if i := strings.Index(s.Name, "."); i != -1 { >> > + if i := strings.IndexByte(s.Name, '.'); i != -1 { >> > return s.Name[0:i] >> > } >> > return "" >> > @@ -49,7 +49,7 @@ >> > // ReceiverName returns the receiver type name of this symbol, >> > // or the empty string if there is none. >> > func (s *Sym) ReceiverName() string { >> > - l := strings.Index(s.Name, ".") >> > + l := strings.IndexByte(s.Name, '.') >> > r := strings.LastIndex(s.Name, ".") >> > if l == -1 || r == -1 || l == r { >> > return "" >> > Index: src/pkg/encoding/json/tags.go >> > =================================================================== >> > --- a/src/pkg/encoding/json/tags.go >> > +++ b/src/pkg/encoding/json/tags.go >> > @@ -15,7 +15,7 @@ >> > // 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 { >> > + if idx := strings.IndexByte(tag, ','); idx != -1 { >> > return tag[:idx], tagOptions(tag[idx+1:]) >> > } >> > return tag, tagOptions("") >> > @@ -31,7 +31,7 @@ >> > s := string(o) >> > for s != "" { >> > var next string >> > - i := strings.Index(s, ",") >> > + i := strings.IndexByte(s, ',') >> > if i >= 0 { >> > s, next = s[:i], s[i+1:] >> > } >> > Index: src/pkg/encoding/xml/typeinfo.go >> > =================================================================== >> > --- a/src/pkg/encoding/xml/typeinfo.go >> > +++ b/src/pkg/encoding/xml/typeinfo.go >> > @@ -113,7 +113,7 @@ >> > >> > // Split the tag from the xml namespace if necessary. >> > tag := f.Tag.Get("xml") >> > - if i := strings.Index(tag, " "); i >= 0 { >> > + if i := strings.IndexByte(tag, ' '); i >= 0 { >> > finfo.xmlns, tag = tag[:i], tag[i+1:] >> > } >> > >> > Index: src/pkg/encoding/xml/xml.go >> > =================================================================== >> > --- a/src/pkg/encoding/xml/xml.go >> > +++ b/src/pkg/encoding/xml/xml.go >> > @@ -1026,7 +1026,7 @@ >> > if !ok { >> > return >> > } >> > - i := strings.Index(s, ":") >> > + i := strings.IndexByte(s, ':') >> > if i < 0 { >> > name.Local = s >> > } else { >> > Index: src/pkg/go/build/build.go >> > =================================================================== >> > --- a/src/pkg/go/build/build.go >> > +++ b/src/pkg/go/build/build.go >> > @@ -877,7 +877,7 @@ >> > >> > // Split at colon. >> > line = strings.TrimSpace(line[4:]) >> > - i := strings.Index(line, ":") >> > + i := strings.IndexByte(line, ':') >> > if i < 0 { >> > return fmt.Errorf("%s: invalid #cgo line: %s", filename, orig) >> > } >> > @@ -1022,7 +1022,7 @@ >> > if name == "" { >> > return false >> > } >> > - if i := strings.Index(name, ","); i >= 0 { >> > + if i := strings.IndexByte(name, ','); i >= 0 { >> > // comma-separated list >> > return ctxt.match(name[:i]) && ctxt.match(name[i+1:]) >> > } >> > @@ -1076,7 +1076,7 @@ >> > // name_$(GOOS)_$(GOARCH)_test.* >> > // >> > func (ctxt *Context) goodOSArchFile(name string) bool { >> > - if dot := strings.Index(name, "."); dot != -1 { >> > + if dot := strings.IndexByte(name, '.'); dot != -1 { >> > name = name[:dot] >> > } >> > l := strings.Split(name, "_") >> > Index: src/pkg/go/printer/printer.go >> > =================================================================== >> > --- a/src/pkg/go/printer/printer.go >> > +++ b/src/pkg/go/printer/printer.go >> > @@ -474,7 +474,7 @@ >> > * Check for vertical "line of stars" and correct prefix accordingly. >> > */ >> > lineOfStars := false >> > - if i := strings.Index(prefix, "*"); i >= 0 { >> > + if i := strings.IndexByte(prefix, '*'); i >= 0 { >> > // Line of stars present. >> > if i > 0 && prefix[i-1] == ' ' { >> > i-- // remove trailing blank from prefix so stars remain aligned >> > Index: src/pkg/math/big/rat.go >> > =================================================================== >> > --- a/src/pkg/math/big/rat.go >> > +++ b/src/pkg/math/big/rat.go >> > @@ -429,7 +429,7 @@ >> > } >> > >> > // check for a quotient >> > - sep := strings.Index(s, "/") >> > + sep := strings.IndexByte(s, '/') >> > if sep >= 0 { >> > if _, ok := z.a.SetString(s[0:sep], 10); !ok { >> > return nil, false >> > @@ -443,7 +443,7 @@ >> > } >> > >> > // check for a decimal point >> > - sep = strings.Index(s, ".") >> > + sep = strings.IndexByte(s, '.') >> > // check for an exponent >> > e := strings.IndexAny(s, "eE") >> > var exp Int >> > Index: src/pkg/mime/mediatype.go >> > =================================================================== >> > --- a/src/pkg/mime/mediatype.go >> > +++ b/src/pkg/mime/mediatype.go >> > @@ -18,7 +18,7 @@ >> > // When any of the arguments result in a standard violation then >> > // FormatMediaType returns the empty string. >> > func FormatMediaType(t string, param map[string]string) string { >> > - slash := strings.Index(t, "/") >> > + slash := strings.IndexByte(t, '/') >> > if slash == -1 { >> > return "" >> > } >> > @@ -91,7 +91,7 @@ >> > // The returned map, params, maps from the lowercase >> > // attribute to the attribute value with its case preserved. >> > func ParseMediaType(v string) (mediatype string, params map[string]string, err error) { >> > - i := strings.Index(v, ";") >> > + i := strings.IndexByte(v, ';') >> > if i == -1 { >> > i = len(v) >> > } >> > @@ -127,7 +127,7 @@ >> > } >> > >> > pmap := params >> > - if idx := strings.Index(key, "*"); idx != -1 { >> > + if idx := strings.IndexByte(key, '*'); idx != -1 { >> > baseName := key[:idx] >> > if continuation == nil { >> > continuation = make(map[string]map[string]string) >> > Index: src/pkg/net/http/cgi/child.go >> > =================================================================== >> > --- a/src/pkg/net/http/cgi/child.go >> > +++ b/src/pkg/net/http/cgi/child.go >> > @@ -40,7 +40,7 @@ >> > func envMap(env []string) map[string]string { >> > m := make(map[string]string) >> > for _, kv := range env { >> > - if idx := strings.Index(kv, "="); idx != -1 { >> > + if idx := strings.IndexByte(kv, '='); idx != -1 { >> > m[kv[:idx]] = kv[idx+1:] >> > } >> > } >> > Index: src/pkg/net/http/cookie.go >> > =================================================================== >> > --- a/src/pkg/net/http/cookie.go >> > +++ b/src/pkg/net/http/cookie.go >> > @@ -47,7 +47,7 @@ >> > continue >> > } >> > parts[0] = strings.TrimSpace(parts[0]) >> > - j := strings.Index(parts[0], "=") >> > + j := strings.IndexByte(parts[0], '=') >> > if j < 0 { >> > continue >> > } >> > @@ -71,7 +71,7 @@ >> > } >> > >> > attr, val := parts[i], "" >> > - if j := strings.Index(attr, "="); j >= 0 { >> > + if j := strings.IndexByte(attr, '='); j >= 0 { >> > attr, val = attr[:j], attr[j+1:] >> > } >> > lowerAttr := strings.ToLower(attr) >> > @@ -188,7 +188,7 @@ >> > continue >> > } >> > name, val := parts[i], "" >> > - if j := strings.Index(name, "="); j >= 0 { >> > + if j := strings.IndexByte(name, '='); j >= 0 { >> > name, val = name[:j], name[j+1:] >> > } >> > if !isCookieNameValid(name) { >> > Index: src/pkg/net/http/fs.go >> > =================================================================== >> > --- a/src/pkg/net/http/fs.go >> > +++ b/src/pkg/net/http/fs.go >> > @@ -467,7 +467,7 @@ >> > if ra == "" { >> > continue >> > } >> > - i := strings.Index(ra, "-") >> > + i := strings.IndexByte(ra, '-') >> > if i < 0 { >> > return nil, errors.New("invalid range") >> > } >> > Index: src/pkg/net/http/request.go >> > =================================================================== >> > --- a/src/pkg/net/http/request.go >> > +++ b/src/pkg/net/http/request.go >> > @@ -408,7 +408,7 @@ >> > if !strings.HasPrefix(vers, "HTTP/") { >> > return 0, 0, false >> > } >> > - dot := strings.Index(vers, ".") >> > + dot := strings.IndexByte(vers, '.') >> > if dot < 0 { >> > return 0, 0, false >> > } >> > @@ -473,8 +473,8 @@ >> > >> > // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. >> > func parseRequestLine(line string) (method, requestURI, proto string, ok bool) { >> > - s1 := strings.Index(line, " ") >> > - s2 := strings.Index(line[s1+1:], " ") >> > + s1 := strings.IndexByte(line, ' ') >> > + s2 := strings.IndexByte(line[s1+1:], ' ') >> > if s1 < 0 || s2 < 0 { >> > return >> > } >> > Index: src/pkg/net/http/server.go >> > =================================================================== >> > --- a/src/pkg/net/http/server.go >> > +++ b/src/pkg/net/http/server.go >> > @@ -1262,7 +1262,7 @@ >> > } >> > >> > var query string >> > - if i := strings.Index(urlStr, "?"); i != -1 { >> > + if i := strings.IndexByte(urlStr, '?'); i != -1 { >> > urlStr, query = urlStr[:i], urlStr[i:] >> > } >> > >> > @@ -1494,7 +1494,7 @@ >> > if pattern[0] != '/' { >> > // In pattern, at least the last character is a '/', so >> > // strings.Index can't be -1. >> > - path = pattern[strings.Index(pattern, "/"):] >> > + path = pattern[strings.IndexByte(pattern, '/'):] >> > } >> > mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern} >> > } >> > Index: src/pkg/net/url/url.go >> > =================================================================== >> > --- a/src/pkg/net/url/url.go >> > +++ b/src/pkg/net/url/url.go >> > @@ -421,7 +421,7 @@ >> > return >> > } >> > userinfo, host := authority[:i], authority[i+1:] >> > - if strings.Index(userinfo, ":") < 0 { >> > + if strings.IndexByte(userinfo, ':') < 0 { >> > if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil { >> > return >> > } >> > @@ -536,7 +536,7 @@ >> > continue >> > } >> > value := "" >> > - if i := strings.Index(key, "="); i >= 0 { >> > + if i := strings.IndexByte(key, '='); i >= 0 { >> > key, value = key[:i], key[i+1:] >> > } >> > key, err1 := QueryUnescape(key) >> > Index: src/pkg/os/os_test.go >> > =================================================================== >> > --- a/src/pkg/os/os_test.go >> > +++ b/src/pkg/os/os_test.go >> > @@ -890,7 +890,7 @@ >> > } >> > want := run(t, []string{"/bin/hostname"}) >> > if hostname != want { >> > - i := strings.Index(hostname, ".") >> > + i := strings.IndexByte(hostname, '.') >> > if i < 0 || hostname[0:i] != want { >> > t.Errorf("Hostname() = %q, want %q", hostname, want) >> > } >> > Index: src/pkg/os/user/lookup_unix.go >> > =================================================================== >> > --- a/src/pkg/os/user/lookup_unix.go >> > +++ b/src/pkg/os/user/lookup_unix.go >> > @@ -105,7 +105,7 @@ >> > // say: "It is expected to be a comma separated list of >> > // personal data where the first item is the full name of the >> > // user." >> > - if i := strings.Index(u.Name, ","); i >= 0 { >> > + if i := strings.IndexByte(u.Name, ','); i >= 0 { >> > u.Name = u.Name[:i] >> > } >> > return u, nil >> > Index: src/pkg/path/match.go >> > =================================================================== >> > --- a/src/pkg/path/match.go >> > +++ b/src/pkg/path/match.go >> > @@ -43,7 +43,7 @@ >> > star, chunk, pattern = scanChunk(pattern) >> > if star && chunk == "" { >> > // Trailing * matches rest of string unless it has a /. >> > - return strings.Index(name, "/") < 0, nil >> > + return strings.IndexByte(name, '/') < 0, nil >> > } >> > // Look for match at current position. >> > t, ok, err := matchChunk(chunk, name) >> > Index: src/pkg/regexp/exec_test.go >> > =================================================================== >> > --- a/src/pkg/regexp/exec_test.go >> > +++ b/src/pkg/regexp/exec_test.go >> > @@ -293,7 +293,7 @@ >> > out[n] = -1 >> > out[n+1] = -1 >> > } else { >> > - k := strings.Index(pair, "-") >> > + k := strings.IndexByte(pair, '-') >> > if k < 0 { >> > t.Fatalf("%s:%d: invalid pair %s", file, lineno, pair) >> > } >> > @@ -456,7 +456,7 @@ >> > continue Reading >> > } >> > case ':': >> > - i := strings.Index(flag[1:], ":") >> > + i := strings.IndexByte(flag[1:], ':') >> > if i < 0 { >> > t.Logf("skip: %s", line) >> > continue Reading >> > Index: src/pkg/regexp/regexp.go >> > =================================================================== >> > --- a/src/pkg/regexp/regexp.go >> > +++ b/src/pkg/regexp/regexp.go >> > @@ -429,7 +429,7 @@ >> > // in Expand, so for instance $1 represents the text of the first submatch. >> > func (re *Regexp) ReplaceAllString(src, repl string) string { >> > n := 2 >> > - if strings.Index(repl, "$") >= 0 { >> > + if strings.IndexByte(repl, '$') >= 0 { >> > n = 2 * (re.numSubexp + 1) >> > } >> > b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte { >> > @@ -753,7 +753,7 @@ >> > >> > func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src string, match []int) []byte { >> > for len(template) > 0 { >> > - i := strings.Index(template, "$") >> > + i := strings.IndexByte(template, '$') >> > if i < 0 { >> > break >> > } >> > Index: src/pkg/unicode/maketables.go >> > =================================================================== >> > --- a/src/pkg/unicode/maketables.go >> > +++ b/src/pkg/unicode/maketables.go >> > @@ -616,7 +616,7 @@ >> > } >> > >> > func parseScript(line string, scripts map[string][]Script) { >> > - comment := strings.Index(line, "#") >> > + comment := strings.IndexByte(line, '#') >> > if comment >= 0 { >> > line = line[0:comment] >> > } >> > >> > >> > -- >> > >> > ---You received this message because you are subscribed to the Google Groups "golang-dev" group. >> > To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+unsubscribe@googlegroups.com. >> > For more options, visit https://groups.google.com/groups/opt_out. >> > >> > >
Sign in to reply to this message.
No. On Tue, Aug 6, 2013 at 8:54 AM, Dave Cheney <dave@cheney.net> wrote: > Do you think it is worthy of a vet check, or go fix -s? > > On 06/08/2013, at 8:50, Brad Fitzpatrick <bradfitz@golang.org> wrote: > > grep -r -E -l 'strings.Index.*"."' pkg | grep go$ | xargs perl -pi -e > "s:strings.Index\((.+), \"(.)\":strings.IndexByte(\$1, '\$2':" > > > On Mon, Aug 5, 2013 at 3:48 PM, Dave Cheney <dave@cheney.net> wrote: > >> Is there a vet rule for this, or did you just hit it with the grep hammer >> ? >> >> On 06/08/2013, at 8:37, bradfitz@golang.org wrote: >> >> > Reviewers: golang-dev1, >> > >> > Message: >> > Hello golang-dev@googlegroups.com, >> > >> > I'd like you to review this change to >> > https://go.googlecode.com/hg/ >> > >> > >> > Description: >> > all: use strings.IndexByte instead of Index where possible >> > >> > Please review this at https://codereview.appspot.com/12486043/ >> > >> > Affected files: >> > M src/pkg/crypto/x509/pem_decrypt.go >> > M src/pkg/debug/gosym/symtab.go >> > M src/pkg/encoding/json/tags.go >> > M src/pkg/encoding/xml/typeinfo.go >> > M src/pkg/encoding/xml/xml.go >> > M src/pkg/go/build/build.go >> > M src/pkg/go/printer/printer.go >> > M src/pkg/math/big/rat.go >> > M src/pkg/mime/mediatype.go >> > M src/pkg/net/http/cgi/child.go >> > M src/pkg/net/http/cookie.go >> > M src/pkg/net/http/fs.go >> > M src/pkg/net/http/request.go >> > M src/pkg/net/http/server.go >> > M src/pkg/net/url/url.go >> > M src/pkg/os/os_test.go >> > M src/pkg/os/user/lookup_unix.go >> > M src/pkg/path/match.go >> > M src/pkg/regexp/exec_test.go >> > M src/pkg/regexp/regexp.go >> > M src/pkg/unicode/maketables.go >> > >> > >> > Index: src/pkg/crypto/x509/pem_decrypt.go >> > =================================================================== >> > --- a/src/pkg/crypto/x509/pem_decrypt.go >> > +++ b/src/pkg/crypto/x509/pem_decrypt.go >> > @@ -115,7 +115,7 @@ >> > return nil, errors.New("x509: no DEK-Info header in block") >> > } >> > >> > - idx := strings.Index(dek, ",") >> > + idx := strings.IndexByte(dek, ',') >> > if idx == -1 { >> > return nil, errors.New("x509: malformed DEK-Info header") >> > } >> > Index: src/pkg/debug/gosym/symtab.go >> > =================================================================== >> > --- a/src/pkg/debug/gosym/symtab.go >> > +++ b/src/pkg/debug/gosym/symtab.go >> > @@ -40,7 +40,7 @@ >> > // PackageName returns the package part of the symbol name, >> > // or the empty string if there is none. >> > func (s *Sym) PackageName() string { >> > - if i := strings.Index(s.Name, "."); i != -1 { >> > + if i := strings.IndexByte(s.Name, '.'); i != -1 { >> > return s.Name[0:i] >> > } >> > return "" >> > @@ -49,7 +49,7 @@ >> > // ReceiverName returns the receiver type name of this symbol, >> > // or the empty string if there is none. >> > func (s *Sym) ReceiverName() string { >> > - l := strings.Index(s.Name, ".") >> > + l := strings.IndexByte(s.Name, '.') >> > r := strings.LastIndex(s.Name, ".") >> > if l == -1 || r == -1 || l == r { >> > return "" >> > Index: src/pkg/encoding/json/tags.go >> > =================================================================== >> > --- a/src/pkg/encoding/json/tags.go >> > +++ b/src/pkg/encoding/json/tags.go >> > @@ -15,7 +15,7 @@ >> > // 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 { >> > + if idx := strings.IndexByte(tag, ','); idx != -1 { >> > return tag[:idx], tagOptions(tag[idx+1:]) >> > } >> > return tag, tagOptions("") >> > @@ -31,7 +31,7 @@ >> > s := string(o) >> > for s != "" { >> > var next string >> > - i := strings.Index(s, ",") >> > + i := strings.IndexByte(s, ',') >> > if i >= 0 { >> > s, next = s[:i], s[i+1:] >> > } >> > Index: src/pkg/encoding/xml/typeinfo.go >> > =================================================================== >> > --- a/src/pkg/encoding/xml/typeinfo.go >> > +++ b/src/pkg/encoding/xml/typeinfo.go >> > @@ -113,7 +113,7 @@ >> > >> > // Split the tag from the xml namespace if necessary. >> > tag := f.Tag.Get("xml") >> > - if i := strings.Index(tag, " "); i >= 0 { >> > + if i := strings.IndexByte(tag, ' '); i >= 0 { >> > finfo.xmlns, tag = tag[:i], tag[i+1:] >> > } >> > >> > Index: src/pkg/encoding/xml/xml.go >> > =================================================================== >> > --- a/src/pkg/encoding/xml/xml.go >> > +++ b/src/pkg/encoding/xml/xml.go >> > @@ -1026,7 +1026,7 @@ >> > if !ok { >> > return >> > } >> > - i := strings.Index(s, ":") >> > + i := strings.IndexByte(s, ':') >> > if i < 0 { >> > name.Local = s >> > } else { >> > Index: src/pkg/go/build/build.go >> > =================================================================== >> > --- a/src/pkg/go/build/build.go >> > +++ b/src/pkg/go/build/build.go >> > @@ -877,7 +877,7 @@ >> > >> > // Split at colon. >> > line = strings.TrimSpace(line[4:]) >> > - i := strings.Index(line, ":") >> > + i := strings.IndexByte(line, ':') >> > if i < 0 { >> > return fmt.Errorf("%s: invalid #cgo line: %s", filename, >> orig) >> > } >> > @@ -1022,7 +1022,7 @@ >> > if name == "" { >> > return false >> > } >> > - if i := strings.Index(name, ","); i >= 0 { >> > + if i := strings.IndexByte(name, ','); i >= 0 { >> > // comma-separated list >> > return ctxt.match(name[:i]) && ctxt.match(name[i+1:]) >> > } >> > @@ -1076,7 +1076,7 @@ >> > // name_$(GOOS)_$(GOARCH)_test.* >> > // >> > func (ctxt *Context) goodOSArchFile(name string) bool { >> > - if dot := strings.Index(name, "."); dot != -1 { >> > + if dot := strings.IndexByte(name, '.'); dot != -1 { >> > name = name[:dot] >> > } >> > l := strings.Split(name, "_") >> > Index: src/pkg/go/printer/printer.go >> > =================================================================== >> > --- a/src/pkg/go/printer/printer.go >> > +++ b/src/pkg/go/printer/printer.go >> > @@ -474,7 +474,7 @@ >> > * Check for vertical "line of stars" and correct prefix accordingly. >> > */ >> > lineOfStars := false >> > - if i := strings.Index(prefix, "*"); i >= 0 { >> > + if i := strings.IndexByte(prefix, '*'); i >= 0 { >> > // Line of stars present. >> > if i > 0 && prefix[i-1] == ' ' { >> > i-- // remove trailing blank from prefix so stars remain >> aligned >> > Index: src/pkg/math/big/rat.go >> > =================================================================== >> > --- a/src/pkg/math/big/rat.go >> > +++ b/src/pkg/math/big/rat.go >> > @@ -429,7 +429,7 @@ >> > } >> > >> > // check for a quotient >> > - sep := strings.Index(s, "/") >> > + sep := strings.IndexByte(s, '/') >> > if sep >= 0 { >> > if _, ok := z.a.SetString(s[0:sep], 10); !ok { >> > return nil, false >> > @@ -443,7 +443,7 @@ >> > } >> > >> > // check for a decimal point >> > - sep = strings.Index(s, ".") >> > + sep = strings.IndexByte(s, '.') >> > // check for an exponent >> > e := strings.IndexAny(s, "eE") >> > var exp Int >> > Index: src/pkg/mime/mediatype.go >> > =================================================================== >> > --- a/src/pkg/mime/mediatype.go >> > +++ b/src/pkg/mime/mediatype.go >> > @@ -18,7 +18,7 @@ >> > // When any of the arguments result in a standard violation then >> > // FormatMediaType returns the empty string. >> > func FormatMediaType(t string, param map[string]string) string { >> > - slash := strings.Index(t, "/") >> > + slash := strings.IndexByte(t, '/') >> > if slash == -1 { >> > return "" >> > } >> > @@ -91,7 +91,7 @@ >> > // The returned map, params, maps from the lowercase >> > // attribute to the attribute value with its case preserved. >> > func ParseMediaType(v string) (mediatype string, params >> map[string]string, err error) { >> > - i := strings.Index(v, ";") >> > + i := strings.IndexByte(v, ';') >> > if i == -1 { >> > i = len(v) >> > } >> > @@ -127,7 +127,7 @@ >> > } >> > >> > pmap := params >> > - if idx := strings.Index(key, "*"); idx != -1 { >> > + if idx := strings.IndexByte(key, '*'); idx != -1 { >> > baseName := key[:idx] >> > if continuation == nil { >> > continuation = make(map[string]map[string]string) >> > Index: src/pkg/net/http/cgi/child.go >> > =================================================================== >> > --- a/src/pkg/net/http/cgi/child.go >> > +++ b/src/pkg/net/http/cgi/child.go >> > @@ -40,7 +40,7 @@ >> > func envMap(env []string) map[string]string { >> > m := make(map[string]string) >> > for _, kv := range env { >> > - if idx := strings.Index(kv, "="); idx != -1 { >> > + if idx := strings.IndexByte(kv, '='); idx != -1 { >> > m[kv[:idx]] = kv[idx+1:] >> > } >> > } >> > Index: src/pkg/net/http/cookie.go >> > =================================================================== >> > --- a/src/pkg/net/http/cookie.go >> > +++ b/src/pkg/net/http/cookie.go >> > @@ -47,7 +47,7 @@ >> > continue >> > } >> > parts[0] = strings.TrimSpace(parts[0]) >> > - j := strings.Index(parts[0], "=") >> > + j := strings.IndexByte(parts[0], '=') >> > if j < 0 { >> > continue >> > } >> > @@ -71,7 +71,7 @@ >> > } >> > >> > attr, val := parts[i], "" >> > - if j := strings.Index(attr, "="); j >= 0 { >> > + if j := strings.IndexByte(attr, '='); j >= 0 { >> > attr, val = attr[:j], attr[j+1:] >> > } >> > lowerAttr := strings.ToLower(attr) >> > @@ -188,7 +188,7 @@ >> > continue >> > } >> > name, val := parts[i], "" >> > - if j := strings.Index(name, "="); j >= 0 { >> > + if j := strings.IndexByte(name, '='); j >= 0 { >> > name, val = name[:j], name[j+1:] >> > } >> > if !isCookieNameValid(name) { >> > Index: src/pkg/net/http/fs.go >> > =================================================================== >> > --- a/src/pkg/net/http/fs.go >> > +++ b/src/pkg/net/http/fs.go >> > @@ -467,7 +467,7 @@ >> > if ra == "" { >> > continue >> > } >> > - i := strings.Index(ra, "-") >> > + i := strings.IndexByte(ra, '-') >> > if i < 0 { >> > return nil, errors.New("invalid range") >> > } >> > Index: src/pkg/net/http/request.go >> > =================================================================== >> > --- a/src/pkg/net/http/request.go >> > +++ b/src/pkg/net/http/request.go >> > @@ -408,7 +408,7 @@ >> > if !strings.HasPrefix(vers, "HTTP/") { >> > return 0, 0, false >> > } >> > - dot := strings.Index(vers, ".") >> > + dot := strings.IndexByte(vers, '.') >> > if dot < 0 { >> > return 0, 0, false >> > } >> > @@ -473,8 +473,8 @@ >> > >> > // parseRequestLine parses "GET /foo HTTP/1.1" into its three parts. >> > func parseRequestLine(line string) (method, requestURI, proto string, >> ok bool) { >> > - s1 := strings.Index(line, " ") >> > - s2 := strings.Index(line[s1+1:], " ") >> > + s1 := strings.IndexByte(line, ' ') >> > + s2 := strings.IndexByte(line[s1+1:], ' ') >> > if s1 < 0 || s2 < 0 { >> > return >> > } >> > Index: src/pkg/net/http/server.go >> > =================================================================== >> > --- a/src/pkg/net/http/server.go >> > +++ b/src/pkg/net/http/server.go >> > @@ -1262,7 +1262,7 @@ >> > } >> > >> > var query string >> > - if i := strings.Index(urlStr, "?"); i != -1 { >> > + if i := strings.IndexByte(urlStr, '?'); i != -1 { >> > urlStr, query = urlStr[:i], urlStr[i:] >> > } >> > >> > @@ -1494,7 +1494,7 @@ >> > if pattern[0] != '/' { >> > // In pattern, at least the last character is a '/', so >> > // strings.Index can't be -1. >> > - path = pattern[strings.Index(pattern, "/"):] >> > + path = pattern[strings.IndexByte(pattern, '/'):] >> > } >> > mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, >> StatusMovedPermanently), pattern: pattern} >> > } >> > Index: src/pkg/net/url/url.go >> > =================================================================== >> > --- a/src/pkg/net/url/url.go >> > +++ b/src/pkg/net/url/url.go >> > @@ -421,7 +421,7 @@ >> > return >> > } >> > userinfo, host := authority[:i], authority[i+1:] >> > - if strings.Index(userinfo, ":") < 0 { >> > + if strings.IndexByte(userinfo, ':') < 0 { >> > if userinfo, err = unescape(userinfo, encodeUserPassword); err >> != nil { >> > return >> > } >> > @@ -536,7 +536,7 @@ >> > continue >> > } >> > value := "" >> > - if i := strings.Index(key, "="); i >= 0 { >> > + if i := strings.IndexByte(key, '='); i >= 0 { >> > key, value = key[:i], key[i+1:] >> > } >> > key, err1 := QueryUnescape(key) >> > Index: src/pkg/os/os_test.go >> > =================================================================== >> > --- a/src/pkg/os/os_test.go >> > +++ b/src/pkg/os/os_test.go >> > @@ -890,7 +890,7 @@ >> > } >> > want := run(t, []string{"/bin/hostname"}) >> > if hostname != want { >> > - i := strings.Index(hostname, ".") >> > + i := strings.IndexByte(hostname, '.') >> > if i < 0 || hostname[0:i] != want { >> > t.Errorf("Hostname() = %q, want %q", hostname, want) >> > } >> > Index: src/pkg/os/user/lookup_unix.go >> > =================================================================== >> > --- a/src/pkg/os/user/lookup_unix.go >> > +++ b/src/pkg/os/user/lookup_unix.go >> > @@ -105,7 +105,7 @@ >> > // say: "It is expected to be a comma separated list of >> > // personal data where the first item is the full name of the >> > // user." >> > - if i := strings.Index(u.Name, ","); i >= 0 { >> > + if i := strings.IndexByte(u.Name, ','); i >= 0 { >> > u.Name = u.Name[:i] >> > } >> > return u, nil >> > Index: src/pkg/path/match.go >> > =================================================================== >> > --- a/src/pkg/path/match.go >> > +++ b/src/pkg/path/match.go >> > @@ -43,7 +43,7 @@ >> > star, chunk, pattern = scanChunk(pattern) >> > if star && chunk == "" { >> > // Trailing * matches rest of string unless it has a /. >> > - return strings.Index(name, "/") < 0, nil >> > + return strings.IndexByte(name, '/') < 0, nil >> > } >> > // Look for match at current position. >> > t, ok, err := matchChunk(chunk, name) >> > Index: src/pkg/regexp/exec_test.go >> > =================================================================== >> > --- a/src/pkg/regexp/exec_test.go >> > +++ b/src/pkg/regexp/exec_test.go >> > @@ -293,7 +293,7 @@ >> > out[n] = -1 >> > out[n+1] = -1 >> > } else { >> > - k := strings.Index(pair, "-") >> > + k := strings.IndexByte(pair, '-') >> > if k < 0 { >> > t.Fatalf("%s:%d: invalid pair %s", file, lineno, >> pair) >> > } >> > @@ -456,7 +456,7 @@ >> > continue Reading >> > } >> > case ':': >> > - i := strings.Index(flag[1:], ":") >> > + i := strings.IndexByte(flag[1:], ':') >> > if i < 0 { >> > t.Logf("skip: %s", line) >> > continue Reading >> > Index: src/pkg/regexp/regexp.go >> > =================================================================== >> > --- a/src/pkg/regexp/regexp.go >> > +++ b/src/pkg/regexp/regexp.go >> > @@ -429,7 +429,7 @@ >> > // in Expand, so for instance $1 represents the text of the first >> submatch. >> > func (re *Regexp) ReplaceAllString(src, repl string) string { >> > n := 2 >> > - if strings.Index(repl, "$") >= 0 { >> > + if strings.IndexByte(repl, '$') >= 0 { >> > n = 2 * (re.numSubexp + 1) >> > } >> > b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte >> { >> > @@ -753,7 +753,7 @@ >> > >> > func (re *Regexp) expand(dst []byte, template string, bsrc []byte, src >> string, match []int) []byte { >> > for len(template) > 0 { >> > - i := strings.Index(template, "$") >> > + i := strings.IndexByte(template, '$') >> > if i < 0 { >> > break >> > } >> > Index: src/pkg/unicode/maketables.go >> > =================================================================== >> > --- a/src/pkg/unicode/maketables.go >> > +++ b/src/pkg/unicode/maketables.go >> > @@ -616,7 +616,7 @@ >> > } >> > >> > func parseScript(line string, scripts map[string][]Script) { >> > - comment := strings.Index(line, "#") >> > + comment := strings.IndexByte(line, '#') >> > if comment >= 0 { >> > line = line[0:comment] >> > } >> > >> > >> > -- >> > >> > ---You received this message because you are subscribed to the Google >> Groups "golang-dev" group. >> > To unsubscribe from this group and stop receiving emails from it, send >> an email to golang-dev+unsubscribe@googlegroups.com. >> > For more options, visit https://groups.google.com/groups/opt_out. >> > >> > >> > > -- > > --- > You received this message because you are subscribed to the Google Groups > "golang-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to golang-dev+unsubscribe@googlegroups.com. > For more options, visit https://groups.google.com/groups/opt_out. > > >
Sign in to reply to this message.
not lgtm This is exactly why strings.IndexByte didn't exist. This code is uglier and not faster.
Sign in to reply to this message.
I still like strings.IndexByte for consistency, regardless of this CL. If we're going to have strings and bytes, the packages should feel the same. I agree this is longer, so could be considered uglier. We can revert it. I disagree about "not faster". I agree that it's not much faster, but this does avoid a function call. I was neutral on this CL, until I remembered when I first started adding APIs to the standard library, I was told to update the tree to use new APIs when I added them. On Mon, Aug 5, 2013 at 3:57 PM, Russ Cox <rsc@golang.org> wrote: > not lgtm > > This is exactly why strings.IndexByte didn't exist. This code is uglier > and not faster. > >
Sign in to reply to this message.
On Mon, Aug 5, 2013 at 7:01 PM, Brad Fitzpatrick <bradfitz@golang.org>wrote: > I still like strings.IndexByte for consistency, regardless of this CL. If > we're going to have strings and bytes, the packages should feel the same. > OK. > I agree this is longer, so could be considered uglier. We can revert it. > > I disagree about "not faster". I agree that it's not much faster, but > this does avoid a function call. > That is true, but it is roughly the same. If you are calling Index or IndexByte enough times that that function call adds up, there is something bigger wrong with your program. I was neutral on this CL, until I remembered when I first started adding > APIs to the standard library, I was told to update the tree to use new APIs > when I added them. > In general that is true. However, during the review you said the motivation was for non-constant arguments. Feel free to use it to replace non-constant single-byte string arguments, but please revert the uses with constants. Thanks. Russ
Sign in to reply to this message.
|