-
Notifications
You must be signed in to change notification settings - Fork 16
/
matcher_suffix_trie.go
143 lines (126 loc) · 2.82 KB
/
matcher_suffix_trie.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package domainset
// DomainSuffixTrie is a trie of domain parts segmented by '.'.
type DomainSuffixTrie struct {
Included bool
Children map[string]*DomainSuffixTrie
}
// Insert inserts a domain suffix to the trie.
// Insertion purges the leaf node's children.
// If say, we insert "www.google.com" and then "google.com",
// The children of node "google" will be purged.
func (dst *DomainSuffixTrie) Insert(domain string) {
cdst := dst
for i := len(domain) - 1; i >= 0; i-- {
if domain[i] != '.' {
continue
}
part := domain[i+1:]
domain = domain[:i]
if cdst.Children == nil {
var ndst DomainSuffixTrie
cdst.Children = map[string]*DomainSuffixTrie{
part: &ndst,
}
cdst = &ndst
} else {
ndst, ok := cdst.Children[part]
switch {
case !ok:
ndst = &DomainSuffixTrie{}
cdst.Children[part] = ndst
cdst = ndst
case ndst.Included:
return
default:
cdst = ndst
}
}
}
if cdst.Children == nil {
cdst.Children = map[string]*DomainSuffixTrie{
domain: {
Included: true,
},
}
} else {
ndst, ok := cdst.Children[domain]
if !ok {
cdst.Children[domain] = &DomainSuffixTrie{
Included: true,
}
} else {
ndst.Included = true
ndst.Children = nil
}
}
}
// Match implements the Matcher Match method.
func (dst *DomainSuffixTrie) Match(domain string) bool {
cdst := dst
for i := len(domain) - 1; i >= 0; i-- {
if domain[i] != '.' {
continue
}
if cdst.Children == nil {
return false
}
ndst, ok := cdst.Children[domain[i+1:]]
if !ok {
return false
}
if ndst.Included {
return true
}
cdst = ndst
domain = domain[:i]
}
ndst, ok := cdst.Children[domain]
if !ok {
return false
}
return ndst.Included
}
// Keys returns the keys of the trie.
func (dst *DomainSuffixTrie) Keys() (keys []string) {
for s, c := range dst.Children {
keys = c.keys(s, keys)
}
return
}
func (dst *DomainSuffixTrie) keys(suffix string, keys []string) []string {
if dst.Included {
keys = append(keys, suffix)
}
for s, c := range dst.Children {
keys = c.keys(s+"."+suffix, keys)
}
return keys
}
// Rules implements the MatcherBuilder Rules method.
func (dst *DomainSuffixTrie) Rules() []string {
return dst.Keys()
}
// MatcherCount implements the MatcherBuilder MatcherCount method.
func (dst *DomainSuffixTrie) MatcherCount() int {
if dst.Children == nil {
return 0
}
return 1
}
// AppendTo implements the MatcherBuilder AppendTo method.
func (dst *DomainSuffixTrie) AppendTo(matchers []Matcher) ([]Matcher, error) {
if dst.Children == nil {
return matchers, nil
}
return append(matchers, dst), nil
}
func NewDomainSuffixTrie(capacity int) MatcherBuilder {
return &DomainSuffixTrie{}
}
func DomainSuffixTrieFromSlice(suffixes []string) *DomainSuffixTrie {
var dst DomainSuffixTrie
for _, s := range suffixes {
dst.Insert(s)
}
return &dst
}