regexr 0.1.2

A high-performance regex engine built from scratch with JIT compilation and SIMD acceleration
Documentation
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Prefilter - SIMD-accelerated candidate position filtering.
//!
//! A prefilter quickly skips to positions in the input where a match might occur,
//! avoiding full regex execution on positions that can't possibly match.
//!
//! # Algorithm Selection
//!
//! - **Single literal prefix**: Use memchr for single-byte, or memmem for substring
//! - **Multiple literal prefixes**: Use aho-corasick packed searcher (SIMD-accelerated)
//! - **No common literals**: No prefilter (full regex on every position)

#[cfg(feature = "simd")]
use crate::simd::Teddy;

use super::Literals;

/// A prefilter for fast candidate position detection.
pub enum Prefilter {
    /// No prefilter available - scan all positions.
    None,
    /// Single-byte prefix search using memchr.
    SingleByte(u8),
    /// Inner byte search - finds a required byte that appears somewhere in the match.
    /// Unlike SingleByte (prefix), this requires backtracking to find the actual match start.
    /// NOTE: Currently disabled in from_literals() due to performance issues.
    #[allow(dead_code)]
    InnerByte {
        /// The byte to search for.
        byte: u8,
        /// How far back to search for the actual match start.
        max_lookback: usize,
    },
    /// Multi-byte literal prefix search using memmem.
    Literal(memchr::memmem::Finder<'static>),
    /// Pattern starts with a digit (0-9). Uses memchr to find digit positions.
    StartsWithDigit,
    /// Multi-pattern search using aho-corasick packed searcher (for prefix candidates).
    AhoCorasick {
        /// The aho-corasick automaton.
        ac: aho_corasick::AhoCorasick,
    },
    /// Complete literal alternation with full match bounds using aho-corasick.
    /// Used when the pattern is just `literal1|literal2|...` with no suffix.
    AhoCorasickFull {
        /// The aho-corasick automaton.
        ac: aho_corasick::AhoCorasick,
    },
    /// Multi-pattern search using Teddy algorithm (for prefix candidates).
    #[cfg(feature = "simd")]
    Teddy(Teddy),
    /// Complete literal alternation: Teddy + pattern lengths for full match bounds.
    #[cfg(feature = "simd")]
    TeddyFull {
        /// The Teddy multi-pattern matcher.
        teddy: Teddy,
        /// Length of each pattern for computing match end positions.
        lengths: Vec<usize>,
    },
}

impl std::fmt::Debug for Prefilter {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::None => write!(f, "Prefilter::None"),
            Self::SingleByte(b) => write!(f, "Prefilter::SingleByte({:#04x})", b),
            Self::InnerByte { byte, max_lookback } => {
                write!(
                    f,
                    "Prefilter::InnerByte({:#04x}, lookback={})",
                    byte, max_lookback
                )
            }
            Self::Literal(_) => write!(f, "Prefilter::Literal"),
            Self::StartsWithDigit => write!(f, "Prefilter::StartsWithDigit"),
            Self::AhoCorasick { ac } => {
                write!(f, "Prefilter::AhoCorasick({} patterns)", ac.patterns_len())
            }
            Self::AhoCorasickFull { ac } => write!(
                f,
                "Prefilter::AhoCorasickFull({} patterns)",
                ac.patterns_len()
            ),
            #[cfg(feature = "simd")]
            Self::Teddy(t) => write!(f, "Prefilter::Teddy({} patterns)", t.pattern_count()),
            #[cfg(feature = "simd")]
            Self::TeddyFull { teddy, .. } => write!(
                f,
                "Prefilter::TeddyFull({} patterns)",
                teddy.pattern_count()
            ),
        }
    }
}

impl Prefilter {
    /// Creates a prefilter from extracted literals.
    pub fn from_literals(literals: &Literals) -> Self {
        if literals.prefixes.is_empty() {
            // No literal prefix - check for specialized prefilters
            if literals.starts_with_digit {
                return Self::StartsWithDigit;
            }
            return Self::None;
        }

        // Single prefix case
        if literals.prefixes.len() == 1 {
            let prefix = &literals.prefixes[0];
            if prefix.len() == 1 {
                return Self::SingleByte(prefix[0]);
            }
            if !prefix.is_empty() {
                // Use memchr's memmem for single literal search
                let finder = memchr::memmem::Finder::new(prefix).into_owned();
                return Self::Literal(finder);
            }
            return Self::None;
        }

        // Multiple prefixes - use aho-corasick with leftmost-first semantics
        let ac = aho_corasick::AhoCorasick::builder()
            .match_kind(aho_corasick::MatchKind::LeftmostFirst)
            .build(&literals.prefixes)
            .expect("aho-corasick build should not fail");

        if literals.prefix_complete {
            return Self::AhoCorasickFull { ac };
        }
        Self::AhoCorasick { ac }
    }

