fuzzy-regex 0.1.0

High-performance fuzzy regular expression engine combining regex with Damerau-Levenshtein distance
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
// Suppress pedantic lints for SIMD code
#![allow(clippy::wildcard_imports)]

//! SIMD-accelerated character class matching.
//!
//! This module provides fast character class membership testing using:
//! 1. 128-bit ASCII bitmap for O(1) single-character lookups
//! 2. SIMD vectorized scanning for finding matches in byte slices

use crate::ir::HirClass;
use crate::parser::ast::{CharClass, CharClassItem, NamedClass};

/// A 128-bit bitmap for fast ASCII character class membership testing.
/// Each bit represents whether the corresponding ASCII byte (0-127) is in the class.
#[derive(Clone, Copy, Debug)]
pub struct AsciiClassBitmap {
    /// Lower 64 bits (bytes 0-63).
    lo: u64,
    /// Upper 64 bits (bytes 64-127).
    hi: u64,
    /// Whether this is a negated class.
    negated: bool,
    /// Whether this class matches non-ASCII characters.
    matches_non_ascii: bool,
}

impl AsciiClassBitmap {
    /// Create an empty bitmap (matches nothing).
    #[must_use]
    pub fn empty() -> Self {
        AsciiClassBitmap {
            lo: 0,
            hi: 0,
            negated: false,
            matches_non_ascii: false,
        }
    }

    /// Create a bitmap that matches all ASCII characters.
    #[must_use]
    pub fn all_ascii() -> Self {
        AsciiClassBitmap {
            lo: u64::MAX,
            hi: u64::MAX,
            negated: false,
            matches_non_ascii: false,
        }
    }

    /// Create a bitmap from an AST `CharClass`.
    #[must_use]
    pub fn from_char_class(class: &CharClass) -> Self {
        let mut bitmap = AsciiClassBitmap::empty();
        bitmap.negated = class.negated;

        for item in &class.items {
            match item {
                CharClassItem::Single(ch) => {
                    if ch.is_ascii() {
                        bitmap.set(*ch as u8);
                    } else {
                        bitmap.matches_non_ascii = true;
                    }
                }
                CharClassItem::Range(start, end) => {
                    let start_byte = if start.is_ascii() { *start as u8 } else { 128 };
                    let end_byte = if end.is_ascii() { *end as u8 } else { 127 };

                    for b in start_byte..=end_byte.min(127) {
                        bitmap.set(b);
                    }
                    // Check if range extends into non-ASCII
                    if *end as u32 > 127 {
                        bitmap.matches_non_ascii = true;
                    }
                }
                CharClassItem::Named(named) => {
                    bitmap.add_named_class(*named);
                }
            }
        }

        bitmap
    }

    /// Create a bitmap from an IR `HirClass`.
    #[must_use]
    pub fn from_hir_class(class: &HirClass) -> Self {
        let mut bitmap = AsciiClassBitmap::empty();
        bitmap.negated = class.negated;

        // Add single characters
        for &ch in &class.chars {
            if ch.is_ascii() {
                bitmap.set(ch as u8);
            } else {
                bitmap.matches_non_ascii = true;
            }
        }

        // Add ranges
        for &(start, end) in &class.ranges {
            let start_byte = if start.is_ascii() { start as u8 } else { 128 };
            let end_byte = if end.is_ascii() { end as u8 } else { 127 };

            for b in start_byte..=end_byte.min(127) {
                bitmap.set(b);
            }
            // Check if range extends into non-ASCII
            if end as u32 > 127 {
                bitmap.matches_non_ascii = true;
            }
        }

        // Add named classes
        for &named in &class.named {
            bitmap.add_named_class(named);
        }

        bitmap
    }

