Skip to content
Lattice

Bytes

Git Source

Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Bytes.sol)

Bytes operations.

Functions

indexOf

Forward search for s in buffer

  • If s is present in the buffer, returns the index of the first instance
  • If s is not present in the buffer, returns type(uint256).max

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's Array.indexOf]

function indexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256);

Parameters

NameTypeDescription
bufferbytes
sbytes1

Returns

NameTypeDescription
<none>uint256

indexOf

Forward search for s in buffer starting at position pos

  • If s is present in the buffer (at or after pos), returns the index of the next instance
  • If s is not present in the buffer (at or after pos), returns type(uint256).max

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's Array.indexOf]

function indexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256);

Parameters

NameTypeDescription
bufferbytes
sbytes1
posuint256

Returns

NameTypeDescription
<none>uint256

lastIndexOf

Backward search for s in buffer

  • If s is present in the buffer, returns the index of the last instance
  • If s is not present in the buffer, returns type(uint256).max

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's Array.lastIndexOf]

function lastIndexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256);

Parameters

NameTypeDescription
bufferbytes
sbytes1

Returns

NameTypeDescription
<none>uint256

lastIndexOf

Backward search for s in buffer starting at position pos

  • If s is present in the buffer (at or before pos), returns the index of the previous instance
  • If s is not present in the buffer (at or before pos), returns type(uint256).max

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's Array.lastIndexOf]

function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256);

Parameters

NameTypeDescription
bufferbytes
sbytes1
posuint256

Returns

NameTypeDescription
<none>uint256

slice

Copies the content of buffer, from start (included) to the end of buffer into a new bytes object in memory.

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's Array.slice]

function slice(bytes memory buffer, uint256 start) internal pure returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
startuint256

Returns

NameTypeDescription
<none>bytes

slice

Copies the content of buffer, from start (included) to end (excluded) into a new bytes object in memory. The end argument is truncated to the length of the buffer.

NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's Array.slice]

function slice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
startuint256
enduint256

Returns

NameTypeDescription
<none>bytes

splice

Moves the content of buffer, from start (included) to the end of buffer to the start of that buffer, and shrinks the buffer length accordingly, effectively overriding the content of buffer with buffer[start:].

NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use slice instead

function splice(bytes memory buffer, uint256 start) internal pure returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
startuint256

Returns

NameTypeDescription
<none>bytes

splice

Moves the content of buffer, from start (included) to end (excluded) to the start of that buffer, and shrinks the buffer length accordingly, effectively overriding the content of buffer with buffer[start:end]. The end argument is truncated to the length of the buffer.

NOTE: This function modifies the provided buffer in place. If you need to preserve the original buffer, use slice instead

function splice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
startuint256
enduint256

Returns

NameTypeDescription
<none>bytes

replace

Replaces bytes in buffer starting at pos with all bytes from replacement.

Parameters are clamped to valid ranges (i.e. pos is clamped to [0, buffer.length]). If pos >= buffer.length, no replacement occurs and the buffer is returned unchanged.

NOTE: This function modifies the provided buffer in place.

function replace(bytes memory buffer, uint256 pos, bytes memory replacement) internal pure returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
posuint256
replacementbytes

Returns

NameTypeDescription
<none>bytes

replace

Replaces bytes in buffer starting at pos with bytes from replacement starting at offset. Copies at most length bytes from replacement to buffer.

Parameters are clamped to valid ranges (i.e. pos is clamped to [0, buffer.length], offset is clamped to [0, replacement.length], and length is clamped to min(length, replacement.length - offset, buffer.length - pos)). If pos >= buffer.length or offset >= replacement.length, no replacement occurs and the buffer is returned unchanged.

NOTE: This function modifies the provided buffer in place.

function replace(bytes memory buffer, uint256 pos, bytes memory replacement, uint256 offset, uint256 length)
        internal
        pure
        returns (bytes memory);

Parameters

NameTypeDescription
bufferbytes
posuint256
replacementbytes
offsetuint256
lengthuint256

Returns

NameTypeDescription
<none>bytes

concat

Concatenate an array of bytes into a single bytes object.

For fixed bytes types, we recommend using the solidity built-in bytes.concat or (equivalent) abi.encodePacked.

NOTE: this could be done in assembly with a single loop that expands starting at the FMP, but that would be significantly less readable. It might be worth benchmarking the savings of the full-assembly approach.

function concat(bytes[] memory buffers) internal pure returns (bytes memory);

Parameters

NameTypeDescription
buffersbytes[]

Returns

NameTypeDescription
<none>bytes

toNibbles

Split each byte in input into two nibbles (4 bits each)

Example: hex"01234567" → hex"0001020304050607"

function toNibbles(bytes memory input) internal pure returns (bytes memory output);

Parameters

NameTypeDescription
inputbytes

Returns

NameTypeDescription
outputbytes

equal

Returns true if the two byte buffers are equal.

function equal(bytes memory a, bytes memory b) internal pure returns (bool);

Parameters

NameTypeDescription
abytes
bbytes

Returns

NameTypeDescription
<none>bool

reverseBytes32

Reverses the byte order of a bytes32 value, converting between little-endian and big-endian. Inspired by https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel[Reverse Parallel]

function reverseBytes32(bytes32 value) internal pure returns (bytes32);

Parameters

NameTypeDescription
valuebytes32

Returns

NameTypeDescription
<none>bytes32

reverseBytes16

Same as reverseBytes32 but optimized for 128-bit values.

function reverseBytes16(bytes16 value) internal pure returns (bytes16);

Parameters

NameTypeDescription
valuebytes16

Returns

NameTypeDescription
<none>bytes16

reverseBytes8

Same as reverseBytes32 but optimized for 64-bit values.

function reverseBytes8(bytes8 value) internal pure returns (bytes8);

Parameters

NameTypeDescription
valuebytes8

Returns

NameTypeDescription
<none>bytes8

reverseBytes4

Same as reverseBytes32 but optimized for 32-bit values.

function reverseBytes4(bytes4 value) internal pure returns (bytes4);

Parameters

NameTypeDescription
valuebytes4

Returns

NameTypeDescription
<none>bytes4

reverseBytes2

Same as reverseBytes32 but optimized for 16-bit values.

function reverseBytes2(bytes2 value) internal pure returns (bytes2);

Parameters

NameTypeDescription
valuebytes2

Returns

NameTypeDescription
<none>bytes2

clz

Counts the number of leading zero bits a bytes array. Returns 8 * buffer.length if the buffer is all zeros.

function clz(bytes memory buffer) internal pure returns (uint256);

Parameters

NameTypeDescription
bufferbytes

Returns

NameTypeDescription
<none>uint256

_unsafeReadBytesOffset

Reads a bytes32 from a bytes array without bounds checking.

NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the assembly block as such would prevent some optimizations.

function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value);

Parameters

NameTypeDescription
bufferbytes
offsetuint256

Returns

NameTypeDescription
valuebytes32