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

View file

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