Skip to content
CryoCryo home
Stdlibencoding

percent

import std::encoding::percent; · source

Percent-encoding (RFC 3986): a byte a URI cannot carry as itself is written % and two hexadecimal digits.

Types

PercentEncoding

type struct PercentEncoding {}

The codec is stateless; its operations are statics. Encoding leaves the unreserved characters (A-Z, a-z, 0-9, -, ., _, ~) as they are and escapes every other byte, with uppercase digits. encode_component is safe as one path segment, query key or query value; encode_path keeps the / separators too, for a whole path.

import std::encoding::percent::{ PercentEncoding };

const query: String = PercentEncoding::encode_component(Str::new("a b&c"));   // "a%20b%26c"
const path: String = PercentEncoding::encode_path(Str::new("/docs/a b"));     // "/docs/a%20b"

decode turns every escape back into its byte; + stays +, since it means a space only in HTML form data. It refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).

Static functions

static encode_component(text: Str) -> String;

text with every byte but the unreserved characters escaped: safe as one path segment, query key or query value.

static encode_path(text: Str) -> String;

text with every byte but the unreserved characters and / escaped: safe as a whole URI path.

static decode(text: Str) -> Result<String, DecodeError>;

text with every %XX escape replaced by its byte. Refuses an escape without two hexadecimal digits (InvalidEscape, at its %) and a result that is not UTF-8 (InvalidUtf8, at the input byte that starts the bad sequence).