From 21d9018175e1c33932b9587982a636064a3b9df7 Mon Sep 17 00:00:00 2001 From: Guillaume Pasquet Date: Wed, 6 Apr 2022 21:11:26 +0100 Subject: Fix #30 - Greedy placeholders --- src/executor.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/executor.rs b/src/executor.rs index b296c65..14f0927 100644 --- a/src/executor.rs +++ b/src/executor.rs @@ -33,7 +33,7 @@ impl Context { pub fn substitute(&self, string: &str) -> String { let mut buffer = string.to_string(); - let re = Regex::new(r"\{(?P[A-Za-z0-9_]+)(?::-(?P.*))?\}").unwrap(); + let re = Regex::new(r"\{(?P[A-Za-z0-9_]+)(?::-(?P.*?))?\}").unwrap(); let empty_string = String::from(""); for var in re.captures_iter(string) { @@ -253,6 +253,18 @@ mod tests_context { ); } + #[test] + fn test_context_substitute_multi_value_with_default() { + let vars: Vec<(String, String)> = vec![(String::from("chewie"), String::from("han"))]; + let ctx = Context::new(vars.into_iter()); + assert_eq!( + ctx.substitute(&String::from( + "blah blah {chewie:-wookie} {c3po:-r2d2} blah" + )), + String::from("blah blah han r2d2 blah") + ); + } + #[test] fn test_context_substitute_empty_default() { let vars: Vec<(String, String)> = vec![]; -- cgit v1.2.3