-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoding.rs
More file actions
53 lines (48 loc) · 1.39 KB
/
encoding.rs
File metadata and controls
53 lines (48 loc) · 1.39 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
/// Hand-rolled percent decoder -- handles `%XX` and `+` for spaces.
/// I didn't want to pull in a dep just for this.
pub fn percent_decode(s: &str) -> String {
let mut result = Vec::with_capacity(s.len());
let bytes = s.as_bytes();
let mut i = 0;
while i < bytes.len() {
if bytes[i] == b'%' && i + 2 < bytes.len() {
if let (Some(hi), Some(lo)) = (hex_val(bytes[i + 1]), hex_val(bytes[i + 2])) {
result.push(hi << 4 | lo);
i += 3;
continue;
}
} else if bytes[i] == b'+' {
result.push(b' ');
i += 1;
continue;
}
result.push(bytes[i]);
i += 1;
}
String::from_utf8(result).unwrap_or_else(|_| s.to_string())
}
fn hex_val(b: u8) -> Option<u8> {
match b {
b'0'..=b'9' => Some(b - b'0'),
b'a'..=b'f' => Some(b - b'a' + 10),
b'A'..=b'F' => Some(b - b'A' + 10),
_ => None,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decode_basic() {
assert_eq!(percent_decode("hello%20world"), "hello world");
assert_eq!(percent_decode("hello+world"), "hello world");
}
#[test]
fn decode_hex_cases() {
assert_eq!(percent_decode("abc%3D123%26key"), "abc=123&key");
}
#[test]
fn decode_passthrough() {
assert_eq!(percent_decode("plain"), "plain");
}
}