]> Cypherpunks repositories - gostls13.git/commitdiff
all: replace strings.Index with strings.Contains where possible
authorNathan VanBenschoten <nvanbenschoten@gmail.com>
Tue, 22 Dec 2015 07:40:47 +0000 (02:40 -0500)
committerRuss Cox <rsc@golang.org>
Fri, 19 Feb 2016 01:06:05 +0000 (01:06 +0000)
Change-Id: Ia613f1c37bfce800ece0533a5326fca91d99a66a
Reviewed-on: https://go-review.googlesource.com/18120
Reviewed-by: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>

26 files changed:
src/cmd/compile/internal/big/ratconv.go
src/cmd/compile/internal/gc/bexport.go
src/encoding/csv/writer.go
src/encoding/gob/codec_test.go
src/encoding/gob/encoder_test.go
src/encoding/gob/gobencdec_test.go
src/encoding/xml/marshal.go
src/fmt/scan_test.go
src/go/doc/comment.go
src/go/types/universe.go
src/math/big/ratconv.go
src/mime/grammar.go
src/net/http/fs.go
src/net/rpc/server_test.go
src/net/url/url.go
src/os/exec/lp_windows.go
src/path/filepath/match.go
src/path/filepath/match_test.go
src/path/match.go
src/regexp/regexp.go
src/regexp/syntax/regexp.go
src/runtime/debug/stack_test.go
src/text/template/exec.go
src/text/template/funcs.go
src/text/template/parse/lex.go
src/time/format_test.go

index 4566ff4e39d74b289413da80bd288e8044fa7ca1..57df124e88fc97b25a21895360ca0c492153b0d0 100644 (file)
@@ -15,7 +15,7 @@ import (
 )
 
 func ratTok(ch rune) bool {
-       return strings.IndexRune("+-/0123456789.eE", ch) >= 0
+       return strings.ContainsRune("+-/0123456789.eE", ch)
 }
 
 // Scan is a support routine for fmt.Scanner. It accepts the formats
