sedx 1.2.0

A safe, modern replacement for GNU sed with automatic backups, preview mode, and rollback
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
//! BRE to PCRE Converter
//!
//! This module provides automatic conversion from Basic Regular Expressions (BRE)
//! to Perl-Compatible Regular Expressions (PCRE), providing GNU sed compatibility.

/// Convert Basic Regular Expression (BRE) to Perl-Compatible Regular Expression (PCRE)
///
/// # Conversion Rules
///
/// - `\(` → `(` - Remove escape from opening parenthesis
/// - `\)` → `)` - Remove escape from closing parenthesis
/// - `\{` → `{` - Remove escape from opening brace
/// - `\}` → `}` - Remove escape from closing brace
/// - `\+` → `+` - Remove escape from plus quantifier
/// - `\?` → `?` - Remove escape from question mark
/// - `\|` → `|` - Remove escape from alternation
/// - `\1`..\`\9` → `$1`..`$9` - Convert backreferences to Rust regex style
/// - `\&` → `$&` - Convert match backreference
/// - `\\` → `\` - Convert double backslash to single
pub fn convert_bre_to_pcre(pattern: &str) -> String {
    let mut result = String::new();
    let mut chars = pattern.chars().peekable();
    let mut escape_next = false;

    while let Some(c) = chars.next() {
        if escape_next {
            match c {
                '(' | ')' | '{' | '}' => {
                    // BRE escaped meta-char → PCRE meta-char
                    result.push(c);
                }
                '+' | '?' | '|' => {
                    // BRE escaped quantifiers/alternation → PCRE
                    result.push(c);
                }
                '\\' => {
                    // Double backslash → single backslash
                    result.push('\\');
                }
                '1'..='9' => {
                    // Backreference: \1 → $1
                    result.push('$');
                    result.push(c);
                }
                '&' => {
                    // Match backreference: \& → $&
                    result.push('$');
                    result.push('&');
                }
                'n' if chars.peek().is_none() => {
                    // \ n at end is literal newline, not escape
                    result.push('\\');
                    result.push(c);
                }
                _ => {
                    // Unknown escape sequence, keep as-is
                    result.push('\\');
                    result.push(c);
                }
            }
            escape_next = false;
        } else if c == '\\' {
            escape_next = true;
        } else {
            result.push(c);
        }
    }

    // Handle trailing backslash
    if escape_next {
        result.push('\\');
    }

    result
}

