Skip to content

Commit

Permalink
support addprefix()
Browse files Browse the repository at this point in the history
addprefix(prefix, base) takes a `prefix` and a (possibly space-separated) `base` string,
and append `prefix` to the beginning of each word in the base string;
for example, addprefix('src', 'foo bar baz') -> 'src/foo src/bar src/baz'
  • Loading branch information
gyreas committed May 16, 2024
1 parent 46be451 commit 627543f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
let function = match name {
"absolute_path" => Unary(absolute_path),
"arch" => Nullary(arch),
"addprefix" => Binary(addprefix),
"blake3" => Unary(blake3),
"blake3_file" => Unary(blake3_file),
"canonicalize" => Unary(canonicalize),
Expand Down Expand Up @@ -255,6 +256,16 @@ fn invocation_directory_native(context: &FunctionContext) -> Result<String, Stri
})
}

fn addprefix(_context: &FunctionContext, pref: &str, base: &str) -> Result<String, String> {
Ok(
base
.split(" ")
.map(|s| format!("{pref}{s}"))
.collect::<Vec<String>>()
.join(" "),
)
}

fn join(
_context: &FunctionContext,
base: &str,
Expand Down
14 changes: 14 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,20 @@ fn trim_end() {
assert_eval_eq("trim_end(' f ')", " f");
}

#[test]
fn addprefix() {
assert_eval_eq("addprefix('8', 'r s t')", "8r 8s 8t");
assert_eval_eq(
"addprefix('src/', 'main sar x11')",
"src/main src/sar src/x11",
);
assert_eval_eq("addprefix('-', 'c v h y')", "-c -v -h -y");
assert_eval_eq(
"addprefix('0000', '11 10 01 00')",
"000011 000010 000001 000000",
);
}

#[test]
#[cfg(not(windows))]
fn join() {
Expand Down

0 comments on commit 627543f

Please sign in to comment.