]> Cypherpunks repositories - gostls13.git/commitdiff
all: prefer strings.LastIndexByte over strings.LastIndex
authorMarvin Stenger <marvin.stenger94@gmail.com>
Mon, 25 Sep 2017 13:47:44 +0000 (15:47 +0200)
committerIan Lance Taylor <iant@golang.org>
Wed, 27 Sep 2017 00:54:24 +0000 (00:54 +0000)
strings.LastIndexByte was introduced in go1.5 and it can be used
effectively wherever the second argument to strings.LastIndex is
exactly one byte long.

This avoids generating unnecessary string symbols and saves
a few calls to strings.LastIndex.

Change-Id: I7b5679d616197b055cffe6882a8675d24a98b574
Reviewed-on: https://go-review.googlesource.com/66372
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
25 files changed:
src/archive/tar/writer.go
src/cmd/doc/main.go
src/cmd/fix/fix.go
src/cmd/vet/print.go
src/crypto/tls/handshake_client.go
src/crypto/tls/tls.go
src/debug/gosym/symtab.go
src/encoding/xml/marshal.go
src/go/build/build.go
src/go/doc/testdata/testing.go
src/go/printer/printer.go
src/go/types/resolver.go
src/internal/trace/parser.go
src/net/http/cookiejar/jar.go
src/net/http/cookiejar/jar_test.go
src/net/http/http.go
src/net/http/request.go
src/net/http/transport.go
src/net/mail/message.go
src/net/rpc/server.go
src/net/url/url.go
src/os/exec/lp_windows.go
src/path/path.go
src/testing/testing.go
src/time/mono_test.go

index 0ae48b8b23f4e8ec13f1dc375d98a81eb6ee81b9..5e183cb2bb9ffe95631360166e09d4df9f9eebee 100644 (file)
@@ -376,7 +376,7 @@ func splitUSTARPath(name string) (prefix, suffix string, ok bool) {
                length--
        }
 
