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
//! Match result types for the public API.

#![allow(clippy::needless_range_loop)]

use std::collections::HashMap;
use std::ops::Range;

use crate::engine::EditCounts;

/// A single match in the text.
#[derive(Debug, Clone)]
pub struct Match<'t> {
    text: &'t str,
    start: usize,
    end: usize,
    similarity: f32,
    edits: EditCounts,
    fuzzy_changes: Option<(Vec<usize>, Vec<usize>, Vec<usize>)>,
    partial: bool,
}

impl<'t> Match<'t> {
    /// Create a new match.
    pub(crate) fn new(
        text: &'t str,
        start: usize,
        end: usize,
        similarity: f32,
        edits: EditCounts,
    ) -> Self {
        Match {
            text,
            start,
            end,
            similarity,
            edits,
            fuzzy_changes: None,
            partial: false,
        }
    }

    /// Create a new match with fuzzy changes (edit positions).
    /// Note: This is not currently used because fuzzy changes are computed lazily
    /// via `fuzzy_changes_with_pattern()` for better performance.
    /// Kept for potential future optimization where fuzzy changes are computed during matching.
    #[allow(dead_code)]
    pub(crate) fn new_with_changes(
        text: &'t str,
        start: usize,
        end: usize,
        similarity: f32,
        edits: EditCounts,
        fuzzy_changes: (Vec<usize>, Vec<usize>, Vec<usize>),
    ) -> Self {
        Match {
            text,
            start,
            end,
            similarity,
            edits,
            fuzzy_changes: Some(fuzzy_changes),
            partial: false,
        }
    }

    /// Create a new match with all options specified.
    pub(crate) fn new_full(
        text: &'t str,
        start: usize,
        end: usize,
        similarity: f32,
        edits: EditCounts,
        fuzzy_changes: Option<(Vec<usize>, Vec<usize>, Vec<usize>)>,
        partial: bool,
    ) -> Self {
        Match {
            text,
            start,
            end,
            similarity,
            edits,
            fuzzy_changes,
            partial,
        }
    }

    /// Get the matched text.
    #[must_use]
    pub fn as_str(&self) -> &'t str {
        &self.text[self.start..self.end]
    }

    /// Get the start byte offset.
    #[must_use]
    pub fn start(&self) -> usize {
        self.start
    }

    /// Get the end byte offset.
    #[must_use]
    pub fn end(&self) -> usize {
        self.end
    }

    /// Get the byte range.
    #[must_use]
    pub fn range(&self) -> Range<usize> {
        self.start..self.end
    }

    /// Get the length in bytes.
    #[must_use]
    pub fn len(&self) -> usize {
        self.end - self.start
    }

    /// Check if empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.start == self.end
    }

    /// Get the similarity score (0.0 - 1.0).
    #[must_use]
    pub fn similarity(&self) -> f32 {
        self.similarity
    }

    /// Get the edit counts.
    #[must_use]
    pub fn edits(&self) -> &EditCounts {
        &self.edits
    }

    /// Get the total number of edits.
    #[must_use]
    pub fn total_edits(&self) -> u8 {
        self.edits.total()
    }

    /// Get fuzzy counts as (insertions, deletions, substitutions).
    ///
    /// This matches the API of mrab-regex's `fuzzy_counts` property.
    /// - insertions: characters in the text not in the pattern
    /// - deletions: characters in the pattern not in the text
    /// - substitutions: characters that differ between pattern and text
    #[must_use]
    pub fn fuzzy_counts(&self) -> (u32, u32, u32) {
        (
            u32::from(self.edits.insertions),
            u32::from(self.edits.deletions),
            u32::from(self.edits.substitutions),
        )
    }

    /// Get fuzzy changes as (insertions, deletions, substitutions).
    ///
    /// This matches the API of mrab-regex's `fuzzy_changes` property.
    /// Returns positions within the matched text where edits occurred.
    ///
    /// Note: This requires the original pattern to compute accurately.
    /// If the pattern was not provided during matching, returns empty vectors.
    #[must_use]
    pub fn fuzzy_changes(&self) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
        if let Some(changes) = &self.fuzzy_changes {
            return changes.clone();
        }

        // If we don't have pre-computed changes, return empty
        // The pattern is needed to compute these accurately
        (Vec::new(), Vec::new(), Vec::new())
    }

    /// Get fuzzy changes given the original pattern.
    ///
    /// This computes the edit positions by comparing the matched text
    /// against the original pattern.
    #[must_use]
    pub fn fuzzy_changes_with_pattern(
        &self,
        pattern: &str,
    ) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
        let matched_text = self.as_str();
        compute_fuzzy_changes(pattern, matched_text)
    }

    /// Check if this is a partial match.
    ///
    /// A partial match is one that reaches the end of the input text
    /// without completing the match. This is useful when searching
    /// through streamed or truncated text.
    #[must_use]
    pub fn partial(&self) -> bool {
        self.partial
    }
}

