stdlib/encodingView source ↗
encoding
37 items · generated by scribe fromstdlib/encoding.fd· synced 2026-11-15
Contents
- enc_bytes_to_str (function)
- enc_str_to_bytes (function)
- b64_encode_bytes (function)
- b64_encode (function)
- b64_decode_bytes (function)
- b64_decode (function)
- b64_valid (function)
- hex_encode_bytes (function)
- hex_encode_bytes_upper (function)
- hex_encode (function)
- hex_encode_upper (function)
- hex_decode_bytes (function)
- hex_decode (function)
- hex_valid (function)
- url_encode_bytes (function)
- url_encode (function)
- url_encode_component (function)
- url_encode_form (function)
- url_decode_bytes (function)
- url_decode (function)
- url_decode_form (function)
- utf8_valid_bytes (function)
- utf8_valid (function)
- utf8_decode (function)
- utf8_len (function)
- utf8_encode (function)
- utf8_encode_bytes (function)
- utf8_encode_all (function)
enc_bytes_to_str
Bytes -> a str holding exactly those bytes (NUL-terminated heap buffer). This is the right way to turn decoded bytes into a string; str_from_bytes re-encodes each element as a codepoint instead.
Example: var b: Vec
pub fn enc_bytes_to_str(bytes: Vec<i64>) -> str {Parameters:
bytes:Vec<i64>
Returns: str
Defined at line 38
enc_str_to_bytes
A str -> its raw bytes (same as str_bytes, kept for API symmetry).
Example: var b = enc_str_to_bytes(“AB”); assert_eq(b.len(), 2); assert_eq(b.get(1), 66);
pub fn enc_str_to_bytes(s: str) -> Vec<i64> {Parameters:
s:str
Returns: Vec<i64>
Defined at line 62
b64_encode_bytes
Encode a byte Vec to base64 text (RFC 4648 with ’=’ padding).
Example: var b: Vec
pub fn b64_encode_bytes(bytes: Vec<i64>) -> str {Parameters:
bytes:Vec<i64>
Returns: str
Defined at line 78
b64_encode
Encode a string’s UTF-8 bytes to base64 text.
Example: var e = b64_encode(“hello”); println(e); assert_eq(e, “aGVsbG8=”);
pub fn b64_encode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 115
b64_decode_bytes
Decode base64 text to a byte Vec. Whitespace is skipped; ’=’ ends the stream. Invalid characters make the decode stop at the first bad group (lenient, like most libraries).
Example: var b = b64_decode_bytes(“AP8=”); assert_eq(b.len(), 2); assert_eq(b.get(1), 255);
pub fn b64_decode_bytes(s: str) -> Vec<i64> {Parameters:
s:str
Returns: Vec<i64>
Defined at line 156
b64_decode
Decode base64 text to a string. Returns "" when the decoded bytes are empty or not valid UTF-8 output is not required — bytes are returned verbatim.
Example: assert_eq(b64_decode(“aGVsbG8=”), “hello”); assert_eq(b64_decode(b64_encode(“round trip”)), “round trip”);
pub fn b64_decode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 209
b64_valid
Check that a string is well-formed base64 (optional whitespace and ’=’ padding allowed).
Example: assert(b64_valid(“aGVsbG8=”)); assert(!b64_valid(“a*b”));
pub fn b64_valid(s: str) -> bool {Parameters:
s:str
Returns: bool
Defined at line 219
hex_encode_bytes
Encode a byte Vec to lowercase hex text (“0a3f…”).
Example: var b: Vec
pub fn hex_encode_bytes(bytes: Vec<i64>) -> str {Parameters:
bytes:Vec<i64>
Returns: str
Defined at line 259
hex_encode_bytes_upper
Encode a byte Vec to uppercase hex text (“0A3F…”).
Example: var b: Vec
pub fn hex_encode_bytes_upper(bytes: Vec<i64>) -> str {Parameters:
bytes:Vec<i64>
Returns: str
Defined at line 280
hex_encode
Encode a string’s bytes to lowercase hex text.
Example: assert_eq(hex_encode(“Hi”), “4869”);
pub fn hex_encode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 299
hex_encode_upper
Encode a string’s bytes to uppercase hex text.
Example: assert_eq(hex_encode_upper(“\n”), “0A”);
pub fn hex_encode_upper(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 307
hex_decode_bytes
Decode hex text to a byte Vec. Whitespace is skipped. Stops at the first invalid character (or an odd trailing half-byte).
Example: var b = hex_decode_bytes(“de ad”); assert_eq(b.len(), 2); assert_eq(b.get(0), 222);
pub fn hex_decode_bytes(s: str) -> Vec<i64> {Parameters:
s:str
Returns: Vec<i64>
Defined at line 335
hex_decode
Decode hex text to a string.
Example: assert_eq(hex_decode(“4869”), “Hi”);
pub fn hex_decode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 365
hex_valid
Check that a string is well-formed hex (even number of digits, whitespace ignored).
Example: assert(hex_valid(“00ff”)); assert(!hex_valid(“0f0”)); assert(!hex_valid(“zz”));
pub fn hex_valid(s: str) -> bool {Parameters:
s:str
Returns: bool
Defined at line 376
url_encode_bytes
Percent-encode a byte Vec: unreserved bytes pass through, everything else becomes %XX (uppercase hex).
Example: var b: Vec
pub fn url_encode_bytes(bytes: Vec<i64>) -> str {Parameters:
bytes:Vec<i64>
Returns: str
Defined at line 422
url_encode
Percent-encode a string for use in a URL component (space becomes %20).
Example: var e = url_encode(“a b&c=d”); println(e); assert_eq(e, “a%20b%26c%3Dd”);
pub fn url_encode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 449
url_encode_component
Percent-encode a string for use in a URL component (alias).
Example: assert_eq(url_encode_component(“x/y”), “x%2Fy”);
pub fn url_encode_component(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 457
url_encode_form
Percent-encode with HTML-form rules: like url_encode but a space becomes ’+’.
Example: assert_eq(url_encode_form(“a b”), “a+b”);
pub fn url_encode_form(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 466
url_decode_bytes
Decode percent-encoded text to a byte Vec. “%XX” -> byte XX, ”+” stays ”+”, a malformed ’%’ is passed through.
Example: var b = url_decode_bytes(“%41+”); assert_eq(b.len(), 2); assert_eq(b.get(0), 65); assert_eq(b.get(1), 43);
pub fn url_decode_bytes(s: str) -> Vec<i64> {Parameters:
s:str
Returns: Vec<i64>
Defined at line 497
url_decode
Decode percent-encoded text to a string. ’+’ is NOT translated (use url_decode_form for query strings).
Example: assert_eq(url_decode(“a%20b+c”), “a b+c”);
pub fn url_decode(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 532
url_decode_form
Decode a form/query-encoded string: “%XX” -> byte, ’+’ -> space.
Example: assert_eq(url_decode_form(“a+b%21”), “a b!”);
pub fn url_decode_form(s: str) -> str {Parameters:
s:str
Returns: str
Defined at line 540
utf8_valid_bytes
Check that a byte Vec is valid UTF-8 (no overlongs, no surrogates, no out-of-range codepoints, truncated sequences rejected).
Example: var ok: Vec
pub fn utf8_valid_bytes(bytes: Vec<i64>) -> bool {Parameters:
bytes:Vec<i64>
Returns: bool
Defined at line 580
utf8_valid
Check that a string’s bytes are valid UTF-8.
Example: assert(utf8_valid(“caf” + utf8_encode(233))); assert(utf8_valid(“plain ascii”));
pub fn utf8_valid(s: str) -> bool {Parameters:
s:str
Returns: bool
Defined at line 643
utf8_decode
Decode a string to a Vec of Unicode codepoints (i64). Invalid sequences decode as U+FFFD (65533) and consume one byte.
Example: var cps = utf8_decode(utf8_encode(233) + ”!”); assert_eq(cps.len(), 2); assert_eq(cps.get(0), 233); assert_eq(cps.get(1), 33);
pub fn utf8_decode(s: str) -> Vec<i64> {Parameters:
s:str
Returns: Vec<i64>
Defined at line 655
utf8_len
Count the number of UTF-8 codepoints (characters) in a string. Invalid bytes each count as one (replacement) character.
Example: var s = “a” + utf8_encode(8364) + “b”; assert_eq(str_len(s), 5); assert_eq(utf8_len(s), 3);
pub fn utf8_len(s: str) -> i64 {Parameters:
s:str
Returns: i64
Defined at line 712
utf8_encode
Encode one Unicode codepoint as its UTF-8 string. Codepoints above U+10FFFF and surrogates encode as U+FFFD.
Example: assert_eq(str_len(utf8_encode(8364)), 3); assert_eq(utf8_encode(65), “A”);
pub fn utf8_encode(cp: i64) -> str {Parameters:
cp:i64
Returns: str
Defined at line 739
utf8_encode_bytes
Raw UTF-8 bytes of one codepoint as a Vec
Example: var b = utf8_encode_bytes(233); assert_eq(b.len(), 2); assert_eq(b.get(0), 195); assert_eq(b.get(1), 169);
pub fn utf8_encode_bytes(cp: i64) -> Vec<i64> {Parameters:
cp:i64
Returns: Vec<i64>
Defined at line 758
utf8_encode_all
Encode a Vec of codepoints as a UTF-8 string.
Example: var cps: Vec
pub fn utf8_encode_all(cps: Vec<i64>) -> str {Parameters:
cps:Vec<i64>
Returns: str
Defined at line 788
Generated by scribe, the Fides documentation generator