Skip to main content

dollar/
dollar.rs

1use onig::{Captures, Regex, Replacer};
2use std::borrow::Cow;
3
4/// A string, with `$1` refering to the first capture group.
5struct Dollarified<'a>(&'a str);
6
7/// Capture Reference to Captured String
8///
9/// Tries to convert a refernece to a capture to the captured text. If
10/// the reference isn't a valid numeric capture group then no text is
11/// returned.
12fn capture_str<'t>(caps: &'t Captures, cap_ref: &str) -> Option<&'t str> {
13    cap_ref.parse::<usize>().ok().and_then(|p| caps.at(p))
14}
15
16impl<'a> Replacer for Dollarified<'a> {
17    fn reg_replace(&mut self, caps: &Captures) -> Cow<'_, str> {
18        let mut replacement = String::new();
19        let mut pattern = self.0;
20        while !pattern.is_empty() {
21            if let Some(position) = pattern.find('$') {
22                // push up to the replacement
23                replacement.push_str(&pattern[..position]);
24                pattern = &pattern[position + 1..];
25
26                // find the end of the capture reference
27                let ref_end = pattern
28                    .find(|c| !char::is_numeric(c))
29                    .unwrap_or(pattern.len());
30
31                // push the capture from this capture reference
32                if let Some(cap) = capture_str(caps, &pattern[..ref_end]) {
33                    replacement.push_str(cap);
34                    pattern = &pattern[ref_end..];
35                } else {
36                    replacement.push('$');
37                }
38            } else {
39                // no replacements left
40                replacement.push_str(pattern);
41                break;
42            }
43        }
44        replacement.into()
45    }
46}
47
48fn test_with(replacement: &str) {
49    let re = Regex::new(r"(\w+) (\w+)").unwrap();
50    let hay = "well (hello world) to you!";
51    println!(
52        "/{}/{}/ -> {}",
53        &hay,
54        &replacement,
55        re.replace(hay, Dollarified(replacement))
56    );
57}
58
59fn main() {
60    test_with("$2 $1");
61    test_with("($2 $1)");
62    test_with("|$2|$1|");
63    test_with("|$0|$2$1");
64    test_with("$$$");
65    test_with("$$$3");
66    test_with("$$2$3");
67    test_with("Literal replacement");
68}