fix: multiple wildcard

This commit is contained in:
Skyxim 2023-04-01 11:17:15 +08:00
parent 971131d951
commit f54bf2d935
2 changed files with 14 additions and 12 deletions

View File

@ -27,6 +27,7 @@ func TestDomainComplexWildcard(t *testing.T) {
"+.baidu.com", "+.baidu.com",
"+.a.baidu.com", "+.a.baidu.com",
"www.baidu.com", "www.baidu.com",
"+.bb.baidu.com",
"test.a.net", "test.a.net",
"test.a.oc", "test.a.oc",
"www.qq.com", "www.qq.com",
@ -40,8 +41,8 @@ func TestDomainComplexWildcard(t *testing.T) {
func TestDomainWildcard(t *testing.T) { func TestDomainWildcard(t *testing.T) {
domainSet := []string{ domainSet := []string{
"*.baidu.com", "*.*.*.baidu.com",
"www.baidu.com", "www.baidu.*",
"*.*.qq.com", "*.*.qq.com",
"test.*.baidu.com", "test.*.baidu.com",
} }

View File

@ -77,7 +77,6 @@ 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
@ -86,15 +85,16 @@ func (ss *DomainSet) Has(key string) bool {
nodeId, bmIdx := 0, 0 nodeId, bmIdx := 0, 0
type wildcardCursor struct { type wildcardCursor struct {
bmIdx, index int bmIdx, index int
find bool
} }
cursor := wildcardCursor{} stack := make([]wildcardCursor, 0)
for i := 0; i < len(key); i++ { for i := 0; i < len(key); i++ {
RESTART: RESTART:
c := key[i] c := key[i]
for ; ; bmIdx++ { for ; ; bmIdx++ {
if getBit(ss.labelBitmap, bmIdx) != 0 { if getBit(ss.labelBitmap, bmIdx) != 0 {
if cursor.find { if len(stack) > 0 {
cursor := stack[len(stack)-1]
stack = stack[0 : len(stack)-1]
// back wildcard and find next node // back wildcard and find next node
nextNodeId := countZeros(ss.labelBitmap, ss.ranks, cursor.bmIdx+1) nextNodeId := countZeros(ss.labelBitmap, ss.ranks, cursor.bmIdx+1)
nextBmIdx := selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nextNodeId-1) + 1 nextBmIdx := selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nextNodeId-1) + 1
@ -102,14 +102,17 @@ func (ss *DomainSet) Has(key string) bool {
for ; j < len(key) && key[j] != domainStepByte; j++ { for ; j < len(key) && key[j] != domainStepByte; j++ {
} }
if j == len(key) { if j == len(key) {
return getBit(ss.leaves, nextNodeId) != 0 if getBit(ss.leaves, nextNodeId) != 0 {
return true
}else {
goto RESTART
}
} }
for ; ; nextBmIdx++ { for ; ; nextBmIdx++ {
if ss.labels[nextBmIdx-nextNodeId] == domainStepByte { if ss.labels[nextBmIdx-nextNodeId] == domainStepByte {
bmIdx = nextBmIdx bmIdx = nextBmIdx
nodeId = nextNodeId nodeId = nextNodeId
i = j i = j
cursor.find = false
goto RESTART goto RESTART
} }
} }
@ -120,13 +123,11 @@ func (ss *DomainSet) Has(key string) bool {
if ss.labels[bmIdx-nodeId] == complexWildcardByte { if ss.labels[bmIdx-nodeId] == complexWildcardByte {
return true return true
} else if ss.labels[bmIdx-nodeId] == wildcardByte { } else if ss.labels[bmIdx-nodeId] == wildcardByte {
cursor.find = true cursor := wildcardCursor{}
cursor.bmIdx = bmIdx cursor.bmIdx = bmIdx
cursor.index = i cursor.index = i
stack = append(stack, cursor)
} else if ss.labels[bmIdx-nodeId] == c { } else if ss.labels[bmIdx-nodeId] == c {
if ss.labels[bmIdx-nodeId] == domainStepByte {
cursor.find = false
}
break break
} }
} }