rexile 0.5.7

A blazing-fast regex engine with 22x faster compilation and optimized case-insensitive matching
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
//! Character class matching implementation
//!
//! Supports: [abc], [a-z], [0-9], [^abc] (negation), [A-Za-z0-9_]

/// Represents a character class pattern like [a-z] or [^0-9]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct CharClass {
    /// Individual characters to match (e.g., 'a', 'b', 'c' from [abc])
    pub(crate) chars: Vec<char>, // Made pub(crate) for optimization checks
    /// Character ranges to match (e.g., ('a', 'z') from [a-z])
    pub(crate) ranges: Vec<(char, char)>, // Made pub(crate) for optimization checks
    /// If true, matches anything NOT in chars/ranges
    pub(crate) negated: bool, // Made pub(crate) for optimization checks
    /// ASCII fast path: bitmap for ASCII characters (0-127)
    ascii_bitmap: Option<[u64; 2]>, // 128 bits = 2 x u64
}

impl CharClass {
    /// Create a new empty character class
    pub fn new() -> Self {
        CharClass {
            chars: Vec::new(),
            ranges: Vec::new(),
            negated: false,
            ascii_bitmap: None,
        }
    }

    /// Add a single character to the class
    pub fn add_char(&mut self, ch: char) {
        self.chars.push(ch);
        self.ascii_bitmap = None; // Invalidate bitmap
    }

    /// Add a character range to the class
    pub fn add_range(&mut self, start: char, end: char) {
        self.ranges.push((start, end));
        self.ascii_bitmap = None; // Invalidate bitmap
    }

    /// Negate this character class
    pub fn negate(&mut self) {
        self.negated = !self.negated;
    }

    /// Finalize the character class by building optimizations
    pub fn finalize(&mut self) {
        self.build_ascii_bitmap();
    }

    /// Parse character class from string like "a-z" or "^0-9"
    pub fn parse(pattern: &str) -> Result<Self, String> {
        if pattern.is_empty() {
            return Err("Empty character class".to_string());
        }

        let mut chars = Vec::new();
        let mut ranges = Vec::new();
        let negated = pattern.starts_with('^');
        let pattern = if negated { &pattern[1..] } else { pattern };

        let pattern_chars: Vec<char> = pattern.chars().collect();
        let mut i = 0;

        while i < pattern_chars.len() {
            // Check for escape sequences like \s, \d, \w
            if pattern_chars[i] == '\\' && i + 1 < pattern_chars.len() {
                let escape_char = pattern_chars[i + 1];
                match escape_char {
                    's' => {
                        // Whitespace: space, tab, newline, carriage return, form feed, vertical tab
                        chars.push(' ');
                        chars.push('\t');
                        chars.push('\n');
                        chars.push('\r');
                        chars.push('\x0C'); // form feed
                        chars.push('\x0B'); // vertical tab
                        i += 2;
                    }
                    'd' => {
                        // Digits 0-9
                        ranges.push(('0', '9'));
                        i += 2;
                    }
                    'w' => {
                        // Word characters: a-z, A-Z, 0-9, _
                        ranges.push(('a', 'z'));
                        ranges.push(('A', 'Z'));
                        ranges.push(('0', '9'));
                        chars.push('_');
                        i += 2;
                    }
                    _ => {
                        // Other escapes like \., \-, etc. - treat as literal
                        chars.push(escape_char);
                        i += 2;
                    }
                }
                continue;
            }

            // Check for range (a-z)
            if i + 2 < pattern_chars.len() && pattern_chars[i + 1] == '-' {
                let start = pattern_chars[i];
                let end = pattern_chars[i + 2];

                if start > end {
                    return Err(format!("Invalid range: {}-{}", start, end));
                }

                ranges.push((start, end));
                i += 3;
            } else {
                // Single character
                chars.push(pattern_chars[i]);
                i += 1;
            }
        }

        let mut cc = CharClass {
            chars,
            ranges,
            negated,
            ascii_bitmap: None,
        };

        // Build ASCII bitmap for fast matching
        cc.build_ascii_bitmap();

        Ok(cc)
    }

