Skip to content

Commit

Permalink
Implement toUintXX for 64, 96 and 128 bits (#25)
Browse files Browse the repository at this point in the history
* include toUintXX for 64, 96 and 128 bits

* Update tests to follow same style as the others
  • Loading branch information
bh2smith authored and GNSPS committed Jun 26, 2019
1 parent 3222bc1 commit 658e88b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
33 changes: 33 additions & 0 deletions contracts/BytesLib.sol
Expand Up @@ -334,6 +334,39 @@ library BytesLib {
return tempUint;
}

function toUint64(bytes memory _bytes, uint _start) internal pure returns (uint64) {
require(_bytes.length >= (_start + 8));
uint64 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0x8), _start))
}

return tempUint;
}

function toUint96(bytes memory _bytes, uint _start) internal pure returns (uint96) {
require(_bytes.length >= (_start + 12));
uint96 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0xc), _start))
}

return tempUint;
}

function toUint128(bytes memory _bytes, uint _start) internal pure returns (uint128) {
require(_bytes.length >= (_start + 16));
uint128 tempUint;

assembly {
tempUint := mload(add(add(_bytes, 0x10), _start))
}

return tempUint;
}

function toUint(bytes memory _bytes, uint _start) internal pure returns (uint256) {
require(_bytes.length >= (_start + 32));
uint256 tempUint;
Expand Down
66 changes: 66 additions & 0 deletions test/TestBytesLib2.sol
Expand Up @@ -195,6 +195,72 @@ contract TestBytesLib2 {
// This should throw;
}

function testToUint64() public {
bytes memory memBytes = hex"f00d0000000000000020feed";

uint64 testUint64 = 32; // 0x20 == 32
uint64 resultUint64;

resultUint64 = memBytes.toUint64(2);
Assert.equal(uint256(resultUint64), uint256(testUint64), "Typecast to 64-bit-wide unsigned integer failed.");

// Testing for the throw conditions below
(bool r, ) = address(this).call(abi.encodePacked(this.toUint64Throw.selector));
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint64Throw() public pure {
bytes memory memBytes = hex"f00d42feed";

uint64 resultUint64;

resultUint64 = memBytes.toUint64(35); // This should throw;
}

function testToUint96() public {
bytes memory memBytes = hex"f00d000000000000000000000020feed";

uint96 testUint96 = 32; // 0x20 == 32
uint96 resultUint96;

resultUint96 = memBytes.toUint96(2);
Assert.equal(uint256(resultUint96), uint256(testUint96), "Typecast to 96-bit-wide unsigned integer failed.");

// Testing for the throw conditions below
(bool r, ) = address(this).call(abi.encodePacked(this.toUint64Throw.selector));
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint96Throw() public pure {
bytes memory memBytes = hex"f00d42feed";

uint96 resultUint96;

resultUint96 = memBytes.toUint96(35); // This should throw;
}

function testToUint128() public {
bytes memory memBytes = hex"f00d00000000000000000000000000000020feed";

uint128 testUint128 = 32; // 0x20 == 32
uint128 resultUint128;

resultUint128 = memBytes.toUint128(2);
Assert.equal(uint256(resultUint128), uint256(testUint128), "Typecast to 128-bit-wide unsigned integer failed.");

// Testing for the throw conditions below
(bool r, ) = address(this).call(abi.encodePacked(this.toUint128Throw.selector));
Assert.isFalse(r, "Typecasting with wrong index should throw");
}

function toUint128Throw() public pure {
bytes memory memBytes = hex"f00d42feed";

uint128 resultUint128;

resultUint128 = memBytes.toUint128(35); // This should throw;
}

function testToUint() public {
bytes memory memBytes = hex"f00d0000000000000000000000000000000000000000000000000000000000000020feed";

Expand Down

0 comments on commit 658e88b

Please sign in to comment.