Skip to content

percent_decode

Decodes the %XX escapes of input.

Every % must be followed by two hexadecimal digits (either case). A + stays a +: turning it into a space is a rule of HTML form encoding, applied by parse_query and not by URL components in general. Returns the input borrowed when it has no %.

use helpers4::url::percent_decode;

Cargo feature url (enabled by default). To compile only this module:

cargo add helpers4 --no-default-features --features url

or in Cargo.toml:

[dependencies]
helpers4 = { version = "0.0.6", default-features = false, features = ["url"] }
pub fn percent_decode(input: &str) -> Result<Cow<'_, str>, PercentDecodeError>
ParameterTypeDescription
input&strThe text to decode.

Result<Cow<'_, str>, PercentDecodeError>Ok on success, otherwise an Err: see Errors.

PercentDecodeError::InvalidEscape for a % not followed by two hex digits, and PercentDecodeError::InvalidUtf8 when the decoded bytes are not valid UTF-8.

use helpers4::url::percent_decode;

assert_eq!(percent_decode("caf%C3%A9 %26 more")?, "café & more");
assert!(percent_decode("100%").is_err());

Why a string could not be percent-decoded.

use helpers4::url::PercentDecodeError;

#[non_exhaustive]
pub enum PercentDecodeError {
    /// A `%` that is not followed by two hexadecimal digits.
    InvalidEscape {
        /// Byte offset of the `%` in the input that was being decoded.
        index: usize,
    },
    /// The decoded bytes are not valid UTF-8.
    InvalidUtf8,
}

src/url/percent_decode.rs