diff --git a/internal/re/regexp.go b/internal/re/regexp.go index 6eb6680..6ce892e 100644 --- a/internal/re/regexp.go +++ b/internal/re/regexp.go @@ -136,6 +136,10 @@ func (this *Regexp) Match(s []byte) bool { return this.rawRegexp.Match(s) } +func (this *Regexp) FindStringSubmatch(s string) []string { + return this.rawRegexp.FindStringSubmatch(s) +} + // ParseKeywords 提取表达式中的关键词 func (this *Regexp) ParseKeywords(exp string) (keywords []string) { if len(exp) == 0 { diff --git a/internal/re/regexp_test.go b/internal/re/regexp_test.go index 4d6cfa5..7e7b955 100644 --- a/internal/re/regexp_test.go +++ b/internal/re/regexp_test.go @@ -125,11 +125,36 @@ func BenchmarkRegexp_MatchString2(b *testing.B) { func BenchmarkRegexp_MatchString_CaseSensitive(b *testing.B) { var r = re.MustCompile("(abc|def|ghi)") b.Log("keywords:", r.Keywords()) + b.ResetTimer() for i := 0; i < b.N; i++ { r.MatchString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36") } } +func BenchmarkRegexp_MatchString_CaseSensitive2(b *testing.B) { + var r = regexp.MustCompile("(abc|def|ghi)") + b.ResetTimer() + for i := 0; i < b.N; i++ { + r.MatchString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36") + } +} + +func BenchmarkRegexp_MatchString_VS_FindSubString1(b *testing.B) { + var r = re.MustCompile("(?i)(chrome)") + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = r.Raw().MatchString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36") + } +} + +func BenchmarkRegexp_MatchString_VS_FindSubString2(b *testing.B) { + var r = re.MustCompile("(?i)(chrome)") + b.ResetTimer() + for i := 0; i < b.N; i++ { + _ = r.Raw().FindStringSubmatch("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36") + } +} + func testCompareStrings(s1 []string, s2 []string) bool { if len(s1) != len(s2) { return false