nupp.codec.base64

Standard base64.

const base64 = require("nupp.codec.base64")

const text = base64.encode("Nupp")

print(text)                          -- TnVwcA==
print(assert(base64.decode(text)))   -- Nupp

Both entries are @aot, and both work a word at a time rather than a byte at a time: twelve input bytes are three aligned words and four twenty-four bit groups, and four output bytes are one word. The alphabet and its inverse are const strings, so a compiled entry reads them out of static arrays whose bounds the code generator holds rather than out of a rooted argument whose length it has to load. With aot = "off" this is the same source running on LuaJIT.

bench/base64 measures what that is worth, and measures it against the vectorized C it could have been.

Module contents

Functions

FunctionKindDescription
decodefunctionDecodes canonical standard base64 with required padding.
encodefunctionEncodes bytes as standard base64, padded.

Functions#

base64.decodefunction#

function base64.decode(text: string): string?, string?

Decodes canonical standard base64 with required padding.

Text that is not canonical padded base64 is an ordinary answer, as it is for nupp.codec.hex.decode: nil and the reason.

assert(nupp.codec.base64.decode("TnVwcA==") == "Nupp")
local bytes, reason = nupp.codec.base64.decode("TnVwcA")
assert(bytes == nil and reason ~= nil)

Arguments

NameTypeDescription
textstring

the text to decode

Returns

TypeDescription
string?

the decoded bytes, or nil when the text is not base64

string?

why the text is not base64, when it is not

base64.encodefunction#

function base64.encode(value: string): string

Encodes bytes as standard base64, padded.

assert(nupp.codec.base64.encode("Nupp") == "TnVwcA==")

Arguments

NameTypeDescription
valuestring

the bytes to encode

Returns

TypeDescription
string

the encoded text