Skip to content

Commit

Permalink
Add prepend() function (casey#2045)
Browse files Browse the repository at this point in the history
  • Loading branch information
gyreas authored and neunenak committed May 18, 2024
1 parent 6902d93 commit 4f18aab
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,12 @@ The process ID is: 420


#### String Manipulation

- `append(suffix, s)`<sup>master</sup> Append `suffix` to whitespace-separated
strings in `s`.
strings in `s`. `append('/src', 'foo bar baz')``'foo/src bar/src baz/src'`
- `prepend(prefix, s)`<sup>master</sup> Prepend `prefix` to
whitespace-separated strings in `s`. `prepend('src/', 'foo bar baz')`
`'src/foo src/bar src/baz'`
- `quote(s)` - Replace all single quotes with `'\''` and prepend and append
single quotes to `s`. This is sufficient to escape special characters for
many shells, including most Bourne shell descendants.
Expand Down
12 changes: 11 additions & 1 deletion src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"arch" => Nullary(arch),
"blake3" => Unary(blake3),
"blake3_file" => Unary(blake3_file),
"canonicalize" => Unary(canonicalize),
"cache_directory" => Nullary(|_| dir("cache", dirs::cache_dir)),
"canonicalize" => Unary(canonicalize),
"capitalize" => Unary(capitalize),
"clean" => Unary(clean),
"config_directory" => Nullary(|_| dir("config", dirs::config_dir)),
Expand Down Expand Up @@ -56,6 +56,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"os_family" => Nullary(os_family),
"parent_directory" => Unary(parent_directory),
"path_exists" => Unary(path_exists),
"prepend" => Binary(prepend),
"quote" => Unary(quote),
"replace" => Ternary(replace),
"replace_regex" => Ternary(replace_regex),
Expand Down Expand Up @@ -265,6 +266,15 @@ fn invocation_directory_native(context: &FunctionContext) -> Result<String, Stri
})
}

fn prepend(_context: &FunctionContext, prefix: &str, s: &str) -> Result<String, String> {
Ok(
s.split_whitespace()
.map(|s| format!("{prefix}{s}"))
.collect::<Vec<String>>()
.join(" "),
)
}

fn join(
_context: &FunctionContext,
base: &str,
Expand Down
22 changes: 22 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,28 @@ fn append() {
);
}

#[test]
fn prepend() {
assert_eval_eq("prepend('8', 'r s t\n \n ')", "8r 8s 8t");
assert_eval_eq(
"prepend('src/', 'main sar x11')",
"src/main src/sar src/x11",
);
assert_eval_eq("prepend('-', 'c\tv h\ny')", "-c -v -h -y");
assert_eval_eq(
"prepend('0000', '11 10 01 00')",
"000011 000010 000001 000000",
);
assert_eval_eq(
"prepend('April-', '
1st,
17th,
20th,
')",
"April-1st, April-17th, April-20th,",
);
}

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

0 comments on commit 4f18aab

Please sign in to comment.