    /// Returns the next candidate position in the haystack starting from `pos`.
    /// Returns None if no candidate is found.
    pub fn find_candidate(&self, haystack: &[u8], pos: usize) -> Option<usize> {
        if pos >= haystack.len() {
            return None;
        }

        match self {
            Self::None => {
                // No prefilter - every position is a candidate
                if pos < haystack.len() {
                    Some(pos)
                } else {
                    None
                }
            }
            Self::SingleByte(needle) => {
                let slice = &haystack[pos..];
                memchr::memchr(*needle, slice).map(|i| pos + i)
            }
            Self::InnerByte { byte, .. } => {
                // For InnerByte, we just find the next occurrence of the byte.
                // The executor will handle the lookback logic.
                let slice = &haystack[pos..];
                memchr::memchr(*byte, slice).map(|i| pos + i)
            }
            Self::Literal(finder) => {
                let slice = &haystack[pos..];
                finder.find(slice).map(|i| pos + i)
            }
            Self::StartsWithDigit => {
                // Find next digit (0-9) position using SIMD range search
                let slice = &haystack[pos..];
                #[cfg(feature = "simd")]
                {
                    // Use AVX2-accelerated range search for digits
                    crate::simd::memchr_range(b'0', b'9', slice).map(|i| pos + i)
                }
                #[cfg(not(feature = "simd"))]
                {
                    // Scalar fallback: search for any digit
                    slice
                        .iter()
                        .position(|&b| b >= b'0' && b <= b'9')
                        .map(|i| pos + i)
                }
            }
            Self::AhoCorasick { ac } => {
                let input = aho_corasick::Input::new(haystack).span(pos..haystack.len());
                ac.find(input).map(|m| m.start())
            }
            Self::AhoCorasickFull { ac } => {
                let input = aho_corasick::Input::new(haystack).span(pos..haystack.len());
                ac.find(input).map(|m| m.start())
            }
            #[cfg(feature = "simd")]
            Self::Teddy(teddy) => {
                let slice = &haystack[pos..];
                teddy.find(slice).map(|(_, i)| pos + i)
            }
            #[cfg(feature = "simd")]
            Self::TeddyFull { teddy, .. } => {
                let slice = &haystack[pos..];
                teddy.find(slice).map(|(_, i)| pos + i)
            }
        }
    }

    /// Finds the next complete match for AhoCorasickFull or TeddyFull patterns.
    /// Returns (start, end) byte offsets for the match.
    pub fn find_full_match(&self, haystack: &[u8], pos: usize) -> Option<(usize, usize)> {
        if pos >= haystack.len() {
            return None;
        }

        match self {
            Self::AhoCorasickFull { ac } => {
                let input = aho_corasick::Input::new(haystack).span(pos..haystack.len());
                ac.find(input).map(|m| (m.start(), m.end()))
            }
            #[cfg(feature = "simd")]
            Self::TeddyFull { teddy, lengths } => {
                let slice = &haystack[pos..];
                if let Some((pattern_id, match_pos)) = teddy.find(slice) {
                    let abs_pos = pos + match_pos;
                    let len = lengths[pattern_id];
                    return Some((abs_pos, abs_pos + len));
                }
                None
            }
            _ => None,
        }
    }

    /// Returns true if this prefilter can provide complete matches.
    pub fn is_full_match(&self) -> bool {
        matches!(self, Self::AhoCorasickFull { .. }) || {
            #[cfg(feature = "simd")]
            {
                matches!(self, Self::TeddyFull { .. })
            }
            #[cfg(not(feature = "simd"))]
            {
                false
            }
        }
    }

    /// Returns an iterator over all candidate positions.
    pub fn find_candidates<'a, 'h>(&'a self, haystack: &'h [u8]) -> CandidateIter<'a, 'h> {
        CandidateIter {
            prefilter: self,
            haystack,
            pos: 0,
        }
    }

