Skip to main content

Buffer

Buffer

_A library for working with mutable byte buffers in Solidity.

Byte buffers are mutable and expandable, and provide a variety of primitives for writing to them. At any time you can fetch a bytes object containing the current contents of the buffer. The bytes object should not be stored between operations, as it may change due to resizing of the buffer._

buffer

struct buffer {
bytes buf;
uint256 capacity;
}

init

function init(struct Buffer.buffer buf, uint256 capacity) internal pure returns (struct Buffer.buffer)

Initializes a buffer with an initial capacity.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to initialize.
capacityuint256The number of bytes of space to allocate the buffer.
NameTypeDescription
[0]struct Buffer.bufferThe buffer, for chaining.

fromBytes

function fromBytes(bytes b) internal pure returns (struct Buffer.buffer)

Initializes a new buffer from an existing bytes object. Changes to the buffer may mutate the original value.

NameTypeDescription
bbytesThe bytes object to initialize the buffer with.
NameTypeDescription
[0]struct Buffer.bufferA new buffer.

resize

function resize(struct Buffer.buffer buf, uint256 capacity) private pure

max

function max(uint256 a, uint256 b) private pure returns (uint256)

truncate

function truncate(struct Buffer.buffer buf) internal pure returns (struct Buffer.buffer)

Sets buffer length to 0.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to truncate.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining..

write

function write(struct Buffer.buffer buf, uint256 off, bytes data, uint256 len) internal pure returns (struct Buffer.buffer)

Writes a byte string to a buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
offuint256The start offset to write to.
databytesThe data to append.
lenuint256The number of bytes to copy.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

append

function append(struct Buffer.buffer buf, bytes data, uint256 len) internal pure returns (struct Buffer.buffer)

Appends a byte string to a buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
databytesThe data to append.
lenuint256The number of bytes to copy.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

append

function append(struct Buffer.buffer buf, bytes data) internal pure returns (struct Buffer.buffer)

Appends a byte string to a buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
databytesThe data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

writeUint8

function writeUint8(struct Buffer.buffer buf, uint256 off, uint8 data) internal pure returns (struct Buffer.buffer)

Writes a byte to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
offuint256The offset to write the byte at.
datauint8The data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

appendUint8

function appendUint8(struct Buffer.buffer buf, uint8 data) internal pure returns (struct Buffer.buffer)

Appends a byte to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
datauint8The data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

write

function write(struct Buffer.buffer buf, uint256 off, bytes32 data, uint256 len) private pure returns (struct Buffer.buffer)

Writes up to 32 bytes to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
offuint256The offset to write at.
databytes32The data to append.
lenuint256The number of bytes to write (left-aligned).
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

writeBytes20

function writeBytes20(struct Buffer.buffer buf, uint256 off, bytes20 data) internal pure returns (struct Buffer.buffer)

Writes a bytes20 to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
offuint256The offset to write at.
databytes20The data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

appendBytes20

function appendBytes20(struct Buffer.buffer buf, bytes20 data) internal pure returns (struct Buffer.buffer)

Appends a bytes20 to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
databytes20The data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chhaining.

appendBytes32

function appendBytes32(struct Buffer.buffer buf, bytes32 data) internal pure returns (struct Buffer.buffer)

Appends a bytes32 to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
databytes32The data to append.
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

writeInt

function writeInt(struct Buffer.buffer buf, uint256 off, uint256 data, uint256 len) private pure returns (struct Buffer.buffer)

Writes an integer to the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
offuint256The offset to write at.
datauint256The data to append.
lenuint256The number of bytes to write (right-aligned).
NameTypeDescription
[0]struct Buffer.bufferThe original buffer, for chaining.

appendInt

function appendInt(struct Buffer.buffer buf, uint256 data, uint256 len) internal pure returns (struct Buffer.buffer)

Appends a byte to the end of the buffer. Resizes if doing so would exceed the capacity of the buffer.

NameTypeDescription
bufstruct Buffer.bufferThe buffer to append to.
datauint256The data to append.
lenuint256
NameTypeDescription
[0]struct Buffer.bufferThe original buffer.