std.base64.Encoder
type pub Encoder
A type for encoding raw input as a base64 sequence of bytes.
Line wrapping isn't performed by this type, so if line sizes must be limited this must be done manually after encoding the data as base64.
Padding is applied by default. This can be disabled by setting
Encoder.padding
to false
:
import std.base64 (Encoder)
let enc = Encoder.new
enc.padding = false
Examples
Encoding input using the standard alphabet:
import std.base64 (Encoder)
let in = 'abcdefghijklmnopqrstuvwxyz0123456789|@{}/&#[]\\!?()<>=+*;\'"`:^%$~'.to_byte_array
let out = ByteArray.new
Encoder.new.encode(in, out)
out.into_string #=> 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5fEB7fS8mI1tdXCE/KCk8Pj0rKjsnImA6XiUkfg=='
Encoding input using the URL safe alphabet:
import std.base64 (Encoder)
let in = 'abcdefghijklmnopqrstuvwxyz0123456789|@{}/&#[]\\!?()<>=+*;\'"`:^%$~'.to_byte_array
let out = ByteArray.new
Encoder.url_safe.encode(in, out)
out.into_string #=> 'YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXowMTIzNDU2Nzg5fEB7fS8mI1tdXCE_KCk8Pj0rKjsnImA6XiUkfg=='
Fields
padding
let pub @padding: Bool
If padding should be applied.
Static methods
new
Show source codeHide source code
fn pub static new -> Encoder {
Encoder(
table0: ENCODE_STD.get(0),
table1: ENCODE_STD.get(1),
table2: ENCODE_STD.get(2),
padding: true,
)
}
fn pub static new -> Encoder
Returns an Encoder
that uses the standard base64 alphabet.
url_safe
Show source codeHide source code
fn pub static url_safe -> Encoder {
Encoder(
table0: ENCODE_URL.get(0),
table1: ENCODE_URL.get(1),
table2: ENCODE_URL.get(2),
padding: true,
)
}
fn pub static url_safe -> Encoder
Returns an Encoder
that uses the URL safe base64 alphabet.
Instance methods
encode
Show source codeHide source code
fn pub encode(input: ref ByteArray, into: mut ByteArray) {
# The implementation here is based on the base64 encoder found in Google
# Chrome (https://github.com/chromium/chromium/blob/fd8a8914ca0183f0add65ae55f04e287543c7d4a/third_party/modp_b64/modp_b64.cc).
let t0 = @table0
let t1 = @table1
let t2 = @table2
let size = input.size
let mut i = 0
if size > 2 {
let max = size - 2
while i < max {
let b0 = input.get(i)
let b1 = input.get(i + 1)
let b2 = input.get(i + 2)
into.push(t0.get(b0))
into.push(t1.get((b0 & 0x03 << 4) | (b1 >> 4 & 0x0F)))
into.push(t1.get((b1 & 0x0F << 2) | (b2 >> 6 & 0x03)))
into.push(t2.get(b2))
i += 3
}
}
match size - i {
case 0 -> {}
case 1 -> {
let b0 = input.get(i)
into.push(t0.get(b0))
into.push(t1.get(b0 & 0x03 << 4))
if @padding {
into.push(PAD)
into.push(PAD)
}
}
case _ -> {
let b0 = input.get(i)
let b1 = input.get(i + 1)
into.push(t0.get(b0))
into.push(t1.get((b0 & 0x03 << 4) | (b1 >> 4 & 0x0F)))
into.push(t2.get(b1 & 0x0F << 2))
if @padding { into.push(PAD) }
}
}
}
fn pub encode(input: ref ByteArray, into: mut ByteArray)
Encodes the input
ByteArray
into a sequence of base64 encoded bytes,
appending the sequence to into.
Examples
import std.base64 (Encoder)
let out = ByteArray.new
Encoder.new.encode('hello world'.to_byte_array, out)
out.into_string #=> 'aGVsbG8gd29ybGQ='