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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
// Ref: https://url.spec.whatwg.org/

use std::borrow::Cow;
use std::io::{self, Write};
use std::str::from_utf8_unchecked;

use crate::percent_encoding::{utf8_percent_encode, AsciiSet};

/// The C0 control percent-encode set are the C0 controls and U+007F (DEL).
pub use percent_encoding::CONTROLS;

/// Not an ASCII letter or digit.
pub use percent_encoding::NON_ALPHANUMERIC;

/// The fragment percent-encode set is the C0 control percent-encode set and U+0020 SPACE, U+0022 ("), U+003C (<), U+003E (>), and U+0060 (`).
pub const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');

/// The query percent-encode set is the C0 control percent-encode set and U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), and U+003E (>).
///
/// The query percent-encode set cannot be defined in terms of the fragment percent-encode set due to the omission of U+0060 (`).
pub const QUERY: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'#').add(b'<').add(b'>');

/// The special-query percent-encode set is the query percent-encode set and U+0027 (').
pub const SPECIAL_QUERY: &AsciiSet = &QUERY.add(b'\'');

/// The path percent-encode set is the query percent-encode set and U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).
pub const PATH: &AsciiSet = &QUERY.add(b'?').add(b'`').add(b'{').add(b'}');

/// The userinfo percent-encode set is the path percent-encode set and U+002F (/), U+003A (:), U+003B (;), U+003D (=), U+0040 (@), U+005B ([) to U+005E (^), inclusive, and U+007C (|).
pub const USERINFO: &AsciiSet = &PATH
    .add(b'/')
    .add(b':')
    .add(b';')
    .add(b'=')
    .add(b'@')
    .add(b'[')
    .add(b'\\')
    .add(b']')
    .add(b'^')
    .add(b'|');

/// The component percent-encode set is the userinfo percent-encode set and U+0024 ($) to U+0026 (&), inclusive, U+002B (+), and U+002C (,).
pub const COMPONENT: &AsciiSet = &USERINFO.add(b'$').add(b'%').add(b'&').add(b'+').add(b',');

/// The application/x-www-form-urlencoded percent-encode set is the component percent-encode set and U+0021 (!), U+0027 (') to U+0029 RIGHT PARENTHESIS, inclusive, and U+007E (~).
pub const X_WWW_FORM_URLENCODED: &AsciiSet =
    &COMPONENT.add(b'!').add(b'\'').add(b'(').add(b')').add(b'~');

/// Encode text.
#[inline]
pub fn encode<'a, S: ?Sized + AsRef<str>>(
    text: &'a S,
    ascii_set: &'static AsciiSet,
) -> Cow<'a, str> {
    Cow::from(utf8_percent_encode(text.as_ref(), ascii_set))
}

/// Write text to a mutable `String` reference and return the encoded string slice.
#[inline]
pub fn encode_to_string<'a, S: AsRef<str>>(
    text: S,
    ascii_set: &'static AsciiSet,
    output: &'a mut String,
) -> &'a str {
    unsafe { from_utf8_unchecked(encode_to_vec(text, ascii_set, output.as_mut_vec())) }
}

/// Write text to a mutable `Vec<u8>` reference and return the encoded data slice.
pub fn encode_to_vec<'a, S: AsRef<str>>(
    text: S,
    ascii_set: &'static AsciiSet,
    output: &'a mut Vec<u8>,
) -> &'a [u8] {
    let text = text.as_ref();
    let text_bytes = text.as_bytes();
    let text_length = text_bytes.len();

    output.reserve(text_length);

    let current_length = output.len();

    let pe = utf8_percent_encode(text, ascii_set);

    output.extend(pe.flat_map(|e| e.bytes()));

    &output[current_length..]
}

/// Write text to a writer.
#[inline]
pub fn encode_to_writer<S: AsRef<str>, W: Write>(
    text: S,
    ascii_set: &'static AsciiSet,
    output: &mut W,
) -> Result<(), io::Error> {
    let pe = utf8_percent_encode(text.as_ref(), ascii_set);

    for s in pe {
        output.write_all(s.as_bytes())?;
    }

    Ok(())
}

