#![allow(unused)]
pub(crate) fn to_hex(n: u8) -> u8 {
(n % 16) + 48 + if n >= 10 { 7 } else { 0 }
}
use std::str;
pub(crate) fn uri_decode(uri: &str) -> String {
let mut buf: Vec<u8> = Vec::with_capacity(uri.as_bytes().len());
let mut bytesiter = uri.as_bytes().iter();
loop {
if let Some(&byte) = bytesiter.next() {
if byte == b'%' {
let b1 = bytesiter.next();
let b2 = bytesiter.next();
if let (Some(&hi), Some(&lo)) = (b1, b2) {
if let Some(byte) = str::from_utf8(&[hi, lo]).ok().and_then(|hex| u8::from_str_radix(hex, 16).ok()) {
buf.push(byte);
}
} else { break; }
}
else { buf.push(byte); }
}
else { break; }
}
String::from_utf8_lossy(&buf).to_string()
}
pub(crate) fn uri_encode(original: &str) -> String {
let mut buf: Vec<u8> = Vec::with_capacity(original.as_bytes().len() * 3);
for &byte in original.as_bytes() {
if byte.is_ascii_alphanumeric() || b"-._~".contains(&byte) {
buf.push(byte);
}
else {
buf.push(b'%');
buf.push(to_hex(byte));
buf.push(to_hex(byte >> 4))
}
}
String::from_utf8_lossy(&buf).to_string()
}