Skip to content

Commit

Permalink
✨ Calldata reader lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Philogy committed Jul 29, 2024
1 parent 0089cd9 commit 0d1cbcc
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/collections/CalldataReader.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

type CalldataReader is uint256;

using CalldataReaderLib for CalldataReader global;

/// @author philogy <https://github.com/philogy>
library CalldataReaderLib {
function from(bytes calldata data) internal pure returns (CalldataReader reader) {
assembly {
reader := data.offset
}
}

function shifted(CalldataReader reader, uint256 n) internal pure returns (CalldataReader newReader) {
assembly {
newReader := add(reader, n)
}
}

function readBool(CalldataReader reader) internal pure returns (bool value) {
assembly {
value := byte(0, calldataload(reader))
}
}

function readU16(CalldataReader reader) internal pure returns (uint16 value) {
assembly {
value := shr(240, calldataload(reader))
}
}

function readU40(CalldataReader reader) internal pure returns (uint40 value) {
assembly {
value := shr(216, calldataload(reader))
}
}

function readU64(CalldataReader reader) internal pure returns (uint64 value) {
assembly {
value := shr(192, calldataload(reader))
}
}

function readU128(CalldataReader reader) internal pure returns (uint128 value) {
assembly {
value := shr(128, calldataload(reader))
}
}
}

0 comments on commit 0d1cbcc

Please sign in to comment.