macro_rules! encode_impl {
    ($(#[$attr: meta])* $escape_set:ident; $(#[$encode_attr: meta])* $encode_name: ident; $(#[$encode_to_string_attr: meta])* $encode_to_string_name: ident; $(#[$encode_to_vec_attr: meta])* $encode_to_vec_name: ident; $(#[$encode_to_writer_attr: meta])* $encode_to_writer_name: ident $(;)*) => {
        $(#[$encode_attr])*
        ///
        $(#[$attr])*
        #[inline]
        pub fn $encode_name<S: ?Sized + AsRef<str>>(text: &S) -> Cow<str> {
            encode(text, $escape_set)
        }

        $(#[$encode_to_string_attr])*
        ///
        $(#[$attr])*
        #[inline]
        pub fn $encode_to_string_name<S: AsRef<str>>(text: S, output: &mut String) -> &str {
            encode_to_string(text, $escape_set, output)
        }

        $(#[$encode_to_vec_attr])*
        ///
        $(#[$attr])*
        #[inline]
        pub fn $encode_to_vec_name<S: AsRef<str>>(text: S, output: &mut Vec<u8>) -> &[u8] {
            encode_to_vec(text, $escape_set, output)
        }

        $(#[$encode_to_writer_attr])*
        ///
        $(#[$attr])*
        #[inline]
        pub fn $encode_to_writer_name<S: AsRef<str>, W: Write>(text: S, output: &mut W) -> Result<(), io::Error> {
            encode_to_writer(text, $escape_set, output)
        }
    };
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `<`
    /// * `>`
    /// * <code>&#096;</code>
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    FRAGMENT;
    /// Encode text used in a fragment part.
    encode_fragment;
    /// Write text used in a fragment part to a mutable `String` reference and return the encoded string slice.
    encode_fragment_to_string;
    /// Write text used in a fragment part to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_fragment_to_vec;
    /// Write text used in a fragment part to a writer.
    encode_fragment_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `#`
    /// * `<`
    /// * `>`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    QUERY;
    /// Encode text used in the query part.
    encode_query;
    /// Write text used in the query part to a mutable `String` reference and return the encoded string slice.
    encode_query_to_string;
    /// Write text used in the query part to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_query_to_vec;
    /// Write text used in the query part to a writer.
    encode_query_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `#`
    /// * `'`
    /// * `<`
    /// * `>`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    ///
    /// The term "special" means whether a URL is special. A URL is special is the scheme of that URL is **ftp**, **file** , **http**, **https**, **ws**, or **wss**.
    SPECIAL_QUERY;
    /// Encode text used in the query part.
    encode_special_query;
    /// Write text used in the query part to a mutable `String` reference and return the encoded string slice.
    encode_special_query_to_string;
    /// Write text used in the query part to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_special_query_to_vec;
    /// Write text used in the query part to a writer.
    encode_special_query_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `#`
    /// * `<`
    /// * `>`
    /// * `?`
    /// * <code>&#096;</code>
    /// * `{`
    /// * `}`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    PATH;
    /// Encode text used in the path part.
    encode_path;
    /// Write text used in the path part to a mutable `String` reference and return the encoded string slice.
    encode_path_to_string;
    /// Write text used in the path part to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_path_to_vec;
    /// Write text used in the path part to a writer.
    encode_path_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `#`
    /// * `/`
    /// * `:`
    /// * `;`
    /// * `<`
    /// * `=`
    /// * `>`
    /// * `?`
    /// * `@`
    /// * `[`
    /// * `\`
    /// * `]`
    /// * `^`
    /// * <code>&#096;</code>
    /// * `{`
    /// * `}`
    /// * `|`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    USERINFO;
    /// Encode text used in the userinfo part.
    encode_userinfo;
    /// Write text used in the userinfo part to a mutable `String` reference and return the encoded string slice.
    encode_userinfo_to_string;
    /// Write text used in the userinfo part to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_userinfo_to_vec;
    /// Write text used in the userinfo part to a writer.
    encode_userinfo_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `"`
    /// * `#`
    /// * `$`
    /// * `%`
    /// * `&`
    /// * `+`
    /// * `,`
    /// * `/`
    /// * `:`
    /// * `;`
    /// * `<`
    /// * `=`
    /// * `>`
    /// * `?`
    /// * `@`
    /// * `[`
    /// * `\`
    /// * `]`
    /// * `^`
    /// * <code>&#096;</code>
    /// * `{`
    /// * `}`
    /// * `|`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    ///
    /// It gives identical results to JavaScript's `encodeURIComponent()`.
    COMPONENT;
    /// Encode text used in a component.
    encode_component;
    /// Write text used in a component to a mutable `String` reference and return the encoded string slice.
    encode_component_to_string;
    /// Write text used in a component to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_component_to_vec;
    /// Write text used in a component to a writer.
    encode_component_to_writer;
}

encode_impl! {
    /// The following characters are escaped:
    ///
    /// C0 controls and,
    ///
    /// * SPACE
    /// * `!`
    /// * `"`
    /// * `#`
    /// * `$`
    /// * `%`
    /// * `&`
    /// * `'`
    /// * `(`
    /// * `)`
    /// * `+`
    /// * `,`
    /// * `/`
    /// * `:`
    /// * `;`
    /// * `<`
    /// * `=`
    /// * `>`
    /// * `?`
    /// * `@`
    /// * `[`
    /// * `\`
    /// * `]`
    /// * `^`
    /// * <code>&#096;</code>
    /// * `{`
    /// * `}`
    /// * `|`
    /// * `~`
    ///
    /// and all code points greater than `~` (U+007E) are escaped.
    X_WWW_FORM_URLENCODED;
    /// Encode text as a www-form-urlencoded text.
    encode_www_form_urlencoded;
    /// Write text as a urlencoded text to a mutable `String` reference and return the encoded string slice.
    encode_www_form_urlencoded_to_string;
    /// Write text as a www-form-urlencoded text to a mutable `Vec<u8>` reference and return the encoded data slice.
    encode_www_form_urlencoded_to_vec;
    /// Write text as a www-form-urlencoded text to a writer.
    encode_www_form_urlencoded_to_writer;
}