    /// Returns an iterator over all complete matches for TeddyFull patterns.
    /// For non-TeddyFull prefilters, this iterator will be empty.
    pub fn find_full_matches<'a, 'h>(&'a self, haystack: &'h [u8]) -> FullMatchIter<'a, 'h> {
        FullMatchIter::new(self, haystack)
    }

    /// Returns true if this prefilter is trivial (matches every position).
    pub fn is_none(&self) -> bool {
        matches!(self, Self::None)
    }

    /// Returns true if this prefilter has good selectivity.
    /// An effective prefilter can significantly reduce the number of candidate positions.
    /// This is used for engine selection: DFA JIT benefits more from effective prefilters,
    /// while ShiftOr/JitShiftOr is often better without one.
    pub fn is_effective(&self) -> bool {
        match self {
            // Effective prefilters - good selectivity
            Self::SingleByte(_) => true,
            Self::Literal(_) => true,
            Self::AhoCorasick { .. } => true,
            Self::AhoCorasickFull { .. } => true,
            #[cfg(feature = "simd")]
            Self::Teddy(_) => true,
            #[cfg(feature = "simd")]
            Self::TeddyFull { .. } => true,
            // Not effective - weak or no selectivity
            Self::None => false,
            Self::StartsWithDigit => false, // Too many candidates in typical text
            Self::InnerByte { .. } => false, // Requires lookback, complex handling
        }
    }

    /// Returns true if this is an InnerByte prefilter.
    pub fn is_inner_byte(&self) -> bool {
        matches!(self, Self::InnerByte { .. })
    }

    /// Returns the max_lookback for InnerByte prefilter.
    pub fn inner_byte_lookback(&self) -> usize {
        match self {
            Self::InnerByte { max_lookback, .. } => *max_lookback,
            _ => 0,
        }
    }
}

/// Iterator over candidate positions.
pub struct CandidateIter<'a, 'h> {
    prefilter: &'a Prefilter,
    haystack: &'h [u8],
    pos: usize,
}

impl<'a, 'h> Iterator for CandidateIter<'a, 'h> {
    type Item = usize;

    fn next(&mut self) -> Option<Self::Item> {
        let candidate = self.prefilter.find_candidate(self.haystack, self.pos)?;
        self.pos = candidate + 1;
        Some(candidate)
    }
}

/// Iterator over complete matches for full-match prefilters.
pub struct FullMatchIter<'a, 'h> {
    inner: FullMatchIterInner<'a, 'h>,
}

enum FullMatchIterInner<'a, 'h> {
    /// Multi-literal using aho-corasick.
    AhoCorasickFull {
        ac_iter: aho_corasick::FindIter<'a, 'h>,
        last_end: usize,
    },
    /// Direct Teddy iterator for TeddyFull patterns.
    #[cfg(feature = "simd")]
    TeddyFull {
        teddy_iter: crate::simd::TeddyIter<'a, 'h>,
        lengths: &'a [usize],
        last_end: usize,
    },
    /// Empty iterator for non-full-match prefilters.
    Empty,
}

impl<'a, 'h> FullMatchIter<'a, 'h> {
    pub(crate) fn new(prefilter: &'a Prefilter, haystack: &'h [u8]) -> Self {
        let inner = match prefilter {
            Prefilter::AhoCorasickFull { ac } => FullMatchIterInner::AhoCorasickFull {
                ac_iter: ac.find_iter(haystack),
                last_end: 0,
            },
            #[cfg(feature = "simd")]
            Prefilter::TeddyFull { teddy, lengths } => FullMatchIterInner::TeddyFull {
                teddy_iter: teddy.find_iter(haystack),
                lengths,
                last_end: 0,
            },
            _ => FullMatchIterInner::Empty,
        };
        Self { inner }
    }
}

impl<'a, 'h> Iterator for FullMatchIter<'a, 'h> {
    type Item = (usize, usize);

