summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar bigfoot547 <[email protected]>2025-01-01 17:15:44 -0600
committerLibravatar bigfoot547 <[email protected]>2025-01-01 17:16:31 -0600
commitf8d8a333a744a2b4270e6c8be8fc7a31fa5756ff (patch)
treedec7154a152757a56837e3507f55809ec36ff6ec /src
parentmake prev_char more concise (diff)
moo
Diffstat (limited to 'src')
-rw-r--r--src/launcher/strsub.rs30
1 files changed, 15 insertions, 15 deletions
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<T>(input: &str, sub: T) -> String
+pub fn replace_str<'rep, T>(input: &str, sub: T) -> String
where
- T: Fn(/*key: */ &str) -> Option<String>
+ T: Fn(/*key: */ &str) -> Option<Cow<'rep, str>>
{
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<T>(input: &mut String, sub: T)
+pub fn replace_string<'rep, T>(input: &mut String, sub: T)
where
- T: Fn(/*key: */ &str) -> Option<String>
+ T: Fn(/*key: */ &str) -> Option<Cow<'rep, str>>
{
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<String> {
+ fn replace_fun(key: &str) -> Option<Cow<'static, str>> {
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)
}
}