Skip to main content

percent_encode_strict

Function percent_encode_strict 

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

Percent-encodes a string, escaping every byte that is not an ASCII alphanumeric character (A-Z a-z 0-9).

This is stricter than percent_encode: the RFC 3986 unreserved punctuation - _ . ~ is escaped as well (_ becomes %5F), matching the byte set of percent_encoding::NON_ALPHANUMERIC. Use it where the established wire contract expects form-style strict encoding, such as OAuth2/OIDC authorization-URL query parameters. Returns Cow::Borrowed when no byte requires encoding.

ยงExamples

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

// Unreserved punctuation is escaped, unlike `percent_encode`.
assert_eq!(percent_encode_strict("test_client_id"), "test%5Fclient%5Fid");
assert_eq!(percent_encode_strict("a-b.c~d"), "a%2Db%2Ec%7Ed");

// Spaces become %20 (never `+`).
assert_eq!(percent_encode_strict("openid profile"), "openid%20profile");

// Purely alphanumeric input is returned without allocation.
assert!(matches!(percent_encode_strict("AZaz09"), Cow::Borrowed(_)));