macro_toolset/string/
urlencoding.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//! URL Encoded string

use super::{StringExtT, StringT};
use crate::wrapper;

#[macro_export]
/// See [`Encode`] or [`Decode`] for more information.
///
/// # Example
///
/// ```
/// # use macro_toolset::{urlencoding_str, string::StringExtT};
/// let data = urlencoding_str!(E: "你好, 世界").to_string_ext();
/// assert_eq!(data, "%E4%BD%A0%E5%A5%BD%2C%20%E4%B8%96%E7%95%8C");
/// let data = urlencoding_str!(D: "%E4%BD%A0%E5%A5%BD%2C%20%E4%B8%96%E7%95%8C").to_string_ext();
/// assert_eq!(data, "你好, 世界");
/// ```
macro_rules! urlencoding_str {
    (E: $data:expr) => {
        $crate::string::urlencoding::Encode { inner: $data }
    };
    (D: $data:expr) => {
        $crate::string::urlencoding::Decode { inner: $data }
    };
}

wrapper! {
    #[derive(Debug)]
    /// `string` which is to be encoded.
    pub Encode<T>(pub T)
}

impl<T> StringT for Encode<T>
where
    T: StringT,
{
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        let mut buf = Vec::with_capacity(64);
        self.inner.encode_to_buf(&mut buf);

        string.reserve_exact(buf.len() | 15);
        buf.into_iter().for_each(|byte| {
            if matches!(byte, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' |  b'-' | b'.' | b'_' | b'~')
            {
                string.push(byte);
            } else {
                string.extend([b'%', to_hex_digit(byte >> 4), to_hex_digit(byte & 15)]);
            }
        });
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        self.encode_to_buf(string);
        string.extend(separator.as_bytes());
    }

    #[inline]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        let mut buf = Vec::with_capacity(64);
        self.inner.encode_to_buf(&mut buf);

        string.reserve(buf.len() | 15);
        buf.into_iter().for_each(|byte| {
            if matches!(byte, b'0'..=b'9' | b'A'..=b'Z' | b'a'..=b'z' |  b'-' | b'.' | b'_' | b'~')
            {
                string.extend(Some(byte));
            } else {
                string.extend([b'%', to_hex_digit(byte >> 4), to_hex_digit(byte & 15)]);
            }
        });
    }

    #[inline]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        self.encode_to_bytes_buf(string);
        string.extend(separator.as_bytes());
    }
}

impl<T> StringExtT for Encode<T> where T: StringT {}

#[inline(always)]
const fn to_hex_digit(digit: u8) -> u8 {
    match digit {
        0..=9 => b'0' + digit,
        10..=255 => b'A' - 10 + digit,
    }
}

wrapper! {
    #[derive(Debug)]
    /// `string` which is to be decoded.
    pub Decode<T>(pub T)
}

impl<T> StringT for Decode<T>
where
    T: AsRef<str>,
{
    #[inline]
    fn encode_to_buf(self, string: &mut Vec<u8>) {
        let encoded_bytes = self.inner.as_ref().as_bytes();

        let mut encoded_bytes_iter = encoded_bytes.split(|&c| c == b'%');
        if let Some(non_escaped_part) = encoded_bytes_iter.next() {
            string.extend(non_escaped_part);
        } else {
            return;
        }
        for escaped_part in encoded_bytes_iter {
            let decoded = escaped_part.get(0..=1).and_then(|escaped_part| {
                Some((
                    from_hex_digit(escaped_part[0])?,
                    from_hex_digit(escaped_part[1])?,
                ))
            });
            if let Some(decoded) = decoded {
                string.push((decoded.0 << 4) | decoded.1);
                if let Some(non_escaped_part) = escaped_part.get(2..) {
                    string.extend(non_escaped_part);
                }
            } else {
                // Error, keep it.
                string.extend(b"%");
                string.extend(escaped_part);
            }
        }
    }

    #[inline]
    fn encode_to_buf_with_separator(self, string: &mut Vec<u8>, separator: &str) {
        self.encode_to_buf(string);
        string.extend(separator.as_bytes());
    }

    #[inline]
    fn encode_to_bytes_buf(self, string: &mut bytes::BytesMut) {
        let encoded_bytes = self.inner.as_ref().as_bytes();

        let mut encoded_bytes_iter = encoded_bytes.split(|&c| c == b'%');
        if let Some(non_escaped_part) = encoded_bytes_iter.next() {
            string.extend(non_escaped_part);
        } else {
            return;
        }
        for escaped_part in encoded_bytes_iter {
            let decoded = escaped_part.get(0..=1).and_then(|escaped_part| {
                Some((
                    from_hex_digit(escaped_part[0])?,
                    from_hex_digit(escaped_part[1])?,
                ))
            });
            if let Some(decoded) = decoded {
                string.extend(Some((decoded.0 << 4) | decoded.1));
                if let Some(non_escaped_part) = escaped_part.get(2..) {
                    string.extend(non_escaped_part);
                }
            } else {
                // Error, keep it.
                string.extend(b"%");
                string.extend(escaped_part);
            }
        }
    }

    #[inline]
    fn encode_to_bytes_buf_with_separator(self, string: &mut bytes::BytesMut, separator: &str) {
        self.encode_to_bytes_buf(string);
        string.extend(separator.as_bytes());
    }
}

impl<T> StringExtT for Decode<T> where T: AsRef<str> {}

#[inline(always)]
const fn from_hex_digit(digit: u8) -> Option<u8> {
    match digit {
        b'0'..=b'9' => Some(digit - b'0'),
        b'A'..=b'F' => Some(digit - b'A' + 10),
        b'a'..=b'f' => Some(digit - b'a' + 10),
        _ => None,
    }
}