MCPcopy Create free account
hub / github.com/dlclark/regexp2 / compileASCIIStringSetPrefixFilter

Function compileASCIIStringSetPrefixFilter

stringprefixfilter.go:258–296  ·  view source on GitHub ↗

compileASCIIStringSetPrefixFilter builds a byte-oriented multi-prefix scanner for the narrow shape where it beats running strings.Index once per prefix: case-sensitive ASCII prefixes with at least two prefixes sharing a first byte. It indexes possible first bytes with strings.IndexAny, then verifies

(prefixes []string, ignoreCase bool, minRequiredLength int)

Source from the content-addressed store, hash-verified

256// It indexes possible first bytes with strings.IndexAny, then verifies only the
257// bucket for the byte found. Other shapes fall back to the old implementation.
258func compileASCIIStringSetPrefixFilter(prefixes []string, ignoreCase bool, minRequiredLength int) (*asciiStringSetPrefixFilter, bool) {
259 if ignoreCase {
260 return nil, false
261 }
262
263 filter := &asciiStringSetPrefixFilter{
264 minRequiredBytes: minRequiredLength,
265 }
266 var firstChars [256]bool
267 var hasSharedFirst bool
268 for _, prefix := range prefixes {
269 if prefix == "" || !isASCIIString(prefix) {
270 return nil, false
271 }
272
273 first := prefix[0]
274 filter.prefixesByFirst[first] = append(filter.prefixesByFirst[first], prefix)
275 if len(filter.prefixesByFirst[first]) > 1 {
276 hasSharedFirst = true
277 }
278 firstChars[first] = true
279 }
280
281 if !hasSharedFirst {
282 return nil, false
283 }
284
285 firstBytes := make([]byte, 0, len(prefixes)*2)
286 for i, ok := range firstChars {
287 if ok {
288 firstBytes = append(firstBytes, byte(i))
289 }
290 }
291 if len(firstBytes) == 0 {
292 return nil, false
293 }
294 filter.firstChars = string(firstBytes)
295 return filter, true
296}
297
298func (f *asciiStringSetPrefixFilter) index(input string, startAt int) (candidateByteIndex int, ok bool) {
299 if !hasMinRequiredBytes(input, startAt, f.minRequiredBytes) {

Callers 2

Calls 1

isASCIIStringFunction · 0.85

Tested by 1