forked from cloudflare/ahocorasick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathahocorasick.go
246 lines (189 loc) · 5.23 KB
/
ahocorasick.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// ahocorasick.go: implementation of the Aho-Corasick string matching
// algorithm. Actually implemented as matching against []byte rather
// than the Go string type. Throughout this code []byte is referred to
// as a blice.
//
// http://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_string_matching_algorithm
//
// Copyright (c) 2013 CloudFlare, Inc.
package ahocorasick
import (
"container/list"
)
// A node in the trie structure used to implement Aho-Corasick
type node struct {
root bool // true if this is the root
b []byte // The blice at this node
output bool // True means this node represents a blice that should
// be output when matching
index int // index into original dictionary if output is true
// The use of fixed size arrays is space-inefficient but fast for
// lookups.
child [256]*node // A non-nil entry in this array means that the
// index represents a byte value which can be
// appended to the current node. Blices in the
// trie are built up byte by byte through these
// child node pointers.
fails [256]*node // Where to fail to (by following the fail
// pointers) for each possible byte
suffix *node // Pointer to the longest possible strict suffix of
// this node
fail *node // Pointer to the next node which is in the dictionary
// which can be reached from here following suffixes. Called fail
// because it is used to fallback in the trie when a match fails.
}
// Matcher is returned by NewMatcher and contains a list of blices to
// match against
type Matcher struct {
trie []node // preallocated block of memory containing all the
// nodes
extent int // offset into trie that is currently free
root *node // Points to trie[0]
}
// A single result
type Hit struct {
Key int
Position int
}
// finndBlice looks for a blice in the trie starting from the root and
// returns a pointer to the node representing the end of the blice. If
// the blice is not found it returns nil.
func (m *Matcher) findBlice(b []byte) *node {
n := &m.trie[0]
for n != nil && len(b) > 0 {
n = n.child[int(b[0])]
b = b[1:]
}
return n
}
// getFreeNode: gets a free node structure from the Matcher's trie
// pool and updates the extent to point to the next free node.
func (m *Matcher) getFreeNode() *node {
m.extent += 1
if m.extent == 1 {
m.root = &m.trie[0]
m.root.root = true
}
return &m.trie[m.extent-1]
}
// buildTrie builds the fundamental trie structure from a set of
// blices.
func (m *Matcher) buildTrie(dictionary [][]byte) {
// Work out the maximum size for the trie (all dictionary entries
// are distinct plus the root). This is used to preallocate memory
// for it.
max := 1
for _, blice := range dictionary {
max += len(blice)
}
m.trie = make([]node, max)
// Calling this an ignoring its argument simply allocated
// m.trie[0] which will be the root element
m.getFreeNode()
// This loop builds the nodes in the trie by following through
// each dictionary entry building the children pointers.
for i, blice := range dictionary {
n := m.root
var path []byte
for _, b := range blice {
path = append(path, b)
c := n.child[int(b)]
if c == nil {
c = m.getFreeNode()
n.child[int(b)] = c
c.b = make([]byte, len(path))
copy(c.b, path)
// Nodes directly under the root node will have the
// root as their fail point as there are no suffixes
// possible.
if len(path) == 1 {
c.fail = m.root
}
c.suffix = m.root
}
n = c
}
// The last value of n points to the node representing a
// dictionary entry
n.output = true
n.index = i
}
l := new(list.List)
l.PushBack(m.root)
for l.Len() > 0 {
n := l.Remove(l.Front()).(*node)
for i := 0; i < 256; i++ {
c := n.child[i]
if c != nil {
l.PushBack(c)
for j := 1; j < len(c.b); j++ {
c.fail = m.findBlice(c.b[j:])
if c.fail != nil {
break
}
}
if c.fail == nil {
c.fail = m.root
}
for j := 1; j < len(c.b); j++ {
s := m.findBlice(c.b[j:])
if s != nil && s.output {
c.suffix = s
break
}
}
}
}
}
for i := 0; i < m.extent; i++ {
for c := 0; c < 256; c++ {
n := &m.trie[i]
for n.child[c] == nil && !n.root {
n = n.fail
}
m.trie[i].fails[c] = n
}
}
m.trie = m.trie[:m.extent]
}
// NewMatcher creates a new Matcher used to match against a set of
// blices
func NewMatcher(dictionary [][]byte) *Matcher {
m := new(Matcher)
m.buildTrie(dictionary)
return m
}
// NewStringMatcher creates a new Matcher used to match against a set
// of strings (this is a helper to make initialization easy)
func NewStringMatcher(dictionary []string) *Matcher {
m := new(Matcher)
var d [][]byte
for _, s := range dictionary {
d = append(d, []byte(s))
}
m.buildTrie(d)
return m
}
// Match searches in for blices and returns all the blices found
func (m *Matcher) Match(in []byte) []Hit {
var hits []Hit
n := m.root
for idx, b := range in {
c := int(b)
if !n.root && n.child[c] == nil {
n = n.fails[c]
}
if n.child[c] != nil {
f := n.child[c]
n = f
if f.output {
hits = append(hits, Hit{Position: idx - len(f.b) + 1, Key: f.index})
}
for !f.suffix.root {
f = f.suffix
hits = append(hits, Hit{Position: idx - len(f.b) + 1, Key: f.index})
}
}
}
return hits
}