Sitelet https://github.com/Vectorized/solady/blob/main/docs/utils/libbytes.md
Skip to content

Latest commit

 

History

History
494 lines (351 loc) · 10.4 KB

File metadata and controls

494 lines (351 loc) · 10.4 KB

LibBytes

Library for byte related operations.

Structs

BytesStorage

struct BytesStorage {
    bytes32 _spacer;
}

Goated bytes storage struct that totally MOGs, no cap, fr.
Uses less gas and bytecode than Solidity's native bytes storage. It's meta af.
Packs length with the first 31 bytes if <255 bytes, so it’s mad tight.

Constants

NOT_FOUND

uint256 internal constant NOT_FOUND = type(uint256).max

The constant returned when the search is not found in the bytes.

Byte Storage Operations

set(BytesStorage,bytes)

function set(BytesStorage storage $, bytes memory s) internal

Sets the value of the bytes storage $ to s.

setCalldata(BytesStorage,bytes)

function setCalldata(BytesStorage storage $, bytes calldata s) internal

Sets the value of the bytes storage $ to s.

clear(BytesStorage)

function clear(BytesStorage storage $) internal

Sets the value of the bytes storage $ to the empty bytes.

isEmpty(BytesStorage)

function isEmpty(BytesStorage storage $) internal view returns (bool)

Returns whether the value stored is $ is the empty bytes "".

length(BytesStorage)

function length(BytesStorage storage $)
    internal
    view
    returns (uint256 result)

Returns the length of the value stored in $.

get(BytesStorage)

function get(BytesStorage storage $)
    internal
    view
    returns (bytes memory result)

Returns the value stored in $.

uint8At(BytesStorage,uint256)

function uint8At(BytesStorage storage $, uint256 i)
    internal
    view
    returns (uint8 result)

Returns the uint8 at index i. If out-of-bounds, returns 0.

Bytes Operations

replace(bytes,bytes,bytes)

function replace(
    bytes memory subject,
    bytes memory needle,
    bytes memory replacement
) internal pure returns (bytes memory result)

Returns subject all occurrences of needle replaced with replacement.

indexOf(bytes,bytes,uint256)

function indexOf(bytes memory subject, bytes memory needle, uint256 from)
    internal
    pure
    returns (uint256 result)

Returns the byte index of the first location of needle in subject,
needleing from left to right, starting from from.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

indexOfByte(bytes,bytes1,uint256)

function indexOfByte(bytes memory subject, bytes1 needle, uint256 from)
    internal
    pure
    returns (uint256 result)

Returns the byte index of the first location of needle in subject,
needleing from left to right, starting from from. Optimized for byte needles.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

indexOfByte(bytes,bytes1)

function indexOfByte(bytes memory subject, bytes1 needle)
    internal
    pure
    returns (uint256 result)

Returns the byte index of the first location of needle in subject,
needleing from left to right. Optimized for byte needles.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

indexOf(bytes,bytes)

