Skip to content

Commit

Permalink
Add encode_uri_component function (#2052)
Browse files Browse the repository at this point in the history
  • Loading branch information
laniakea64 authored May 19, 2024
1 parent 7fa6ed8 commit 4961f49
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 24 deletions.
55 changes: 31 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ lexiclean = "0.0.1"
libc = "0.2.0"
log = "0.4.4"
num_cpus = "1.15.0"
percent-encoding = "2.3.1"
rand = "0.8.5"
regex = "1.10.4"
semver = "1.0.20"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1427,6 +1427,9 @@ The process ID is: 420
- `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'`
- `encode_uri_component(s)`<sup>master</sup> - Percent-encode characters in `s`
except `[A-Za-z0-9_.!~*'()-]`, matching the behavior of the
[JavaScript `encodeURIComponent` function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent).
- `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
15 changes: 15 additions & 0 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub(crate) fn get(name: &str) -> Option<Function> {
"config_local_directory" => Nullary(|_| dir("local config", dirs::config_local_dir)),
"data_directory" => Nullary(|_| dir("data", dirs::data_dir)),
"data_local_directory" => Nullary(|_| dir("local data", dirs::data_local_dir)),
"encode_uri_component" => Unary(encode_uri_component),
"env" => UnaryOpt(env),
"env_var" => Unary(env_var),
"env_var_or_default" => Binary(env_var_or_default),
Expand Down Expand Up @@ -204,6 +205,20 @@ fn dir(name: &'static str, f: fn() -> Option<PathBuf>) -> Result<String, String>
}
}

fn encode_uri_component(_evaluator: &Evaluator, s: &str) -> Result<String, String> {
static PERCENT_ENCODE: percent_encoding::AsciiSet = percent_encoding::NON_ALPHANUMERIC
.remove(b'-')
.remove(b'_')
.remove(b'.')
.remove(b'!')
.remove(b'~')
.remove(b'*')
.remove(b'\'')
.remove(b'(')
.remove(b')');
Ok(percent_encoding::utf8_percent_encode(s, &PERCENT_ENCODE).to_string())
}

fn env_var(evaluator: &Evaluator, key: &str) -> Result<String, String> {
use std::env::VarError::*;

Expand Down
9 changes: 9 additions & 0 deletions tests/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -793,3 +793,12 @@ fn canonicalize() {
.stdout_regex(".*/justfile")
.run();
}

#[test]
fn encode_uri_component() {
Test::new()
.justfile("x := encode_uri_component(\"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~ \\t\\r\\n🌐\")")
.args(["--evaluate", "x"])
.stdout("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!%22%23%24%25%26'()*%2B%2C-.%2F%3A%3B%3C%3D%3E%3F%40%5B%5C%5D%5E_%60%7B%7C%7D~%20%09%0D%0A%F0%9F%8C%90")
.run();
}

0 comments on commit 4961f49

Please sign in to comment.