ripsed-core 0.2.8

Core edit engine for ripsed — pure logic, no I/O
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
use crate::error::RipsedError;
use crate::operation::Op;
use regex::Regex;

/// Abstraction over literal and regex matching.
#[derive(Debug)]
pub enum Matcher {
    Literal {
        pattern: String,
    },
    /// A regex matcher — used for both explicit `--regex` patterns and as the
    /// implementation backing case-insensitive literal matching (via
    /// `regex::escape` + `(?i)`), which avoids byte-offset mismatches from
    /// `str::to_lowercase()` on multi-byte Unicode characters.
    Regex(Regex),
}

impl Matcher {
    /// Create a new matcher from an operation.
    pub fn new(op: &Op) -> Result<Self, RipsedError> {
        let pattern = op.find_pattern();
        let is_regex = op.is_regex();
        let case_insensitive = op.is_case_insensitive();

        if is_regex || case_insensitive {
            // For case-insensitive literals, escape the pattern and delegate to
            // the regex engine which handles Unicode casing correctly.
            let re_src = if is_regex {
                pattern.to_string()
            } else {
                regex::escape(pattern)
            };
            let re_pattern = if case_insensitive {
                format!("(?i){re_src}")
            } else {
                re_src
            };
            Regex::new(&re_pattern).map(Matcher::Regex).map_err(|e| {
                let mut err = RipsedError::invalid_regex(0, pattern, &e.to_string());
                err.operation_index = None;
                err
            })
        } else {
            Ok(Matcher::Literal {
                pattern: pattern.to_string(),
            })
        }
    }

    /// Check if the given text matches.
    pub fn is_match(&self, text: &str) -> bool {
        match self {
            Matcher::Literal { pattern, .. } => text.contains(pattern.as_str()),
            Matcher::Regex(re) => re.is_match(text),
        }
    }