/// Convert sed-style backreferences in replacement string to Rust regex style
///
/// # Conversion Rules
///
/// - `\1`..`\9` → `$1`..`$9` - Backreference conversion
/// - `\&` → `$&` - Match reference
/// - `\\` → `\` - Escape backslash
///
/// This is used separately from pattern conversion because replacement strings
/// have slightly different rules than patterns.
pub fn convert_sed_backreferences(replacement: &str) -> String {
    let mut result = String::new();
    let chars = replacement.chars().peekable();
    let mut escape_next = false;

    for c in chars {
        if escape_next {
            match c {
                '1'..='9' => {
                    // Backreference: \1 → $1
                    result.push('$');
                    result.push(c);
                }
                '&' => {
                    // Match backreference: \& → $&
                    result.push('$');
                    result.push('&');
                }
                '\\' => {
                    // Double backslash → single
                    result.push('\\');
                }
                'n' => {
                    // Newline escape
                    result.push('\\');
                    result.push('n');
                }
                _ => {
                    // Unknown escape, keep literal
                    result.push('\\');
                    result.push(c);
                }
            }
            escape_next = false;
        } else if c == '\\' {
            escape_next = true;
        } else {
            result.push(c);
        }
    }

    // Handle trailing backslash
    if escape_next {
        result.push('\\');
    }

    result
}

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

    /// Detect if a pattern is in Basic Regular Expression (BRE) format (test helper).
    fn is_bre_pattern(pattern: &str) -> bool {
        pattern.contains("\\(")
            || pattern.contains("\\)")
            || pattern.contains("\\{")
            || pattern.contains("\\}")
            || pattern.contains("\\+")
            || pattern.contains("\\?")
            || pattern.contains("\\|")
            || (pattern.contains("\\1")
                || pattern.contains("\\2")
                || pattern.contains("\\3")
                || pattern.contains("\\4")
                || pattern.contains("\\5")
                || pattern.contains("\\6")
                || pattern.contains("\\7")
                || pattern.contains("\\8")
                || pattern.contains("\\9"))
    }

    #[test]
    fn test_convert_parentheses() {
        assert_eq!(convert_bre_to_pcre(r#"\(foo\)"#), "(foo)");
        assert_eq!(convert_bre_to_pcre(r#"\(a\)\(b\)"#), "(a)(b)");
        assert_eq!(convert_bre_to_pcre(r#"foo\(bar\)"#), "foo(bar)");
    }

    #[test]
    fn test_convert_braces() {
        assert_eq!(convert_bre_to_pcre(r#"foo\{3\}"#), "foo{3}");
        assert_eq!(convert_bre_to_pcre(r#"\{3,5\}"#), "{3,5}");
    }

    #[test]
    fn test_convert_quantifiers() {
        assert_eq!(convert_bre_to_pcre(r#"foo\+"#), "foo+");
        assert_eq!(convert_bre_to_pcre(r#"foo\?"#), "foo?");
        assert_eq!(convert_bre_to_pcre(r#"foo\*"#), r#"foo\*"#); // \* is literal asterisk in both BRE and PCRE
    }

    #[test]
    fn test_convert_alternation() {
        assert_eq!(convert_bre_to_pcre(r#"foo\|bar"#), "foo|bar");
    }

    #[test]
    fn test_convert_backreferences() {
        assert_eq!(convert_bre_to_pcre(r#"\1"#), "$1");
        assert_eq!(convert_bre_to_pcre(r#"\2\1"#), "$2$1");
        assert_eq!(convert_bre_to_pcre(r#"\&"#), "$&");
    }

    #[test]
    fn test_convert_backslash() {
        assert_eq!(convert_bre_to_pcre(r#"\\"#), "\\");
        assert_eq!(convert_bre_to_pcre(r#"foo\\"#), "foo\\");
        assert_eq!(convert_bre_to_pcre(r#"\\\\)"#), r#"\\)"#); // \\ → \
    }

    #[test]
    fn test_no_conversion_needed() {
        assert_eq!(convert_bre_to_pcre(r#"(foo)"#), "(foo)");
        assert_eq!(convert_bre_to_pcre(r#"foo+"#), "foo+");
        assert_eq!(convert_bre_to_pcre(r#"foo|bar"#), "foo|bar");
    }

    #[test]
    fn test_is_bre_pattern() {
        assert!(is_bre_pattern(r#"\(foo\)"#));
        assert!(is_bre_pattern(r#"foo\+"#));
        assert!(is_bre_pattern(r#"foo\{3\}"#));
        assert!(is_bre_pattern(r#"foo\|bar"#));
        assert!(is_bre_pattern(r#"\1"#));

        assert!(!is_bre_pattern(r#"(foo)"#));
        assert!(!is_bre_pattern(r#"foo+"#));
        assert!(!is_bre_pattern(r#"foo|bar"#));
    }

    #[test]
    fn test_convert_sed_backreferences() {
        assert_eq!(convert_sed_backreferences(r#"\1"#), "$1");
        assert_eq!(convert_sed_backreferences(r#"\2\1"#), "$2$1");
        assert_eq!(convert_sed_backreferences(r#"\&"#), "$&");
        assert_eq!(convert_sed_backreferences(r#"\\"#), "\\");
        assert_eq!(convert_sed_backreferences(r#"\n"#), "\\n");
        assert_eq!(convert_sed_backreferences(r#"foo\1bar"#), "foo$1bar");
    }

    #[test]
    fn test_no_backreference_conversion() {
        assert_eq!(convert_sed_backreferences(r#"foo"#), "foo");
        assert_eq!(convert_sed_backreferences(r#"foo bar"#), "foo bar");
    }

    #[test]
    fn test_complex_bre_pattern() {
        // BRE: \(foo\)\(bar\) \2\1
        // PCRE: (foo)(bar) $2$1
        let bre_pattern = r#"\(foo\)\(bar\) \2\1"#;
        let pcre_pattern = convert_bre_to_pcre(bre_pattern);
        assert_eq!(pcre_pattern, r#"(foo)(bar) $2$1"#);
    }

    #[test]
    fn test_pcre_pattern_unchanged() {
        // PCRE patterns should pass through unchanged
        assert_eq!(convert_bre_to_pcre(r#"(foo|bar)+"#), r#"(foo|bar)+"#);
        assert_eq!(convert_bre_to_pcre(r#"foo{3,5}"#), r#"foo{3,5}"#);
    }

    // Additional comprehensive tests

    #[test]
    fn test_simple_patterns() {
        // Simple patterns should pass through unchanged
        assert_eq!(convert_bre_to_pcre("foo"), "foo");
        assert_eq!(convert_bre_to_pcre("bar123"), "bar123");
        assert_eq!(convert_bre_to_pcre("test_pattern"), "test_pattern");
        assert_eq!(convert_bre_to_pcre(""), "");
    }

    #[test]
    fn test_anchors() {
        // Anchors are the same in BRE and PCRE
        assert_eq!(convert_bre_to_pcre("^foo"), "^foo");
        assert_eq!(convert_bre_to_pcre("bar$"), "bar$");
        assert_eq!(convert_bre_to_pcre("^start$"), "^start$");
        assert_eq!(convert_bre_to_pcre(r#"\^foo"#), r#"\^foo"#); // Escaped anchor
    }

    #[test]
    fn test_character_classes() {
        // Character classes are the same in BRE and PCRE
        assert_eq!(convert_bre_to_pcre("[a-z]"), "[a-z]");
        assert_eq!(convert_bre_to_pcre("[A-Z0-9]"), "[A-Z0-9]");
        assert_eq!(convert_bre_to_pcre("[^abc]"), "[^abc]");
        assert_eq!(convert_bre_to_pcre("[[:alpha:]]"), "[[:alpha:]]");
        assert_eq!(convert_bre_to_pcre(r#"[a\]z]"#), r#"[a\]z]"#); // Escaped ] in char class
    }

    #[test]
    fn test_escaped_sequences() {
        // Various escape sequences
        assert_eq!(convert_bre_to_pcre(r#"\t"#), r#"\t"#); // Unknown escape, keep as-is
        assert_eq!(convert_bre_to_pcre(r#"\n"#), r#"\n"#); // Unknown escape, keep as-is
        assert_eq!(convert_bre_to_pcre(r#"\s"#), r#"\s"#); // Unknown escape, keep as-is
        assert_eq!(convert_bre_to_pcre(r#"\w"#), r#"\w"#); // Unknown escape, keep as-is
    }

    #[test]
    fn test_wildcard() {
        // Wildcard is the same in BRE and PCRE
        assert_eq!(convert_bre_to_pcre("f.o"), "f.o");
        assert_eq!(convert_bre_to_pcre(".*"), ".*");
        assert_eq!(convert_bre_to_pcre(r#"\.\*"#), r#"\.\*"#); // Escaped dot and star
    }

    #[test]
    fn test_complex_nested_patterns() {
        // Nested groups
        assert_eq!(convert_bre_to_pcre(r#"\(foo\(bar\)\)"#), "(foo(bar))");
        assert_eq!(convert_bre_to_pcre(r#"\(a\|\(b\|c\)\)"#), "(a|(b|c))");

        // Multiple groups with quantifiers
        assert_eq!(convert_bre_to_pcre(r#"\(foo\)\+"#), "(foo)+");
        assert_eq!(convert_bre_to_pcre(r#"\(bar\)\{2,5\}"#), "(bar){2,5}");

        // Complex BRE pattern: \(foo\)\{3\} \(bar\|baz\)
        assert_eq!(
            convert_bre_to_pcre(r#"\(foo\)\{3\} \(bar\|baz\)"#),
            r#"(foo){3} (bar|baz)"#
        );
    }

    #[test]
    fn test_multiple_backreferences_in_replacement() {
        // Multiple backreferences
        assert_eq!(convert_sed_backreferences(r#"\1\2\3"#), "$1$2$3");
        assert_eq!(
            convert_sed_backreferences(r#"\9\8\7\6\5\4\3\2\1"#),
            "$9$8$7$6$5$4$3$2$1"
        );

        // Backreferences with text
        assert_eq!(
            convert_sed_backreferences(r#"start\1middle\2end"#),
            "start$1middle$2end"
        );

        // Multiple consecutive same backreference
        assert_eq!(convert_sed_backreferences(r#"\1\1\1"#), "$1$1$1");
    }

    #[test]
    fn test_match_reference_in_replacement() {
        // Match reference \&
        assert_eq!(convert_sed_backreferences(r#"\&"#), "$&");
        assert_eq!(convert_sed_backreferences(r#"foo\&bar"#), "foo$&bar");
        assert_eq!(convert_sed_backreferences(r#"\&\&"#), "$&$&");
        assert_eq!(convert_sed_backreferences(r#"\1\&\2"#), "$1$&$2");
    }

    #[test]
    fn test_mixed_backreferences_and_text() {
        // Complex replacement patterns
        assert_eq!(
            convert_sed_backreferences(r#"prefix_\1_suffix"#),
            "prefix_$1_suffix"
        );
        assert_eq!(
            convert_sed_backreferences(r#"Result: \1, \2"#),
            "Result: $1, $2"
        );
        assert_eq!(convert_sed_backreferences(r#"\1:\&:\2"#), "$1:$&:$2");
    }

    #[test]
    fn test_no_backreferences_in_text() {
        // Regular text without backreferences
        assert_eq!(convert_sed_backreferences("simple text"), "simple text");
        assert_eq!(convert_sed_backreferences("1234567890"), "1234567890");
        assert_eq!(convert_sed_backreferences("!@#$%^&*()"), "!@#$%^&*()");
        assert_eq!(convert_sed_backreferences(""), "");
    }

    #[test]
    fn test_trailing_backslash_pattern() {
        // Trailing backslash should be preserved
        assert_eq!(convert_bre_to_pcre(r#"foo\"#), r#"foo\"#);
        assert_eq!(convert_bre_to_pcre(r#"\("#), r#"("#); // Just opening paren
        assert_eq!(convert_bre_to_pcre(r#"\"#), r#"\"#); // Just backslash
    }

    #[test]
    fn test_trailing_backslash_replacement() {
        // Trailing backslash in replacement
        assert_eq!(convert_sed_backreferences(r#"foo\"#), r#"foo\"#);
        assert_eq!(convert_sed_backreferences(r#"\"#), r#"\"#);
        assert_eq!(convert_sed_backreferences(r#"\1\"#), r#"$1\"#);
    }

    #[test]
    fn test_double_backslash_conversion() {
        // Double backslash to single
        assert_eq!(convert_bre_to_pcre(r#"\\"#), "\\");
        assert_eq!(convert_bre_to_pcre(r#"foo\\bar"#), "foo\\bar");
        assert_eq!(convert_bre_to_pcre(r#"\\("#), r#"\("#); // \\ then \( → \ then (

        // Triple and quadruple backslash
        assert_eq!(convert_bre_to_pcre(r#"\\\"#), r#"\\"#); // \\\" → \\
        assert_eq!(convert_bre_to_pcre(r#"\\\\"#), r#"\\"#); // \\\\ → \\
    }

    #[test]
    fn test_double_backslash_replacement() {
        // Double backslash to single in replacement
        assert_eq!(convert_sed_backreferences(r#"\\"#), "\\");
        assert_eq!(convert_sed_backreferences(r#"foo\\bar"#), "foo\\bar");
        assert_eq!(convert_sed_backreferences(r#"\1\\n"#), "$1\\n");
    }

    #[test]
    fn test_alternation_patterns() {
        // Various alternation patterns
        assert_eq!(convert_bre_to_pcre(r#"foo\|bar"#), "foo|bar");
        // Note: \baz gets \b converted (unknown escape) and then literal baz
        assert_eq!(
            convert_bre_to_pcre(r#"\(foo\|bar\|\baz\)"#),
            r#"(foo|bar|\baz)"#
        );
        assert_eq!(convert_bre_to_pcre(r#"a\|b\|c"#), "a|b|c");
        // Clean alternation with escaped bars only
        assert_eq!(convert_bre_to_pcre(r#"\(foo\|bar\)\+"#), "(foo|bar)+");
    }

    #[test]
    fn test_repetition_quantifiers() {
        // All BRE quantifiers
        assert_eq!(convert_bre_to_pcre(r#"foo\+"#), "foo+");
        assert_eq!(convert_bre_to_pcre(r#"foo\?"#), "foo?");
        assert_eq!(convert_bre_to_pcre(r#"foo\{3\}"#), "foo{3}");
        assert_eq!(convert_bre_to_pcre(r#"foo\{3,5\}"#), "foo{3,5}");
        assert_eq!(convert_bre_to_pcre(r#"foo\{3,\}"#), "foo{3,}");
        assert_eq!(convert_bre_to_pcre(r#"foo\{,5\}"#), "foo{,5}");

        // Escaped quantifiers remain escaped (literal)
        assert_eq!(convert_bre_to_pcre(r#"foo\*"#), r#"foo\*"#);
    }

    #[test]
    fn test_grouped_commands() {
        // BRE patterns commonly used with grouped commands
        assert_eq!(convert_bre_to_pcre(r#"/foo\|bar/"#), r#"/foo|bar/"#);
        // Backreferences in patterns are converted to $1 for SedX internal representation
        assert_eq!(convert_bre_to_pcre(r#"\(test\).*\1"#), r#"(test).*$1"#);
    }

    #[test]
    fn test_digit_backreferences_in_pattern() {
        // In patterns, \1-\9 convert to $1-$9
        // Note: This is for SedX's internal representation
        assert_eq!(convert_bre_to_pcre(r#"\1"#), "$1");
        assert_eq!(convert_bre_to_pcre(r#"\2"#), "$2");
        assert_eq!(convert_bre_to_pcre(r#"\9"#), "$9");

        // Digits following backslash that aren't backreferences
        assert_eq!(convert_bre_to_pcre(r#"\0"#), r#"\0"#); // \0 is not a backreference
    }

    #[test]
    fn test_special_characters_preserved() {
        // Characters that should remain unchanged
        assert_eq!(convert_bre_to_pcre(r#"."#), ".");
        assert_eq!(convert_bre_to_pcre(r#"*"#), "*");
        assert_eq!(convert_bre_to_pcre(r#"^"#), "^");
        assert_eq!(convert_bre_to_pcre(r#"$"#), "$");
        assert_eq!(convert_bre_to_pcre(r#"["#), "[");
        assert_eq!(convert_bre_to_pcre(r#"]"#), "]");
    }

    #[test]
    fn test_newline_escape_at_end() {
        // \n at end of pattern is literal newline escape
        assert_eq!(convert_bre_to_pcre(r#"foo\n"#), r#"foo\n"#);
        assert_eq!(convert_bre_to_pcre(r#"\n"#), r#"\n"#);
    }

    #[test]
    fn test_empty_groups() {
        // Empty or simple groups
        assert_eq!(convert_bre_to_pcre(r#"\(\)"#), "()");
        assert_eq!(convert_bre_to_pcre(r#"\(\+\)"#), "(+)");
    }

    #[test]
    fn test_unicode_patterns() {
        // Unicode characters should pass through
        assert_eq!(convert_bre_to_pcre("föö"), "föö");
        assert_eq!(convert_bre_to_pcre(r#"\(日本語\)"#), "(日本語)");
        assert_eq!(convert_bre_to_pcre("test_测试"), "test_测试");
    }
}