Skip to main content

percent_decode

Function percent_decode 

Source
pub fn percent_decode(input: &str) -> Result<Cow<'_, str>, FromUtf8Error>
Expand description

Decodes a percent-encoded string per RFC 3986.

%XX sequences are decoded with case-insensitive hexadecimal digits. Malformed or truncated sequences (%, %2, %ZZ, …) are passed through unchanged instead of producing an error; an error is returned only when the decoded byte sequence is not valid UTF-8.

Returns Cow::Borrowed when the input contains no % character.

This is a drop-in replacement for urlencoding::decode.

§Errors

Returns FromUtf8Error when the decoded bytes are not valid UTF-8 (for example "%FF").

§Examples

use oxirs_core::encoding::percent_decode;

assert_eq!(percent_decode("hello%20world")?, "hello world");
// Hex digits are case-insensitive.
assert_eq!(percent_decode("%e6%97%A5")?, "日");
// Malformed sequences pass through unchanged.
assert_eq!(percent_decode("100%")?, "100%");
// Invalid UTF-8 in the decoded output is the only error case.
assert!(percent_decode("%FF").is_err());