Skip to main content

percent_encode

Function percent_encode 

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

Percent-encodes a string per RFC 3986.

Every byte that is not in the unreserved set (A-Z a-z 0-9 - _ . ~) is replaced by %XX where XX is the byte value in uppercase hexadecimal. Multi-byte UTF-8 characters are encoded byte by byte. Returns Cow::Borrowed when no byte requires encoding.

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

§Examples

use std::borrow::Cow;
use oxirs_core::encoding::percent_encode;

// Spaces become %20 (never `+`), reserved characters are escaped.
assert_eq!(percent_encode("a b/c?d#e"), "a%20b%2Fc%3Fd%23e");

// Multi-byte UTF-8 is encoded byte by byte with uppercase hex.
assert_eq!(percent_encode("日本"), "%E6%97%A5%E6%9C%AC");

// Unreserved input is returned without allocation.
assert!(matches!(percent_encode("AZaz09-_.~"), Cow::Borrowed(_)));