优化正则表达式/修复一些测试用例

This commit is contained in:
GoEdgeLab
2022-01-08 12:20:18 +08:00
parent 2526dd323b
commit bcb4592549
2 changed files with 32 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ import (
"strings" "strings"
) )
var prefixReg = regexp.MustCompile(`^\(\?(\w+)\)`) // (?x) var prefixReg = regexp.MustCompile(`^\(\?([\w\s]+)\)`) // (?x)
var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ... var prefixReg2 = regexp.MustCompile(`^\(\?([\w\s]*:)`) // (?x: ...
var braceZero = regexp.MustCompile(`^{\s*0*\s*}`) // {0} var braceZero = regexp.MustCompile(`^{\s*0*\s*}`) // {0}
var braceZero2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x} var braceZero2 = regexp.MustCompile(`^{\s*0*\s*,`) // {0, x}
@@ -18,6 +18,7 @@ type Regexp struct {
isStrict bool isStrict bool
isCaseInsensitive bool isCaseInsensitive bool
keywords []string
keywordsMap RuneMap keywordsMap RuneMap
} }
@@ -67,11 +68,24 @@ func (this *Regexp) init() {
} }
var keywords = this.ParseKeywords(exp) var keywords = this.ParseKeywords(exp)
this.keywords = keywords
if len(keywords) > 0 { if len(keywords) > 0 {
this.keywordsMap = NewRuneTree(keywords) this.keywordsMap = NewRuneTree(keywords)
} }
} }
func (this *Regexp) Keywords() []string {
return this.keywords
}
func (this *Regexp) Raw() *regexp.Regexp {
return this.rawRegexp
}
func (this *Regexp) IsCaseInsensitive() bool {
return this.isCaseInsensitive
}
func (this *Regexp) MatchString(s string) bool { func (this *Regexp) MatchString(s string) bool {
if this.keywordsMap != nil { if this.keywordsMap != nil {
var b = this.keywordsMap.Lookup(s, this.isCaseInsensitive) var b = this.keywordsMap.Lookup(s, this.isCaseInsensitive)

View File

@@ -1,6 +1,7 @@
package utils package utils
import ( import (
"github.com/TeaOSLab/EdgeNode/internal/re"
"net/http" "net/http"
"regexp" "regexp"
"runtime" "runtime"
@@ -11,14 +12,14 @@ import (
) )
func TestMatchStringCache(t *testing.T) { func TestMatchStringCache(t *testing.T) {
regex := regexp.MustCompile(`\d+`) regex := re.MustCompile(`\d+`)
t.Log(MatchStringCache(regex, "123")) t.Log(MatchStringCache(regex, "123"))
t.Log(MatchStringCache(regex, "123")) t.Log(MatchStringCache(regex, "123"))
t.Log(MatchStringCache(regex, "123")) t.Log(MatchStringCache(regex, "123"))
} }
func TestMatchBytesCache(t *testing.T) { func TestMatchBytesCache(t *testing.T) {
regex := regexp.MustCompile(`\d+`) regex := re.MustCompile(`\d+`)
t.Log(MatchBytesCache(regex, []byte("123"))) t.Log(MatchBytesCache(regex, []byte("123")))
t.Log(MatchBytesCache(regex, []byte("123"))) t.Log(MatchBytesCache(regex, []byte("123")))
t.Log(MatchBytesCache(regex, []byte("123"))) t.Log(MatchBytesCache(regex, []byte("123")))
@@ -52,7 +53,7 @@ func BenchmarkMatchStringCache(b *testing.B) {
runtime.GOMAXPROCS(1) runtime.GOMAXPROCS(1)
data := strings.Repeat("HELLO", 512) data := strings.Repeat("HELLO", 512)
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
_ = MatchStringCache(regex, data) _ = MatchStringCache(regex, data)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
@@ -63,9 +64,18 @@ func BenchmarkMatchStringCache(b *testing.B) {
func BenchmarkMatchStringCache_WithoutCache(b *testing.B) { func BenchmarkMatchStringCache_WithoutCache(b *testing.B) {
runtime.GOMAXPROCS(1) runtime.GOMAXPROCS(1)
data := strings.Repeat("HELLO", 512) data := strings.Repeat("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", 8)
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
for i := 0; i < b.N; i++ {
_ = regex.MatchString(data)
}
}
func BenchmarkMatchStringCache_WithoutCache2(b *testing.B) {
runtime.GOMAXPROCS(1)
data := strings.Repeat("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", 8)
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = regex.MatchString(data) _ = regex.MatchString(data)
} }
@@ -74,8 +84,8 @@ func BenchmarkMatchStringCache_WithoutCache(b *testing.B) {
func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) { func BenchmarkMatchBytesCache_WithoutCache(b *testing.B) {
runtime.GOMAXPROCS(1) runtime.GOMAXPROCS(1)
data := []byte(strings.Repeat("HELLO", 512)) data := []byte(strings.Repeat("HELLO", 128))
regex := regexp.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`) regex := re.MustCompile(`(?iU)\b(eval|system|exec|execute|passthru|shell_exec|phpinfo)\b`)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
_ = regex.Match(data) _ = regex.Match(data)