/// Compute fuzzy changes: positions of insertions, deletions, and substitutions.
/// Returns (insertions, deletions, substitutions) as position vectors.
#[allow(clippy::cast_possible_truncation)]
fn compute_fuzzy_changes(pattern: &str, text: &str) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
    let a = pattern.as_bytes();
    let b = text.as_bytes();
    let a_len = a.len();
    let b_len = b.len();

    if a_len == 0 {
        let insertions: Vec<usize> = (0..b_len).collect();
        return (insertions, Vec::new(), Vec::new());
    }
    if b_len == 0 {
        let deletions: Vec<usize> = (0..a_len).collect();
        return (Vec::new(), deletions, Vec::new());
    }

    // Build the DP matrix
    let mut matrix = vec![vec![0u32; b_len + 1]; a_len + 1];

    for i in 0..=a_len {
        matrix[i][0] = i as u32;
    }
    for j in 0..=b_len {
        matrix[0][j] = j as u32;
    }

    for i in 1..=a_len {
        for j in 1..=b_len {
            let cost = u32::from(a[i - 1] != b[j - 1]);
            matrix[i][j] = (matrix[i - 1][j] + 1)
                .min(matrix[i][j - 1] + 1)
                .min(matrix[i - 1][j - 1] + cost);
        }
    }

    // Backtrack to find edit positions
    let mut insertions = Vec::new();
    let mut deletions = Vec::new();
    let mut substitutions = Vec::new();

    let mut i = a_len;
    let mut j = b_len;

    while i > 0 || j > 0 {
        if i > 0 && j > 0 && a[i - 1] == b[j - 1] {
            i -= 1;
            j -= 1;
        } else if i > 0 && j > 0 && matrix[i][j] == matrix[i - 1][j - 1] + 1 {
            substitutions.push(j - 1);
            i -= 1;
            j -= 1;
        } else if j > 0 && matrix[i][j] == matrix[i][j - 1] + 1 {
            insertions.push(j - 1);
            j -= 1;
        } else if i > 0 && matrix[i][j] == matrix[i - 1][j] + 1 {
            deletions.push(i - 1);
            i -= 1;
        } else {
            break;
        }
    }

    (insertions, deletions, substitutions)
}

/// Capture groups from a match.
#[derive(Debug, Clone)]
pub struct Captures<'t> {
    text: &'t str,
    slots: Vec<Option<(usize, usize)>>,
    names: HashMap<String, usize>,
    similarity: f32,
    edits: EditCounts,
}

impl<'t> Captures<'t> {
    /// Create captures from match result.
    pub(crate) fn new(
        text: &'t str,
        slots: Vec<Option<(usize, usize)>>,
        names: HashMap<String, usize>,
        similarity: f32,
        edits: EditCounts,
    ) -> Self {
        Captures {
            text,
            slots,
            names,
            similarity,
            edits,
        }
    }

