Add replace_regex function (#1393)

This commit is contained in:
miles 2022-11-08 14:47:33 +08:00 committed by GitHub
parent 0a2c2692b3
commit 7d6483b995
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 0 deletions

View File

@ -1145,6 +1145,7 @@ The executable is at: /bin/just
- `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. - `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.
- `replace(s, from, to)` - Replace all occurrences of `from` in `s` to `to`. - `replace(s, from, to)` - Replace all occurrences of `from` in `s` to `to`.
- `replace_regex(s, regex, replacement)` - Replace all occurrences of `regec` in `s` to `replacement`.
- `trim(s)` - Remove leading and trailing whitespace from `s`. - `trim(s)` - Remove leading and trailing whitespace from `s`.
- `trim_end(s)` - Remove trailing whitespace from `s`. - `trim_end(s)` - Remove trailing whitespace from `s`.
- `trim_end_match(s, pat)` - Remove suffix of `s` matching `pat`. - `trim_end_match(s, pat)` - Remove suffix of `s` matching `pat`.

View File

@ -43,6 +43,7 @@ lazy_static! {
("path_exists", Unary(path_exists)), ("path_exists", Unary(path_exists)),
("quote", Unary(quote)), ("quote", Unary(quote)),
("replace", Ternary(replace)), ("replace", Ternary(replace)),
("replace_regex", Ternary(replace_regex)),
("sha256", Unary(sha256)), ("sha256", Unary(sha256)),
("sha256_file", Unary(sha256_file)), ("sha256_file", Unary(sha256_file)),
("shoutykebabcase", Unary(shoutykebabcase)), ("shoutykebabcase", Unary(shoutykebabcase)),
@ -283,6 +284,20 @@ fn replace(_context: &FunctionContext, s: &str, from: &str, to: &str) -> Result<
Ok(s.replace(from, to)) Ok(s.replace(from, to))
} }
fn replace_regex(
_context: &FunctionContext,
s: &str,
regex: &str,
replacement: &str,
) -> Result<String, String> {
Ok(
Regex::new(regex)
.map_err(|err| err.to_string())?
.replace_all(s, replacement)
.to_string(),
)
}
fn sha256(_context: &FunctionContext, s: &str) -> Result<String, String> { fn sha256(_context: &FunctionContext, s: &str) -> Result<String, String> {
use sha2::{Digest, Sha256}; use sha2::{Digest, Sha256};
let mut hasher = Sha256::new(); let mut hasher = Sha256::new();

View File

@ -373,6 +373,34 @@ test! {
stderr: "echo foofoofoo\n", stderr: "echo foofoofoo\n",
} }
test! {
name: replace_regex,
justfile: "
foo:
echo {{ replace_regex('123bar123bar123bar', '\\d+bar', 'foo') }}
",
stdout: "foofoofoo\n",
stderr: "echo foofoofoo\n",
}
test! {
name: invalid_replace_regex,
justfile: "
foo:
echo {{ replace_regex('barbarbar', 'foo\\', 'foo') }}
",
stderr:
"error: Call to function `replace_regex` failed: regex parse error:
foo\\
^
error: incomplete escape sequence, reached end of pattern prematurely
|
2 | echo {{ replace_regex('barbarbar', 'foo\\', 'foo') }}
| ^^^^^^^^^^^^^
",
status: EXIT_FAILURE,
}
test! { test! {
name: capitalize, name: capitalize,
justfile: " justfile: "