    fn next(&mut self) -> Option<Self::Item> {
        match &mut self.inner {
            FullMatchIterInner::AhoCorasickFull { ac_iter, last_end } => {
                // Skip matches that overlap with the previous match
                loop {
                    let m = ac_iter.next()?;
                    if m.start() >= *last_end {
                        *last_end = m.end();
                        return Some((m.start(), m.end()));
                    }
                }
            }
            #[cfg(feature = "simd")]
            FullMatchIterInner::TeddyFull {
                teddy_iter,
                lengths,
                last_end,
            } => {
                // Skip matches that overlap with the previous match
                loop {
                    let (pattern_id, pos) = teddy_iter.next()?;
                    if pos >= *last_end {
                        let len = lengths[pattern_id];
                        let end = pos + len;
                        *last_end = end;
                        return Some((pos, end));
                    }
                }
            }
            FullMatchIterInner::Empty => None,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_single_byte_prefilter() {
        let pf = Prefilter::SingleByte(b'x');
        assert_eq!(pf.find_candidate(b"hello x world", 0), Some(6));
        assert_eq!(pf.find_candidate(b"hello x world", 7), None);
    }

    #[test]
    fn test_literal_prefilter() {
        let finder = memchr::memmem::Finder::new(b"world").into_owned();
        let pf = Prefilter::Literal(finder);
        assert_eq!(pf.find_candidate(b"hello world", 0), Some(6));
        assert_eq!(pf.find_candidate(b"hello world", 7), None);
    }

    #[test]
    fn test_none_prefilter() {
        let pf = Prefilter::None;
        assert_eq!(pf.find_candidate(b"hello", 0), Some(0));
        assert_eq!(pf.find_candidate(b"hello", 3), Some(3));
        assert_eq!(pf.find_candidate(b"hello", 5), None);
    }

    #[test]
    fn test_from_literals_single() {
        let literals = Literals {
            prefixes: vec![b"hello".to_vec()],
            suffixes: vec![],
            prefix_complete: true,
            starts_with_digit: false,
        };
        let pf = Prefilter::from_literals(&literals);
        assert!(matches!(pf, Prefilter::Literal(_)));
    }

    #[test]
    fn test_from_literals_single_byte() {
        let literals = Literals {
            prefixes: vec![b"h".to_vec()],
            suffixes: vec![],
            prefix_complete: true,
            starts_with_digit: false,
        };
        let pf = Prefilter::from_literals(&literals);
        assert!(matches!(pf, Prefilter::SingleByte(b'h')));
    }

    #[test]
    fn test_from_literals_empty() {
        let literals = Literals {
            prefixes: vec![],
            suffixes: vec![],
            prefix_complete: false,
            starts_with_digit: false,
        };
        let pf = Prefilter::from_literals(&literals);
        assert!(pf.is_none());
    }

    #[test]
    fn test_multi_literal_prefilter() {
        let literals = Literals {
            prefixes: vec![b"hello".to_vec(), b"world".to_vec()],
            suffixes: vec![],
            prefix_complete: true,
            starts_with_digit: false,
        };
        let pf = Prefilter::from_literals(&literals);
        // With prefix_complete=true, we get AhoCorasickFull for complete literal matching
        assert!(matches!(pf, Prefilter::AhoCorasickFull { .. }));

        // Should find "hello" at position 4
        assert_eq!(pf.find_candidate(b"say hello there", 0), Some(4));
        // Should find "world" at position 6
        assert_eq!(pf.find_candidate(b"hello world", 6), Some(6));

        // Test full match functionality
        assert_eq!(pf.find_full_match(b"say hello there", 0), Some((4, 9)));
        assert_eq!(pf.find_full_match(b"hello world", 0), Some((0, 5)));
    }

    #[test]
    fn test_multi_literal_prefilter_incomplete() {
        let literals = Literals {
            prefixes: vec![b"hello".to_vec(), b"world".to_vec()],
            suffixes: vec![],
            prefix_complete: false, // Not a complete literal match
            starts_with_digit: false,
        };
        let pf = Prefilter::from_literals(&literals);
        // With prefix_complete=false, we get regular AhoCorasick
        assert!(matches!(pf, Prefilter::AhoCorasick { .. }));

        // Should find "hello" at position 4
        assert_eq!(pf.find_candidate(b"say hello there", 0), Some(4));
    }

    #[test]
    fn test_candidate_iter() {
        let pf = Prefilter::SingleByte(b'o');
        let candidates: Vec<_> = pf.find_candidates(b"hello world").collect();
        assert_eq!(candidates, vec![4, 7]);
    }
}