forked from petar-dambovaliev/aho-corasick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
classes.go
79 lines (67 loc) · 1.21 KB
/
classes.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
package aho_corasick
import (
"math"
)
type byteClassRepresentatives struct {
classes *byteClasses
bbyte int
lastClass *byte
}
func (b *byteClassRepresentatives) next() *byte {
for b.bbyte < 256 {
bbyte := byte(b.bbyte)
class := b.classes.bytes[bbyte]
b.bbyte += 1
if b.lastClass == nil || *b.lastClass != class {
c := class
b.lastClass = &c
return &bbyte
}
}
return nil
}
type byteClassBuilder []bool
func (b byteClassBuilder) setRange(start, end byte) {
if start > 0 {
b[int(start)-1] = true
}
b[int(end)] = true
}
func (b byteClassBuilder) build() byteClasses {
var classes byteClasses
var class byte
i := 0
for {
classes.bytes[byte(i)] = class
if i >= 255 {
break
}
if b[i] {
if class+1 > math.MaxUint8 {
panic("shit happens")
}
class += 1
}
i += 1
}
return classes
}
func newByteClassBuilder() byteClassBuilder {
return make([]bool, 256)
}
type byteClasses struct {
bytes [256]byte
}
func singletons() byteClasses {
var bc byteClasses
for i := range bc.bytes {
bc.bytes[i] = byte(i)
}
return bc
}
func (b byteClasses) alphabetLen() int {
return int(b.bytes[255]) + 1
}
func (b byteClasses) isSingleton() bool {
return b.alphabetLen() == 256
}