Represents a struct buffer instance created by buffer()
.
const buf = struct.buffer();
buf.put('I', 12345);
const value = buf.get('I');
- Source
- See
Methods
end() → {buffer}
Set the buffer position to the end.
The buffer instance.
buf.end();
- Source
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.
Name | Type | Description |
---|---|---|
format | string | | The format string specifying how to unpack the data. |
The unpacked value.
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')
- Source
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.
Name | Type | Description |
---|---|---|
format | string | The format string specifying how to unpack the data. |
An array containing the unpacked values.
const values = buf.get('II');
- Source
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.
Name | Type | Description |
---|---|---|
length | number | (optional) The length to set. If omitted, the current length is returned. |
If called without arguments, returns the current length. If called with a length argument, returns the buffer instance for chaining.
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"
- Source
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.
Name | Type | Description |
---|---|---|
position | number | (optional) The position to set. If omitted, the current position is returned. |
If called without arguments, returns the current position. If called with a position argument, returns the buffer instance for chaining.
const currentPos = buf.pos();
buf.pos(10); // Set position to 10
- Source
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.
A string containing all the buffer content.
const allData = buf.pull();
- Source
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.
Name | Type | Description |
---|---|---|
format | string | The format string specifying how to pack the data. |
values | * | (repeatable) The values to pack into the buffer. |
The buffer instance.
buf.put('II', 1234, 5678);
- Source
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.
Name | Type | Description |
---|---|---|
value | number | | (optional, default: 0 )The byte value to use when overwriting buffer contents. When a string is given, the first character is used as value. |
start | number | (optional, default: 0 )The position to start overwriting from. |
end | number | (optional, default: buffer.length() )The position to end overwriting (exclusive). |
The buffer instance.
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"
- Source
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.
Name | Type | Description |
---|---|---|
start | number | (optional, default: 0 )The starting position of the slice. |
end | number | (optional, default: buffer.length() )The ending position of the slice (exclusive). |
A string containing the specified slice of the buffer content.
const slice = buf.slice(4, 8);
- Source
start() → {buffer}
Set the buffer position to the start (0).
The buffer instance.
buf.start();
- Source