Documentation
¶
Overview ¶
Package base64 is a pure-Go (no cgo), MRI-4.0.5-faithful reimplementation of Ruby's standard-library Base64 module — the deterministic, interpreter- independent core of `require "base64"`.
It reproduces, byte-for-byte, what MRI's Base64 methods compute:
- Encode64 — RFC 2045 (pack("m")): standard +/ alphabet, a newline every 60 output characters and a trailing newline.
- Decode64 — lenient (unpack1("m")): every non-alphabet byte is dropped, padding is optional, an orphaned final sextet is discarded.
- StrictEncode64 — pack("m0"): standard alphabet, no newlines.
- StrictDecode64 — unpack1("m0"): strict, returns ErrInvalid on any byte that is not part of a well-formed padded base64 string.
- UrlsafeEncode64 — the -_ alphabet; padding is on by default and stripped when padding=false.
- UrlsafeDecode64 — the -_ alphabet, RFC-4648; accepts correctly-padded or unpadded input, rejects everything else.
The hot standard-alphabet encode/decode paths run on the SIMD kernels of github.com/go-simd/base64 (go-asmgen: amd64 SSE/AVX2, arm64 NEON, ppc64le VSX, s390x vector; stdlib fallback elsewhere), so the output stays bit-identical to encoding/base64.StdEncoding while going faster on the supported arches. Only the MRI-specific framing (60-column wrapping, lenient filtering, the urlsafe padding rules) is hand-written here.
It is the Base64 backend for github.com/go-embedded-ruby/ruby, but is a standalone, reusable module with no dependency on the Ruby runtime — a sibling of go-ruby-yaml/yaml, go-ruby-regexp/regexp and go-ruby-erb/erb.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrInvalid = errors.New("invalid base64")
ErrInvalid is returned by StrictDecode64 and UrlsafeDecode64 for input that is not a well-formed base64 string. It mirrors the ArgumentError("invalid base64") MRI raises from the strict decoders.
Functions ¶
func Decode64 ¶
Decode64 decodes a base64 string leniently, matching MRI's Base64.decode64 (String#unpack1("m")). It reproduces the C unpack("m") semantics exactly: bytes outside the standard alphabet are dropped; the surviving characters are gathered into 4-char quads; a '=' that lands on a 2- or 3-char partial quad finalises that quad and stops decoding (trailing padding terminates the stream), while a '=' on a quad boundary is ignored; and a lone trailing sextet at end-of-input is discarded.
Rather than fold sextets one byte at a time (the shape of MRI's C loop, but slow in Go), it de-spaces the input with github.com/go-simd/base64's vectorised Compact kernel — a branch-free SWAR pass that copies the alphabet bytes, drops whitespace/newlines/stray bytes, and applies the very same '='-on-a-partial-quad stop rule MRI's C loop uses — then hands the packed alphabet run to that package's batched SIMD decoder. Both passes run over a single mutable copy of the input: Compact de-spaces in place and Decode decodes in place (its documented in-place contract — the write cursor always trails the read cursor), so the whole decode costs one buffer for the working copy plus the result string, matching the allocation profile of MRI's C unpack("m") instead of the extra scratch+output buffers a naive Go port needs. Only the 2/3-char tail is finished by hand. The result is byte-identical to the previous decoder, taking the lenient path from behind MRI to ahead of it (including YJIT).
func Encode64 ¶
Encode64 returns the RFC-2045 base64 encoding of s, matching MRI's Base64.encode64 (Array#pack("m")): the standard +/ alphabet with a newline inserted every 60 output characters and a trailing newline. The empty string encodes to the empty string (no trailing newline), exactly as MRI.
func StrictDecode64 ¶
StrictDecode64 decodes a strictly-padded standard base64 string, matching MRI's Base64.strict_decode64 (String#unpack1("m0")). It returns ErrInvalid for any input that is not a well-formed padded base64 string (stray characters, whitespace, wrong padding).
func StrictEncode64 ¶
StrictEncode64 returns the base64 encoding of s with no newlines, matching MRI's Base64.strict_encode64 (Array#pack("m0")).
func UrlsafeDecode64 ¶
UrlsafeDecode64 decodes a url-safe (-_) RFC-4648 base64 string, matching MRI's Base64.urlsafe_decode64: correctly-padded or unpadded input is accepted, and anything else (stray characters, wrong padding) returns ErrInvalid.
func UrlsafeEncode64 ¶
UrlsafeEncode64 returns the url-safe (-_) base64 encoding of s, matching MRI's Base64.urlsafe_encode64. Padding is included by default; when padding is false the trailing = characters are stripped. The variadic padding argument lets the Go caller mirror Ruby's `padding:` keyword (default true).
Types ¶
This section is empty.