    /// Add a named class to the bitmap.
    fn add_named_class(&mut self, class: NamedClass) {
        match class {
            NamedClass::Digit => {
                for b in b'0'..=b'9' {
                    self.set(b);
                }
            }
            NamedClass::NotDigit => {
                // Set all except digits
                for b in 0u8..=127 {
                    if !b.is_ascii_digit() {
                        self.set(b);
                    }
                }
                self.matches_non_ascii = true;
            }
            NamedClass::Word => {
                for b in b'a'..=b'z' {
                    self.set(b);
                }
                for b in b'A'..=b'Z' {
                    self.set(b);
                }
                for b in b'0'..=b'9' {
                    self.set(b);
                }
                self.set(b'_');
            }
            NamedClass::NotWord => {
                for b in 0u8..=127 {
                    let is_word = b.is_ascii_lowercase()
                        || b.is_ascii_uppercase()
                        || b.is_ascii_digit()
                        || b == b'_';
                    if !is_word {
                        self.set(b);
                    }
                }
                self.matches_non_ascii = true;
            }
            NamedClass::Whitespace => {
                self.set(b' ');
                self.set(b'\t');
                self.set(b'\n');
                self.set(b'\r');
                self.set(0x0C); // form feed
                self.set(0x0B); // vertical tab
            }
            NamedClass::NotWhitespace => {
                for b in 0u8..=127 {
                    if !matches!(b, b' ' | b'\t' | b'\n' | b'\r' | 0x0C | 0x0B) {
                        self.set(b);
                    }
                }
                self.matches_non_ascii = true;
            }
            NamedClass::Any | NamedClass::AnyExceptNewline => {
                // Set all ASCII
                self.lo = u64::MAX;
                self.hi = u64::MAX;
                if matches!(class, NamedClass::AnyExceptNewline) {
                    self.clear(b'\n');
                    self.clear(b'\r');
                }
                self.matches_non_ascii = true;
            }
        }
    }

    /// Set a bit for the given ASCII byte.
    #[inline]
    fn set(&mut self, byte: u8) {
        if byte < 64 {
            self.lo |= 1u64 << byte;
        } else if byte < 128 {
            self.hi |= 1u64 << (byte - 64);
        }
    }

    /// Clear a bit for the given ASCII byte.
    #[inline]
    fn clear(&mut self, byte: u8) {
        if byte < 64 {
            self.lo &= !(1u64 << byte);
        } else if byte < 128 {
            self.hi &= !(1u64 << (byte - 64));
        }
    }

    /// Check if a byte is in the class.
    #[inline]
    #[must_use]
    pub fn contains(&self, byte: u8) -> bool {
        let in_bitmap = if byte < 64 {
            (self.lo & (1u64 << byte)) != 0
        } else if byte < 128 {
            (self.hi & (1u64 << (byte - 64))) != 0
        } else {
            self.matches_non_ascii
        };

        if self.negated { !in_bitmap } else { in_bitmap }
    }

    /// Check if a character is in the class.
    #[inline]
    #[must_use]
    pub fn contains_char(&self, ch: char) -> bool {
        if ch.is_ascii() {
            self.contains(ch as u8)
        } else {
            let in_class = self.matches_non_ascii;
            if self.negated { !in_class } else { in_class }
        }
    }

    /// Find the first position in the slice where any byte matches the class.
    /// Returns None if no match is found.
    #[must_use]
    pub fn find_first(&self, haystack: &[u8]) -> Option<usize> {
        // Use SIMD-optimized path for longer slices
        #[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
        {
            if haystack.len() >= 16 {
                return self.find_first_simd(haystack);
            }
        }

        #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
        {
            if haystack.len() >= 16 {
                return self.find_first_simd(haystack);
            }
        }

        // Scalar fallback
        self.find_first_scalar(haystack)
    }

    /// Scalar implementation of `find_first`.
    #[inline]
    fn find_first_scalar(&self, haystack: &[u8]) -> Option<usize> {
        for (i, &byte) in haystack.iter().enumerate() {
            if self.contains(byte) {
                return Some(i);
            }
        }
        None
    }

