strings: Contains

Tiny helper to avoid strings.Index(s, sub) != -1

R=rsc, r2, r
CC=golang-dev
https://golang.org/cl/2265044
This commit is contained in:
Brad Fitzpatrick 2010-11-01 14:32:48 -07:00 committed by Rob Pike
parent e8436689ad
commit e198a5086a
11 changed files with 38 additions and 12 deletions

View 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)
}
}
}