Created
June 14, 2015 08:25
-
-
Save bemasher/7964ef00a50922edb217 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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