From f8d8a333a744a2b4270e6c8be8fc7a31fa5756ff Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Wed, 1 Jan 2025 17:15:44 -0600 Subject: moo --- src/launcher/strsub.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) (limited to 'src/launcher/strsub.rs') diff --git a/src/launcher/strsub.rs b/src/launcher/strsub.rs index a50555d..cdf395b 100644 --- a/src/launcher/strsub.rs +++ b/src/launcher/strsub.rs @@ -1,6 +1,8 @@ // a cheap-o implementation of StrSubstitutor from apache commons // (does not need to support recursive evaluation or preserving escapes, it was never enabled in +use std::borrow::Cow; + const ESCAPE: char = '$'; const VAR_BEGIN: &str = "${"; const VAR_END: &str = "}"; @@ -11,9 +13,9 @@ fn prev_char(slice: &str, idx: usize) -> Option<(usize, char)> { } // basically the same thing as replace_string, but it creates the String itself and returns it. -pub fn replace_str(input: &str, sub: T) -> String +pub fn replace_str<'rep, T>(input: &str, sub: T) -> String where - T: Fn(/*key: */ &str) -> Option + T: Fn(/*key: */ &str) -> Option> { let mut input = String::from(input); replace_string(&mut input, sub); @@ -23,21 +25,19 @@ where // handles ${replacements} on this string IN-PLACE. Calls the "sub" function for each key it receives. // if "sub" returns None, it will use a default value or ignore the ${substitution}. // There are no "invalid inputs" and this function should never panic unless "sub" panics. -pub fn replace_string(input: &mut String, sub: T) +pub fn replace_string<'rep, T>(input: &mut String, sub: T) where - T: Fn(/*key: */ &str) -> Option + T: Fn(/*key: */ &str) -> Option> { let mut cursor = input.len(); while let Some(idx) = input[..cursor].rfind(VAR_BEGIN) { // note: for some reason, apache processes escapes BEFORE checking if it's even a valid // replacement expression. strange behavior IMO. - if let Some((pidx, pc)) = prev_char(input.as_ref(), idx) { - if pc == ESCAPE { - // this "replacement" is escaped. remove the escape marker and continue. - input.remove(pidx); - cursor = pidx; - continue; - } + if let Some((pidx, ESCAPE)) = prev_char(input.as_ref(), idx) { + // this "replacement" is escaped. remove the escape marker and continue. + input.remove(pidx); + cursor = pidx; + continue; } let Some(endidx) = input[idx..cursor].find(VAR_END).map(|v| v + idx) else { @@ -58,7 +58,7 @@ where def_opt = None; } - if let Some(sub_val) = sub(name).map_or_else(|| def_opt.map(|d| d.to_owned()), |v| Some(v)) { + if let Some(sub_val) = sub(name).map_or_else(|| def_opt.map(|d| Cow::Owned(d.to_owned())), |v| Some(v)) { input.replace_range(idx..(endidx + VAR_END.len()), sub_val.as_ref()); } @@ -70,11 +70,11 @@ where mod tests { use super::*; - fn replace_fun(key: &str) -> Option { + fn replace_fun(key: &str) -> Option> { match key { - "exists" => Some("value123".into()), + "exists" => Some(Cow::Borrowed("value123")), "empty" => None, - "borger" => Some("\u{1f354}".into()), + "borger" => Some(Cow::Borrowed("\u{1f354}")), _ => panic!("replace_fun called with unexpected key: {}", key) } } -- cgit v1.2.3-70-g09d2