// (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
}
}
}
}
- 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
}
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]
}
key = string(line[0:i])
- if strings.Index(key, " ") >= 0 {
+ if strings.Contains(key, " ") {
// Key field has space - no good.
goto Malformed
}
if !exists {
return false
}
- return strings.Index(strings.ToLower(value), "keep-alive") != -1
+ return strings.Contains(strings.ToLower(value), "keep-alive")
}
// 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
// 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
}
return true
}
v = strings.ToLower(v)
- if strings.Index(v, "keep-alive") == -1 {
+ if !strings.Contains(v, "keep-alive") {
return true
}
return false
// 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
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()
s = s[1 : n-1]
if quote == '`' {
- if strings.Index(s, "`") >= 0 {
+ if strings.Contains(s, "`") {
return "", os.EINVAL
}
return s, nil
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)
}
}
}
+
+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)
+ }
+ }
+}