function indexOf(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (uint256)

Returns the byte index of the first location of needle in subject,
needleing from left to right.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

lastIndexOf(bytes,bytes,uint256)

function lastIndexOf(
    bytes memory subject,
    bytes memory needle,
    uint256 from
) internal pure returns (uint256 result)

Returns the byte index of the first location of needle in subject,
needleing from right to left, starting from from.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

lastIndexOf(bytes,bytes)

function lastIndexOf(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (uint256)

Returns the byte index of the first location of needle in subject,
needleing from right to left.
Returns NOT_FOUND (i.e. type(uint256).max) if the needle is not found.

contains(bytes,bytes)

function contains(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (bool)

Returns true if needle is found in subject, false otherwise.

startsWith(bytes,bytes)

function startsWith(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (bool result)

Returns whether subject starts with needle.

endsWith(bytes,bytes)

function endsWith(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (bool result)

Returns whether subject ends with needle.

repeat(bytes,uint256)

function repeat(bytes memory subject, uint256 times)
    internal
    pure
    returns (bytes memory result)

Returns subject repeated times.

slice(bytes,uint256,uint256)

function slice(bytes memory subject, uint256 start, uint256 end)
    internal
    pure
    returns (bytes memory result)

Returns a copy of subject sliced from start to end (exclusive).
start and end are byte offsets.

slice(bytes,uint256)

function slice(bytes memory subject, uint256 start)
    internal
    pure
    returns (bytes memory result)

Returns a copy of subject sliced from start to the end of the bytes.
start is a byte offset.

sliceCalldata(bytes,uint256,uint256)

function sliceCalldata(bytes calldata subject, uint256 start, uint256 end)
    internal
    pure
    returns (bytes calldata result)

Returns a copy of subject sliced from start to end (exclusive).
start and end are byte offsets. Faster than Solidity's native slicing.

sliceCalldata(bytes,uint256)

function sliceCalldata(bytes calldata subject, uint256 start)
    internal
    pure
    returns (bytes calldata result)

Returns a copy of subject sliced from start to the end of the bytes.
start is a byte offset. Faster than Solidity's native slicing.

truncate(bytes,uint256)

function truncate(bytes memory subject, uint256 n)
    internal
    pure
    returns (bytes memory result)

Reduces the size of subject to n.
If n is greater than the size of subject, this will be a no-op.

truncatedCalldata(bytes,uint256)

function truncatedCalldata(bytes calldata subject, uint256 n)
    internal
    pure
    returns (bytes calldata result)

Returns a copy of subject, with the length reduced to n.
If n is greater than the size of subject, this will be a no-op.

indicesOf(bytes,bytes)

function indicesOf(bytes memory subject, bytes memory needle)
    internal
    pure
    returns (uint256[] memory result)

Returns all the indices of needle in subject.
The indices are byte offsets.

split(bytes,bytes)

function split(bytes memory subject, bytes memory delimiter)
    internal
    pure
    returns (bytes[] memory result)

Returns an arrays of bytess based on the delimiter inside of the subject bytes.

concat(bytes,bytes)

function concat(bytes memory a, bytes memory b)
    internal
    pure
    returns (bytes memory result)

Returns a concatenated bytes of a and b.
Cheaper than bytes.concat() and does not de-align the free memory pointer.

eq(bytes,bytes)

function eq(bytes memory a, bytes memory b)
    internal
    pure
    returns (bool result)

Returns whether a equals b.

eqs(bytes,bytes32)

function eqs(bytes memory a, bytes32 b)
    internal
    pure
    returns (bool result)

Returns whether a equals b, where b is a null-terminated small bytes.

cmp(bytes,bytes)

function cmp(bytes memory a, bytes memory b)
    internal
    pure
    returns (int256 result)

Returns 0 if a == b, -1 if a < b, +1 if a > b.
If a == b[:a.length], and a.length < b.length`, returns -1.

directReturn(bytes)

function directReturn(bytes memory a) internal pure

Directly returns a without copying.

directReturn(bytes[])

function directReturn(bytes[] memory a) internal pure

Directly returns a with minimal copying.

load(bytes,uint256)

function load(bytes memory a, uint256 offset)
    internal
    pure
    returns (bytes32 result)

Returns the word at offset, without any bounds checks.

loadCalldata(bytes,uint256)

function loadCalldata(bytes calldata a, uint256 offset)
    internal
    pure
    returns (bytes32 result)

Returns the word at offset, without any bounds checks.

staticStructInCalldata(bytes,uint256)

function staticStructInCalldata(bytes calldata a, uint256 offset)
    internal
    pure
    returns (bytes calldata result)

Returns a slice representing a static struct in the calldata. Performs bounds checks.

dynamicStructInCalldata(bytes,uint256)

function dynamicStructInCalldata(bytes calldata a, uint256 offset)
    internal
    pure
    returns (bytes calldata result)

Returns a slice representing a dynamic struct in the calldata. Performs bounds checks.

bytesInCalldata(bytes,uint256)

function bytesInCalldata(bytes calldata a, uint256 offset)
    internal
    pure
    returns (bytes calldata result)

Returns bytes in calldata. Performs bounds checks.

checkInCalldata(bytes,bytes)

function checkInCalldata(bytes calldata x, bytes calldata a)
    internal
    pure

Checks if x is in a. Assumes a has been checked.

checkInCalldata(bytes[],bytes)

function checkInCalldata(bytes[] calldata x, bytes calldata a)
    internal
    pure

Checks if x is in a. Assumes a has been checked.

emptyCalldata()

function emptyCalldata() internal pure returns (bytes calldata result)

Returns empty calldata bytes. For silencing the compiler.

msbToAddress(bytes32)

function msbToAddress(bytes32 x) internal pure returns (address)

Returns the most significant 20 bytes as an address.

lsbToAddress(bytes32)

function lsbToAddress(bytes32 x) internal pure returns (address)

Returns the least significant 20 bytes as an address.