-       i := strings.LastIndex(name[:length], "/")
+       i := strings.LastIndexByte(name[:length], '/')
        nlen := len(name) - i - 1 // nlen is length of suffix
        plen := i                 // plen is length of prefix
        if i <= 0 || nlen > nameSize || nlen == 0 || plen > prefixSize {
index 09f551a45d5b54f51a9f59d94ca25c7c349ca568..78ea038e61e6e24c7148d471be17441af594bd63 100644 (file)
@@ -205,7 +205,7 @@ func parseArgs(args []string) (pkg *build.Package, path, symbol string, more boo
        }
        // If it has a slash, it must be a package path but there is a symbol.
        // It's the last package path we care about.
-       slash := strings.LastIndex(arg, "/")
+       slash := strings.LastIndexByte(arg, '/')
        // There may be periods in the package path before or after the slash
        // and between a symbol and method.
        // Split the string at various periods to see what we find.
index cdc3c839d32f7903c666b3b47e25cff17796eccb..f12d1c7fe0e1149486d9693d0b02503b40f379c6 100644 (file)
@@ -719,7 +719,7 @@ func usesImport(f *ast.File, path string) (used bool) {
        case "<nil>":
                // If the package name is not explicitly specified,
                // make an educated guess. This is not guaranteed to be correct.
-               lastSlash := strings.LastIndex(path, "/")
+               lastSlash := strings.LastIndexByte(path, '/')
                if lastSlash == -1 {
                        name = path
                } else {
index 67a96ed2c4febccb41c7393490a599d97fde7058..f64e449f6d79275f229d23c1b6948e31bc1b8342 100644 (file)
@@ -38,7 +38,7 @@ func initPrintFlags() {
 
                // Backwards compatibility: skip optional first argument
                // index after the colon.
-               if colon := strings.LastIndex(name, ":"); colon > 0 {
+               if colon := strings.LastIndexByte(name, ':'); colon > 0 {
                        name = name[:colon]
                }
 
index f8db66279f79b731929acce50051144af89bcebd..38d01bf06815d8f05eb1f17634bfe6d0e7c9697d 100644 (file)
@@ -848,7 +848,7 @@ func hostnameInSNI(name string) string {
        if len(host) > 0 && host[0] == '[' && host[len(host)-1] == ']' {
                host = host[1 : len(host)-1]
        }
-       if i := strings.LastIndex(host, "%"); i > 0 {
+       if i := strings.LastIndexByte(host, '%'); i > 0 {
                host = host[:i]
        }
        if net.ParseIP(host) != nil {
index 615d1e5576fdaab8c772eadae62a3d146757cc6d..1c91daeb1ce2bc2d6d8481b499acfb7aaf6e8ff7 100644 (file)
@@ -122,7 +122,7 @@ func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*
                return nil, err
        }
 
-       colonPos := strings.LastIndex(addr, ":")
+       colonPos := strings.LastIndexByte(addr, ':')
        if colonPos == -1 {
                colonPos = len(addr)
        }
index b81f872801e18d0401c29c2832916bebe9bcd421..eab255a3596cb7ce7e9de9172f7fbe87f023db7f 100644 (file)
@@ -40,7 +40,7 @@ func (s *Sym) Static() bool { return s.Type >= 'a' }
 // PackageName returns the package part of the symbol name,
 // or the empty string if there is none.
 func (s *Sym) PackageName() string {
-       pathend := strings.LastIndex(s.Name, "/")
+       pathend := strings.LastIndexByte(s.Name, '/')
        if pathend < 0 {
                pathend = 0
        }
@@ -54,12 +54,12 @@ func (s *Sym) PackageName() string {
 // ReceiverName returns the receiver type name of this symbol,
 // or the empty string if there is none.
 func (s *Sym) ReceiverName() string {
-       pathend := strings.LastIndex(s.Name, "/")
+       pathend := strings.LastIndexByte(s.Name, '/')
        if pathend < 0 {
                pathend = 0
        }
        l := strings.IndexByte(s.Name[pathend:], '.')
-       r := strings.LastIndex(s.Name[pathend:], ".")
+       r := strings.LastIndexByte(s.Name[pathend:], '.')
        if l == -1 || r == -1 || l == r {
                return ""
        }
@@ -68,7 +68,7 @@ func (s *Sym) ReceiverName() string {
 
 // BaseName returns the symbol name without the package or receiver name.
 func (s *Sym) BaseName() string {
-       if i := strings.LastIndex(s.Name, "."); i != -1 {
+       if i := strings.LastIndexByte(s.Name, '.'); i != -1 {
                return s.Name[i+1:]
        }
        return s.Name
index 42133a75abc056bd1f1dc8fd72e26d5f8f9c8ded..773f0f2862ee6eb2420e7ee2e4eb0357c8409ffb 100644 (file)
@@ -333,7 +333,7 @@ func (p *printer) createAttrPrefix(url string) string {
        // Pick a name. We try to use the final element of the path
        // but fall back to _.
        prefix := strings.TrimRight(url, "/")
-       if i := strings.LastIndex(prefix, "/"); i >= 0 {
+       if i := strings.LastIndexByte(prefix, '/'); i >= 0 {
                prefix = prefix[i+1:]
        }
        if prefix == "" || !isName([]byte(prefix)) || strings.Contains(prefix, ":") {
index 9822affa8a6fdb7735ec938ce6830948a4810899..c449ad95e801adc48922226c3f467817dfe15381 100644 (file)
@@ -465,7 +465,7 @@ func (e *MultiplePackageError) Error() string {
 }
 
 func nameExt(name string) string {
-       i := strings.LastIndex(name, ".")
+       i := strings.LastIndexByte(name, '.')
        if i < 0 {
                return ""
        }
@@ -610,7 +610,7 @@ func (ctxt *Context) Import(path string, srcDir string, mode ImportMode) (*Packa
                                                }
                                                tried.vendor = append(tried.vendor, dir)
                                        }
-                                       i := strings.LastIndex(sub, "/")
+                                       i := strings.LastIndexByte(sub, '/')
                                        if i < 0 {
                                                break
                                        }
@@ -1064,7 +1064,7 @@ func (ctxt *Context) matchFile(dir, name string, allTags map[string]bool, binary
                return
        }
 
-       i := strings.LastIndex(name, ".")
+       i := strings.LastIndexByte(name, '.')
        if i < 0 {
                i = len(name)
        }
index 52810f7a564c17389dd6e5f30404dc517ba8d967..066e4a43db7b3fe4a564dc7d35822c52805c68d8 100644 (file)
@@ -93,7 +93,7 @@ func decorate(s string, addFileLine bool) string {
                _, file, line, ok := runtime.Caller(3) // decorate + log + public function.
                if ok {
                        // Truncate file name at last file name separator.
-                       if index := strings.LastIndex(file, "/"); index >= 0 {
+                       if index := strings.LastIndexByte(file, '/'); index >= 0 {
                                file = file[index+1:]
                        } else if index = strings.LastIndex(file, "\\"); index >= 0 {
                                file = file[index+1:]
index 4f4beadcab23e5456ccda9df28e99a3b51ede3ce..6a954e21fee9c37ccf51f28fac404f593cfff32e 100644 (file)
@@ -624,7 +624,7 @@ func (p *printer) writeComment(comment *ast.Comment) {
        if strings.HasPrefix(text, linePrefix) && (!pos.IsValid() || pos.Column == 1) {
                // possibly a line directive
                ldir := strings.TrimSpace(text[len(linePrefix):])
-               if i := strings.LastIndex(ldir, ":"); i >= 0 {
+               if i := strings.LastIndexByte(ldir, ':'); i >= 0 {
                        if line, err := strconv.Atoi(ldir[i+1:]); err == nil && line > 0 {
                                // The line directive we are about to print changed
                                // the Filename and Line number used for subsequent
index ba75a0dc2398c67454407af4bb49a936ac33adb0..253f8a0334ea4f37dec772d664ffef45313fc58f 100644 (file)
@@ -172,7 +172,7 @@ func (check *Checker) importPackage(pos token.Pos, path, dir string) *Package {
                                if i := len(name); i > 0 && name[i-1] == '/' {
                                        name = name[:i-1]
                                }
-                               if i := strings.LastIndex(name, "/"); i >= 0 {
+                               if i := strings.LastIndexByte(name, '/'); i >= 0 {
                                        name = name[i+1:]
                                }
                                imp = NewPackage(path, name)
@@ -516,7 +516,7 @@ func (check *Checker) unusedImports() {
 
 // pkgName returns the package name (last element) of an import path.
 func pkgName(path string) string {
-       if i := strings.LastIndex(path, "/"); i >= 0 {
+       if i := strings.LastIndexByte(path, '/'); i >= 0 {
                path = path[i+1:]
        }
        return path
index a774bf14c96ae77c1e8719dfdb99192a42abf616..1033d5222b6c9352252f8ecb8aacba6154e5b2d9 100644 (file)
@@ -812,7 +812,7 @@ func symbolize(events []*Event, bin string) error {
                f := &Frame{PC: pc}
                f.Fn = fn[:len(fn)-1]
                f.File = file[:len(file)-1]
-               if colon := strings.LastIndex(f.File, ":"); colon != -1 {
+               if colon := strings.LastIndexByte(f.File, ':'); colon != -1 {
                        ln, err := strconv.Atoi(f.File[colon+1:])
                        if err == nil {
                                f.File = f.File[:colon]
index ef8c35bf0a167700e5fc41f2e66df2978923783f..f147eceb189cd67cc03cf19455d7a039267c2fe1 100644 (file)
@@ -330,7 +330,7 @@ func jarKey(host string, psl PublicSuffixList) string {
 
        var i int
        if psl == nil {
-               i = strings.LastIndex(host, ".")
+               i = strings.LastIndexByte(host, '.')
                if i <= 0 {
                        return host
                }
@@ -349,7 +349,7 @@ func jarKey(host string, psl PublicSuffixList) string {
                // here on, so it is okay if psl.PublicSuffix("www.buggy.psl")
                // returns "com" as the jar key is generated from host.
        }
-       prevDot := strings.LastIndex(host[:i-1], ".")
+       prevDot := strings.LastIndexByte(host[:i-1], '.')
        return host[prevDot+1:]
 }
 
@@ -365,7 +365,7 @@ func defaultPath(path string) string {
                return "/" // Path is empty or malformed.
        }
 
-       i := strings.LastIndex(path, "/") // Path starts with "/", so i != -1.
+       i := strings.LastIndexByte(path, '/') // Path starts with "/", so i != -1.
        if i == 0 {
                return "/" // Path has the form "/abc".
        }
index 47fb1abdaafa4a24dd1335b9ce242ba3cf63dc64..ed4baae4c616c80171afe90f9e40c5e517fc7279 100644 (file)
@@ -37,7 +37,7 @@ func (testPSL) PublicSuffix(d string) string {
        if d == "www2.buggy.psl" {
                return "com"
        }
-       return d[strings.LastIndex(d, ".")+1:]
+       return d[strings.LastIndexByte(d, '.')+1:]
 }
 
 // newTestJar creates an empty Jar with testPSL as the public suffix list.
index b95ca89f4094c5f04445c83ad5c3d9069cc777f9..7d5b74092e68408d5dbca00a2c1efb17560de776 100644 (file)
@@ -35,7 +35,7 @@ func (k *contextKey) String() string { return "net/http context value " + k.name
 
 // Given a string of the form "host", "host:port", or "[ipv6::address]:port",
 // return true if the string includes a port.
-func hasPort(s string) bool { return strings.LastIndex(s, ":") > strings.LastIndex(s, "]") }
+func hasPort(s string) bool { return strings.LastIndexByte(s, ':') > strings.LastIndexByte(s, ']') }
 
 // removeEmptyPort strips the empty port in ":port" to ""
 // as mandated by RFC 3986 Section 6.2.3.
index b7fcf806ba117992d677452388ae9d5d6997da23..e38451766867e9a27863fb7354052d39a9c1af79 100644 (file)
@@ -688,11 +688,11 @@ func removeZone(host string) string {
        if !strings.HasPrefix(host, "[") {
                return host
        }
-       i := strings.LastIndex(host, "]")
+       i := strings.LastIndexByte(host, ']')
        if i < 0 {
                return host
        }
-       j := strings.LastIndex(host[:i], "%")
+       j := strings.LastIndexByte(host[:i], '%')
        if j < 0 {
                return host
        }
index 5f2ace7b4b4fcf703f00c10a987720f35f0592ea..034d016cb36f9be8b00247075c71e3d8b36b13dd 100644 (file)
@@ -1236,7 +1236,7 @@ func useProxy(addr string) bool {
 
        addr = strings.ToLower(strings.TrimSpace(addr))
        if hasPort(addr) {
-               addr = addr[:strings.LastIndex(addr, ":")]
+               addr = addr[:strings.LastIndexByte(addr, ':')]
        }
 
        for _, p := range strings.Split(noProxy, ",") {
@@ -1245,7 +1245,7 @@ func useProxy(addr string) bool {
                        continue
                }
                if hasPort(p) {
-                       p = p[:strings.LastIndex(p, ":")]
+                       p = p[:strings.LastIndexByte(p, ':')]
                }
                if addr == p {
                        return false
@@ -1317,7 +1317,7 @@ func (cm *connectMethod) addr() string {
 func (cm *connectMethod) tlsHost() string {
        h := cm.targetAddr
        if hasPort(h) {
-               h = h[:strings.LastIndex(h, ":")]
+               h = h[:strings.LastIndexByte(h, ':')]
        }
        return h
 }
index e080e017da3554891942945faa0e9b694c673f50..9c29e812c01afebaa14c4fbdccbe2a3acc83a22c 100644 (file)
@@ -178,7 +178,7 @@ func (p *AddressParser) ParseList(list string) ([]*Address, error) {
 // the name will be rendered according to RFC 2047.
 func (a *Address) String() string {
        // Format address local@domain
-       at := strings.LastIndex(a.Address, "@")
+       at := strings.LastIndexByte(a.Address, '@')
        var local, domain string
        if at < 0 {
                // This is a malformed address ("@" is required in addr-spec);
index 29aae7ee7ff04b19815cfac7ebaf6b82266435d4..0499f4abe20564189bdf1dc588be35e735c503a4 100644 (file)
@@ -592,7 +592,7 @@ func (server *Server) readRequestHeader(codec ServerCodec) (svc *service, mtype
        // we can still recover and move on to the next request.
        keepReading = true
 
-       dot := strings.LastIndex(req.ServiceMethod, ".")
+       dot := strings.LastIndexByte(req.ServiceMethod, '.')
        if dot < 0 {
                err = errors.New("rpc: service/method request ill-formed: " + req.ServiceMethod)
                return
index 02d446e4a2dbacf441c16a3e6e48caeecf3a4b7d..a317d096f265ab695fc37af8c027df4845c42e54 100644 (file)
@@ -532,7 +532,7 @@ func parse(rawurl string, viaRequest bool) (*URL, error) {
 }
 
 func parseAuthority(authority string) (user *Userinfo, host string, err error) {
-       i := strings.LastIndex(authority, "@")
+       i := strings.LastIndexByte(authority, '@')
        if i < 0 {
                host, err = parseHost(authority)
        } else {
@@ -569,7 +569,7 @@ func parseHost(host string) (string, error) {
        if strings.HasPrefix(host, "[") {
                // Parse an IP-Literal in RFC 3986 and RFC 6874.
                // E.g., "[fe80::1]", "[fe80::1%25en0]", "[fe80::1]:80".
-               i := strings.LastIndex(host, "]")
+               i := strings.LastIndexByte(host, ']')
                if i < 0 {
                        return "", errors.New("missing ']' in host")
                }
@@ -885,7 +885,7 @@ func resolvePath(base, ref string) string {
        if ref == "" {
                full = base
        } else if ref[0] != '/' {
-               i := strings.LastIndex(base, "/")
+               i := strings.LastIndexByte(base, '/')
                full = base[:i+1] + ref
        } else {
                full = ref
index 793d4d98b3a6aef429196c9f4a5186af131944ae..4e1ffb0014660aef79d9757ea4ae07a2f251a59d 100644 (file)
@@ -26,7 +26,7 @@ func chkStat(file string) error {
 }
 
 func hasExt(file string) bool {
-       i := strings.LastIndex(file, ".")
+       i := strings.LastIndexByte(file, '.')
        if i < 0 {
                return false
        }
index 5c905110a1bd72a4d34e49654d3583c19be5eaf8..074cfff67ae5913a4e38338fb0eedc9ec6fbd309 100644 (file)
@@ -145,7 +145,7 @@ func Clean(path string) string {
 // file set to path.
 // The returned values have the property that path = dir+file.
 func Split(path string) (dir, file string) {
-       i := strings.LastIndex(path, "/")
+       i := strings.LastIndexByte(path, '/')
        return path[:i+1], path[i+1:]
 }
 
@@ -187,7 +187,7 @@ func Base(path string) string {
                path = path[0 : len(path)-1]
        }
        // Find the last element
-       if i := strings.LastIndex(path, "/"); i >= 0 {
+       if i := strings.LastIndexByte(path, '/'); i >= 0 {
                path = path[i+1:]
        }
        // If empty now, it had only slashes.
index a170cd0fd9a46e222f6288ca80e6eb8ae9120640..d5c045f25387ad6fe75ae742d26dc780ae068cec 100644 (file)
@@ -360,7 +360,7 @@ func (c *common) decorate(s string) string {
        _, file, line, ok := runtime.Caller(skip)
        if ok {
                // Truncate file name at last file name separator.
-               if index := strings.LastIndex(file, "/"); index >= 0 {
+               if index := strings.LastIndexByte(file, '/'); index >= 0 {
                        file = file[index+1:]
                } else if index = strings.LastIndex(file, "\\"); index >= 0 {
                        file = file[index+1:]
index 8778ab78a03ac4886c07eb8bf1af0d55f4c3bc46..cc24db07f1bff9c8f9446805462895e8b05661d1 100644 (file)
@@ -253,7 +253,7 @@ func TestMonotonicString(t *testing.T) {
                t1 := Now()
                SetMono(&t1, tt.mono)
                s := t1.String()
-               got := s[strings.LastIndex(s, " ")+1:]
+               got := s[strings.LastIndexByte(s, ' ')+1:]
                if got != tt.want {
                        t.Errorf("with mono=%d: got %q; want %q", tt.mono, got, tt.want)
                }