You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
package strmatcher
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
func breakDomain(domain string) []string {
|
|
|
|
return strings.Split(domain, ".")
|
|
|
|
}
|
|
|
|
|
|
|
|
type node struct {
|
|
|
|
value uint32
|
|
|
|
sub map[string]*node
|
|
|
|
}
|
|
|
|
|
|
|
|
type DomainMatcherGroup struct {
|
|
|
|
root *node
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *DomainMatcherGroup) Add(domain string, value uint32) {
|
|
|
|
if g.root == nil {
|
|
|
|
g.root = &node{
|
|
|
|
sub: make(map[string]*node),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
current := g.root
|
|
|
|
parts := breakDomain(domain)
|
|
|
|
for i := len(parts) - 1; i >= 0; i-- {
|
|
|
|
part := parts[i]
|
|
|
|
next := current.sub[part]
|
|
|
|
if next == nil {
|
|
|
|
next = &node{sub: make(map[string]*node)}
|
|
|
|
current.sub[part] = next
|
|
|
|
}
|
|
|
|
current = next
|
|
|
|
}
|
|
|
|
|
|
|
|
current.value = value
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *DomainMatcherGroup) addMatcher(m domainMatcher, value uint32) {
|
|
|
|
g.Add(string(m), value)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *DomainMatcherGroup) Match(domain string) uint32 {
|
|
|
|
current := g.root
|
|
|
|
if current == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
parts := breakDomain(domain)
|
|
|
|
for i := len(parts) - 1; i >= 0; i-- {
|
|
|
|
part := parts[i]
|
|
|
|
next := current.sub[part]
|
|
|
|
if next == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
current = next
|
|
|
|
}
|
|
|
|
return current.value
|
|
|
|
}
|