    /// Replace all matches in the given text. Returns None if no matches.
    pub fn replace(&self, text: &str, replacement: &str) -> Option<String> {
        match self {
            Matcher::Literal { pattern, .. } => {
                if text.contains(pattern.as_str()) {
                    Some(text.replace(pattern.as_str(), replacement))
                } else {
                    None
                }
            }
            Matcher::Regex(re) => {
                if re.is_match(text) {
                    Some(re.replace_all(text, replacement).into_owned())
                } else {
                    None
                }
            }
        }
    }
}

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

    #[test]
    fn test_literal_match() {
        let op = Op::Replace {
            find: "hello".to_string(),
            replace: "hi".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("say hello world"));
        assert!(!m.is_match("say Hi world"));
    }

    #[test]
    fn test_literal_case_insensitive() {
        let op = Op::Replace {
            find: "hello".to_string(),
            replace: "hi".to_string(),
            regex: false,
            case_insensitive: true,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("say HELLO world"));
        assert!(m.is_match("say Hello world"));
    }

    #[test]
    fn test_regex_match() {
        let op = Op::Replace {
            find: r"fn\s+(\w+)".to_string(),
            replace: "fn new_$1".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("fn old_func() {"));
        assert!(!m.is_match("let x = 5;"));
    }

    #[test]
    fn test_regex_replace_with_captures() {
        let op = Op::Replace {
            find: r"fn\s+old_(\w+)".to_string(),
            replace: "fn new_$1".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("fn old_function() {", "fn new_$1");
        assert_eq!(result, Some("fn new_function() {".to_string()));
    }

    #[test]
    fn test_invalid_regex() {
        let op = Op::Replace {
            find: "fn (foo".to_string(),
            replace: "bar".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let err = Matcher::new(&op).unwrap_err();
        assert_eq!(err.code, crate::error::ErrorCode::InvalidRegex);
    }

    // ---------------------------------------------------------------
    // Empty pattern behavior
    // ---------------------------------------------------------------

    #[test]
    fn test_empty_pattern_literal_matches_everything() {
        let op = Op::Replace {
            find: "".to_string(),
            replace: "x".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        // An empty string is contained in every string
        assert!(m.is_match("anything"));
        assert!(m.is_match(""));
    }

    #[test]
    fn test_empty_pattern_literal_replace() {
        let op = Op::Replace {
            find: "".to_string(),
            replace: "x".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        // Rust's str::replace("", "x") inserts "x" between every char and at start/end
        let result = m.replace("ab", "x");
        assert_eq!(result, Some("xaxbx".to_string()));
    }

    #[test]
    fn test_empty_pattern_regex_matches_everything() {
        let op = Op::Replace {
            find: "".to_string(),
            replace: "x".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("anything"));
        assert!(m.is_match(""));
    }

    // ---------------------------------------------------------------
    // Pattern that matches entire line
    // ---------------------------------------------------------------

    #[test]
    fn test_pattern_matches_entire_line_literal() {
        let op = Op::Replace {
            find: "hello world".to_string(),
            replace: "goodbye".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("hello world", "goodbye");
        assert_eq!(result, Some("goodbye".to_string()));
    }

    #[test]
    fn test_pattern_matches_entire_line_regex() {
        let op = Op::Replace {
            find: r"^.*$".to_string(),
            replace: "replaced".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("anything here", "replaced");
        assert_eq!(result, Some("replaced".to_string()));
    }

    #[test]
    fn test_regex_anchored_full_line() {
        let op = Op::Replace {
            find: r"^fn main\(\)$".to_string(),
            replace: "fn start()".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("fn main()"));
        assert!(!m.is_match("  fn main()")); // leading whitespace
        assert!(!m.is_match("fn main() {")); // trailing content
    }

    // ---------------------------------------------------------------
    // Case-insensitive with unicode (Turkish I problem, etc.)
    // ---------------------------------------------------------------

    #[test]
    fn test_case_insensitive_ascii() {
        let op = Op::Replace {
            find: "Hello".to_string(),
            replace: "hi".to_string(),
            regex: false,
            case_insensitive: true,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("HELLO"));
        assert!(m.is_match("hello"));
        assert!(m.is_match("HeLLo"));
        let result = m.replace("say HELLO there", "hi");
        assert_eq!(result, Some("say hi there".to_string()));
    }

    #[test]
    fn test_case_insensitive_german_eszett() {
        // German sharp-s: lowercase to_lowercase() of "SS" is "ss",
        // and to_lowercase() of "\u{00DF}" (sharp-s) is "\u{00DF}"
        // This tests that the engine handles non-trivial unicode casing
        let op = Op::Replace {
            find: "stra\u{00DF}e".to_string(), // "strasse" with sharp-s
            replace: "street".to_string(),
            regex: false,
            case_insensitive: true,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("STRA\u{00DF}E"));
    }

    #[test]
    fn test_case_insensitive_turkish_i_lowercase() {
        // Turkish dotted I: \u{0130} (capital I with dot above)
        // This is a known edge case. We test that the matcher doesn't panic
        // and behaves consistently with Unicode simple case folding.
        let op = Op::Replace {
            find: "i".to_string(),
            replace: "x".to_string(),
            regex: false,
            case_insensitive: true,
        };
        let m = Matcher::new(&op).unwrap();
        // Standard ASCII: "I" simple-folds to "i", so this matches
        assert!(m.is_match("I"));
        // \u{0130} (İ) has no simple case fold to "i" in Unicode — the full
        // fold is "i\u{0307}" but the regex engine only uses simple folds.
        // This correctly does NOT match, avoiding false positives from the
        // old to_lowercase()-based byte-offset approach.
        assert!(!m.is_match("\u{0130}"));
    }

    // ---------------------------------------------------------------
    // Regex special characters in literal mode
    // ---------------------------------------------------------------

    #[test]
    fn test_literal_mode_regex_metacharacters() {
        // All these are regex metacharacters but should be treated literally
        let patterns = vec![
            (".", "dot"),
            ("*", "star"),
            ("+", "plus"),
            ("?", "question"),
            ("(", "paren"),
            ("[", "bracket"),
            ("{", "brace"),
            ("^", "caret"),
            ("$", "dollar"),
            ("|", "pipe"),
            ("\\", "backslash"),
        ];
        for (pat, name) in patterns {
            let op = Op::Replace {
                find: pat.to_string(),
                replace: "X".to_string(),
                regex: false,
                case_insensitive: false,
            };
            let m = Matcher::new(&op).unwrap();
            let text = format!("before {pat} after");
            assert!(
                m.is_match(&text),
                "Literal mode should match '{name}' ({pat}) as a literal character"
            );
            let result = m.replace(&text, "X");
            assert_eq!(
                result,
                Some("before X after".to_string()),
                "Literal mode should replace '{name}' ({pat}) as a literal"
            );
        }
    }

    // ---------------------------------------------------------------
    // Multiple matches on same line
    // ---------------------------------------------------------------

    #[test]
    fn test_multiple_matches_same_line() {
        let op = Op::Replace {
            find: "ab".to_string(),
            replace: "X".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("ab cd ab ef ab", "X");
        assert_eq!(result, Some("X cd X ef X".to_string()));
    }

    #[test]
    fn test_replace_with_empty_string() {
        let op = Op::Replace {
            find: "remove".to_string(),
            replace: "".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("please remove this", "");
        assert_eq!(result, Some("please  this".to_string()));
    }

    #[test]
    fn test_no_match_returns_none() {
        let op = Op::Replace {
            find: "xyz".to_string(),
            replace: "abc".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.replace("nothing here", "abc").is_none());
    }

    // ---------------------------------------------------------------
    // Pathological / adversarial pattern tests
    //
    // These lock in behavior for patterns that look like they ought to
    // break something: regex metacharacters misused in literal mode,
    // empty inputs, patterns with backreference-like replacement strings,
    // and regex that would blow up a backtracking engine.
    // ---------------------------------------------------------------

    /// A literal pattern of `$1` (which would be a capture backreference in
    /// a regex replacement context) must match the literal two-character
    /// sequence in text and be replaceable without invoking capture-group
    /// semantics. Regression guard against anyone accidentally swapping
    /// `str::replace` for `Regex::replace_all` in the literal path.
    #[test]
    fn test_literal_dollar_one_pattern() {
        let op = Op::Replace {
            find: "$1".to_string(),
            replace: "REPLACED".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        assert!(m.is_match("value is $1 here"));
        let result = m.replace("value is $1 here", "REPLACED");
        assert_eq!(result, Some("value is REPLACED here".to_string()));
    }

    /// A regex pattern whose replacement string contains `$0`, `$1`, etc.
    /// should be interpreted as a capture-backreference in regex mode.
    /// This is intended behavior; locking it in so nobody accidentally
    /// escapes it.
    #[test]
    fn test_regex_backreferences_work_in_replace() {
        let op = Op::Replace {
            find: r"hello (\w+)".to_string(),
            replace: "greetings, $1!".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("hello world", "greetings, $1!");
        assert_eq!(result, Some("greetings, world!".to_string()));
    }

    /// **Adversarial**: the classic "catastrophic backtracking" pattern
    /// `(a+)+$` on a long non-matching input is O(2^n) in a naive NFA.
    /// The `regex` crate uses a DFA/bounded-time engine so this should
    /// complete effectively instantly. Lock in that we've picked a safe
    /// engine — switching to a backtracking regex crate would hang here.
    #[test]
    fn test_regex_no_catastrophic_backtracking() {
        let op = Op::Replace {
            find: r"(a+)+$".to_string(),
            replace: "X".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        // 30 'a's followed by 'b' — classic ReDoS trigger for backtracking engines.
        let mut input = "a".repeat(30);
        input.push('b');
        let start = std::time::Instant::now();
        let result = m.is_match(&input);
        let elapsed = start.elapsed();
        assert!(!result, "pattern should not match 'aaaa...b'");
        // Generous bound — should actually complete in microseconds.
        assert!(
            elapsed < std::time::Duration::from_millis(500),
            "regex took too long ({elapsed:?}) — possible ReDoS"
        );
    }

    /// **Adversarial**: the replacement string is NUL-separated or contains
    /// control characters. Must pass through unchanged (no shell-like
    /// interpretation).
    #[test]
    fn test_replacement_with_control_chars() {
        let op = Op::Replace {
            find: "placeholder".to_string(),
            replace: "\x07bell\x1bescape\x00nul".to_string(),
            regex: false,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        let result = m.replace("use placeholder here", "\x07bell\x1bescape\x00nul");
        assert_eq!(
            result,
            Some("use \x07bell\x1bescape\x00nul here".to_string())
        );
    }

    /// **Adversarial**: a regex that is a valid-but-empty-matching pattern
    /// (like `(?:)`) produces an empty match at every position. This is a
    /// weird edge case that can blow up naive replace loops. Lock in that
    /// we produce *some* deterministic output without panicking.
    #[test]
    fn test_empty_regex_match_does_not_panic() {
        let op = Op::Replace {
            find: r"(?:)".to_string(),
            replace: "X".to_string(),
            regex: true,
            case_insensitive: false,
        };
        let m = Matcher::new(&op).unwrap();
        // Must not panic — actual content of the result is implementation-defined.
        let _ = m.replace("abc", "X");
    }
}

// ---------------------------------------------------------------
// Property-based tests (proptest)
// ---------------------------------------------------------------
#[cfg(test)]
mod proptests {
    use super::*;
    use proptest::prelude::*;

    proptest! {
        /// Invariant: in literal mode, `Matcher::is_match(text)` ⟺
        /// `text.contains(pattern)`. This guards against a future optimization
        /// accidentally changing the semantics of literal matching.
        #[test]
        fn prop_literal_matches_iff_contains(
            pattern in "[a-zA-Z0-9 ]{1,10}",
            text in "[a-zA-Z0-9 ]{0,60}",
        ) {
            let op = Op::Replace {
                find: pattern.clone(),
                replace: "".into(),
                regex: false,
                case_insensitive: false,
            };
            let m = Matcher::new(&op).unwrap();
            prop_assert_eq!(m.is_match(&text), text.contains(&pattern));
        }

        /// Invariant: `replace(text, pat)` returns `None` iff `is_match(text)`
        /// is `false`. A mismatch here means we'd record a spurious "change"
        /// with no actual edit.
        #[test]
        fn prop_replace_none_iff_not_match(
            pattern in "[a-zA-Z0-9]{1,6}",
            text in "[a-zA-Z0-9]{0,40}",
            replacement in "[a-zA-Z0-9]{0,6}",
        ) {
            let op = Op::Replace {
                find: pattern.clone(),
                replace: replacement.clone(),
                regex: false,
                case_insensitive: false,
            };
            let m = Matcher::new(&op).unwrap();
            let is_match = m.is_match(&text);
            let replaced = m.replace(&text, &replacement);
            prop_assert_eq!(replaced.is_some(), is_match);
        }

        /// Invariant: replacing pattern with itself is a no-op on content
        /// (the returned String equals the input). This is a fixed-point
        /// test that catches mis-implementations of the literal replace path.
        #[test]
        fn prop_replace_with_self_is_identity(
            pattern in "[a-zA-Z0-9]{1,6}",
            text in "[a-zA-Z0-9 ]{0,50}",
        ) {
            let op = Op::Replace {
                find: pattern.clone(),
                replace: pattern.clone(),
                regex: false,
                case_insensitive: false,
            };
            let m = Matcher::new(&op).unwrap();
            if let Some(replaced) = m.replace(&text, &pattern) {
                prop_assert_eq!(replaced, text);
            }
        }

        /// Invariant: case-insensitive literal matching is symmetric —
        /// `Matcher(p, ci=true).is_match(t)` equals
        /// `Matcher(t.to_lowercase(), ci=false).is_match(p.to_lowercase())`
        /// for ASCII patterns. (Restricts to ASCII because Unicode case folding
        /// is famously asymmetric; our ASCII invariant is what callers rely on.)
        #[test]
        fn prop_case_insensitive_ascii_symmetric(
            pattern in "[a-zA-Z]{1,6}",
            text in "[a-zA-Z]{0,30}",
        ) {
            let op = Op::Replace {
                find: pattern.clone(),
                replace: String::new(),
                regex: false,
                case_insensitive: true,
            };
            let m = Matcher::new(&op).unwrap();
            let matches = m.is_match(&text);
            prop_assert_eq!(
                matches,
                text.to_ascii_lowercase().contains(&pattern.to_ascii_lowercase())
            );
        }
    }
}