forked from petar-dambovaliev/aho-corasick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ahocorasick.go
441 lines (376 loc) · 11.5 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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package aho_corasick
import (
"strings"
"sync"
"unicode"
)
type findIter struct {
fsm imp
prestate *prefilterState
haystack []byte
pos int
matchOnlyWholeWords bool
}
func newFindIter(ac AhoCorasick, haystack []byte) findIter {
prestate := prefilterState{
skips: 0,
skipped: 0,
maxMatchLen: ac.i.MaxPatternLen(),
inert: false,
lastScanAt: 0,
}
return findIter{
fsm: ac.i,
prestate: &prestate,
haystack: haystack,
pos: 0,
matchOnlyWholeWords: ac.matchOnlyWholeWords,
}
}
// Iter is an iterator over matches found on the current haystack
// it gives the user more granular control. You can chose how many and what kind of matches you need.
type Iter interface {
Next() *Match
}
// Next gives a pointer to the next match yielded by the iterator or nil, if there is none
func (f *findIter) Next() *Match {
for {
if f.pos > len(f.haystack) {
return nil
}
result := f.fsm.FindAtNoState(f.prestate, f.haystack, f.pos)
if result == nil {
return nil
}
f.pos = result.end - result.len + 1
if f.matchOnlyWholeWords {
if result.Start()-1 >= 0 && (unicode.IsLetter(rune(f.haystack[result.Start()-1])) || unicode.IsDigit(rune(f.haystack[result.Start()-1]))) {
continue
}
if result.end < len(f.haystack) && (unicode.IsLetter(rune(f.haystack[result.end])) || unicode.IsDigit(rune(f.haystack[result.end]))) {
continue
}
}
return result
}
}
type overlappingIter struct {
fsm imp
prestate *prefilterState
haystack []byte
pos int
stateID stateID
matchIndex int
matchOnlyWholeWords bool
}
func (f *overlappingIter) Next() *Match {
for {
if f.pos > len(f.haystack) {
return nil
}
result := f.fsm.OverlappingFindAt(f.prestate, f.haystack, f.pos, &f.stateID, &f.matchIndex)
if result == nil {
return nil
}
f.pos = result.End()
if f.matchOnlyWholeWords {
if result.Start()-1 >= 0 && (unicode.IsLetter(rune(f.haystack[result.Start()-1])) || unicode.IsDigit(rune(f.haystack[result.Start()-1]))) {
continue
}
if result.end < len(f.haystack) && (unicode.IsLetter(rune(f.haystack[result.end])) || unicode.IsDigit(rune(f.haystack[result.end]))) {
continue
}
}
return result
}
}
func newOverlappingIter(ac AhoCorasick, haystack []byte) overlappingIter {
prestate := prefilterState{
skips: 0,
skipped: 0,
maxMatchLen: ac.i.MaxPatternLen(),
inert: false,
lastScanAt: 0,
}
return overlappingIter{
fsm: ac.i,
prestate: &prestate,
haystack: haystack,
pos: 0,
stateID: ac.i.StartState(),
matchIndex: 0,
matchOnlyWholeWords: ac.matchOnlyWholeWords,
}
}
// make sure the AhoCorasick data structure implements the Finder interface
var _ Finder = (*AhoCorasick)(nil)
// AhoCorasick is the main data structure that does most of the work
type AhoCorasick struct {
i imp
matchKind matchKind
matchOnlyWholeWords bool
}
func (ac AhoCorasick) PatternCount() int {
return ac.i.PatternCount()
}
// Iter gives an iterator over the built patterns
func (ac AhoCorasick) Iter(haystack string) Iter {
return ac.IterByte(unsafeBytes(haystack))
}
// IterByte gives an iterator over the built patterns
func (ac AhoCorasick) IterByte(haystack []byte) Iter {
iter := newFindIter(ac, haystack)
return &iter
}
// Iter gives an iterator over the built patterns with overlapping matches
func (ac AhoCorasick) IterOverlapping(haystack string) Iter {
return ac.IterOverlappingByte(unsafeBytes(haystack))
}
// IterOverlappingByte gives an iterator over the built patterns with overlapping matches
func (ac AhoCorasick) IterOverlappingByte(haystack []byte) Iter {
if ac.matchKind != StandardMatch {
panic("only StandardMatch allowed for overlapping matches")
}
i := newOverlappingIter(ac, haystack)
return &i
}
var pool = sync.Pool{
New: func() interface{} {
return strings.Builder{}
},
}
type Replacer struct {
finder Finder
}
func NewReplacer(finder Finder) Replacer {
return Replacer{finder: finder}
}
// ReplaceAllFunc replaces the matches found in the haystack according to the user provided function
// it gives fine grained control over what is replaced.
// A user can chose to stop the replacing process early by returning false in the lambda
// In that case, everything from that point will be kept as the original haystack
func (r Replacer) ReplaceAllFunc(haystack string, f func(match Match) (string, bool)) string {
return r.replaceAll(haystack, func(_ []Match) int { return -1 }, f)
}
// ReplaceAll replaces the matches found in the haystack according to the user provided slice `replaceWith`
// It panics, if `replaceWith` has length different from the patterns that it was built with
func (r Replacer) ReplaceAll(haystack string, replaceWith []string) string {
if len(replaceWith) != r.finder.PatternCount() {
panic("replaceWith needs to have the same length as the pattern count")
}
return r.replaceAll(
haystack,
func(matches []Match) int {
size, start := 0, 0
for _, m := range matches {
size += m.Start() - start + len(replaceWith[m.pattern])
start = m.Start() + m.len
}
if start-1 < len(haystack) {
size += len(haystack[start:])
}
return size
},
func(match Match) (string, bool) {
return replaceWith[match.pattern], true
},
)
}
// ReplaceAllWith replaces the matches found in the haystack according with replacement.
func (r Replacer) ReplaceAllWith(haystack, replacement string) string {
return r.replaceAll(
haystack,
func(matches []Match) int {
size, start := 0, 0
for _, m := range matches {
size += m.Start() - start + len(replacement)
start = m.Start() + m.len
}
if start-1 < len(haystack) {
size += len(haystack[start:])
}
return size
},
func(match Match) (string, bool) {
return replacement, true
},
)
}
func (r Replacer) replaceAll(haystack string, measure func(matches []Match) int, f func(match Match) (string, bool)) string {
matches := r.finder.FindAll(haystack)
if len(matches) == 0 {
return haystack
}
str := pool.Get().(strings.Builder)
defer func() {
str.Reset()
pool.Put(str)
}()
// right-size the buffer
switch size := measure(matches); size {
case 0:
return ""
case -1:
// do nothing
default:
str.Grow(size)
}
start := 0
for _, match := range matches {
rw, ok := f(match)
if !ok {
str.WriteString(haystack[start:])
return str.String()
}
str.WriteString(haystack[start:match.Start()])
str.WriteString(rw)
start = match.Start() + match.len
}
if start-1 < len(haystack) {
str.WriteString(haystack[start:])
}
return str.String()
}
type Finder interface {
FindAll(haystack string) []Match
PatternCount() int
}
// FindAll returns the matches found in the haystack
func (ac AhoCorasick) FindAll(haystack string) []Match {
iter := newFindIter(ac, unsafeBytes(haystack))
var matches []Match
for {
next := iter.Next()
if next == nil {
break
}
matches = append(matches, *next)
}
return matches
}
// AhoCorasickBuilder defines a set of options applied before the patterns are built
type AhoCorasickBuilder struct {
dfaBuilder *iDFABuilder
nfaBuilder *iNFABuilder
dfa bool
matchOnlyWholeWords bool
}
// Opts defines a set of options applied before the patterns are built
// MatchOnlyWholeWords does filtering after matching with MatchKind
// this could lead to situations where, in this case, nothing is matched
//
// trieBuilder := NewAhoCorasickBuilder(Opts{
// MatchOnlyWholeWords: true,
// MatchKind: LeftMostLongestMatch,
// DFA: false,
// })
//
// trie := trieBuilder.Build([]string{"testing", "testing 123"})
// result := trie.FindAll("testing 12345")
// len(result) == 0
//
// this is due to the fact LeftMostLongestMatch is the matching strategy
// "testing 123" is found but then is filtered out by MatchOnlyWholeWords
// use MatchOnlyWholeWords with caution
type Opts struct {
AsciiCaseInsensitive bool
MatchOnlyWholeWords bool
MatchKind matchKind
DFA bool
}
// NewAhoCorasickBuilder creates a new AhoCorasickBuilder based on Opts
func NewAhoCorasickBuilder(o Opts) AhoCorasickBuilder {
return AhoCorasickBuilder{
dfaBuilder: newDFABuilder(),
nfaBuilder: newNFABuilder(o.MatchKind, o.AsciiCaseInsensitive),
dfa: o.DFA,
matchOnlyWholeWords: o.MatchOnlyWholeWords,
}
}
// Build builds a (non)deterministic finite automata from the user provided patterns
func (a *AhoCorasickBuilder) Build(patterns []string) AhoCorasick {
bytePatterns := make([][]byte, len(patterns))
for pati, pat := range patterns {
bytePatterns[pati] = unsafeBytes(pat)
}
return a.BuildByte(bytePatterns)
}
// BuildByte builds a (non)deterministic finite automata from the user provided patterns
func (a *AhoCorasickBuilder) BuildByte(patterns [][]byte) AhoCorasick {
nfa := a.nfaBuilder.build(patterns)
match_kind := nfa.matchKind
if a.dfa {
dfa := a.dfaBuilder.build(nfa)
return AhoCorasick{dfa, match_kind, a.matchOnlyWholeWords}
}
return AhoCorasick{nfa, match_kind, a.matchOnlyWholeWords}
}
type imp interface {
MatchKind() *matchKind
StartState() stateID
MaxPatternLen() int
PatternCount() int
Prefilter() prefilter
UsePrefilter() bool
OverlappingFindAt(prestate *prefilterState, haystack []byte, at int, state_id *stateID, match_index *int) *Match
EarliestFindAt(prestate *prefilterState, haystack []byte, at int, state_id *stateID) *Match
FindAtNoState(prestate *prefilterState, haystack []byte, at int) *Match
}
type matchKind int
const (
// Use standard match semantics, which support overlapping matches. When
// used with non-overlapping matches, matches are reported as they are seen.
StandardMatch matchKind = iota
// Use leftmost-first match semantics, which reports leftmost matches.
// When there are multiple possible leftmost matches, the match
// corresponding to the pattern that appeared earlier when constructing
// the automaton is reported.
// This does **not** support overlapping matches or stream searching
LeftMostFirstMatch
// Use leftmost-longest match semantics, which reports leftmost matches.
// When there are multiple possible leftmost matches, the longest match is chosen.
LeftMostLongestMatch
)
func (m matchKind) supportsOverlapping() bool {
return m.isStandard()
}
func (m matchKind) supportsStream() bool {
return m.isStandard()
}
func (m matchKind) isStandard() bool {
return m == StandardMatch
}
func (m matchKind) isLeftmost() bool {
return m == LeftMostFirstMatch || m == LeftMostLongestMatch
}
func (m matchKind) isLeftmostFirst() bool {
return m == LeftMostFirstMatch
}
// A representation of a match reported by an Aho-Corasick automaton.
//
// A match has two essential pieces of information: the identifier of the
// pattern that matched, along with the start and end offsets of the match
// in the haystack.
type Match struct {
pattern int
len int
end int
}
// Pattern returns the index of the pattern in the slice of the patterns provided by the user that
// was matched
func (m *Match) Pattern() int {
return m.pattern
}
// End gives the index of the last character of this match inside the haystack
func (m *Match) End() int {
return m.end
}
// Start gives the index of the first character of this match inside the haystack
func (m *Match) Start() int {
return m.end - m.len
}
type stateID uint
const (
failedStateID stateID = 0
deadStateID stateID = 1
)