diff --git a/component/trie/domain_set.go b/component/trie/domain_set.go index 1d8ec3759..574237599 100644 --- a/component/trie/domain_set.go +++ b/component/trie/domain_set.go @@ -162,16 +162,20 @@ func (ss *DomainSet) Has(key string) bool { stack = stack[0 : len(stack)-1] // back wildcard and find next node nextNodeId := countZeros(ss.labelBitmap, ss.ranks, cursor.bmIdx+1) - nextBmIdx := selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nextNodeId-1) + 1 j := cursor.index for ; j < len(key) && revLowerAt(key, j) != domainStepByte; j++ { } if j == len(key) { if getBit(ss.leaves, nextNodeId) != 0 { + // The wildcard consumed the rest of the key and reached a + // terminal node, so this branch matches. return true } + // Otherwise, this node is not terminal and this wildcard branch + // has no input left for child edges. Try any remaining saved wildcards. continue } + nextBmIdx := selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nextNodeId-1) + 1 for ; getBit(ss.labelBitmap, nextBmIdx) == 0; nextBmIdx++ { if ss.labels[nextBmIdx-nextNodeId] == domainStepByte { bmIdx = nextBmIdx @@ -196,11 +200,20 @@ func (ss *DomainSet) Has(key string) bool { } } nodeId = bmIdx - nodeId + 1 // countZeros(ss.labelBitmap, ss.ranks, bmIdx+1) - bmIdx = selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nodeId-1) + 1 - if i == len(key)-1 && getBit(ss.leaves, nodeId) == 0 && len(stack) > 0 { + if i == len(key)-1 { + if getBit(ss.leaves, nodeId) != 0 { + // All input is consumed at a terminal node, so the key matches. + return true + } + if len(stack) == 0 { + // No input remains for this non-terminal node's child edges. + // With no saved wildcard branches left, the key cannot match. + return false + } bmIdx = selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nodeId) goto RESTART } + bmIdx = selectIthOne(ss.labelBitmap, ss.ranks, ss.selects, nodeId-1) + 1 } return getBit(ss.leaves, nodeId) != 0