Repository navigation
Expand file tree
/
Copy patheach_root.go
More file actions
274 lines (256 loc) · 8.97 KB
/
Copy patheach_root.go
File metadata and controls
274 lines (256 loc) · 8.97 KB
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
package casei
import (
"encoding/binary"
"unicode/utf8"
)
const (
// rootBucketEachMinBytes is the first length where the existing four-byte
// bucket kernel can examine a full 64-start block (the last prefix uses bytes
// 63 through 66). Shorter input keeps the ordinary Each route.
rootBucketEachMinBytes = 64 + 3
rootASCIIWordMaxUnits = 24
rootASCIIWordMaxWords = (rootASCIIWordMaxUnits + 7) / 8
rootASCIIWordMaxPatterns = 5
)
const (
rootASCIIHighBits uint64 = 0x8080808080808080
rootASCIIWordHeaderBytes = 2
rootASCIIWordEntryBytes = 1 + rootASCIIWordMaxWords*2*8
)
type rootASCIIWordCert struct {
units uint8
words [rootASCIIWordMaxWords]uint64
fold [rootASCIIWordMaxWords]uint64
}
type rootASCIIWordCerts struct {
maxUnits uint8
count uint8
entries [rootASCIIWordMaxPatterns]rootASCIIWordCert
}
func rootASCIIWordCertBytes(count uint8) int {
return rootASCIIWordHeaderBytes + int(count)*rootASCIIWordEntryBytes
}
// write stores the bounded certificates after the existing bucket table.
func (c rootASCIIWordCerts) write(dst []byte) {
dst[0], dst[1] = c.maxUnits, c.count
at := rootASCIIWordHeaderBytes
for i := 0; i < int(c.count); i++ {
cert := &c.entries[i]
dst[at] = cert.units
at++
for _, word := range cert.words {
binary.LittleEndian.PutUint64(dst[at:at+8], word)
at += 8
}
for _, word := range cert.fold {
binary.LittleEndian.PutUint64(dst[at:at+8], word)
at += 8
}
}
}
// makeRootASCIIWordCerts compiles every pattern token into its complete ASCII
// fold class, retaining entries in pattern-ID order. A token with no exact ASCII
// representation makes this plan use the trie for all candidates; the bounded
// representation is only for short words.
func makeRootASCIIWordCerts(p *searchPlan, patterns []string) (rootASCIIWordCerts, bool) {
var certs rootASCIIWordCerts
if p.patternCount < 4 || p.patternCount > rootASCIIWordMaxPatterns || len(patterns) != p.patternCount ||
p.empty >= 0 || p.maxUnits == 0 || p.maxUnits > rootASCIIWordMaxUnits {
return certs, false
}
certs.maxUnits = uint8(p.maxUnits)
for patternID, pattern := range patterns {
if pattern == "" {
return rootASCIIWordCerts{}, false
}
cert := &certs.entries[patternID]
units := 0
for at := 0; at < len(pattern); units++ {
r, size := utf8.DecodeRuneInString(pattern[at:])
var token uint32
if r == utf8.RuneError && size == 1 {
token = p.byteTokens[pattern[at]]
} else {
token = p.runes[r]
}
if token == 0 {
return rootASCIIWordCerts{}, false
}
kind, value := p.asciiTokenKind(token)
if kind == rootGeneric {
return rootASCIIWordCerts{}, false
}
word, shift := units/8, uint((units%8)*8)
cert.words[word] |= uint64(value) << shift
if kind == rootASCIIFold {
cert.fold[word] |= uint64(0x20) << shift
}
at += size
}
if units == 0 || units > rootASCIIWordMaxUnits || units > p.maxUnits {
return rootASCIIWordCerts{}, false
}
cert.units = uint8(units)
}
certs.count = uint8(len(patterns))
return certs, true
}
// rootASCIIWordCertData returns the optional plan-owned tail after the verified
// bucket table.
func rootASCIIWordCertData(bucket tripleBucketFilter) []byte {
if len(bucket) < tripleBucketFilterBytes+rootASCIIWordHeaderBytes ||
bucket[tripleBucketTableBytes] == 0 || bucket[tripleBucketFilterBytes-1] != tripleBucketValidMarker {
return nil
}
data := bucket[tripleBucketFilterBytes:]
count := data[1]
if count < 4 || count > rootASCIIWordMaxPatterns || data[0] == 0 ||
data[0] > rootASCIIWordMaxUnits || len(data) < rootASCIIWordCertBytes(count) {
return nil
}
return data[:rootASCIIWordCertBytes(count)]
}
// matchRootASCIIWordCertData checks the complete maxUnits-byte prefix before
// using ASCII words. known is false when the prefix is unknown or incomplete
// and needs trie replay; otherwise ok and match contain the ordered result.
func matchRootASCIIWordCertData(data []byte, haystack string, start int) (match Match, width int, ok, known bool) {
if len(data) < rootASCIIWordHeaderBytes {
return Match{}, 0, false, false
}
maxUnits, count := int(data[0]), int(data[1])
if maxUnits == 0 || maxUnits > rootASCIIWordMaxUnits || count < 4 || count > rootASCIIWordMaxPatterns ||
len(data) < rootASCIIWordCertBytes(uint8(count)) || start < 0 || start > len(haystack) ||
len(haystack)-start < maxUnits {
return Match{}, 0, false, false
}
var words [rootASCIIWordMaxWords]uint64
fullWords := maxUnits / 8
for word := 0; word < fullWords; word++ {
at := start + word*8
value := binary.LittleEndian.Uint64([]byte(haystack[at : at+8]))
if value&rootASCIIHighBits != 0 {
return Match{}, 0, false, false
}
words[word] = value
}
for unit := fullWords * 8; unit < maxUnits; unit++ {
value := haystack[start+unit]
if value >= utf8.RuneSelf {
return Match{}, 0, false, false
}
words[unit/8] |= uint64(value) << uint((unit%8)*8)
}
entryAt := rootASCIIWordHeaderBytes
for patternID := 0; patternID < count; patternID++ {
units := int(data[entryAt])
entryAt++
if units == 0 || units > maxUnits {
return Match{}, 0, false, false
}
wordsAt := entryAt
foldAt := wordsAt + rootASCIIWordMaxWords*8
entryAt = foldAt + rootASCIIWordMaxWords*8
wordCount := (units + 7) / 8
matched := true
for word := 0; word < wordCount; word++ {
valueAt := wordsAt + word*8
foldWordAt := foldAt + word*8
value := words[word] | binary.LittleEndian.Uint64(data[foldWordAt:foldWordAt+8])
if word == wordCount-1 && units%8 != 0 {
validBits := uint(units%8) * 8
value &= (uint64(1) << validBits) - 1
}
if value != binary.LittleEndian.Uint64(data[valueAt:valueAt+8]) {
matched = false
break
}
}
if matched {
return Match{Pattern: patternID, Start: start}, units, true, true
}
}
return Match{}, 0, false, true
}
// rootBucketEachFilter limits the alternate iterator to complete generic
// three-to-five-literal root plans with conservative Shufti coverage and a
// compiled bucket. Covered roots make p.filter unusable for this shape; the
// existing bucket/Shufti wrapper is the active candidate screen.
func (p *searchPlan) rootBucketEachFilter(haystack string) (tripleBucketFilter, bool) {
if p.empty >= 0 || p.patternCount < 3 || p.patternCount > 5 || p.opaqueContinuation ||
p.rootKind != rootGeneric || p.rawByteMulti.usable() || !p.triplesComplete ||
!p.triples.shufti.usable() || !asciiPairVBMIEnabled() || len(haystack) < rootBucketEachMinBytes {
return nil, false
}
bucket := p.tripleBucketFilter()
return bucket, bucket.usable()
}
// matchAtStart replays one byte-boundary candidate through the shared trie. It
// considers every terminal at exactly start and chooses the lowest pattern ID,
// retaining the consumed source width of that pattern.
func (p *searchPlan) matchAtStart(haystack string, start int) (Match, int, bool) {
if start < 0 || start >= len(haystack) {
return Match{}, 0, false
}
state, at := 0, start
best := Match{Pattern: -1, Start: -1}
bestWidth := 0
for units := 1; units <= p.maxUnits && at < len(haystack); units++ {
token, size := p.haystackToken(haystack, at)
if token == 0 {
break
}
next, ok := p.nodes[state].edges[token]
if !ok {
break
}
state = next
at += size
// finish propagates shorter failure-link outputs into each node. An
// output whose unit count equals the anchored prefix length belongs to
// this start; shorter suffix outputs begin after it and must not win.
if output := p.nodes[state].output; output.pattern >= 0 && output.units == units &&
(best.Pattern < 0 || output.pattern < best.Pattern) {
best = Match{Pattern: output.pattern, Start: start}
bestWidth = at - start
}
}
if best.Pattern < 0 {
return Match{}, 0, false
}
return best, bestWidth, true
}
func (p *searchPlan) matchRootBucketCandidate(haystack string, start int, bucket tripleBucketFilter) (Match, int, bool) {
if data := rootASCIIWordCertData(bucket); data != nil {
match, width, ok, known := matchRootASCIIWordCertData(data, haystack, start)
if known {
return match, width, ok
}
}
return p.matchAtStart(haystack, start)
}
// eachRootBucket enumerates candidate starts in source order through the
// complete plan's existing root-triple filter. A complete ASCII word
// certificate confirms bounded ASCII prefixes in pattern-ID order; unknown or
// incomplete prefixes retain the exact trie replay. The bucket or Shufti
// survivor is still only a nomination. The chosen source width owns non-overlap
// advancement. This route replaces general suffix search but may rescan
// unconsumed lanes, so it does not claim a one-load root screen.
func (p *searchPlan) eachRootBucket(haystack string, bucket tripleBucketFilter, yield func(Match, int) bool) bool {
at := 0
for at+2 < len(haystack) {
at += tripleBucketSkipBytes(haystack, at, bucket, &p.triples.shufti)
if at+2 >= len(haystack) {
break
}
match, width, ok := p.matchRootBucketCandidate(haystack, at, bucket)
if !ok {
at++
continue
}
if !yield(match, width) {
return false
}
at = match.Start + width
}
return true
}