stdlib.encoding¶
Encoding Utilities
Generated from
v0.60.1. 8 source files, 76 documented symbols.
ascii85.xi¶
fn ascii85_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes as an Ascii85 string ('!'..'u'; runs of four zero bytes collapse to 'z'). Empty input yields "". Complexity: O(n).
fn ascii85_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes an Ascii85 string back into bytes. Accepts 'z' for zero runs. Returns Err on an invalid character, a 'z' inside a group, an out-of-range group value, or a degenerate tail group. Complexity: O(n).
fn ascii85_encode_str(s: Str) -> Str¶
Encodes a string's UTF-8 bytes as Ascii85. Complexity: O(n).
fn ascii85_decode_str(s: Str) -> Result[Str, Str]¶
Decodes Ascii85 into a UTF-8 string (bytes copied verbatim; the caller is responsible for the UTF-8 validity of the decoded content). Returns Err on invalid Ascii85. Complexity: O(n).
fn ascii85_encode_with_delim(data: &Vec[UInt8]) -> Str¶
Encodes bytes as Ascii85 wrapped in the Adobe delimiters "<~" and "~>". Complexity: O(n).
fn ascii85_decode_with_delim(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes an Ascii85 string that is wrapped in the Adobe delimiters "<~" and "~>". Returns Err if the delimiters are missing or the payload is invalid. Complexity: O(n).
base32.xi¶
fn base32_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes as an RFC 4648 base32 string (alphabet A-Z, 2-7), padded with '=' to a multiple of 8 characters. Complexity: O(n).
fn base32_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes an RFC 4648 base32 string to bytes. Returns Err on invalid input. Complexity: O(n).
fn base32hex_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes as a base32hex string (RFC 4648 section 7, alphabet 0-9, A-V), padded with '='. Complexity: O(n).
fn base32hex_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes a base32hex string to bytes (both digit cases accepted). Returns Err on invalid input. Complexity: O(n).
fn base32_encode_str(s: Str) -> Str¶
Encodes a string's UTF-8 bytes as base32. Complexity: O(n).
fn base32_decode_str(s: Str) -> Result[Str, Str]¶
Decodes base32 into a UTF-8 string (bytes copied verbatim; callers are responsible for UTF-8 validity). Returns Err on invalid base32. Complexity: O(n).
base64.xi¶
fn base64_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes as a standard base64 string with mandatory '=' padding. Empty input yields "". Complexity: O(n).
fn base64_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes a standard base64 string to bytes. Accepts optional '=' padding. Returns Err on a non-multiple-of-4 length or an invalid character. Complexity: O(n).
fn base64_encode_str(s: Str) -> Str¶
Encodes a string's UTF-8 bytes as standard base64. Complexity: O(n).
fn base64_decode_str(s: Str) -> Result[Str, Str]¶
Decodes base64 into a UTF-8 string (bytes copied verbatim; callers are responsible for UTF-8 validity). Returns Err on invalid base64. Complexity: O(n).
fn base64_encode_padded(data: &Vec[UInt8]) -> Str¶
Encodes bytes as base64 with mandatory padding. Standard base64 always pads, so this is equivalent to base64_encode. Complexity: O(n).
fn base64_decode_padded(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes a base64 string that MUST carry correct padding: the string length must be a multiple of 4 and the number of trailing '=' characters must match the data length (0, 1 or 2 for full, 2- and 1-byte tails). Returns Err on malformed padding or an invalid character. Complexity: O(n).
fn base64url_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes as an unpadded URL-safe base64 string (alphabet A-Za-z0-9-_). Empty input yields "". Complexity: O(n).
fn base64url_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes an unpadded URL-safe base64 string to bytes. Optional '=' padding is tolerated. Returns Err on an invalid character. Complexity: O(n).
encoding.xi¶
fn base64_encode(data: &Vec[UInt8]) -> Str¶
=== Base64 ===
- Postcondition:
result.len() == ((data.len() + 2) / 3) * 4
fn base64_decode(encoded: Str) -> Result[Vec[UInt8], Str]¶
Decode standard base64; Err on invalid input.
- Precondition:
encoded.len() % 4 == 0 - Postcondition:
result is Ok(_) => result.len() <= (encoded.len() / 4) * 3
fn base64url_encode(data: &Vec[UInt8]) -> Str¶
URL-safe base64 encoding (no padding).
- Postcondition:
result.len() >= 0
fn base64url_decode(encoded: Str) -> Result[Vec[UInt8], Str]¶
Decode URL-safe base64; Err on invalid input.
- Postcondition:
result is Ok(_) => result.len() <= (encoded.len() * 3) / 4
fn hex_encode(data: &Vec[UInt8]) -> Str¶
=== Hex ===
- Postcondition:
result.len() == data.len() * 2
fn hex_decode(encoded: Str) -> Result[Vec[UInt8], Str]¶
Decode hex (either case); Err on bad input.
- Precondition:
encoded.len() % 2 == 0 - Postcondition:
result is Ok(_) => result.len() == encoded.len() / 2
fn hex_encode_upper(data: &Vec[UInt8]) -> Str¶
Uppercase hex encoding.
- Postcondition:
result.len() == data.len() * 2
fn url_encode(data: Str) -> Str¶
=== URL encoding ===
- Postcondition:
result.len() >= data.len()
fn url_decode(encoded: Str) -> Result[Str, Str]¶
Percent-decode a URL component; Err on malformed escapes.
- Postcondition:
result is Ok(_) => result.len() <= encoded.len()
fn percent_encode(data: Str) -> Str¶
=== Percent encoding ===
- Postcondition:
result.len() >= data.len()
fn percent_decode(encoded: Str) -> Result[Str, Str]¶
Alias of url_decode (percent-decoding).
- Postcondition:
result is Ok(_) => result.len() <= encoded.len()
fn utf8_encode(s: Str) -> Vec[UInt8]¶
=== UTF-8 ===
- Postcondition:
result.len() >= s.len()
fn utf8_decode(data: &Vec[UInt8]) -> Result[Str, Str]¶
Decode UTF-8 bytes to a Str; Err on invalid sequences.
- Precondition:
data.len() > 0 - Postcondition:
result is Ok(_) => result.len() <= data.len()
fn utf8_valid(data: &Vec[UInt8]) -> Bool¶
True when the bytes are valid UTF-8.
- Postcondition:
result == true => utf8_decode(data) is Ok(_)
fn utf8_char_len(first_byte: UInt8) -> Int¶
UTF-8 sequence length implied by the first byte (0 when invalid).
- Postcondition:
result >= 1 && result <= 4
fn binary_to_text(data: &Vec[UInt8], format: Int) -> Str¶
=== Binary to text ===
- Precondition:
format >= 0 && format <= 2 - Postcondition:
format == 0 => result.len() == ((data.len() + 2) / 3) * 4 - Postcondition:
format == 1 => result.len() == data.len() * 2 - Postcondition:
result.len() >= 0
fn text_to_binary(text: Str, format: Int) -> Result[Vec[UInt8], Str]¶
Encode text into bytes using the given format code; Err on unknown format.
- Precondition:
format >= 0 && format <= 2 - Postcondition:
format == 0 => (result is Ok(_) => result.len() <= (text.len() / 4) * 3) - Postcondition:
format == 1 => (result is Ok(_) => result.len() == text.len() / 2) - Postcondition:
format == 2 => (result is Ok(_) => result.len() <= (text.len() / 4) * 3)
fn base32_encode(data: &Vec[UInt8]) -> Str¶
Encodes bytes to a Base32 string using RFC 4648 alphabet (A-Z, 2-7). Processes 5-byte blocks into 8 Base32 characters. Padding with '=' to multiple of 8. Complexity: O(n), n = data length.
fn base32_decode(encoded: Str) -> Result[Vec[UInt8], Str]¶
Decodes a Base32 string (RFC 4648, with optional '=' padding). Returns the decoded bytes or an error string. Complexity: O(n), n = encoded string length.
fn base16_encode(data: &Vec[UInt8]) -> Str¶
Alias for hex_encode. Converts bytes to lowercase hex string.
fn int_to_hex(n: Int) -> Str¶
Converts an integer to a lowercase hexadecimal string. Complexity: O(log16(n)).
fn hex_to_int(s: Str) -> Option[Int]¶
Converts a hexadecimal string to an integer. Returns None if the string contains invalid hex characters. Complexity: O(n), n = string length.
fn base64_encode_str(s: Str) -> Str¶
Encodes a string to Base64 by first converting to UTF-8 bytes. Complexity: O(n), n = string length.
- Precondition:
true
fn base64_decode_str(encoded: Str) -> Result[Str, Str]¶
Decodes a Base64 string and returns the original string. Complexity: O(n), n = encoded string length.
hex.xi¶
fn hex_encode(data: &Vec[UInt8]) -> Str¶
Encodes a byte vector as a lowercase hexadecimal string (two digits per byte). Empty input yields "". Complexity: O(n).
fn hex_decode(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes a hexadecimal string into bytes. Accepts both digit cases. Returns Err on an odd length or an invalid hex character. Complexity: O(n).
fn hex_encode_upper(data: &Vec[UInt8]) -> Str¶
Encodes a byte vector as an uppercase hexadecimal string. Complexity: O(n).
fn hex_encode_str(s: Str) -> Str¶
Encodes the UTF-8 bytes of a string as lowercase hex. Complexity: O(n).
fn hex_decode_str(s: Str) -> Result[Str, Str]¶
Decodes a hex string back into a string (decoded bytes copied verbatim; callers are responsible for UTF-8 validity). Returns Err on invalid hex. Complexity: O(n).
fn hex_encode_int(n: Int) -> Str¶
Encodes an integer as a lowercase hex string (no sign, no prefix; 0 -> "0"). Complexity: O(log16(n)).
fn hex_decode_int(s: Str) -> Result[Int, Str]¶
Parses a hex string into an integer. Returns Err on an empty string or an invalid hex character. Complexity: O(n).
fn hex_nibble_to_int(c: Char) -> Option[Int]¶
The numeric value of a hex digit character (0-15), or None if
cis not a hex digit. Complexity: O(1).
fn hex_int_to_nibble(n: Int) -> Char¶
The hex digit character for a 0-15 value (lowercase). Returns '\0' for a value outside that range. Complexity: O(1).
idna.xi¶
fn idna_split_labels(domain: Str) -> Vec[Str]¶
Splits a domain at the dot separators. Empty labels (e.g. a trailing dot) are preserved. Complexity: O(n).
fn idna_join_labels(labels: &Vec[Str]) -> Str¶
Joins labels back into a domain with '.' separators. Complexity: O(n * total length).
fn idna_to_ascii(s: Str) -> Result[Str, Str]¶
Converts a Unicode domain to its ASCII A-label form: ASCII labels are lowercased and kept; non-ASCII labels are Punycode-encoded with the "xn--" prefix. Returns Err on an empty domain, an oversized label/domain, or a punycode failure. Complexity: O(sum of label^2).
fn idna_to_unicode(s: Str) -> Result[Str, Str]¶
Converts an A-label domain to Unicode: labels with the "xn--" prefix are Punycode-decoded, all others pass through. Returns Err on a punycode failure. Complexity: O(sum of label^2).
fn idna_is_valid(s: Str) -> Bool¶
Reports whether a domain conforms to IDNA requirements: total length at most 253 bytes, every label 1..63 bytes, ASCII-only, LDH-shaped (or a valid "xn--" A-label whose payload decodes), and neither starting nor ending with a hyphen. Returns false for any violation. Complexity: O(total length + sum of label^2).
fn idna_uts46_normalize(s: Str) -> Result[Str, Str]¶
Applies the simplified UTS-46 mapping to a domain string: ASCII uppercasing, fullwidth-to-ASCII and ideographic-space mapping, and rejection of control/surrogate characters. (NFC normalization is omitted; the code points are otherwise preserved.) Returns Err on a disallowed character. Complexity: O(n).
fn idna_nameprep(s: Str) -> Result[Str, Str]¶
Applies the older Nameprep profile (RFC 3491 subset) to a domain string: case folding (ASCII lowercase), rejection of whitespace, control and surrogate characters. Returns Err on a disallowed character. Complexity: O(n).
fn idna_is_bidi_valid(s: Str) -> Bool¶
Reports whether
ssatisfies the IDNA bidi rule (RFC 5893 subset): a label containing an RTL code point must start with an RTL code point, end with an RTL code point or a digit, and contain no LTR letters; labels with no RTL code point are always acceptable. Applies to every dot-separated label. Complexity: O(n).
percent.xi¶
fn percent_encode(s: Str) -> Str¶
Percent-encodes every non-unreserved character of
sper its UTF-8 bytes; spaces become '%20'. Unreserved characters (A-Z a-z 0-9 - _ . ~) pass through. Complexity: O(n).
fn percent_decode(s: Str) -> Result[Str, Str]¶
Decodes percent-escapes in
s. '+' is left as a literal '+'. Returns Err on a truncated or malformed escape. Complexity: O(n).
fn percent_encode_component(s: Str) -> Str¶
Percent-encodes a single URL path or query component: only unreserved characters pass through; everything else -- including '/', '?', '&', '=' -- is percent-encoded per UTF-8 byte. Complexity: O(n).
fn percent_decode_component(s: Str) -> Result[Str, Str]¶
Decodes a URL component: '%XX' escapes are decoded; '+' is left as a literal '+'. Returns Err on a truncated or malformed escape. Complexity: O(n).
fn percent_encode_bytes(data: &Vec[UInt8]) -> Str¶
Percent-encodes raw bytes: unreserved ASCII bytes pass through, all other bytes become '%XX'. Complexity: O(n).
fn percent_decode_bytes(s: Str) -> Result[Vec[UInt8], Str]¶
Decodes percent-escapes into bytes; '+' is left as a literal '+'. Returns Err on a truncated or malformed escape. Complexity: O(n).
fn percent_encode_www_form(s: Str) -> Str¶
Encodes as application/x-www-form-urlencoded: spaces become '+', all other non-unreserved characters are percent-encoded per UTF-8 byte. Complexity: O(n).
fn percent_decode_www_form(s: Str) -> Result[Str, Str]¶
Decodes a form body: '+' becomes a space and '%XX' escapes are decoded. Returns Err on a truncated or malformed escape. Complexity: O(n).
punycode.xi¶
fn punycode_adapt(delta: Int, numpoints: Int, firsttime: Bool) -> Int¶
The RFC-3492 bias adaptation function. Maps a delta to a new bias value. Complexity: O(log delta).
fn punycode_encode_digit(d: Int) -> Char¶
Maps a 0-35 value to its Punycode digit character (a-z, then 0-9). Returns '\0' for a value outside 0..35. Complexity: O(1).
fn punycode_decode_digit(c: Char) -> Int¶
Maps a Punycode digit character back to a 0-35 value; -1 for invalid. Complexity: O(1).
fn punycode_encode(s: Str) -> Result[Str, Str]¶
Encodes a single Unicode label to Punycode (RFC 3492 section 6.3). Returns Err on an invalid code point or an arithmetic overflow. Complexity: O(n^2), n = label length in code points.
fn punycode_decode(s: Str) -> Result[Str, Str]¶
Decodes a Punycode label to Unicode (RFC 3492 section 6.2). Returns Err on an invalid digit, a truncated sequence, an overflow, a basic decoded code point, or an invalid code point. Complexity: O(n^2), n = encoded length.
fn punycode_encode_domain(domain: Str) -> Result[Str, Str]¶
Encodes every label of a full domain: ASCII labels pass through unchanged, non-ASCII labels are Punycode-encoded and prefixed with "xn--" (RFC 3490 A-label form). Empty labels are preserved. Returns Err on a punycode failure. Complexity: O(sum of label^2).
fn punycode_decode_domain(domain: Str) -> Result[Str, Str]¶
Decodes every label of an A-label domain: labels starting with "xn--" (case-insensitive) are Punycode-decoded, all others pass through. Empty labels are preserved. Returns Err on a punycode failure. Complexity: O(sum of label^2).