struct. buffer

Represents a struct buffer instance created by buffer().

Example
const buf = struct.buffer();

buf.put('I', 12345);

const value = buf.get('I');

Methods

end() → {buffer}

Set the buffer position to the end.

Returns: buffer

The buffer instance.

Example
buf.end();

get(format) → {*}

Unpack a single value from the buffer at the current position.

The get() function unpacks a single value from the buffer according to the specified format string, starting at the current buffer position. The format string follows the same syntax as used in struct.unpack().

For a detailed explanation of the format string syntax, refer to the "Format Strings" section in the module documentation.

Alternatively, get() accepts a postive or negative integer as format, which specifies the length of a string to unpack before or after the current position. Negative values extract that many bytes before the current offset while postive ones extracts that many bytes after.

Parameters:
NameTypeDescription
formatstring | number

The format string specifying how to unpack the data.

Returns: *

The unpacked value.

Example
const val = buf.get('I');
const str = buf.get(5);    // equivalent to buf.get('5s')
const str = buf.get(-3);   // equivalent to buf.pos(buf.pos() - 3).get('3s')

get(format) → {array}

Unpack multiple values from the buffer at the current position.

The read() function unpacks multiple values from the buffer according to the specified format string, starting at the current buffer position. The format string follows the same syntax as used in struct.unpack().

For a detailed explanation of the format string syntax, refer to the "Format Strings" section in the module documentation.

Parameters:
NameTypeDescription
formatstring

The format string specifying how to unpack the data.

Returns: array

An array containing the unpacked values.

Example
const values = buf.get('II');

length(lengthopt) → {number|buffer}

Get or set the current buffer length.

If called without arguments, returns the current length of the buffer. If called with a length argument, sets the buffer length to that value, padding the data with trailing zero bytes or truncating it depending on whether the updated length is larger or smaller than the current length respectively.

In case the updated length is smaller than the current buffer offset, the position is updated accordingly, so that it points to the new end of the truncated buffer data.

Parameters:
NameTypeDescription
lengthnumber(optional)

The length to set. If omitted, the current length is returned.

Returns: number | buffer

If called without arguments, returns the current length. If called with a length argument, returns the buffer instance for chaining.

Example
const buf = struct.buffer("abc"); // Initialize buffer with three bytes
const currentLen = buf.length();  // Returns 3

buf.length(6);                    // Extend to 6 bytes
buf.slice();                      // Trailing null bytes: "abc\x00\x00\x00"

buf.length(2);                    // Truncate to 2 bytes
buf.slice();                      // Truncated data: "ab"

pos(positionopt) → {number|buffer}

Get or set the current position in the buffer.

If called without arguments, returns the current position. If called with a position argument, sets the current position to that value.

Parameters:
NameTypeDescription
positionnumber(optional)

The position to set. If omitted, the current position is returned.

Returns: number | buffer

If called without arguments, returns the current position. If called with a position argument, returns the buffer instance for chaining.

Example
const currentPos = buf.pos();
buf.pos(10);  // Set position to 10

pull() → {string}

Extract and remove all content from the buffer.

The pull() function returns all content of the buffer as a string and resets the buffer to an empty state.

Returns: string

A string containing all the buffer content.

Example
const allData = buf.pull();

put(format, …values) → {buffer}

Pack data into the buffer at the current position.

The put() function packs the given values into the buffer according to the specified format string, starting at the current buffer position. The format string follows the same syntax as used in struct.pack().

For a detailed explanation of the format string syntax, refer to the "Format Strings" section in the module documentation.

Parameters:
NameTypeDescription
formatstring

The format string specifying how to pack the data.

values*(repeatable)

The values to pack into the buffer.

Returns: buffer

The buffer instance.

Example
buf.put('II', 1234, 5678);

set(valueopt, startopt, endopt) → {buffer}

Set a slice of the buffer content to given byte value.

The set() function overwrites a substring of the buffer content with the given byte value, similar to the C memset() function, between the specified start and end positions.

Both the start and end position values may be negative, in which case they're relative to the end of the buffer, e.g. set(0, -2) will overwrite the last two bytes of data with \x00.

When the start or end positions are beyond the current buffer length, the buffer is grown accordingly.

Parameters:
NameTypeDescription
valuenumber | string(optional, default: 0)

The byte value to use when overwriting buffer contents. When a string is given, the first character is used as value.

startnumber(optional, default: 0)

The position to start overwriting from.

endnumber(optional, default: buffer.length())

The position to end overwriting (exclusive).

Returns: buffer

The buffer instance.

Example
const buf = struct.buffer("abcde");
buf.set("X", 2, 4).slice();  // Buffer content is now "abXXe"
buf.set().slice();           // Buffer content is now "\x00\x00\x00\x00\x00"

slice(startopt, endopt) → {string}

Extract a slice of the buffer content.

The slice() function returns a substring of the buffer content between the specified start and end positions.

Both the start and end position values may be negative, in which case they're relative to the end of the buffer, e.g. slice(-3) will extract the last three bytes of data.

Parameters:
NameTypeDescription
startnumber(optional, default: 0)

The starting position of the slice.

endnumber(optional, default: buffer.length())

The ending position of the slice (exclusive).

Returns: string

A string containing the specified slice of the buffer content.

Example
const slice = buf.slice(4, 8);

start() → {buffer}

Set the buffer position to the start (0).

Returns: buffer

The buffer instance.

Example
buf.start();