-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuilder.go
218 lines (184 loc) · 4.08 KB
/
builder.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ahocorasick
import (
"bufio"
"encoding/hex"
"os"
"strings"
)
type state struct {
id int64
value byte
parent *state
trans map[byte]*state
dict int64
failLink *state
dictLink *state
pattern int64
}
// TrieBuilder is used to build Tries.
type TrieBuilder struct {
states []*state
root *state
numPatterns int64
}
// NewTrieBuilder creates and initializes a new TrieBuilder.
func NewTrieBuilder() *TrieBuilder {
tb := &TrieBuilder{
states: make([]*state, 0),
root: nil,
numPatterns: 0,
}
tb.addState(0, nil)
tb.addState(0, nil)
tb.root = tb.states[1]
return tb
}
func (tb *TrieBuilder) addState(value byte, parent *state) *state {
s := &state{
id: int64(len(tb.states)),
value: value,
parent: parent,
trans: make(map[byte]*state),
dict: 0,
failLink: nil,
dictLink: nil,
pattern: 0,
}
tb.states = append(tb.states, s)
return s
}
// AddPattern adds a byte pattern to the Trie under construction.
func (tb *TrieBuilder) AddPattern(pattern []byte) *TrieBuilder {
s := tb.root
var t *state
var ok bool
for _, c := range pattern {
if t, ok = s.trans[c]; !ok {
t = tb.addState(c, s)
s.trans[c] = t
}
s = t
}
s.dict = int64(len(pattern))
s.pattern = tb.numPatterns
tb.numPatterns++
return tb
}
// AddPatterns adds multiple byte patterns to the Trie.
func (tb *TrieBuilder) AddPatterns(patterns [][]byte) *TrieBuilder {
for _, pattern := range patterns {
tb.AddPattern(pattern)
}
return tb
}
// AddString adds a string pattern to the Trie under construction.
func (tb *TrieBuilder) AddString(pattern string) *TrieBuilder {
return tb.AddPattern([]byte(pattern))
}
// AddStrings add multiple strings to the Trie.
func (tb *TrieBuilder) AddStrings(patterns []string) *TrieBuilder {
for _, pattern := range patterns {
tb.AddString(pattern)
}
return tb
}
// LoadPatterns loads byte patterns from a file. Expects one pattern per line in hexadecimal form.
func (tb *TrieBuilder) LoadPatterns(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
str := strings.TrimSpace(s.Text())
if len(str) != 0 {
pattern, err := hex.DecodeString(str)
if err != nil {
return err
}
tb.AddPattern(pattern)
}
}
return s.Err()
}
// LoadStrings loads string patterns from a file. Expects one pattern per line.
func (tb *TrieBuilder) LoadStrings(path string) error {
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
str := strings.TrimSpace(s.Text())
if len(str) != 0 {
tb.AddString(str)
}
}
return s.Err()
}
// Build constructs the Trie.
func (tb *TrieBuilder) Build() *Trie {
tb.computeFailLinks(tb.root)
tb.computeDictLinks(tb.root)
numStates := len(tb.states)
dict := make([]int64, numStates)
trans := make([][256]int64, numStates)
failLink := make([]int64, numStates)
dictLink := make([]int64, numStates)
pattern := make([]int64, numStates)
for i, s := range tb.states {
dict[i] = s.dict
pattern[i] = s.pattern
for c, t := range s.trans {
trans[i][c] = t.id
}
if s.failLink != nil {
failLink[i] = s.failLink.id
}
if s.dictLink != nil {
dictLink[i] = s.dictLink.id
}
}
return &Trie{dict, trans, failLink, dictLink, pattern}
}
func (tb *TrieBuilder) computeFailLinks(s *state) {
if s.failLink != nil {
return
}
if s == tb.root || s.parent == tb.root {
s.failLink = tb.root
} else {
var ok bool
for t := s.parent.failLink; t != tb.root; t = t.failLink {
if t.failLink == nil {
tb.computeFailLinks(t)
}
if s.failLink, ok = t.trans[s.value]; ok {
break
}
}
if s.failLink == nil {
if s.failLink, ok = tb.root.trans[s.value]; !ok {
s.failLink = tb.root
}
}
}
for _, t := range s.trans {
tb.computeFailLinks(t)
}
}
func (tb *TrieBuilder) computeDictLinks(s *state) {
if s != tb.root {
for t := s.failLink; t != tb.root; t = t.failLink {
if t.dict != 0 {
s.dictLink = t
break
}
}
}
for _, t := range s.trans {
tb.computeDictLinks(t)
}
}