@@ -25,7 +25,7 @@ func (z *Rat) Scan(s fmt.ScanState, ch rune) error {
        if err != nil {
                return err
        }
-       if strings.IndexRune("efgEFGv", ch) < 0 {
+       if !strings.ContainsRune("efgEFGv", ch) {
                return errors.New("Rat.Scan: invalid verb")
        }
        if _, ok := z.SetString(string(tok)); !ok {
index b49f0fb55298bd17ef2aaaf57c5df539bba62edc..3edd0488e731e2179c32afe590a95c0da9c4d31b 100644 (file)
@@ -877,7 +877,7 @@ func (p *exporter) byte(b byte) {
 // tracef is like fmt.Printf but it rewrites the format string
 // to take care of indentation.
 func (p *exporter) tracef(format string, args ...interface{}) {
-       if strings.IndexAny(format, "<>\n") >= 0 {
+       if strings.ContainsAny(format, "<>\n") {
                var buf bytes.Buffer
                for i := 0; i < len(format); i++ {
                        // no need to deal with runes
index 353d91f238f80fa49dbb3fbebe37b28633f35fc0..a6056285b4637c82d3e60924e9ab3c91192b5573 100644 (file)
@@ -130,7 +130,7 @@ func (w *Writer) fieldNeedsQuotes(field string) bool {
        if field == "" {
                return false
        }
-       if field == `\.` || strings.IndexRune(field, w.Comma) >= 0 || strings.IndexAny(field, "\"\r\n") >= 0 {
+       if field == `\.` || strings.ContainsRune(field, w.Comma) || strings.ContainsAny(field, "\"\r\n") {
                return true
        }
 
index 8efcdc78fff1e6cd422ae426f554e645574e4aae..b772171f930adc43ebf66a93ba57fc65e1650d21 100644 (file)
@@ -970,7 +970,7 @@ func TestBadRecursiveType(t *testing.T) {
        err := NewEncoder(b).Encode(&rec)
        if err == nil {
                t.Error("expected error; got none")
-       } else if strings.Index(err.Error(), "recursive") < 0 {
+       } else if !strings.Contains(err.Error(), "recursive") {
                t.Error("expected recursive type error; got", err)
        }
        // Can't test decode easily because we can't encode one, so we can't pass one to a Decoder.
index 570d79696bb76f62d7a611b2c10a9ec6cd6582da..811dd2b18ce797bcb0eb1cfacea4f2ec3c90b29b 100644 (file)
@@ -280,7 +280,7 @@ func TestValueError(t *testing.T) {
        }
        t4p := &Type4{3}
        var t4 Type4 // note: not a pointer.
-       if err := encAndDec(t4p, t4); err == nil || strings.Index(err.Error(), "pointer") < 0 {
+       if err := encAndDec(t4p, t4); err == nil || !strings.Contains(err.Error(), "pointer") {
                t.Error("expected error about pointer; got", err)
        }
 }
@@ -388,7 +388,7 @@ func TestSingletons(t *testing.T) {
                        t.Errorf("expected error decoding %v: %s", test.in, test.err)
                        continue
                case err != nil && test.err != "":
-                       if strings.Index(err.Error(), test.err) < 0 {
+                       if !strings.Contains(err.Error(), test.err) {
                                t.Errorf("wrong error decoding %v: wanted %s, got %v", test.in, test.err, err)
                        }
                        continue
@@ -414,7 +414,7 @@ func TestStructNonStruct(t *testing.T) {
        var ns NonStruct
        if err := encAndDec(s, &ns); err == nil {
                t.Error("should get error for struct/non-struct")
-       } else if strings.Index(err.Error(), "type") < 0 {
+       } else if !strings.Contains(err.Error(), "type") {
                t.Error("for struct/non-struct expected type error; got", err)
        }
        // Now try the other way
@@ -424,7 +424,7 @@ func TestStructNonStruct(t *testing.T) {
        }
        if err := encAndDec(ns, &s); err == nil {
                t.Error("should get error for non-struct/struct")
-       } else if strings.Index(err.Error(), "type") < 0 {
+       } else if !strings.Contains(err.Error(), "type") {
                t.Error("for non-struct/struct expected type error; got", err)
        }
 }
index eb76b481d198365e1552dd3b76ef76fa5bebacc0..d674f0c784d0944c6675596a5deac8644da4e11b 100644 (file)
@@ -548,7 +548,7 @@ func TestGobEncoderFieldTypeError(t *testing.T) {
        if err == nil {
                t.Fatal("expected decode error for mismatched fields (encoder to non-decoder)")
        }
-       if strings.Index(err.Error(), "type") < 0 {
+       if !strings.Contains(err.Error(), "type") {
                t.Fatal("expected type error; got", err)
        }
        // Non-encoder to GobDecoder: error
@@ -562,7 +562,7 @@ func TestGobEncoderFieldTypeError(t *testing.T) {
        if err == nil {
                t.Fatal("expected decode error for mismatched fields (non-encoder to decoder)")
        }
-       if strings.Index(err.Error(), "type") < 0 {
+       if !strings.Contains(err.Error(), "type") {
                t.Fatal("expected type error; got", err)
        }
 }
index 8ebd693030eb78b939458fe0c6e608ad523878e9..9fcd5d769597917bc416c9d5d8c0f8fa869cdf1f 100644 (file)
@@ -850,7 +850,7 @@ func (p *printer) marshalStruct(tinfo *typeInfo, val reflect.Value) error {
                        switch k {
                        case reflect.String:
                                s := vf.String()
-                               dashDash = strings.Index(s, "--") >= 0
+                               dashDash = strings.Contains(s, "--")
                                dashLast = s[len(s)-1] == '-'
                                if !dashDash {
                                        p.WriteString(s)
index 7ac74dcb4bd7ce1b22ce050e6ea8609b8b9fbb93..ce6f08659acfda6420f884eb080f8e2bbafd4644 100644 (file)
@@ -519,7 +519,7 @@ func testScanfMulti(name string, t *testing.T) {
                if err != nil {
                        if test.err == "" {
                                t.Errorf("got error scanning (%q, %q): %q", test.format, test.text, err)
-                       } else if strings.Index(err.Error(), test.err) < 0 {
+                       } else if !strings.Contains(err.Error(), test.err) {
                                t.Errorf("got wrong error scanning (%q, %q): %q; expected %q", test.format, test.text, err, test.err)
                        }
                        continue
@@ -613,7 +613,7 @@ func TestScanNotPointer(t *testing.T) {
        _, err := Fscan(r, a)
        if err == nil {
                t.Error("expected error scanning non-pointer")
-       } else if strings.Index(err.Error(), "pointer") < 0 {
+       } else if !strings.Contains(err.Error(), "pointer") {
                t.Errorf("expected pointer error scanning non-pointer, got: %s", err)
        }
 }
@@ -623,7 +623,7 @@ func TestScanlnNoNewline(t *testing.T) {
        _, err := Sscanln("1 x\n", &a)
        if err == nil {
                t.Error("expected error scanning string missing newline")
-       } else if strings.Index(err.Error(), "newline") < 0 {
+       } else if !strings.Contains(err.Error(), "newline") {
                t.Errorf("expected newline error scanning string missing newline, got: %s", err)
        }
 }
@@ -634,7 +634,7 @@ func TestScanlnWithMiddleNewline(t *testing.T) {
        _, err := Fscanln(r, &a, &b)
        if err == nil {
                t.Error("expected error scanning string with extra newline")
-       } else if strings.Index(err.Error(), "newline") < 0 {
+       } else if !strings.Contains(err.Error(), "newline") {
                t.Errorf("expected newline error scanning string with extra newline, got: %s", err)
        }
 }
index f414ca4090e27dfbdb26774969eecb1853468045..5631539abc9dc89f7b4cbfa39178125d0665cffa 100644 (file)
@@ -225,7 +225,7 @@ func heading(line string) string {
        }
 
        // exclude lines with illegal characters
-       if strings.IndexAny(line, ",.;:!?+*/=()[]{}_^°&§~%#@<\">\\") >= 0 {
+       if strings.ContainsAny(line, ",.;:!?+*/=()[]{}_^°&§~%#@<\">\\") {
                return ""
        }
 
index 40185c1ad42419bad4fc79b71b41a2259324d4ec..cc3bd5a370bd9a6621cc82e8ea7c037d0736bbb4 100644 (file)
@@ -196,7 +196,7 @@ func init() {
 //
 func def(obj Object) {
        name := obj.Name()
-       if strings.Index(name, " ") >= 0 {
+       if strings.Contains(name, " ") {
                return // nothing to do
        }
        // fix Obj link for named types
index 4566ff4e39d74b289413da80bd288e8044fa7ca1..57df124e88fc97b25a21895360ca0c492153b0d0 100644 (file)
@@ -15,7 +15,7 @@ import (
 )
 
 func ratTok(ch rune) bool {
-       return strings.IndexRune("+-/0123456789.eE", ch) >= 0
+       return strings.ContainsRune("+-/0123456789.eE", ch)
 }
 
 // Scan is a support routine for fmt.Scanner. It accepts the formats
@@ -25,7 +25,7 @@ func (z *Rat) Scan(s fmt.ScanState, ch rune) error {
        if err != nil {
                return err
        }
-       if strings.IndexRune("efgEFGv", ch) < 0 {
+       if !strings.ContainsRune("efgEFGv", ch) {
                return errors.New("Rat.Scan: invalid verb")
        }
        if _, ok := z.SetString(string(tok)); !ok {
index 31b66e8f03a39d6f6dba87f5f9d70e91d7aae909..6a6f71dbd40ed8ad20e36dc5cc1761cb017ed344 100644 (file)
@@ -11,7 +11,7 @@ import (
 // isTSpecial reports whether rune is in 'tspecials' as defined by RFC
 // 1521 and RFC 2045.
 func isTSpecial(r rune) bool {
-       return strings.IndexRune(`()<>@,;:\"/[]?=`, r) != -1
+       return strings.ContainsRune(`()<>@,;:\"/[]?=`, r)
 }
 
 // isTokenChar reports whether rune is in 'token' as defined by RFC
index f61c138c1d944390f3fc1f29d9e7d382f1e6e601..8a5b8bba37516fe6f01c4a3ee09348628d9994a4 100644 (file)
@@ -34,7 +34,7 @@ import (
 type Dir string
 
 func (d Dir) Open(name string) (File, error) {
-       if filepath.Separator != '/' && strings.IndexRune(name, filepath.Separator) >= 0 ||
+       if filepath.Separator != '/' && strings.ContainsRune(name, filepath.Separator) ||
                strings.Contains(name, "\x00") {
                return nil, errors.New("http: invalid character in file path")
        }
index 8871c88133c845f1da874dd4e0444aad8975c4f2..cf171ac4fb1ea4dc8c010e50ce936a4a07d5b1c3 100644 (file)
@@ -183,7 +183,7 @@ func testRPC(t *testing.T, addr string) {
        err = client.Call("Arith.Unknown", args, reply)
        if err == nil {
                t.Error("expected error calling unknown service")
-       } else if strings.Index(err.Error(), "method") < 0 {
+       } else if !strings.Contains(err.Error(), "method") {
                t.Error("expected error about method; got", err)
        }
 
@@ -226,7 +226,7 @@ func testRPC(t *testing.T, addr string) {
        err = client.Call("Arith.Add", reply, reply) // args, reply would be the correct thing to use
        if err == nil {
                t.Error("expected error calling Arith.Add with wrong arg type")
-       } else if strings.Index(err.Error(), "type") < 0 {
+       } else if !strings.Contains(err.Error(), "type") {
                t.Error("expected error about type; got", err)
        }
 
index 1a93e3496edb00446c0c667aff6ea66258c6344f..b7e25ecfcbc2824f12e2096f0e930d34c676a232 100644 (file)
@@ -511,7 +511,7 @@ func parseAuthority(authority string) (user *Userinfo, host string, err error) {
                return nil, host, nil
        }
        userinfo := authority[:i]
-       if strings.Index(userinfo, ":") < 0 {
+       if !strings.Contains(userinfo, ":") {
                if userinfo, err = unescape(userinfo, encodeUserPassword); err != nil {
                        return nil, "", err
                }
index c3efd67e9e1e99b5ec2dcf233ac2dffa0e2bdb97..0b0712dcad79374ebc87822b4b26561c4233ac9f 100644 (file)
@@ -70,7 +70,7 @@ func LookPath(file string) (f string, err error) {
                }
                exts = append(exts, e)
        }
-       if strings.IndexAny(file, `:\/`) != -1 {
+       if strings.ContainsAny(file, `:\/`) {
                if f, err = findExecutable(file, exts); err == nil {
                        return
                }
index 89f16de355e0bab2465d317c3f2e22ed50c66936..d64bf84fc0a9efc38a746f9cf51105c02900858f 100644 (file)
@@ -49,7 +49,7 @@ Pattern:
                star, chunk, pattern = scanChunk(pattern)
                if star && chunk == "" {
                        // Trailing * matches rest of string unless it has a /.
-                       return strings.Index(name, string(Separator)) < 0, nil
+                       return !strings.Contains(name, string(Separator)), nil
                }
                // Look for match at current position.
                t, ok, err := matchChunk(chunk, name)
@@ -305,5 +305,5 @@ func glob(dir, pattern string, matches []string) (m []string, e error) {
 // recognized by Match.
 func hasMeta(path string) bool {
        // TODO(niemeyer): Should other magic characters be added here?
-       return strings.IndexAny(path, "*?[") >= 0
+       return strings.ContainsAny(path, "*?[")
 }
index 0edbfc70c4775b75238f1b6580858af4de0508b5..d8bab7f4da3969265c6de021e9aa93ce0b3f04a1 100644 (file)
@@ -88,7 +88,7 @@ func TestMatch(t *testing.T) {
                pattern := tt.pattern
                s := tt.s
                if runtime.GOOS == "windows" {
-                       if strings.Index(pattern, "\\") >= 0 {
+                       if strings.Contains(pattern, "\\") {
                                // no escape allowed on windows.
                                continue
                        }
index 75dd3b38e7c5a82fa925b975cb47de8d34744b26..8d9aa513b1870c2b620baf94ea65c9c23ba02da3 100644 (file)
@@ -43,7 +43,7 @@ Pattern:
                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.Contains(name, "/"), nil
                }
                // Look for match at current position.
                t, ok, err := matchChunk(chunk, name)
index d7d0edb993f3a0b07fab8c163fbd72fc181ae9df..42ae6e1d7a24fa42cc3ea9ec0bb817d6f7aa102c 100644 (file)
@@ -454,7 +454,7 @@ func Match(pattern string, b []byte) (matched bool, err error) {
 // 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.Contains(repl, "$") {
                n = 2 * (re.numSubexp + 1)
        }
        b := re.replaceAll(nil, src, n, func(dst []byte, match []int) []byte {
index 75822cf981e3442868cc1b20e8287b19ea394abd..ca5724063bc32a1cab5ea6976e56027cd49e8d29 100644 (file)
@@ -252,7 +252,7 @@ const meta = `\.+*?()|[]{}^$`
 
 func escape(b *bytes.Buffer, r rune, force bool) {
        if unicode.IsPrint(r) {
-               if strings.IndexRune(meta, r) >= 0 || force {
+               if strings.ContainsRune(meta, r) || force {
                        b.WriteRune('\\')
                }
                b.WriteRune(r)
index f54437231b05b715bf3102743ccac818d0a4f783..9376e82b84561319f220a037365f78a84c65dfeb 100644 (file)
@@ -59,7 +59,7 @@ func TestStack(t *testing.T) {
 }
 
 func check(t *testing.T, line, has string) {
-       if strings.Index(line, has) < 0 {
+       if !strings.Contains(line, has) {
                t.Errorf("expected %q in %q", has, line)
        }
 }
index efe1817173f5883d5979cc2682c78cf1a45b726b..5ea45a4c53f04749443ace9165d3abdabd90cf59 100644 (file)
@@ -446,7 +446,7 @@ func (s *state) idealConstant(constant *parse.NumberNode) reflect.Value {
        switch {
        case constant.IsComplex:
                return reflect.ValueOf(constant.Complex128) // incontrovertible.
-       case constant.IsFloat && !isHexConstant(constant.Text) && strings.IndexAny(constant.Text, ".eE") >= 0:
+       case constant.IsFloat && !isHexConstant(constant.Text) && strings.ContainsAny(constant.Text, ".eE"):
                return reflect.ValueOf(constant.Float64)
        case constant.IsInt:
                n := int(constant.Int64)
index 49e9e7419a49f58624655c1a12a8e2407e3589e4..58b8ea372de4f2466cab676c2415129a98296593 100644 (file)
@@ -515,7 +515,7 @@ func HTMLEscape(w io.Writer, b []byte) {
 // HTMLEscapeString returns the escaped HTML equivalent of the plain text data s.
 func HTMLEscapeString(s string) string {
        // Avoid allocation if we can.
-       if strings.IndexAny(s, `'"&<>`) < 0 {
+       if !strings.ContainsAny(s, `'"&<>`) {
                return s
        }
        var b bytes.Buffer
index ea93e051425441bd5f92c81078b29f069bfa7f01..079c0ea6f7166706f938e679af431b73cfdf8811 100644 (file)
@@ -155,7 +155,7 @@ func (l *lexer) ignore() {
 
 // accept consumes the next rune if it's from the valid set.
 func (l *lexer) accept(valid string) bool {
-       if strings.IndexRune(valid, l.next()) >= 0 {
+       if strings.ContainsRune(valid, l.next()) {
                return true
        }
        l.backup()
@@ -164,7 +164,7 @@ func (l *lexer) accept(valid string) bool {
 
 // acceptRun consumes a run of runes from the valid set.
 func (l *lexer) acceptRun(valid string) {
-       for strings.IndexRune(valid, l.next()) >= 0 {
+       for strings.ContainsRune(valid, l.next()) {
        }
        l.backup()
 }
index af950a7c25b8e1b8f00c8d88335af3112da8526d..8c47dbcdd1cfe72739a8107d48e7eec96f702631 100644 (file)
@@ -447,7 +447,7 @@ func TestParseErrors(t *testing.T) {
                _, err := Parse(test.format, test.value)
                if err == nil {
                        t.Errorf("expected error for %q %q", test.format, test.value)
-               } else if strings.Index(err.Error(), test.expect) < 0 {
+               } else if !strings.Contains(err.Error(), test.expect) {
                        t.Errorf("expected error with %q for %q %q; got %s", test.expect, test.format, test.value, err)
                }
        }