use alloc::string::String;
use alloc::string::ToString;
use alloc::vec::Vec;
pub(crate) fn percent_decode(input: &str) -> String {
let bytes = input.as_bytes();
let mut out = Vec::with_capacity(bytes.len());
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
if let (Some(hi), Some(lo)) = (hex_val(bytes[i + 1]), hex_val(bytes[i + 2])) {
out.push((hi << 4) | lo);
i += 3;
continue;
}
}
out.push(bytes[i]);
i += 1;
}
String::from_utf8(out).unwrap_or_else(|_| input.to_string())
}
fn hex_val(c: u8) -> Option<u8> {
match c {
b'0'..=b'9' => Some(c - b'0'),
b'A'..=b'F' => Some(c - b'A' + 10),
b'a'..=b'f' => Some(c - b'a' + 10),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_plain() {
assert_eq!(percent_decode("hello"), "hello");
}
#[test]
fn decode_percent() {
assert_eq!(percent_decode("hello%20world"), "hello world");
assert_eq!(percent_decode("%E4%B8%AD%E6%96%87"), "中文");
}
#[test]
fn decode_plus_literal() {
assert_eq!(percent_decode("a+b"), "a+b");
}
#[test]
fn decode_lenient_on_invalid() {
assert_eq!(percent_decode("%ZZ"), "%ZZ");
assert_eq!(percent_decode("%"), "%");
}
}