Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/URL-decoding/Rust/url-decoding.rust
Normal file
36
Task/URL-decoding/Rust/url-decoding.rust
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
const INPUT1: &str = "http%3A%2F%2Ffoo%20bar%2F";
|
||||
const INPUT2: &str = "google.com/search?q=%60Abdu%27l-Bah%C3%A1";
|
||||
|
||||
fn append_frag(text: &mut String, frag: &mut String) {
|
||||
if !frag.is_empty() {
|
||||
let encoded = frag.chars()
|
||||
.collect::<Vec<char>>()
|
||||
.chunks(2)
|
||||
.map(|ch| {
|
||||
u8::from_str_radix(&ch.iter().collect::<String>(), 16).unwrap()
|
||||
}).collect::<Vec<u8>>();
|
||||
text.push_str(&std::str::from_utf8(&encoded).unwrap());
|
||||
frag.clear();
|
||||
}
|
||||
}
|
||||
|
||||
fn decode(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut encoded_ch = String::new();
|
||||
let mut iter = text.chars();
|
||||
while let Some(ch) = iter.next() {
|
||||
if ch == '%' {
|
||||
encoded_ch.push_str(&format!("{}{}", iter.next().unwrap(), iter.next().unwrap()));
|
||||
} else {
|
||||
append_frag(&mut output, &mut encoded_ch);
|
||||
output.push(ch);
|
||||
}
|
||||
}
|
||||
append_frag(&mut output, &mut encoded_ch);
|
||||
output
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("{}", decode(INPUT1));
|
||||
println!("{}", decode(INPUT2));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue