append

Takes an integral value, converts it to the given endianness, and appends it to the given range of ubytes (using put) as a sequence of T.sizeof ubytes starting at index. hasSlicing!R must be true.

void
append
(
T
Endian endianness = Endian.bigEndian
R
)
(,
const T value
)
if (
canSwapEndianness!T &&
isOutputRange!(R, ubyte)
)

Parameters

T

The integral type to convert the first T.sizeof bytes to.

endianness

The endianness to write the bytes in.

range R

The range to append to.

value T

The value to append.

Examples

import std.array;
auto buffer = appender!(const ubyte[])();
buffer.append!ushort(261);
assert(buffer.data == [1, 5]);

buffer.append!uint(369700095u);
assert(buffer.data == [1, 5, 22, 9, 44, 255]);

buffer.append!ubyte(8);
assert(buffer.data == [1, 5, 22, 9, 44, 255, 8]);

bool

import std.array : appender;
auto buffer = appender!(const ubyte[])();

buffer.append!bool(true);
assert(buffer.data == [1]);

buffer.append!bool(false);
assert(buffer.data == [1, 0]);

char wchar dchar

import std.array : appender;
auto buffer = appender!(const ubyte[])();

buffer.append!char('a');
assert(buffer.data == [97]);

buffer.append!char('b');
assert(buffer.data == [97, 98]);

buffer.append!wchar('ą');
assert(buffer.data == [97, 98, 1, 5]);

buffer.append!dchar('ą');
    assert(buffer.data == [97, 98, 1, 5, 0, 0, 1, 5]);

float double

import std.array : appender;
auto buffer = appender!(const ubyte[])();

buffer.append!float(32.0f);
assert(buffer.data == [66, 0, 0, 0]);

buffer.append!double(32.0);
assert(buffer.data == [66, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]);

enum

import std.array : appender;
auto buffer = appender!(const ubyte[])();

enum Foo
{
    one = 10,
    two = 20,
    three = 30
}

buffer.append!Foo(Foo.one);
assert(buffer.data == [0, 0, 0, 10]);

buffer.append!Foo(Foo.two);
assert(buffer.data == [0, 0, 0, 10, 0, 0, 0, 20]);

buffer.append!Foo(Foo.three);
assert(buffer.data == [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 30]);

enum - bool

import std.array : appender;
auto buffer = appender!(const ubyte[])();

enum Bool: bool
{
    bfalse = false,
    btrue = true,
}

buffer.append!Bool(Bool.btrue);
assert(buffer.data == [1]);

buffer.append!Bool(Bool.bfalse);
assert(buffer.data == [1, 0]);

buffer.append!Bool(Bool.btrue);
assert(buffer.data == [1, 0, 1]);

enum - float

import std.array : appender;
auto buffer = appender!(const ubyte[])();

enum Float: float
{
    one = 32.0f,
    two = 25.0f
}

buffer.append!Float(Float.one);
assert(buffer.data == [66, 0, 0, 0]);

buffer.append!Float(Float.two);
assert(buffer.data == [66, 0, 0, 0, 65, 200, 0, 0]);

enum - double

import std.array : appender;
auto buffer = appender!(const ubyte[])();

enum Double: double
{
    one = 32.0,
    two = 25.0
}

buffer.append!Double(Double.one);
assert(buffer.data == [64, 64, 0, 0, 0, 0, 0, 0]);

buffer.append!Double(Double.two);
assert(buffer.data == [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]);

enum - real

import std.array : appender;
auto buffer = appender!(const ubyte[])();

enum Real: real
{
    one = 32.0,
    two = 25.0
}

static assert(!__traits(compiles, buffer.append!Real(Real.one)));

Meta