percent_encode

Function percent_encode 

Source
pub fn percent_encode(input: &str) -> String
Expand description

What: Percent-encode a string for use in URLs according to RFC 3986.

Inputs:

  • input: String to encode.

Output:

  • Returns a percent-encoded string where reserved characters are escaped.

Details:

  • Unreserved characters as per RFC 3986 (A-Z, a-z, 0-9, -, ., _, ~) are left as-is.
  • Space is encoded as %20 (not +).
  • All other bytes are encoded as two uppercase hexadecimal digits prefixed by %.
  • Operates on raw bytes from the input string; any non-ASCII bytes are hex-escaped.

ยงExamples

use pacsea::util::percent_encode;

// Encoding a package name for a URL, like in API calls to the AUR
assert_eq!(percent_encode("linux-zen"), "linux-zen");

// Encoding a search query with spaces for the package database
assert_eq!(percent_encode("terminal emulator"), "terminal%20emulator");

// Encoding a maintainer name with special characters
assert_eq!(percent_encode("John Doe <john@example.com>"), "John%20Doe%20%3Cjohn%40example.com%3E");