mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2025-11-23 07:00:25 +08:00
优化代码
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/TeaOSLab/EdgeNode/internal/re"
|
||||
"github.com/iwind/TeaGo/assert"
|
||||
"regexp"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -27,12 +28,14 @@ func TestRegexp_MatchString(t *testing.T) {
|
||||
var r = re.MustCompile("abc")
|
||||
a.IsTrue(r.MatchString("abc"))
|
||||
a.IsFalse(r.MatchString("ab"))
|
||||
a.IsFalse(r.MatchString("ABC"))
|
||||
}
|
||||
|
||||
{
|
||||
var r = re.MustCompile("(?i)abc|def|ghi")
|
||||
a.IsTrue(r.MatchString("DEF"))
|
||||
a.IsFalse(r.MatchString("ab"))
|
||||
a.IsTrue(r.MatchString("ABC"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,39 +49,50 @@ func TestRegexp_Sub(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestRegexp_ParseKeywords(t *testing.T) {
|
||||
var r = re.MustCompile("")
|
||||
|
||||
{
|
||||
var keywords = r.ParseKeywords(`\n\t\n\f\r\v\x123`)
|
||||
t.Log(keywords)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegexp_ParseKeywords2(t *testing.T) {
|
||||
var a = assert.NewAssertion(t)
|
||||
|
||||
var r = re.MustCompile("")
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)def"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)|(?:def)"), []string{}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)|def"), []string{}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)def"), []string{"abcdef"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)|(?:def)"), []string{"abc", "def"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc)"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(abc|def|ghi)"), []string{"abc", "def", "ghi"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("(?i:abc)"), []string{}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("\babc"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(" \babc"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("\babc\b"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("\b(abc)"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`\babc`), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(` \babc`), []string{" "}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`\babc\b`), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`\b(abc)`), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc|efg|hij"), []string{"abc", "efg", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg*|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg?|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg+|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg{2,10}|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg{0,10}|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\|efg.+|hij"), []string{"abc", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("A(abc|bcd)"), []string{"abc", "bcd"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg|hij`), []string{"abc|efg", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg*|hij`), []string{"abc|ef", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg?|hij`), []string{"abc|ef", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg+|hij`), []string{"abc|ef", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg{2,10}|hij`), []string{"abc|ef", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg{0,10}|hij`), []string{"abc|ef", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc\|efg.+|hij`), []string{"abc|efg", "hij"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("A(abc|bcd)"), []string{"Aabc", "Abcd"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("^abc"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc$"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\$"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`abc$`), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc\\d"), []string{"abc"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("abc{0,4}"), []string{"ab"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("{0,4}"), []string{}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("{1,4}"), []string{}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords("中文|北京|上海|golang"), []string{"中文", "北京", "上海", "golang"}))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`(onmouseover|onmousemove|onmousedown|onmouseup|onerror|onload|onclick|ondblclick)\s*=`), strings.Split("onmouseover|onmousemove|onmousedown|onmouseup|onerror|onload|onclick|ondblclick", "|")))
|
||||
a.IsTrue(testCompareStrings(r.ParseKeywords(`/\*(!|\x00)`), []string{"/*"}))
|
||||
}
|
||||
|
||||
func TestRegexp_ParseKeywords2(t *testing.T) {
|
||||
func TestRegexp_ParseKeywords3(t *testing.T) {
|
||||
var r = re.MustCompile("")
|
||||
|
||||
var policy = firewallconfigs.HTTPFirewallTemplate()
|
||||
@@ -94,14 +108,23 @@ func TestRegexp_ParseKeywords2(t *testing.T) {
|
||||
}
|
||||
|
||||
func BenchmarkRegexp_MatchString(b *testing.B) {
|
||||
var r = re.MustCompile("(?i)abc|def|ghi")
|
||||
var r = re.MustCompile("(?i)(onmouseover|onmousemove|onmousedown|onmouseup|onerror|onload|onclick|ondblclick|onkeydown|onkeyup|onkeypress)(\\s|%09|%0A|(\\+|%20))*(=|%3D)")
|
||||
//b.Log("keywords:", r.Keywords())
|
||||
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_MatchString2(b *testing.B) {
|
||||
var r = regexp.MustCompile("(?i)abc|def|ghi")
|
||||
var r = regexp.MustCompile("(?i)(onmouseover|onmousemove|onmousedown|onmouseup|onerror|onload|onclick|ondblclick|onkeydown|onkeyup|onkeypress)(\\s|%09|%0A|(\\+|%20))*(=|%3D)")
|
||||
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_CaseSensitive(b *testing.B) {
|
||||
var r = re.MustCompile("(abc|def|ghi)")
|
||||
b.Log("keywords:", r.Keywords())
|
||||
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")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user