dbus_server_address_parser/encode/
escape.rs1#[inline]
2fn is_optionally_escaped(c: u8) -> bool {
3 c.is_ascii_alphanumeric() || c == b'-' || c == b'_' || c == b'/' || c == b'.' || c == b'\\'
6}
7
8pub(super) fn to_hex(b: u8) -> u8 {
9 match b {
10 0 => b'0',
11 1 => b'1',
12 2 => b'2',
13 3 => b'3',
14 4 => b'4',
15 5 => b'5',
16 6 => b'6',
17 7 => b'7',
18 8 => b'8',
19 9 => b'9',
20 10 => b'a',
21 11 => b'b',
22 12 => b'c',
23 13 => b'd',
24 14 => b'e',
25 15 => b'f',
26 b => panic!("This should not happend: {}", b),
27 }
28}
29
30fn add_hex(s: &mut Vec<u8>, b: u8) {
31 s.push(b'%');
32 s.push(to_hex((b & 0b1111_0000) >> 4));
33 s.push(to_hex(b & 0b0000_1111));
34}
35
36pub(super) fn escape(path: &str) -> String {
37 let mut result = Vec::with_capacity(path.len());
38 for c in path.bytes() {
39 if is_optionally_escaped(c) {
40 result.push(c);
41 } else {
42 add_hex(&mut result, c);
43 }
44 }
45 String::from_utf8(result).unwrap()
46}