1pub fn decode(url: String) -> String {
2 let mut decoded = String::from("");
3 let mut skip = 0;
4 for i in 0..url.len() {
5 if skip != 0 {
6 skip -= 1;
7 continue;
8 }
9 let c: char = url.chars().nth(i).unwrap();
10 if c == '%' {
11 let left = url.chars().nth(i + 1).unwrap();
12 let right = url.chars().nth(i + 2).unwrap();
13 let byte = u8::from_str_radix(&format!("{}{}", left, right), 16).unwrap();
14 decoded += &(byte as char).to_string();
15 skip = 2;
16 } else {
17 decoded += &c.to_string();
18 }
19 }
20 decoded
21}
22
23#[cfg(test)]
24mod tests {
25 #[test]
26 fn it_works() {
27 assert_eq!("https://github.com", crate::decode(String::from("https%3A%2F%2Fgithub.com")));
28 }
29}