-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathurl.rs
41 lines (36 loc) · 1.08 KB
/
url.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
use crate::error::Result;
use std::borrow::Cow;
use url_dep::form_urlencoded::byte_serialize;
byond_fn!(fn url_encode(data) {
Some(encode(data))
});
byond_fn!(fn url_decode(data) {
decode(data).ok()
});
fn encode(string: &str) -> String {
byte_serialize(string.as_bytes()).collect()
}
fn decode(string: &str) -> Result<String> {
let replaced = replace_plus(string.as_bytes());
// into_owned() is not strictly necessary here, but saves some refactoring work.
Ok(percent_encoding::percent_decode(&replaced)
.decode_utf8_lossy()
.into_owned())
}
// From `url` crate.
/// Replace b'+' with b' '
fn replace_plus(input: &[u8]) -> Cow<[u8]> {
match input.iter().position(|&b| b == b'+') {
None => Cow::Borrowed(input),
Some(first_position) => {
let mut replaced = input.to_owned();
replaced[first_position] = b' ';
for byte in &mut replaced[first_position + 1..] {
if *byte == b'+' {
*byte = b' ';
}
}
Cow::Owned(replaced)
}
}
}