The OpenD Programming Language

encodeBase64

Encode a ubyte array as Base64, placing the result onto an Appender.

  1. string encodeBase64(const(ubyte)[] buf, char plusChar, char slashChar)
  2. void encodeBase64(const(ubyte)[] input, Appender appender, char plusChar, char slashChar)
    @safe pure
    void
    encodeBase64
    (
    Appender
    )
    (
    scope const(ubyte)[] input
    ,
    scope ref Appender appender
    ,
    char plusChar = '+'
    ,
    char slashChar = '/'
    )

Examples

Test encoding of data which has a length that can be cleanly encoded.

// 3 bytes
{
    enum data = cast(immutable(ubyte)[])"ABC";
    assert(data.encodeBase64 == "QUJD");
}

// 6 bytes
{
    enum data = cast(immutable(ubyte)[])"ABCDEF";
    assert(data.encodeBase64 == "QUJDREVG");
}

// 9 bytes
{
    enum data = cast(immutable(ubyte)[])"ABCDEFGHI";
    assert(data.encodeBase64 == "QUJDREVGR0hJ");
}

// 12 bytes
{
    enum data = cast(immutable(ubyte)[])"ABCDEFGHIJKL";
    assert(data.encodeBase64 == "QUJDREVGR0hJSktM");
}

Test encoding of data which has a length which CANNOT be cleanly encoded. This typically means that there's padding.

// 1 byte 
{
    enum data = cast(immutable(ubyte)[])"A";
    assert(data.encodeBase64 == "QQ==");
}
// 2 bytes
{
    enum data = cast(immutable(ubyte)[])"AB";
    assert(data.encodeBase64 == "QUI=");
}
// 2 bytes
{
    enum data = [0xFF, 0xFF];
    assert(data.encodeBase64 == "//8=");
}
// 4 bytes
{
    enum data = [0xDE, 0xAD, 0xBA, 0xBE];
    assert(data.encodeBase64 == "3q26vg==");
}
// 37 bytes
{
    enum data = cast(immutable(ubyte)[])"A Very Very Very Very Large Test Blob";
    assert(data.encodeBase64 == "QSBWZXJ5IFZlcnkgVmVyeSBWZXJ5IExhcmdlIFRlc3QgQmxvYg==");
}

Test nogc encoding

import mir.appender : scopedBuffer;

{
    enum data = cast(immutable(ubyte)[])"A Very Very Very Very Large Test Blob";
    auto appender = scopedBuffer!char();
    data.encodeBase64(appender); 
    assert(appender.data == "QSBWZXJ5IFZlcnkgVmVyeSBWZXJ5IExhcmdlIFRlc3QgQmxvYg==");     
}

{
    enum data = cast(immutable(ubyte)[])"abc123!?$*&()'-=@~";
    auto appender = scopedBuffer!char();
    data.encodeBase64(appender);
    assert(appender.data == "YWJjMTIzIT8kKiYoKSctPUB+");
}

Make sure we can decode what we encode.

// Test an example string
{
    enum data = cast(immutable(ubyte)[])"abc123!?$*&()'-=@~";
    assert(data.encodeBase64.decodeBase64 == data);
}
// Test an example from Ion data
{
    enum data = cast(immutable(ubyte)[])"a b c d e f g h i j k l m n o p q r s t u v w x y z";
    assert(data.encodeBase64.decodeBase64 == data);
}

Meta