fix: processing data

This commit is contained in:
Skyxim 2023-04-01 10:38:30 +08:00
parent a6ee3348df
commit 07fb529e36
2 changed files with 11 additions and 4 deletions

View File

@ -12,9 +12,12 @@ func TestDomain(t *testing.T) {
"baidu.com", "baidu.com",
"google.com", "google.com",
"www.google.com", "www.google.com",
"test.a.net",
"test.a.oc",
} }
set := trie.NewDomainSet(domainSet) set := trie.NewDomainSet(domainSet)
assert.NotNil(t, set) assert.NotNil(t, set)
assert.True(t, set.Has("test.a.net"))
assert.True(t, set.Has("google.com")) assert.True(t, set.Has("google.com"))
assert.False(t, set.Has("www.baidu.com")) assert.False(t, set.Has("www.baidu.com"))
} }
@ -24,6 +27,8 @@ func TestDomainComplexWildcard(t *testing.T) {
"+.baidu.com", "+.baidu.com",
"+.a.baidu.com", "+.a.baidu.com",
"www.baidu.com", "www.baidu.com",
"test.a.net",
"test.a.oc",
"www.qq.com", "www.qq.com",
} }
set := trie.NewDomainSet(domainSet) set := trie.NewDomainSet(domainSet)

View File

@ -33,9 +33,8 @@ func NewDomainSet(keys []string) *DomainSet {
domainTrie.Foreach(func(domain string, data struct{}) { domainTrie.Foreach(func(domain string, data struct{}) {
reserveDomains = append(reserveDomains, utils.Reverse(domain)) reserveDomains = append(reserveDomains, utils.Reverse(domain))
}) })
sort.Slice(reserveDomains, func(i, j int) bool { // ensure that the same prefix is continuous
return len(reserveDomains[i]) < len(reserveDomains[j]) sort.Strings(reserveDomains)
})
keys = reserveDomains keys = reserveDomains
if len(keys) == 0 { if len(keys) == 0 {
return nil return nil
@ -77,6 +76,7 @@ func (ss *DomainSet) Has(key string) bool {
if ss == nil { if ss == nil {
return false return false
} }
key = strings.TrimSpace(key)
key = utils.Reverse(key) key = utils.Reverse(key)
key = strings.ToLower(key) key = strings.ToLower(key)
// no more labels in this node // no more labels in this node
@ -123,7 +123,9 @@ func (ss *DomainSet) Has(key string) bool {
cursor.bmIdx = bmIdx cursor.bmIdx = bmIdx
cursor.index = i cursor.index = i
} else if ss.labels[bmIdx-nodeId] == c { } else if ss.labels[bmIdx-nodeId] == c {
if ss.labels[bmIdx-nodeId] == domainStepByte {
cursor.find = false cursor.find = false
}
break break
} }
} }