pub(super) fn escape_text(text: &str) -> String {
let mut out = String::with_capacity(text.len());
for symbol in text.chars() {
match symbol {
'&' => out.push_str("&"),
'<' => out.push_str("<"),
'>' => out.push_str(">"),
'"' => out.push_str("""),
'\'' => out.push_str("'"),
other => out.push(other),
}
}
out
}
pub(super) fn encode_segment(name: &str) -> String {
let mut out = String::with_capacity(name.len());
for byte in name.bytes() {
match byte {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
out.push(byte as char);
}
b' ' => out.push_str("%20"),
other => {
out.push('%');
out.push(hex_digit(other >> 4));
out.push(hex_digit(other & 0x0F));
}
}
}
out
}
fn hex_digit(value: u8) -> char {
match value {
0..=9 => char::from(b'0' + value),
10..=15 => char::from(b'A' + value - 10),
_ => '0',
}
}