    /// SIMD implementation for `x86_64` with SSE2.
    #[cfg(all(target_arch = "x86_64", target_feature = "sse2"))]
    fn find_first_simd(&self, haystack: &[u8]) -> Option<usize> {
        use std::arch::x86_64::*;

        // For negated classes or classes matching non-ASCII, fall back to scalar
        // (SIMD path handles simpler cases more efficiently)
        if self.negated || self.matches_non_ascii {
            return self.find_first_scalar(haystack);
        }

        let len = haystack.len();
        let mut i = 0;

        // Process 16 bytes at a time
        unsafe {
            while i + 16 <= len {
                let chunk = _mm_loadu_si128(haystack.as_ptr().add(i).cast::<__m128i>());

                // Check each byte against the bitmap using lookup
                // We use a different strategy: check if any byte is in our set
                // by building a mask of matching positions
                let mut mask = 0u16;

                // Extract bytes and check individually (SSE2 doesn't have good gather)
                let bytes: [u8; 16] = std::mem::transmute(chunk);
                for (j, &b) in bytes.iter().enumerate() {
                    if self.contains(b) {
                        mask |= 1 << j;
                    }
                }

                if mask != 0 {
                    return Some(i + mask.trailing_zeros() as usize);
                }

                i += 16;
            }
        }

        // Handle remaining bytes
        (i..len).find(|&j| self.contains(haystack[j]))
    }

    /// SIMD implementation for aarch64 with NEON.
    #[cfg(all(target_arch = "aarch64", target_feature = "neon"))]
    fn find_first_simd(&self, haystack: &[u8]) -> Option<usize> {
        // For negated classes or classes matching non-ASCII, fall back to scalar
        if self.negated || self.matches_non_ascii {
            return self.find_first_scalar(haystack);
        }

        let len = haystack.len();
        let mut i = 0;

        // Process 16 bytes at a time
        unsafe {
            use std::arch::aarch64::*;

            while i + 16 <= len {
                let chunk = vld1q_u8(haystack.as_ptr().add(i));

                // Check each byte against the bitmap
                let bytes: [u8; 16] = std::mem::transmute(chunk);
                for (j, &b) in bytes.iter().enumerate() {
                    if self.contains(b) {
                        return Some(i + j);
                    }
                }

                i += 16;
            }
        }

        // Handle remaining bytes
        (i..len).find(|&j| self.contains(haystack[j]))
    }

    /// Find all positions where bytes match the class.
    /// Returns a vector of indices.
    #[must_use]
    pub fn find_all(&self, haystack: &[u8]) -> Vec<usize> {
        let mut results = Vec::new();
        let mut pos = 0;

        while pos < haystack.len() {
            if let Some(offset) = self.find_first(&haystack[pos..]) {
                results.push(pos + offset);
                pos += offset + 1;
            } else {
                break;
            }
        }

        results
    }

    /// Count how many bytes in the slice match the class.
    #[must_use]
    pub fn count_matches(&self, haystack: &[u8]) -> usize {
        haystack.iter().filter(|&&b| self.contains(b)).count()
    }

    /// Check if the bitmap matches any byte in the slice.
    #[inline]
    #[must_use]
    pub fn matches_any(&self, haystack: &[u8]) -> bool {
        self.find_first(haystack).is_some()
    }
}

impl Default for AsciiClassBitmap {
    fn default() -> Self {
        Self::empty()
    }
}

/// A precompiled character class for fast matching.
/// Combines bitmap for ASCII and handles non-ASCII via the original `CharClass`.
#[derive(Clone, Debug)]
pub struct CompiledCharClass {
    /// Fast ASCII bitmap.
    pub bitmap: AsciiClassBitmap,
    /// Original char class for non-ASCII and complex cases.
    pub original: CharClass,
    /// Unicode mode - enable Unicode character classes.
    pub unicode: bool,
}

impl CompiledCharClass {
    /// Create a compiled character class.
    #[must_use]
    pub fn new(class: &CharClass) -> Self {
        CompiledCharClass {
            bitmap: AsciiClassBitmap::from_char_class(class),
            original: class.clone(),
            unicode: false,
        }
    }

    /// Create a compiled character class with unicode mode.
    #[must_use]
    pub fn new_with_unicode(class: &CharClass, unicode: bool) -> Self {
        CompiledCharClass {
            bitmap: AsciiClassBitmap::from_char_class(class),
            original: class.clone(),
            unicode,
        }
    }