    /// Get the full match (group 0).
    #[must_use]
    pub fn get(&self, index: usize) -> Option<Match<'t>> {
        self.slots
            .get(index)
            .copied()
            .flatten()
            .map(|(start, end)| {
                Match::new(self.text, start, end, self.similarity, self.edits.clone())
            })
    }

    /// Get a capture by name.
    #[must_use]
    pub fn name(&self, name: &str) -> Option<Match<'t>> {
        self.names.get(name).and_then(|&idx| self.get(idx))
    }

    /// Get the number of capture groups (including group 0).
    #[must_use]
    pub fn len(&self) -> usize {
        self.slots.len()
    }

    /// Check if there are no captures.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.slots.is_empty()
    }

    /// Iterate over all captures.
    #[must_use]
    pub fn iter(&self) -> CapturesIter<'_, 't> {
        CapturesIter {
            captures: self,
            index: 0,
        }
    }

    /// Get the similarity score.
    #[must_use]
    pub fn similarity(&self) -> f32 {
        self.similarity
    }

    /// Get edit counts.
    #[must_use]
    pub fn edits(&self) -> &EditCounts {
        &self.edits
    }

    /// Get fuzzy counts as (insertions, deletions, substitutions).
    ///
    /// This matches the API of mrab-regex's `fuzzy_counts` property.
    #[must_use]
    pub fn fuzzy_counts(&self) -> (u32, u32, u32) {
        (
            u32::from(self.edits.insertions),
            u32::from(self.edits.deletions),
            u32::from(self.edits.substitutions),
        )
    }

    /// Get fuzzy changes as (insertions, deletions, substitutions).
    ///
    /// This matches the API of mrab-regex's `fuzzy_changes` property.
    #[must_use]
    pub fn fuzzy_changes(&self) -> (Vec<usize>, Vec<usize>, Vec<usize>) {
        (Vec::new(), Vec::new(), Vec::new())
    }

    /// Expand a replacement string with capture references.
    ///
    /// Supports `$0`, `$1`, etc. for numbered groups and `$name` for named groups.
    #[must_use]
    pub fn expand(&self, replacement: &str) -> String {
        let mut result = String::new();
        let mut chars = replacement.chars().peekable();

        while let Some(ch) = chars.next() {
            if ch == '$' {
                match chars.peek() {
                    Some('$') => {
                        chars.next();
                        result.push('$');
                    }
                    Some(&c) if c.is_ascii_digit() => {
                        let mut num = 0usize;
                        while let Some(&c) = chars.peek() {
                            if let Some(d) = c.to_digit(10) {
                                num = num * 10 + d as usize;
                                chars.next();
                            } else {
                                break;
                            }
                        }
                        if let Some(m) = self.get(num) {
                            result.push_str(m.as_str());
                        }
                    }
                    Some(&c) if c.is_alphabetic() || c == '_' => {
                        let mut name = String::new();
                        while let Some(&c) = chars.peek() {
                            if c.is_alphanumeric() || c == '_' {
                                name.push(c);
                                chars.next();
                            } else {
                                break;
                            }
                        }
                        if let Some(m) = self.name(&name) {
                            result.push_str(m.as_str());
                        }
                    }
                    Some('{') => {
                        chars.next(); // consume '{'
                        let mut name = String::new();
                        while let Some(&c) = chars.peek() {
                            if c == '}' {
                                chars.next();
                                break;
                            }
                            name.push(c);
                            chars.next();
                        }
                        // Try as number first
                        if let Ok(num) = name.parse::<usize>() {
                            if let Some(m) = self.get(num) {
                                result.push_str(m.as_str());
                            }
                        } else if let Some(m) = self.name(&name) {
                            result.push_str(m.as_str());
                        }
                    }
                    _ => result.push('$'),
                }
            } else {
                result.push(ch);
            }
        }

        result
    }
}

/// Iterator over captures.
pub struct CapturesIter<'c, 't> {
    captures: &'c Captures<'t>,
    index: usize,
}

impl<'t> Iterator for CapturesIter<'_, 't> {
    type Item = Option<Match<'t>>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.index < self.captures.slots.len() {
            let result = self.captures.get(self.index);
            self.index += 1;
            Some(result)
        } else {
            None
        }
    }
}

impl<'c, 't> IntoIterator for &'c Captures<'t> {
    type Item = Option<Match<'t>>;
    type IntoIter = CapturesIter<'c, 't>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// Iterator over all matches.
pub struct Matches<'t> {
    matches: std::vec::IntoIter<Match<'t>>,
}

impl<'t> Matches<'t> {
    /// Create a Matches iterator from pre-collected matches.
    pub(crate) fn new(matches: Vec<Match<'t>>) -> Self {
        Matches {
            matches: matches.into_iter(),
        }
    }
}

impl<'t> Iterator for Matches<'t> {
    type Item = Match<'t>;

    fn next(&mut self) -> Option<Self::Item> {
        self.matches.next()
    }
}

/// Iterator over all capture groups.
pub struct CaptureMatches<'r, 't> {
    pub(crate) regex: &'r super::regex::FuzzyRegex,
    pub(crate) text: &'t str,
    pub(crate) pos: usize,
}

impl<'t> Iterator for CaptureMatches<'_, 't> {
    type Item = Captures<'t>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos > self.text.len() {
            return None;
        }

        let result = self.regex.captures_at(self.text, self.pos);

        if let Some(caps) = result {
            if let Some(m) = caps.get(0) {
                self.pos = if m.end() > self.pos {
                    m.end()
                } else {
                    self.text[self.pos..]
                        .char_indices()
                        .nth(1)
                        .map_or(self.text.len() + 1, |(i, _)| self.pos + i)
                };
            } else {
                self.pos = self.text.len() + 1;
            }
            Some(caps)
        } else {
            self.pos = self.text.len() + 1;
            None
        }
    }
}

/// Iterator over split segments.
pub struct Split<'r, 't> {
    pub(crate) regex: &'r super::regex::FuzzyRegex,
    pub(crate) text: &'t str,
    pub(crate) pos: usize,
    pub(crate) done: bool,
}

impl<'t> Iterator for Split<'_, 't> {
    type Item = &'t str;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        if self.pos > self.text.len() {
            self.done = true;
            return None;
        }

        // Use find_from to search from current position onwards
        let result = self.regex.find_from(self.text, self.pos);

        if let Some(m) = result {
            let segment = &self.text[self.pos..m.start()];
            self.pos = m.end();
            Some(segment)
        } else {
            let segment = &self.text[self.pos..];
            self.done = true;
            Some(segment)
        }
    }
}