std.encoding.base64
API summary (generated from the Beans source by npm run coverage): 3 package functions · 1 type · 3 instance methods · 4 enum variants.
std.encoding.base64 turns bytes into Base64 text and back. It supports the
standard alphabet and the URL-safe alphabet, each with or without padding.
Underneath it uses simdutf (MIT). Read the source at
stdlib/std/encoding/base64/base64.b.
import std.encoding.base64Note: a build made with --runtime freestanding refuses std.encoding.
Encoding
Section titled “Encoding”Encoding is an enum with the four RFC 4648 alphabets and padding modes.
pub enum Encodingstandardstandard_no_padurl_safeurl_safe_no_padstandardis the normal alphabet with=padding.standard_no_padis the normal alphabet without padding.url_safeuses-and_in place of+and/, with padding.url_safe_no_padis the URL-safe alphabet without padding.
Each encoding value has three methods:
pub fn encode(data: Bytes) -> stringpub fn decode(text: string) -> Result<Bytes>pub fn decode_forgiving(text: string) -> Result<Bytes>encodeturns bytes into Base64 text. The output length is exact for the chosen encoding. Native builds allocate that final string once and fill it directly.decodeis strict RFC 4648. On bad input it returns an error whose kind tells you what was wrong:invalid(a byte outside the alphabet),length(a lone trailing character),padding(padding that does not match the encoding),bits(non-zero trailing padding bits), orwhitespace(whitespace, which strict mode rejects). The message carries the byte position.decode_forgivingfollows the WHATWG “forgiving base64” rules: it skips ASCII whitespace, accepts a partial final group with or without padding, and ignores non-zero trailing padding bits. Bytes outside the alphabet are still errors.
Decode fills its result Bytes directly and shrinks that same allocation to the
decoded length. Strict no-padding forms validate the final group without making
a padded copy of the input. These are implementation gains; the API and owned
result behavior do not change.
import std.ioimport std.encoding.base64
fn main() { let text: string = base64.Encoding.url_safe.encode(Bytes.from("hi?")) io.println(text) let back: Bytes = base64.Encoding.url_safe.decode(text).expect("decode") io.println(back.to_string()) // hi?}Module-level shortcuts
Section titled “Module-level shortcuts”For the common case (standard alphabet, padded, strict decoding) call these
directly without naming an encoding. Each is the matching Encoding.standard
method in one call.
pub fn encode(data: Bytes) -> stringpub fn decode(text: string) -> Result<Bytes>pub fn decode_forgiving(text: string) -> Result<Bytes>import std.ioimport std.encoding.base64
fn main() { let text: string = base64.encode(Bytes.from("beans")) io.println(text) // YmVhbnM= io.println(base64.decode(text).expect("decode").to_string()) // beans}