    /// Check if a character matches this class.
    #[inline]
    #[must_use]
    pub fn matches(&self, ch: char) -> bool {
        if ch.is_ascii() {
            self.bitmap.contains(ch as u8)
        } else if self.unicode {
            // In unicode mode, check if non-ASCII chars match via the original class
            // which now has NamedClass with unicode-aware matching
            self.original.matches_unicode(ch)
        } else {
            self.original.matches(ch)
        }
    }

    /// Find the first position where any byte matches.
    #[inline]
    #[must_use]
    pub fn find_first(&self, haystack: &[u8]) -> Option<usize> {
        self.bitmap.find_first(haystack)
    }
}

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

    #[test]
    fn test_ascii_bitmap_single() {
        let class = CharClass::new(false, vec![CharClassItem::Single('a')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert!(bitmap.contains(b'a'));
        assert!(!bitmap.contains(b'b'));
        assert!(!bitmap.contains(b'A'));
    }

    #[test]
    fn test_ascii_bitmap_range() {
        let class = CharClass::new(false, vec![CharClassItem::Range('a', 'z')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert!(bitmap.contains(b'a'));
        assert!(bitmap.contains(b'm'));
        assert!(bitmap.contains(b'z'));
        assert!(!bitmap.contains(b'A'));
        assert!(!bitmap.contains(b'0'));
    }

    #[test]
    fn test_ascii_bitmap_negated() {
        let class = CharClass::new(true, vec![CharClassItem::Range('a', 'z')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert!(!bitmap.contains(b'a'));
        assert!(!bitmap.contains(b'z'));
        assert!(bitmap.contains(b'A'));
        assert!(bitmap.contains(b'0'));
        assert!(bitmap.contains(b' '));
    }

    #[test]
    fn test_ascii_bitmap_digit() {
        let class = CharClass::digit();
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        for b in b'0'..=b'9' {
            assert!(bitmap.contains(b), "Should contain digit {}", b as char);
        }
        assert!(!bitmap.contains(b'a'));
        assert!(!bitmap.contains(b' '));
    }

    #[test]
    fn test_ascii_bitmap_word() {
        let class = CharClass::word();
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert!(bitmap.contains(b'a'));
        assert!(bitmap.contains(b'Z'));
        assert!(bitmap.contains(b'5'));
        assert!(bitmap.contains(b'_'));
        assert!(!bitmap.contains(b' '));
        assert!(!bitmap.contains(b'-'));
    }

    #[test]
    fn test_find_first() {
        let class = CharClass::new(false, vec![CharClassItem::Range('a', 'z')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert_eq!(bitmap.find_first(b"123abc"), Some(3));
        assert_eq!(bitmap.find_first(b"ABC"), None);
        assert_eq!(bitmap.find_first(b"hello"), Some(0));
        assert_eq!(bitmap.find_first(b""), None);
    }

    #[test]
    fn test_find_first_long() {
        let class = CharClass::new(false, vec![CharClassItem::Single('x')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        // Test with text longer than 16 bytes to exercise SIMD path
        let text = b"0123456789abcdefxyz";
        assert_eq!(bitmap.find_first(text), Some(16));

        let text2 = b"01234567890123456789x";
        assert_eq!(bitmap.find_first(text2), Some(20));
    }

    #[test]
    fn test_find_all() {
        let class = CharClass::new(false, vec![CharClassItem::Range('a', 'z')]);
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        let positions = bitmap.find_all(b"a1b2c3");
        assert_eq!(positions, vec![0, 2, 4]);
    }

    #[test]
    fn test_count_matches() {
        let class = CharClass::digit();
        let bitmap = AsciiClassBitmap::from_char_class(&class);

        assert_eq!(bitmap.count_matches(b"abc123def456"), 6);
        assert_eq!(bitmap.count_matches(b"no digits"), 0);
    }

    #[test]
    fn test_compiled_char_class() {
        let class = CharClass::word();
        let compiled = CompiledCharClass::new(&class);

        assert!(compiled.matches('a'));
        assert!(compiled.matches('Z'));
        assert!(compiled.matches('5'));
        assert!(!compiled.matches(' '));
    }
}