On Thursday, 27 November 2014 at 16:08:13 UTC, Andre wrote:
Hi,
I implement a network protocol and use an Appender!(ubyte[])().
I have following issue. The first three bytes I have to fill,
the following bytes are reserved and must be 0. In this example
the overall header length must be 8.
import std.array: appender;
const HEADER_LENGTH = 8;
auto app = appender!(ubyte[])();
app.put(cast(ubyte)40);
app.put(cast(ubyte)5);
app.put(cast(ubyte)234);
// ... add 5 times 0
// variable length body will follow
In case of dynamic array I can simple set the length to 8.
Appender doesn't have a length attribute.
Is there some other nice D functionaliy I can use?
Maybe some functionality in std.array is missing: app.fill(0,
HEADER_LENGTH)?
Currently I do a work around with a for loop.
Kind regards
André
You can initialize the appender with an existing array, or put an
entire array into it at once:
import std.array: appender;
ubyte[] temp;
temp.reserve(8); // reserve first, so that only one
allocation happens
temp[0 .. 3] = [40, 5, 234];
temp.length = 8;
auto app = appender!(ubyte[])(temp);
// or:
app.put(temp);
The array will not be copied when the appender is constructed.