Module
std.base64 — RFC 4648 base-64, standard and URL-safe. Stability tier: core.
**The byte sequence is List[byte] (sc35).** For eleven sprints it was List[int] with every element in 0..255 — an interim this header took on std.hex's reasoning, with the promise that "every signature keeps its shape when Bytes lands" (F-0035). D72/s135 landed the type and s136 (wolf-lang#231) moved the eight byte builtins to it, so the promise was kept literally: five signatures took the new element type and no body here changed shape.
**byte_at used to be the boundary check and is now one cast.** It read an element and trapped assert on anything outside 0..255; the ELEMENT TYPE holds that contract now, both guards were unreachable code, and what remains is [type.byte.cast]'s widening — spelled, because the 24-bit group arithmetic below is int's and [type.byte.op] widens every operand anyway. The three pushes in decode_with narrow back the same way (… as byte), which is the only other line that moved.
Strictness is spelled in the name, and documented on each function. decode is the strict RFC 4648 §4 profile: the standard alphabet, the length a multiple of four, padding present and correct, and the unused low bits of a truncated group required to be ZERO — so exactly one text decodes to any given byte sequence, which is what makes encode(decode(s)) == s hold as well as the other direction. decode_lenient relaxes padding and canonicity for input that came from somewhere less careful. url_decode is §5's alphabet with optional padding, because url_encode omits it. encode_raw/decode_raw are strict UNPADDED standard base64 for age v1: no padding, whitespace, alternate alphabet or nonzero unused bits. Their walks are linear in input/output length. Encoding and decoding branch/index on byte values: neither is a fixed-time codec.
Round-tripping is the doctrine: decode(encode(b)) is b for every byte sequence, held over the shared adversarial fixtures in tests/base64/round_trip.lu — including the three group remainders, which is where every base-64 implementation's bugs live.
Decoding is TOTAL over arbitrary UTF-8: the alphabet probe runs against literals this module owns, so a non-ASCII character is a parse miss and never a bounds trap. The tag is parse and not the contract's base64 because a row tag that shares a name with a module in scope resolves to the MODULE, silently (F-0036) — and this module is named base64.
Functions and methods
fn · Source line 57
pub fn encode_raw(b: List[byte]) -> str
RFC 4648 section 4 standard-alphabet base64 without padding, as age specifies. Empty bytes encode to empty text. Pure, comptime-safe; total, no rows or traps on well-typed input. Not fixed-time.
base64.encode_raw("f".bytes()) == "Zg"
base64.encode_raw("foo".bytes()) == "Zm9v"
fn · Source line 86
pub fn decode_raw(s: str) -> List[byte] ! {parse}
Strict unpadded standard base64. Raises parse on padding, any nonalphabet byte (including whitespace, URL characters and non-ASCII), a one-character residual group, or nonzero unused trailing bits. Empty text succeeds with empty bytes. Total over UTF-8, no traps. Pure, comptime-safe; not fixed-time. Never includes input in errors.
let b = base64.decode_raw("Zg")?
base64.encode_raw(b) == "Zg"
fn · Source line 310
pub fn encode(b: List[byte]) -> str
b encoded with the standard alphabet and = padding — RFC 4648 §4, the encoding every Authorization: Basic header and every PEM block uses. The empty sequence encodes to the empty string; output length is always a multiple of four.
Traps assert when an element is outside 0..255 (§2's trap rule). Total otherwise: no error row.
var b = List[byte]()
(mut b).push(77 as byte)
base64.encode(b) == "TQ=="
(mut b).push(97 as byte)
base64.encode(b) == "TWE="
(mut b).push(110 as byte)
base64.encode(b) == "TWFu"
var empty = List[byte]()
base64.encode(empty) == ""
fn · Source line 327
pub fn url_encode(b: List[byte]) -> str
b encoded with the URL- and filename-safe alphabet (- and _ for + and /) and NO padding — RFC 4648 §5 with its padding sentence taken: = needs escaping in a query string, which is the whole point of the variant. url_decode accepts padding anyway, so text from a padding-emitting encoder still reads.
var b = List[byte]()
(mut b).push(255 as byte)
(mut b).push(239 as byte)
base64.url_encode(b) == "_-8"
base64.encode(b) == "/+8="
fn · Source line 354
pub fn decode(s: str) -> List[byte] ! {parse}
s decoded with the standard alphabet, STRICTLY: parse unless the length is a multiple of four, the padding is exactly what the byte count requires, every other character is in the alphabet, and the unused low bits of a short final group are zero. That last clause is what makes the encoding canonical — "TQ==" decodes and "TR==", which names the same byte with different ignored bits, does not — and it is why encode(decode(s)) == s holds for every s this function accepts.
Total: no traps. Non-ASCII input is an ordinary miss.
let one = base64.decode("TQ==") else List[byte]()
one.len == 1
let three = base64.decode("TWFu") else List[byte]()
three.len == 3
let unpadded = base64.decode("TQ") else List[byte]()
unpadded.len == 0
let noncanonical = base64.decode("TR==") else List[byte]()
noncanonical.len == 0
let bad = base64.decode("T!==") else List[byte]()
bad.len == 0
fn · Source line 370
pub fn url_decode(s: str) -> List[byte] ! {parse}
s decoded with the URL-safe alphabet, padding optional. parse on any character outside that alphabet — including + and /, which belong to the other variant and are a sign the caller mixed the two.
let a = base64.url_decode("_-8") else List[byte]()
a.len == 2
let b = base64.url_decode("_-8=") else List[byte]()
b.len == 2
let mixed = base64.url_decode("/+8=") else List[byte]()
mixed.len == 0
fn · Source line 393
pub fn decode_lenient(s: str) -> List[byte] ! {parse}
s decoded with the standard alphabet, forgivingly: padding may be absent or short, and a final group's unused bits may be anything. parse still answers for a character outside the alphabet and for a trailing group of one character, which encodes no byte at all.
Use it for input from elsewhere; use decode for input your program will re-encode, because only the strict profile promises the text back unchanged.
let a = base64.decode_lenient("TQ") else List[byte]()
a.len == 1
let b = base64.decode_lenient("TR") else List[byte]()
b.len == 1
let c = base64.decode_lenient("TWFu") else List[byte]()
c.len == 3
let bad = base64.decode_lenient("T") else List[byte]()
bad.len == 0