    /// Build bitmap for ASCII characters (0-127) for fast lookup
    fn build_ascii_bitmap(&mut self) {
        let mut bitmap = [0u64; 2]; // 128 bits

        // Set bits for individual chars
        for &ch in &self.chars {
            if (ch as u32) < 128 {
                let idx = ch as usize;
                bitmap[idx / 64] |= 1u64 << (idx % 64);
            }
        }

        // Set bits for ranges
        for &(start, end) in &self.ranges {
            let start_val = start as u32;
            let end_val = end as u32;

            if start_val < 128 {
                let end_ascii = end_val.min(127);
                for ch in start_val..=end_ascii {
                    let idx = ch as usize;
                    bitmap[idx / 64] |= 1u64 << (idx % 64);
                }
            }
        }

        self.ascii_bitmap = Some(bitmap);
    }

    /// Get the pre-computed ASCII bitmap for direct inline lookup
    #[inline(always)]
    pub fn get_ascii_bitmap(&self) -> Option<&[u64; 2]> {
        self.ascii_bitmap.as_ref()
    }

    /// Check if an ASCII byte matches using pre-extracted bitmap (no function call overhead)
    #[inline(always)]
    pub fn matches_byte_bitmap(bitmap: &[u64; 2], negated: bool, byte: u8) -> bool {
        let idx = byte as usize;
        let bit_set = (bitmap[idx / 64] & (1u64 << (idx % 64))) != 0;
        if negated {
            !bit_set
        } else {
            bit_set
        }
    }

    /// Check if a character matches this character class
    #[inline]
    pub fn matches(&self, ch: char) -> bool {
        let ch_val = ch as u32;

        // Fast path: ASCII bitmap lookup (uses bit operations, very fast)
        if ch_val < 128 {
            if let Some(bitmap) = &self.ascii_bitmap {
                let idx = ch_val as usize;
                let bit_set = (bitmap[idx / 64] & (1u64 << (idx % 64))) != 0;
                return if self.negated { !bit_set } else { bit_set };
            }
        }

        // Slow path: check ranges and individual chars
        let mut matched = false;

        // Check individual characters
        if self.chars.contains(&ch) {
            matched = true;
        }

        // Check ranges
        if !matched {
            for &(start, end) in &self.ranges {
                if ch >= start && ch <= end {
                    matched = true;
                    break;
                }
            }
        }

        // Apply negation
        if self.negated {
            !matched
        } else {
            matched
        }
    }

    /// Check if this is a dot wildcard class [^\n] (matches any char except newline)
    pub fn is_dot_class(&self) -> bool {
        self.negated && self.chars.len() == 1 && self.chars[0] == '\n' && self.ranges.is_empty()
    }

    /// Check if this is a digit-only character class [0-9]
    pub fn is_digit_class(&self) -> bool {
        !self.negated
            && self.chars.is_empty()
            && self.ranges.len() == 1
            && self.ranges[0] == ('0', '9')
    }

    /// Check if this is a word character class [a-zA-Z0-9_]
    pub fn is_word_class(&self) -> bool {
        if self.negated {
            return false;
        }
        // Check if it's exactly \w (word chars: a-z, A-Z, 0-9, _)
        let has_lowercase = self.ranges.contains(&('a', 'z'));
        let has_uppercase = self.ranges.contains(&('A', 'Z'));
        let has_digits = self.ranges.contains(&('0', '9'));
        let has_underscore = self.chars.contains(&'_');

        // Must have exactly these components
        has_lowercase
            && has_uppercase
            && has_digits
            && has_underscore
            && self.ranges.len() == 3
            && self.chars.len() == 1
    }

    /// Check if this is a whitespace-only character class \s
    pub fn is_whitespace_class(&self) -> bool {
        !self.negated
            && self.chars.contains(&' ')
            && self.chars.contains(&'\t')
            && self.chars.contains(&'\n')
            && self.chars.contains(&'\r')
            && self.ranges.is_empty()
    }

