write

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

  1. void write(R range, T value, size_t index)
  2. void write(R range, T value, size_t* index)
    void
    write
    (
    T
    Endian endianness = Endian.bigEndian
    R
    )
    (,
    const T value
    ,
    size_t* index
    )
    if (
    canSwapEndianness!T &&
    &&
    &&
    is(ElementType!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 write to.

value T

The value to write.

index size_t*

The index to start writing to. If index is a pointer, then it is updated to the index after the bytes read.

Examples

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];
buffer.write!uint(29110231u, 0);
assert(buffer == [1, 188, 47, 215, 0, 0, 0, 0]);

buffer.write!ushort(927, 0);
assert(buffer == [3, 159, 47, 215, 0, 0, 0, 0]);

buffer.write!ubyte(42, 0);
assert(buffer == [42, 159, 47, 215, 0, 0, 0, 0]);
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0];
buffer.write!uint(142700095u, 2);
assert(buffer == [0, 0, 8, 129, 110, 63, 0, 0, 0]);

buffer.write!ushort(19839, 2);
assert(buffer == [0, 0, 77, 127, 110, 63, 0, 0, 0]);

buffer.write!ubyte(132, 2);
assert(buffer == [0, 0, 132, 127, 110, 63, 0, 0, 0]);
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];
size_t index = 0;
buffer.write!ushort(261, &index);
assert(buffer == [1, 5, 0, 0, 0, 0, 0, 0]);
assert(index == 2);

buffer.write!uint(369700095u, &index);
assert(buffer == [1, 5, 22, 9, 44, 255, 0, 0]);
assert(index == 6);

buffer.write!ubyte(8, &index);
assert(buffer == [1, 5, 22, 9, 44, 255, 8, 0]);
assert(index == 7);

bool

ubyte[] buffer = [0, 0];
buffer.write!bool(false, 0);
assert(buffer == [0, 0]);

buffer.write!bool(true, 0);
assert(buffer == [1, 0]);

buffer.write!bool(true, 1);
assert(buffer == [1, 1]);

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

size_t index = 0;
buffer.write!bool(false, &index);
assert(buffer == [0, 0]);
assert(index == 1);

buffer.write!bool(true, &index);
assert(buffer == [0, 1]);
assert(index == 2);

char(8-bit)

ubyte[] buffer = [0, 0, 0];

buffer.write!char('a', 0);
assert(buffer == [97, 0, 0]);

buffer.write!char('b', 1);
assert(buffer == [97, 98, 0]);

size_t index = 0;
buffer.write!char('a', &index);
assert(buffer == [97, 98, 0]);
assert(index == 1);

buffer.write!char('b', &index);
assert(buffer == [97, 98, 0]);
assert(index == 2);

buffer.write!char('c', &index);
assert(buffer == [97, 98, 99]);
assert(index == 3);

wchar (16bit - 2x ubyte)

ubyte[] buffer = [0, 0, 0, 0];

buffer.write!wchar('ą', 0);
assert(buffer == [1, 5, 0, 0]);

buffer.write!wchar('”', 2);
assert(buffer == [1, 5, 32, 29]);

size_t index = 0;
buffer.write!wchar('ć', &index);
assert(buffer == [1, 7, 32, 29]);
assert(index == 2);

buffer.write!wchar('ą', &index);
assert(buffer == [1, 7, 1, 5]);
assert(index == 4);

dchar (32bit - 4x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

buffer.write!dchar('ą', 0);
assert(buffer == [0, 0, 1, 5, 0, 0, 0, 0]);

buffer.write!dchar('”', 4);
assert(buffer == [0, 0, 1, 5, 0, 0, 32, 29]);

size_t index = 0;
buffer.write!dchar('ć', &index);
assert(buffer == [0, 0, 1, 7, 0, 0, 32, 29]);
assert(index == 4);

buffer.write!dchar('ą', &index);
assert(buffer == [0, 0, 1, 7, 0, 0, 1, 5]);
assert(index == 8);

float (32bit - 4x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

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

buffer.write!float(25.0f, 4);
assert(buffer == [66, 0, 0, 0, 65, 200, 0, 0]);

size_t index = 0;
buffer.write!float(25.0f, &index);
assert(buffer == [65, 200, 0, 0, 65, 200, 0, 0]);
assert(index == 4);

buffer.write!float(32.0f, &index);
assert(buffer == [65, 200, 0, 0, 66, 0, 0, 0]);
assert(index == 8);

double (64bit - 8x ubyte)

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

buffer.write!double(32.0, 0);
assert(buffer == [64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);

buffer.write!double(25.0, 8);
assert(buffer == [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]);

size_t index = 0;
buffer.write!double(25.0, &index);
assert(buffer == [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]);
assert(index == 8);

buffer.write!double(32.0, &index);
assert(buffer == [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]);
assert(index == 16);

enum

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

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

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

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

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

size_t index = 0;
buffer.write!Foo(Foo.three, &index);
assert(buffer == [0, 0, 0, 30, 0, 0, 0, 20, 0, 0, 0, 30]);
assert(index == 4);

buffer.write!Foo(Foo.one, &index);
assert(buffer == [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 30]);
assert(index == 8);

buffer.write!Foo(Foo.two, &index);
assert(buffer == [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 20]);
assert(index == 12);

enum - float

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];

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

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

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

size_t index = 0;
buffer.write!Float(Float.two, &index);
assert(buffer == [65, 200, 0, 0, 65, 200, 0, 0]);
assert(index == 4);

buffer.write!Float(Float.one, &index);
assert(buffer == [65, 200, 0, 0, 66, 0, 0, 0]);
assert(index == 8);

enum - double

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

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

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

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

size_t index = 0;
buffer.write!Double(Double.two, &index);
assert(buffer == [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]);
assert(index == 8);

buffer.write!Double(Double.one, &index);
assert(buffer == [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]);
assert(index == 16);

enum - real

ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

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

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

Meta