...
1
2
3
4
5 package colltab
6
7 import "unicode/utf8"
8
9
10
11 type ContractTrieSet []struct{ L, H, N, I uint8 }
12
13
14
15
16
17
18
19
20
21 type ctScanner struct {
22 states ContractTrieSet
23 s []byte
24 n int
25 index int
26 pindex int
27 done bool
28 }
29
30 type ctScannerString struct {
31 states ContractTrieSet
32 s string
33 n int
34 index int
35 pindex int
36 done bool
37 }
38
39 func (t ContractTrieSet) scanner(index, n int, b []byte) ctScanner {
40 return ctScanner{s: b, states: t[index:], n: n}
41 }
42
43 func (t ContractTrieSet) scannerString(index, n int, str string) ctScannerString {
44 return ctScannerString{s: str, states: t[index:], n: n}
45 }
46
47
48
49 func (s *ctScanner) result() (i, p int) {
50 return s.index, s.pindex
51 }
52
53 func (s *ctScannerString) result() (i, p int) {
54 return s.index, s.pindex
55 }
56
57 const (
58 final = 0
59 noIndex = 0xFF
60 )
61
62
63
64 func (s *ctScanner) scan(p int) int {
65 pr := p
66 str := s.s
67 states, n := s.states, s.n
68 for i := 0; i < n && p < len(str); {
69 e := states[i]
70 c := str[p]
71
72
73
74
75 if c >= e.L {
76 if e.L == c {
77 p++
78 if e.I != noIndex {
79 s.index = int(e.I)
80 s.pindex = p
81 }
82 if e.N != final {
83 i, states, n = 0, states[int(e.H)+n:], int(e.N)
84 if p >= len(str) || utf8.RuneStart(str[p]) {
85 s.states, s.n, pr = states, n, p
86 }
87 } else {
88 s.done = true
89 return p
90 }
91 continue
92 } else if e.N == final && c <= e.H {
93 p++
94 s.done = true
95 s.index = int(c-e.L) + int(e.I)
96 s.pindex = p
97 return p
98 }
99 }
100 i++
101 }
102 return pr
103 }
104
105
106 func (s *ctScannerString) scan(p int) int {
107 pr := p
108 str := s.s
109 states, n := s.states, s.n
110 for i := 0; i < n && p < len(str); {
111 e := states[i]
112 c := str[p]
113
114
115
116
117 if c >= e.L {
118 if e.L == c {
119 p++
120 if e.I != noIndex {
121 s.index = int(e.I)
122 s.pindex = p
123 }
124 if e.N != final {
125 i, states, n = 0, states[int(e.H)+n:], int(e.N)
126 if p >= len(str) || utf8.RuneStart(str[p]) {
127 s.states, s.n, pr = states, n, p
128 }
129 } else {
130 s.done = true
131 return p
132 }
133 continue
134 } else if e.N == final && c <= e.H {
135 p++
136 s.done = true
137 s.index = int(c-e.L) + int(e.I)
138 s.pindex = p
139 return p
140 }
141 }
142 i++
143 }
144 return pr
145 }
146
View as plain text