# dmd.common.outbuffer

An expandable buffer in which you can write text or binary data.

#### Authors

Walter Bright, https://www.digitalmars.com

#### License

Boost License 1.0

Source: root/outbuffer.d

Documentation: https://dlang.org/phobos/dmd_root_outbuffer.html

Coverage: https://codecov.io/gh/dlang/dmd/src/master/src/dmd/root/outbuffer.d

- `OutBuffer`

  #### Declaration

  `struct `OutBuffer`;`

  ``OutBuffer`` is a write-only output stream of untyped data. It is backed up by a contiguous array or a memory-mapped file.

  - `doindent`

    #### Declaration

    `bool `doindent`;`

    Whether to indent

  - `spaces`

    #### Declaration

    `bool `spaces`;`

    Whether to indent by 4 `spaces` or by tabs;

  - `level`

    #### Declaration

    `int `level`;`

    Current indent `level`

  - `this`

    #### Declaration

    `nothrow @safe this(size_t `initialSize`);`

    Construct given size.

  - `this`

    #### Declaration

    `nothrow @trusted this(const(char)* `filename`);`

    Construct from `filename`. Will map the file into memory (or create it anew if necessary) and start writing at the beginning of it.

    #### Parameters

    `const(char)* `filename`` | zero-terminated name of file to map into memory

  - `dtor`

    #### Declaration

    `pure nothrow @trusted void `dtor`();`

    Frees resources associated.

  - `buf`

    #### Declaration

    `nothrow @system ubyte* `buf`();`

    For porting with ease from dmd.backend.outbuf.Outbuffer

  - `bufptr`

    #### Declaration

    `nothrow @system ubyte** `bufptr`();`

    For porting with ease from dmd.backend.outbuf.Outbuffer

  - `extractData`

    #### Declaration

    `pure nothrow @nogc @trusted char* `extractData`();`

    Transfer ownership of the allocated data to the caller.

    #### Return Value

    pointer to the allocated data

  - `destroy`

    #### Declaration

    `pure nothrow @trusted void `destroy`();`

    Releases all resources associated with `this` and resets it as an empty memory buffer. The config variables `notlinehead`, `doindent` etc. are not changed.

  - `reserve`

    #### Declaration

    `pure nothrow @trusted void `reserve`(size_t `nbytes`);`

    Reserves ``nbytes`` bytes of additional memory (or file space) in advance. The resulting capacity is at least the previous length plus ``nbytes``.

    #### Parameters

    `size_t `nbytes`` | the number of additional bytes to `reserve`

  - `setsize`

    #### Declaration

    `pure nothrow @nogc @safe void `setsize`(size_t `size`);`

    Shrink the `size` of the data to ``size``.

    #### Parameters

    `size_t `size`` | new `size` of data, must be <= `.length`

  - `write16n`

    #### Declaration

    `nothrow @trusted void `write16n`(int `v`);`

    Writes a 16 bit value, no reserve check.

  - `write16`

    #### Declaration

    `nothrow void `write16`(int `v`);`

    Writes a 16 bit value.

  - `write32`

    #### Declaration

    `nothrow @trusted void `write32`(int `v`);`

    Writes a 32 bit int.

  - `write64`

    #### Declaration

    `nothrow @trusted void `write64`(long `v`);`

    Writes a 64 bit int.

  - `writestring`

    #### Declaration

    `pure nothrow @system void `writestring`(const(char)* `s`);
    pure nothrow @safe void `writestring`(scope const(char)[] `s`);
    pure nothrow @safe void `writestring`(scope string `s`);`

    NOT zero-terminated

  - `writestringln`

    #### Declaration

    `pure nothrow @safe void `writestringln`(const(char)[] `s`);`

    NOT zero-terminated, followed by newline

  - `writeStringz`

    #### Declaration

    `pure nothrow @system void `writeStringz`(const(char)* `s`);
    pure nothrow @safe void `writeStringz`(const(char)[] `s`);
    pure nothrow @safe void `writeStringz`(string `s`);`

    Write string to buffer, ensure it is zero terminated

  - `writenl`

    #### Declaration

    `pure nothrow @safe void `writenl`();`

    strip trailing tabs or spaces, write newline

  - `writeByten`

    #### Declaration

    `nothrow @trusted void `writeByten`(int `b`);`

    Writes an 8 bit byte, no reserve check.

  - `allocate`

    #### Declaration

    `pure nothrow @safe char[] `allocate`(size_t `nbytes`);`

    Allocate space, but leave it uninitialized.

    #### Parameters

    `size_t `nbytes`` | amount to `allocate`

    #### Return Value

    slice of the allocated space to be filled in

  - `print`

    #### Declaration

    `pure nothrow @safe void `print`(ulong `u`);`

    Convert ``u`` to a string and append it to the buffer.

    #### Parameters

    `ulong `u`` | integral value to append

  - `bracket`

    #### Declaration

    `pure nothrow @system size_t `bracket`(size_t `i`, const(char)* `left`, size_t `j`, const(char)* `right`);`

    Insert `left` at `i`, and `right` at `j`. Return index just past `right`.

  - `insert`

    #### Declaration

    `pure nothrow @system size_t `insert`(size_t `offset`, const(void)* `p`, size_t `nbytes`);`

    #### Return Value

    `offset` + `nbytes`

  - `opSlice`

    #### Declaration

    `const pure nothrow @nogc @safe const(char)[] `opSlice`();`

    #### Return Value

    a non-owning const slice of the buffer contents

  - `extractSlice`

    #### Declaration

    `pure nothrow char[] `extractSlice`(bool `nullTerminate` = false);`

    Extract the data as a slice and take ownership of it.

    #### Discussion

    When `true` is passed as an argument, this function behaves like `dmd.utils.toDString(thisbuffer.extractChars())`.

    #### Parameters

    `bool `nullTerminate`` | When `true`, the data will be `null` terminated. This is useful to call C functions or store the result in `Strings`. Defaults to `false`.

  - `moveToFile`

    #### Declaration

    `nothrow @system bool `moveToFile`(const char* `filename`);`

    Destructively saves the contents of `this` to ``filename``. As an optimization, if the file already has identical contents with the buffer, no copying is done. This is because on SSD drives reading is often much faster than writing and because there's a high likelihood an identical file is written during the build process.

    #### Parameters

    `char* `filename`` | the name of the file to receive the contents

    #### Return Value

    `true` iff the operation succeeded.