Expand description
URLs use special characters to indicate the parts of the request.
For example, a ?
question mark marks the end of a path and the start of a query string.
In order for that character to exist inside a path, it needs to be encoded differently.
Percent encoding replaces reserved characters with the %
escape character
followed by a byte value as two hexadecimal digits.
For example, an ASCII space is replaced with %20
.
When encoding, the set of characters that can (and should, for readability) be left alone
depends on the context.
The ?
question mark mentioned above is not a separator when used literally
inside of a query string, and therefore does not need to be encoded.
The AsciiSet
parameter of percent_encode
and utf8_percent_encode
lets callers configure this.
This crate deliberately does not provide many different sets.
Users should consider in what context the encoded string will be used,
read relevant specifications, and define their own set.
This is done by using the add
method of an existing set.
§Examples
use percent_encoding::{utf8_percent_encode, AsciiSet, CONTROLS};
/// https://url.spec.whatwg.org/#fragment-percent-encode-set
const FRAGMENT: &AsciiSet = &CONTROLS.add(b' ').add(b'"').add(b'<').add(b'>').add(b'`');
assert_eq!(utf8_percent_encode("foo <bar>", FRAGMENT).to_string(), "foo%20%3Cbar%3E");
Structs§
- Ascii
Set - Represents a set of characters or bytes in the ASCII range.
- Percent
Decode - The return type of
percent_decode
. - Percent
Encode - The return type of
percent_encode
andutf8_percent_encode
.
Constants§
- CONTROLS
- The set of 0x00 to 0x1F (C0 controls), and 0x7F (DEL).
- NON_
ALPHANUMERIC - Everything that is not an ASCII letter or digit.
Functions§
- percent_
decode - Percent-decode the given bytes.
- percent_
decode_ str - Percent-decode the given string.
- percent_
encode - Percent-encode the given bytes with the given set.
- percent_
encode_ byte - Return the percent-encoding of the given byte.
- utf8_
percent_ encode - Percent-encode the UTF-8 encoding of the given string.