std.base64.Encoder
type pub EncoderA 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: BoolIf padding should be applied.
Static methods
new
Show source codeHide source code
fn pub static new -> Encoder {
Encoder(
table0: ENCODE_STD.get(0).or_panic,
table1: ENCODE_STD.get(1).or_panic,
table2: ENCODE_STD.get(2).or_panic,
padding: true,
)
}fn pub static new -> EncoderReturns 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).or_panic,
table1: ENCODE_URL.get(1).or_panic,
table2: ENCODE_URL.get(2).or_panic,
padding: true,
)
}fn pub static url_safe -> EncoderReturns an Encoder that uses the URL safe base64 alphabet.
Instance methods
encode
Show source codeHide source code
fn pub encode[B: Bytes](input: ref B, 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).or_panic
let b1 = input.get(i + 1).or_panic
let b2 = input.get(i + 2).or_panic
into.push(t0.get(b0).or_panic)
into.push(t1.get((b0 & 0x03 << 4) | (b1 >> 4 & 0x0F)).or_panic)
into.push(t1.get((b1 & 0x0F << 2) | (b2 >> 6 & 0x03)).or_panic)
into.push(t2.get(b2).or_panic)
i += 3
}
}
match size - i {
case 0 -> {}
case 1 -> {
let b0 = input.get(i).or_panic
into.push(t0.get(b0).or_panic)
into.push(t1.get(b0 & 0x03 << 4).or_panic)
if @padding {
into.push(PAD)
into.push(PAD)
}
}
case _ -> {
let b0 = input.get(i).or_panic
let b1 = input.get(i + 1).or_panic
into.push(t0.get(b0).or_panic)
into.push(t1.get((b0 & 0x03 << 4) | (b1 >> 4 & 0x0F)).or_panic)
into.push(t2.get(b1 & 0x0F << 2).or_panic)
if @padding { into.push(PAD) }
}
}
}fn pub encode[B: Bytes](input: ref B, into: mut ByteArray)Encodes the input Bytes 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', out)
out.into_string #=> 'aGVsbG8gd29ybGQ='