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

concatUint and fromUint methods #44

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
63 changes: 63 additions & 0 deletions contracts/BytesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,15 @@ library BytesLib {
}

return tempUint;
}

function concatUint8(bytes memory _preBytes, uint8 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint8(uint8 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint16(bytes memory _bytes, uint256 _start) internal pure returns (uint16) {
Expand All @@ -331,6 +340,15 @@ library BytesLib {
return tempUint;
}

function concatUint16(bytes memory _preBytes, uint16 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint16(uint16 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint32(bytes memory _bytes, uint256 _start) internal pure returns (uint32) {
require(_start + 4 >= _start, "toUint32_overflow");
require(_bytes.length >= _start + 4, "toUint32_outOfBounds");
Expand All @@ -342,6 +360,15 @@ library BytesLib {

return tempUint;
}

function concatUint32(bytes memory _preBytes, uint8 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint32(uint32 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint64(bytes memory _bytes, uint256 _start) internal pure returns (uint64) {
require(_start + 8 >= _start, "toUint64_overflow");
Expand All @@ -354,6 +381,15 @@ library BytesLib {

return tempUint;
}

function concatUint64(bytes memory _preBytes, uint64 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint64(uint64 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint96(bytes memory _bytes, uint256 _start) internal pure returns (uint96) {
require(_start + 12 >= _start, "toUint96_overflow");
Expand All @@ -367,6 +403,15 @@ library BytesLib {
return tempUint;
}

function concatUint96(bytes memory _preBytes, uint96 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint96(uint96 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint128(bytes memory _bytes, uint256 _start) internal pure returns (uint128) {
require(_start + 16 >= _start, "toUint128_overflow");
require(_bytes.length >= _start + 16, "toUint128_outOfBounds");
Expand All @@ -379,6 +424,15 @@ library BytesLib {
return tempUint;
}

function concatUint128(bytes memory _preBytes, uint128 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint128(uint128 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toUint256(bytes memory _bytes, uint256 _start) internal pure returns (uint256) {
require(_start + 32 >= _start, "toUint256_overflow");
require(_bytes.length >= _start + 32, "toUint256_outOfBounds");
Expand All @@ -390,6 +444,15 @@ library BytesLib {

return tempUint;
}

function concatUint256(bytes memory _preBytes, uint256 _val) internal pure returns (bytes memory) {
bytes memory _postBytes = abi.encodePacked(_val);
return concat(_preBytes, _postBytes);
}

function fromUint256(uint256 _val) internal pure returns (bytes memory) {
return abi.encodePacked(_val);
}

function toBytes32(bytes memory _bytes, uint256 _start) internal pure returns (bytes32) {
require(_start + 32 >= _start, "toBytes32_overflow");
Expand Down