#codec #urlencode #url #encoded-string #percent #decoding #set

no-std percent-encoding

Percent encoding and decoding

7 stable releases

2.3.1 Nov 22, 2023
2.3.0 Jun 5, 2023
2.2.0 Sep 8, 2022
2.1.0 Aug 5, 2019
1.0.0 Jun 13, 2017

#90 in Text processing

Download history 1690901/week @ 2023-11-27 1666634/week @ 2023-12-04 1687989/week @ 2023-12-11 1483452/week @ 2023-12-18 915825/week @ 2023-12-25 1423701/week @ 2024-01-01 1698270/week @ 2024-01-08 1744197/week @ 2024-01-15 1830325/week @ 2024-01-22 1973862/week @ 2024-01-29 1976747/week @ 2024-02-05 1967019/week @ 2024-02-12 1943711/week @ 2024-02-19 2088342/week @ 2024-02-26 2036108/week @ 2024-03-04 784870/week @ 2024-03-11

6,986,247 downloads per month
Used in 28,704 crates (822 directly)

MIT/Apache

18KB
400 lines

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");

No runtime deps

Features