    /// Check if this character class overlaps with another
    /// (i.e., there exists at least one character matching both)
    pub fn overlaps_with(&self, other: &CharClass) -> bool {
        // If either is negated, they almost certainly overlap
        // (negated classes match the vast majority of characters)
        if self.negated || other.negated {
            return true;
        }

        // Check if any range in self overlaps with any range in other
        for &(s1, e1) in &self.ranges {
            for &(s2, e2) in &other.ranges {
                if s1 <= e2 && s2 <= e1 {
                    return true;
                }
            }
            // Check if any char in other falls within self's ranges
            for &ch in &other.chars {
                if ch >= s1 && ch <= e1 {
                    return true;
                }
            }
        }

        // Check if any char in self falls within other's ranges
        for &ch in &self.chars {
            for &(s2, e2) in &other.ranges {
                if ch >= s2 && ch <= e2 {
                    return true;
                }
            }
            // Check if any char matches directly
            if other.chars.contains(&ch) {
                return true;
            }
        }

        false
    }

    /// Find first character in text that matches this class (SIMD-optimized for ASCII)
    /// Returns byte position if found, None otherwise
    pub fn find_first(&self, text: &str) -> Option<usize> {
        let bytes = text.as_bytes();

        // Fast path for ASCII-only text with bitmap
        if let Some(bitmap) = &self.ascii_bitmap {
            // Check if text is ASCII-only by scanning in chunks
            if bytes.iter().all(|&b| b < 128) {
                // SIMD-friendly: Process bytes directly using bitmap
                for (idx, &byte) in bytes.iter().enumerate() {
                    let bit_set = (bitmap[byte as usize / 64] & (1u64 << (byte % 64))) != 0;
                    let matches = if self.negated { !bit_set } else { bit_set };
                    if matches {
                        return Some(idx);
                    }
                }
                return None;
            }
        }

        // Fallback: UTF-8 aware character-by-character scan
        for (idx, ch) in text.char_indices() {
            if self.matches(ch) {
                return Some(idx);
            }
        }
        None
    }

    /// Check if text starts with a character matching this class
    /// Returns Some(char_len) if matched, None otherwise
    pub fn match_at(&self, text: &str, pos: usize) -> Option<usize> {
        let ch = text[pos..].chars().next()?;
        if self.matches(ch) {
            Some(ch.len_utf8())
        } else {
            None
        }
    }
}

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

    #[test]
    fn test_simple_chars() {
        let cc = CharClass::parse("abc").unwrap();
        assert!(cc.matches('a'));
        assert!(cc.matches('b'));
        assert!(cc.matches('c'));
        assert!(!cc.matches('d'));
        assert!(!cc.matches('z'));
    }

    #[test]
    fn test_range() {
        let cc = CharClass::parse("a-z").unwrap();
        assert!(cc.matches('a'));
        assert!(cc.matches('m'));
        assert!(cc.matches('z'));
        assert!(!cc.matches('A'));
        assert!(!cc.matches('0'));
    }

    #[test]
    fn test_multiple_ranges() {
        let cc = CharClass::parse("a-zA-Z0-9").unwrap();
        assert!(cc.matches('a'));
        assert!(cc.matches('Z'));
        assert!(cc.matches('5'));
        assert!(!cc.matches('!'));
        assert!(!cc.matches(' '));
    }

    #[test]
    fn test_negation() {
        let cc = CharClass::parse("^abc").unwrap();
        assert!(!cc.matches('a'));
        assert!(!cc.matches('b'));
        assert!(!cc.matches('c'));
        assert!(cc.matches('d'));
        assert!(cc.matches('z'));
        assert!(cc.matches('1'));
    }

    #[test]
    fn test_negated_range() {
        let cc = CharClass::parse("^0-9").unwrap();
        assert!(!cc.matches('0'));
        assert!(!cc.matches('5'));
        assert!(!cc.matches('9'));
        assert!(cc.matches('a'));
        assert!(cc.matches('Z'));
    }

    #[test]
    fn test_mixed() {
        let cc = CharClass::parse("a-z_0-9").unwrap();
        assert!(cc.matches('a'));
        assert!(cc.matches('_'));
        assert!(cc.matches('5'));
        assert!(!cc.matches('A'));
        assert!(!cc.matches('-'));
    }

    #[test]
    fn test_ascii_bitmap() {
        let cc = CharClass::parse("a-z").unwrap();
        // Should use ASCII bitmap for fast lookup
        assert!(cc.ascii_bitmap.is_some());
        assert!(cc.matches('a'));
        assert!(cc.matches('z'));
    }
}