Skip to content

Instantly share code, notes, and snippets.

@bemasher
Created June 14, 2015 08:25
Show Gist options
  • Save bemasher/7964ef00a50922edb217 to your computer and use it in GitHub Desktop.
Save bemasher/7964ef00a50922edb217 to your computer and use it in GitHub Desktop.
package contains
import (
"regexp"
"strings"
)
func ContainsToUpper(s, substr string) bool {
return strings.Contains(strings.ToUpper(s), strings.ToUpper(substr))
}
func ContainsRegex(s, substr string) (match bool) {
match, _ = regexp.MatchString("(?i)"+substr, s)
return match
}
package contains
import "testing"
// Run benchmarks with: go test -bench .
var tests = []struct {
str, substr string
result bool
}{
{"foobar", "foo", true},
{"foObar", "foo", true},
{"foobar", "foO", true},
{"foobar", "foO1", false},
}
func TestContainsToUpper(t *testing.T) {
for _, test := range tests {
if ContainsToUpper(test.str, test.substr) != test.result {
t.Fatal(test)
}
}
}
func TestContainsRegex(t *testing.T) {
for _, test := range tests {
if ContainsRegex(test.str, test.substr) != test.result {
t.Fatal(test)
}
}
}
func BenchmarkContainsToUpper(b *testing.B) {
for idx := 0; idx < b.N; idx++ {
for _, test := range tests {
ContainsToUpper(test.str, test.substr)
}
}
}
func BenchmarkContainsRegex(b *testing.B) {
for idx := 0; idx < b.N; idx++ {
for _, test := range tests {
ContainsRegex(test.str, test.substr)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment