Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page.Requires a signed-in GitHub account. This works well for small changes.If you'd like to make larger changes you may want to consider usinga local clone.

std.outbuffer

Serialize data toubyte arrays.
License:
Boost License 1.0.
Authors:
Walter Bright

Sourcestd/outbuffer.d

classOutBuffer;
OutBuffer provides a way to build up an array of bytes out of raw data. It is useful for things like preparing an array of bytes to write out to a file. OutBuffer's byte order is the format native to the computer. To control the byte order (endianness), use a class derived from OutBuffer.
OutBuffer's internal buffer is allocated with the GC. Pointers stored into the buffer are scanned by the GC, but you have to ensure proper alignment, e.g. by usingalignSize((void*).sizeof).
Examples:
import std.string : cmp;OutBuffer buf =newOutBuffer();writeln(buf.offset);// 0buf.write("hello");buf.write(cast(byte) 0x20);buf.write("world");buf.writef(" %d", 62665);writeln(cmp(buf.toString(),"hello world 62665"));// 0buf.clear();writeln(cmp(buf.toString(),""));// 0buf.write("New data");writeln(cmp(buf.toString(),"New data"));// 0
pure nothrow @safe inout(ubyte)[]toBytes() inout scope;
Convert to array of bytes.
pure nothrow @trusted voidreserve(size_tnbytes);
Preallocate nbytes more to the size of the internal buffer.
This is a speed optimization, a good guess at the maximum size of the resulting buffer will improve performance by eliminating reallocations and copying.
aliasput = write;
put enables OutBuffer to be used as an OutputRange.
pure nothrow @safe voidwrite(scope const(ubyte)[]bytes);

pure nothrow @safe voidwrite(byteb);

pure nothrow @safe voidwrite(charc);

pure nothrow @safe voidwrite(dcharc);

pure nothrow @safe voidwrite(shorts);

pure nothrow @safe voidwrite(inti);

pure nothrow @safe voidwrite(longl);
Append data to the internal buffer.
pure nothrow @safe voidfill(size_tnbytes, ubyteval = 0);
Append nbytes of val to the internal buffer.
Parameters:
size_tnbytesNumber of bytes to fill.
ubytevalValue to fill, defaults to 0.
pure nothrow @safe voidfill0(size_tnbytes);
Append nbytes of 0 to the internal buffer.

Paramnbytes - number of bytes to fill.

pure nothrow @safe voidalignSize(size_talignsize, ubyteval = 0);
Append bytes until the buffer aligns on a power of 2 boundary.
By default fills with 0 bytes.
Parameters:
size_talignsizeAlignment value. Must be power of 2.
ubytevalValue to fill, defaults to 0.
Examples:
OutBuffer buf =new OutBuffer();buf.write(cast(ubyte) 1);buf.align2();writeln(buf.toBytes());// "\x01\x00"buf.write(cast(ubyte) 2);buf.align4();writeln(buf.toBytes());// "\x01\x00\x02\x00"buf.write(cast(ubyte) 3);buf.alignSize(8);writeln(buf.toBytes());// "\x01\x00\x02\x00\x03\x00\x00\x00"
Examples:
ditto
OutBuffer buf =new OutBuffer();buf.write(cast(ubyte) 1);buf.align2(0x55);writeln(buf.toBytes());// "\x01\x55"buf.write(cast(ubyte) 2);buf.align4(0x55);writeln(buf.toBytes());// "\x01\x55\x02\x55"buf.write(cast(ubyte) 3);buf.alignSize(8, 0x55);writeln(buf.toBytes());// "\x01\x55\x02\x55\x03\x55\x55\x55"
pure nothrow @safe voidclear();
Clear the data in the buffer
pure nothrow @safe voidalign2(ubyteval = 0);
Optimize common special case alignSize(2)
Parameters:
ubytevalValue to fill, defaults to 0.
pure nothrow @safe voidalign4(ubyteval = 0);
Optimize common special case alignSize(4)
Parameters:
ubytevalValue to fill, defaults to 0.
pure nothrow @safe stringtoString() const;
Convert internal buffer to array of chars.
nothrow @system voidvprintf(scope stringformat, va_listargs);
Append output of C's vprintf() to internal buffer.
@system voidprintf(scope stringformat, ...);
Append output of C's printf() to internal buffer.
voidwritef(Char, A...)(scope const(Char)[]fmt, Aargs);

voidwritef(alias fmt, A...)(Aargs)
if (isSomeString!(typeof(fmt)));
Formats and writes its arguments in text format to the OutBuffer.
Parameters:
const(Char)[]fmtformat string as described instd.format.formattedWrite
Aargsarguments to be formatted
Examples:
OutBuffer b =new OutBuffer();b.writef("a%sb", 16);writeln(b.toString());// "a16b"
Examples:
OutBuffer b =new OutBuffer();b.writef!"a%sb"(16);writeln(b.toString());// "a16b"
voidwritefln(Char, A...)(scope const(Char)[]fmt, Aargs);

voidwritefln(alias fmt, A...)(Aargs)
if (isSomeString!(typeof(fmt)));
Formats and writes its arguments in text format to the OutBuffer, followed by a newline.
Parameters:
const(Char)[]fmtformat string as described instd.format.formattedWrite
Aargsarguments to be formatted
Examples:
OutBuffer b =new OutBuffer();b.writefln("a%sb", 16);writeln(b.toString());// "a16b\n"
Examples:
OutBuffer b =new OutBuffer();b.writefln!"a%sb"(16);writeln(b.toString());// "a16b\n"
pure nothrow @safe voidspread(size_tindex, size_tnbytes);
At offset index into buffer, create nbytes of space by shifting upwards all data past index.
Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Fri Feb 20 17:58:45 2026

[8]ページ先頭

©2009-2026 Movatter.jp