use crate::eval::coercion::to_string_val;
use crate::eval::functions::check_arity;
use crate::types::Value;
pub fn encodeurl_fn(args: &[Value]) -> Value {
if let Some(err) = check_arity(args, 1, 1) {
return err;
}
let text = match to_string_val(args[0].clone()) {
Ok(s) => s,
Err(e) => return e,
};
Value::Text(percent_encode(&text))
}
fn percent_encode(s: &str) -> String {
let mut out = String::with_capacity(s.len());
for byte in s.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9'
| b'-' | b'_' | b'.' | b'~' => {
out.push(byte as char);
}
_ => {
out.push('%');
out.push(hex_digit(byte >> 4));
out.push(hex_digit(byte & 0x0f));
}
}
}
out
}
fn hex_digit(n: u8) -> char {
match n {
0..=9 => (b'0' + n) as char,
_ => (b'A' + n - 10) as char,
}
}
#[cfg(test)]
mod tests;