Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ LibBytes truncate #1166

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/utils/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,34 @@ library LibBytes {
result = slice(subject, start, type(uint256).max);
}

/// @dev Reduces the size of `subject` to `n`.
/// If `n` is greater than the size of `subject`, this will be a no-op.
function truncate(bytes memory subject, uint256 n)
internal
pure
returns (bytes memory result)
{
/// @solidity memory-safe-assembly
assembly {
result := subject
mstore(mul(lt(n, mload(result)), result), n)
}
}

/// @dev 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.
function truncatedCalldata(bytes calldata subject, uint256 n)
internal
pure
returns (bytes calldata result)
{
/// @solidity memory-safe-assembly
assembly {
result.offset := subject.offset
result.length := xor(n, mul(xor(n, subject.length), lt(subject.length, n)))
}
}

/// @dev Returns all the indices of `needle` in `subject`.
/// The indices are byte offsets.
function indicesOf(bytes memory subject, bytes memory needle)
Expand Down
14 changes: 14 additions & 0 deletions src/utils/g/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,20 @@ library LibBytes {
result = slice(subject, start, type(uint256).max);
}

/// @dev Reduces the size of `subject` to `n`.
/// If `n` is greater than the size of `subject`, this will be a no-op.
function truncate(bytes memory subject, uint256 n)
internal
pure
returns (bytes memory result)
{
/// @solidity memory-safe-assembly
assembly {
result := subject
mstore(mul(lt(n, mload(result)), result), n)
}
}

/// @dev Returns all the indices of `needle` in `subject`.
/// The indices are byte offsets.
function indicesOf(bytes memory subject, bytes memory needle)
Expand Down
13 changes: 13 additions & 0 deletions test/LibBytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,17 @@ contract LibBytesTest is SoladyTest {
bytes memory expected = LibBytes.slice(a, o, o + 32);
assertEq(abi.encode(LibBytes.loadCalldata(a, o)), expected);
}

function testTruncate(bytes memory a, uint256 n) public {
bytes memory sliced = LibBytes.slice(a, 0, n);
bytes memory truncated = LibBytes.truncate(a, n);
assertEq(truncated, sliced);
assertEq(a, sliced);
}

function testTruncatedCalldata(bytes calldata a, uint256 n) public {
bytes memory sliced = LibBytes.slice(a, 0, n);
bytes memory truncated = LibBytes.truncatedCalldata(a, n);
assertEq(truncated, sliced);
}
}
Loading