Bytes
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
sis present in the buffer, returns the index of the first instance - If
sis 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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| s | bytes1 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
indexOf
Forward search for s in buffer starting at position pos
- If
sis present in the buffer (at or afterpos), returns the index of the next instance - If
sis not present in the buffer (at or afterpos), 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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| s | bytes1 | |
| pos | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
lastIndexOf
Backward search for s in buffer
- If
sis present in the buffer, returns the index of the last instance - If
sis 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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| s | bytes1 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 |
lastIndexOf
Backward search for s in buffer starting at position pos
- If
sis present in the buffer (at or beforepos), returns the index of the previous instance - If
sis not present in the buffer (at or beforepos), 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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| s | bytes1 | |
| pos | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| start | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| start | uint256 | |
| end | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| start | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| start | uint256 | |
| end | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| pos | uint256 | |
| replacement | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| pos | uint256 | |
| replacement | bytes | |
| offset | uint256 | |
| length | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffers | bytes[] |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| input | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| output | bytes |
equal
Returns true if the two byte buffers are equal.
function equal(bytes memory a, bytes memory b) internal pure returns (bool);Parameters
| Name | Type | Description |
|---|---|---|
| a | bytes | |
| b | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| value | bytes32 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes32 |
reverseBytes16
Same as reverseBytes32 but optimized for 128-bit values.
function reverseBytes16(bytes16 value) internal pure returns (bytes16);Parameters
| Name | Type | Description |
|---|---|---|
| value | bytes16 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes16 |
reverseBytes8
Same as reverseBytes32 but optimized for 64-bit values.
function reverseBytes8(bytes8 value) internal pure returns (bytes8);Parameters
| Name | Type | Description |
|---|---|---|
| value | bytes8 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes8 |
reverseBytes4
Same as reverseBytes32 but optimized for 32-bit values.
function reverseBytes4(bytes4 value) internal pure returns (bytes4);Parameters
| Name | Type | Description |
|---|---|---|
| value | bytes4 |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | bytes4 |
reverseBytes2
Same as reverseBytes32 but optimized for 16-bit values.
function reverseBytes2(bytes2 value) internal pure returns (bytes2);Parameters
| Name | Type | Description |
|---|---|---|
| value | bytes2 |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| <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
| Name | Type | Description |
|---|---|---|
| buffer | bytes | |
| offset | uint256 |
Returns
| Name | Type | Description |
|---|---|---|
| value | bytes32 |