Skip to main content

Module encoding

Module encoding 

Source
Expand description

RFC 3986 percent-encoding utilities.

Pure-std, in-house replacement for the external urlencoding crate with 1:1 API semantics so call sites translate directly:

  • percent_encode mirrors urlencoding::encode: every byte outside the RFC 3986 unreserved set (A-Z a-z 0-9 - _ . ~) is encoded as %XX with uppercase hexadecimal digits. Spaces become %20 (never +).
  • percent_decode mirrors urlencoding::decode: %XX sequences are decoded case-insensitively, malformed or truncated % runs are passed through unchanged, and an error is returned only when the decoded bytes are not valid UTF-8.
  • percent_encode_strict mirrors percent_encoding::NON_ALPHANUMERIC: every byte that is not an ASCII alphanumeric is encoded, including the unreserved punctuation - _ . ~ (so _ becomes %5F). Used where form-style strict encoding is the established wire contract (e.g. OAuth2 query parameters).

All functions return Cow::Borrowed when the input needs no changes, avoiding allocation on the fast path.

§Examples

use oxirs_core::encoding::{percent_encode, percent_decode};

let encoded = percent_encode("SELECT * WHERE { ?s ?p ?o }");
assert_eq!(
    encoded,
    "SELECT%20%2A%20WHERE%20%7B%20%3Fs%20%3Fp%20%3Fo%20%7D"
);
assert_eq!(percent_decode(&encoded)?, "SELECT * WHERE { ?s ?p ?o }");

Functions§

percent_decode
Decodes a percent-encoded string per RFC 3986.
percent_encode
Percent-encodes a string per RFC 3986.
percent_encode_strict
Percent-encodes a string, escaping every byte that is not an ASCII alphanumeric character (A-Z a-z 0-9).