use std::borrow::Cow;
use super::AsciiSet;
const DIGITS : &[ u8; 16 ] = b"0123456789ABCDEF";
pub const ENCODE_DEFAULT_CHARS : AsciiSet = AsciiSet::from(";/?:@&=+$,-_.!~*'()#");
pub const ENCODE_COMPONENT_CHARS : AsciiSet = AsciiSet::from("-_.!~*'()");
pub fn encode(string: &str, exclude: AsciiSet, keep_escaped: bool) -> Cow<str> {
let exclude = exclude.add_alphanumeric();
let mut result = Vec::new();
let bytes = string.as_bytes();
let len = bytes.len();
let mut i = 0;
while i < len {
let byte = bytes[i];
let should_encode = byte >= 0x80 || !exclude.has(byte);
#[allow(clippy::collapsible_if)]
if keep_escaped && byte == b'%' && i + 2 < len {
if bytes[i + 1].is_ascii_hexdigit() && bytes[i + 2].is_ascii_hexdigit() {
result.push(bytes[i]);
result.push(bytes[i + 1]);
result.push(bytes[i + 2]);
i += 3;
continue;
}
}
if should_encode {
result.push(b'%');
result.push(DIGITS[(byte >> 4) as usize]);
result.push(DIGITS[(byte & 0xF) as usize]);
} else {
result.push(byte);
}
i += 1;
}
Cow::Owned(String::from_utf8(result).unwrap())
}
#[cfg(test)]
mod tests {
use super::encode;
use super::AsciiSet;
const SET : AsciiSet = AsciiSet::from(";/?:@&=+$,-_.!~*'()#");
#[test]
fn should_encode_percent() {
assert_eq!(encode("%%%", SET, true), "%25%25%25");
}
#[test]
fn should_encode_control_chars() {
assert_eq!(encode("\r\n", SET, true), "%0D%0A");
}
#[test]
fn should_not_encode_parts_of_an_url() {
assert_eq!(encode("?#", SET, true), "?#");
}
#[test]
fn should_encode_square_brackets_commonmark_tests() {
assert_eq!(encode("[]^", SET, true), "%5B%5D%5E");
}
#[test]
fn should_encode_spaces() {
assert_eq!(encode("my url", SET, true), "my%20url");
}
#[test]
fn should_encode_unicode() {
assert_eq!(encode("φου", SET, true), "%CF%86%CE%BF%CF%85");
}
#[test]
fn should_encode_percent_if_it_doesnt_start_a_valid_escape_seq() {
assert_eq!(encode("%FG", SET, true), "%25FG");
}
#[test]
fn should_preserve_non_utf8_encoded_characters() {
assert_eq!(encode("%00%FF", SET, true), "%00%FF");
}
#[test]
fn should_encode_characters_on_the_cache_borders() {
assert_eq!(encode("\x00\x7F\u{80}", SET, true), "%00%7F%C2%80");
}
#[test]
fn arguments_encode_string_unescapedset() {
assert_eq!(encode("!@#$", AsciiSet::from("@$"), true), "%21@%23$");
}
#[test]
fn arguments_keepescaped_true() {
assert_eq!(encode("%20%2G", SET, true), "%20%252G");
}
#[test]
fn arguments_keepescaped_false() {
assert_eq!(encode("%20%2G", SET, false), "%2520%252G");
}
}