]> Cypherpunks repositories - gostls13.git/commitdiff
strings: Contains
authorBrad Fitzpatrick <brad@danga.com>
Mon, 1 Nov 2010 21:32:48 +0000 (14:32 -0700)
committerRob Pike <r@golang.org>
Mon, 1 Nov 2010 21:32:48 +0000 (14:32 -0700)
Tiny helper to avoid strings.Index(s, sub) != -1

R=rsc, r2, r
CC=golang-dev
https://golang.org/cl/2265044

src/pkg/exec/lp_unix.go
src/pkg/exec/lp_windows.go
src/pkg/fmt/fmt_test.go
src/pkg/http/request.go
src/pkg/http/server.go
src/pkg/http/transfer.go
src/pkg/http/url.go
src/pkg/smtp/smtp.go
src/pkg/strconv/quote.go
src/pkg/strings/strings.go
src/pkg/strings/strings_test.go

index 10f3da19e6bd5c3e078cdff4b3e754a7f2da1207..b2feecd10e1479963808dea4cbea91303666d53b 100644 (file)
@@ -25,7 +25,7 @@ func LookPath(file string) (string, os.Error) {
        // (only bypass the path if file begins with / or ./ or ../)
        // but that would not match all the Unix shells.
 
-       if strings.Index(file, "/") >= 0 {
+       if strings.Contains(file, "/") {
                if canExec(file) {
                        return file, nil
                }
index bdf6e00de151c42a686d3f919dc311751255aa6f..9d5dc1a1441672b3afd80ad2ef3a646870e19add 100644 (file)
@@ -45,7 +45,7 @@ func LookPath(file string) (string, os.Error) {
                        }
                }
        }
-       if strings.Index(file, `\`) >= 0 || strings.Index(file, `/`) >= 0 {
+       if strings.Contains(file, `\`) || strings.Contains(file, `/`) {
                if f, ok := canExec(file, exts); ok {
                        return f, nil
                }
index 2b50532863241f58b9320ecad978ac8700136464..2c09e0713b7290e027e69f413e2b4cbce9404454 100644 (file)
@@ -382,7 +382,7 @@ var fmttests = []fmtTest{
 func TestSprintf(t *testing.T) {
        for _, tt := range fmttests {
                s := Sprintf(tt.fmt, tt.val)
-               if i := strings.Index(s, "0x"); i >= 0 && strings.Index(tt.out, "PTR") >= 0 {
+               if i := strings.Index(s, "0x"); i >= 0 && strings.Contains(tt.out, "PTR") {
                        j := i + 2
                        for ; j < len(s); j++ {
                                c := s[j]
index 45533fab521b2c93875311476e58f646e85b68a9..b88689988d846e708ff6a47041c468513c1b7e47 100644 (file)
@@ -299,7 +299,7 @@ func readKeyValue(b *bufio.Reader) (key, value string, err os.Error) {
        }
 
        key = string(line[0:i])
-       if strings.Index(key, " ") >= 0 {
+       if strings.Contains(key, " ") {
                // Key field has space - no good.
                goto Malformed
        }
@@ -689,5 +689,5 @@ func (r *Request) wantsHttp10KeepAlive() bool {
        if !exists {
                return false
        }
-       return strings.Index(strings.ToLower(value), "keep-alive") != -1
+       return strings.Contains(strings.ToLower(value), "keep-alive")
 }
index 23c36c10c7b765dbc03ee5991fa5864d18a1add5..68fd32b5f3698318c0c4a2f0c8865d802531f79d 100644 (file)
@@ -317,9 +317,9 @@ func errorKludge(w *response) {
        // Is it a broken browser?
        var msg string
        switch agent := w.req.UserAgent; {
-       case strings.Index(agent, "MSIE") >= 0:
+       case strings.Contains(agent, "MSIE"):
                msg = "Internet Explorer"
-       case strings.Index(agent, "Chrome/") >= 0:
+       case strings.Contains(agent, "Chrome/"):
                msg = "Chrome"
        default:
                return
index 40945e49fc94bb0dbc36c94ca900169d90739b00..75030e87dfbb537738fb7712148217ae03ea3838 100644 (file)
@@ -340,7 +340,7 @@ func fixLength(status int, requestMethod string, header map[string]string, te []
        // Logic based on media type. The purpose of the following code is just
        // to detect whether the unsupported "multipart/byteranges" is being
        // used. A proper Content-Type parser is needed in the future.
-       if strings.Index(strings.ToLower(header["Content-Type"]), "multipart/byteranges") >= 0 {
+       if strings.Contains(strings.ToLower(header["Content-Type"]), "multipart/byteranges") {
                return -1, ErrNotSupported
        }
 
@@ -360,7 +360,7 @@ func shouldClose(major, minor int, header map[string]string) bool {
                        return true
                }
                v = strings.ToLower(v)
-               if strings.Index(v, "keep-alive") == -1 {
+               if !strings.Contains(v, "keep-alive") {
                        return true
                }
                return false
index 23abc62a97cc5a7f8f87b85e4bd9bb34c3a812b7..b878c009f9a1f569555dcb12bf92ad998dddd7d4 100644 (file)
@@ -439,7 +439,7 @@ func ParseURL(rawurl string) (url *URL, err os.Error) {
                // instead.  Clients that wish to use RawAuthority will have to
                // interpret it themselves: RFC 2396 does not define the meaning.
 
-               if strings.Index(rawHost, "%") >= 0 {
+               if strings.Contains(rawHost, "%") {
                        // Host cannot contain escaped characters.
                        err = os.ErrorString("hexadecimal escape in host")
                        goto Error
index 778d8c8839b049947363763bc38c20e9813e8536..3b805166efcecd5fc2e6ae48b0904376c8e0aa55 100644 (file)
@@ -57,7 +57,7 @@ func NewClient(conn net.Conn, host string) (*Client, os.Error) {
                return nil, err
        }
        c := &Client{Text: text, conn: conn, serverName: host}
-       if strings.Index(msg, "ESMTP") >= 0 {
+       if strings.Contains(msg, "ESMTP") {
                err = c.ehlo()
        } else {
                err = c.helo()
index ca62296d647ef4b6f5de5859dedc90cd7a0c5c76..ed588972363d534639241f20458d017987d580a7 100644 (file)
@@ -234,7 +234,7 @@ func Unquote(s string) (t string, err os.Error) {
        s = s[1 : n-1]
 
        if quote == '`' {
-               if strings.Index(s, "`") >= 0 {
+               if strings.Contains(s, "`") {
                        return "", os.EINVAL
                }
                return s, nil
index 431e3f82ea802f1448476256ada5944508b9df75..f08b855999e8b6039ffef5f2d29ca7092b778432 100644 (file)
@@ -61,6 +61,11 @@ func Count(s, sep string) int {
        return n
 }
 
+// Contains returns true if substr is within s.
+func Contains(s, substr string) bool {
+       return Index(s, substr) != -1
+}
+
 // Index returns the index of the first instance of sep in s, or -1 if sep is not present in s.
 func Index(s, sep string) int {
        n := len(sep)
index 13c21bf77a3e7e564cc0fcf69082adbc907151af..657c8e89064242595740332ca8848337635fcd0e 100644 (file)
@@ -739,3 +739,24 @@ func TestTitle(t *testing.T) {
                }
        }
 }
+
+type ContainsTest struct {
+       str, substr string
+       expected    bool
+}
+
+var ContainsTests = []ContainsTest{
+       {"abc", "bc", true},
+       {"abc", "bcd", false},
+       {"abc", "", true},
+       {"", "a", false},
+}
+
+func TestContains(t *testing.T) {
+       for _, ct := range ContainsTests {
+               if Contains(ct.str, ct.substr) != ct.expected {
+                       t.Errorf("Contains(%s, %s) = %v, want %v",
+                               ct.str, ct.substr, !ct.expected, ct.expected)
+               }
+       }
+}