Skip to content

Commit

Permalink
v0.0.7 - Upgrade to Solidity v0.5.0 (#22)
Browse files Browse the repository at this point in the history
* (WIP) Upgrade to Solidity v0.5.0 (#21)

* (refactor) Changed codebase to Solidity v0.5.0

* Style changes
* Added `memory` everywhere to bytes and strings
* `target.call(data)` now returns a tuple

* Adding note about compiler support in the README + typo fix

* Replaced the ThrowProxy pattern for a better v0.5.x one

ThrowProxy was giving problems while testing with the JS-VM with the Constantinople hard-fork. So I changed the tests to stop using that pattern and use function selectors since that's now a thing.

It is cleaner now.

* Update package-lock.json

* Bump version to v0.0.7

* Bump version in package-lock.json

* Update dependency versions

* Fix typo in package-lock.json
  • Loading branch information
GNSPS authored Feb 27, 2019
1 parent 9776282 commit 14ca2bd
Show file tree
Hide file tree
Showing 9 changed files with 910 additions and 1,249 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ The library lets you concatenate, slice and type cast bytes arrays both in memor

Given this library has an all-internal collection of methods it doesn't make sense having it reside in the mainnet. Instead it will only be available in EPM as an installable package.

_Note_: Since version `v0.0.7` the library will only compile on Solidity versions `>0.4.22` so, if you need `v0.4.x` support for your project just use `v0.0.6` of the library with:
```
$ truffle install [email protected]
```
or
```
$ npm install [email protected]
```

## Usage

You can use the library here present by direct download and importing with:
Expand Down Expand Up @@ -61,7 +70,6 @@ Contributions are more than welcome in any way shape or form! 😄

TODOs:
* Two storage bytes arrays concatenation
* Two storage bytes arrays concatenation
* Slicing directly from storage
* Implement inline assembly functions for better readability

Expand Down
14 changes: 7 additions & 7 deletions contracts/AssertBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* This library is compliant with the test event convention that the Truffle suite uses.
*/

pragma solidity ^0.4.21;
pragma solidity ^0.5.0;


library AssertBytes {
Expand Down Expand Up @@ -69,15 +69,15 @@ library AssertBytes {
return returnBool;
}

function equal(bytes memory _a, bytes memory _b, string message) internal returns (bool) {
function equal(bytes memory _a, bytes memory _b, string memory message) internal returns (bool) {
bool returnBool = _equal(_a, _b);

_report(returnBool, message);

return returnBool;
}

function notEqual(bytes memory _a, bytes memory _b, string message) internal returns (bool) {
function notEqual(bytes memory _a, bytes memory _b, string memory message) internal returns (bool) {
bool returnBool = _equal(_a, _b);

_report(!returnBool, message);
Expand Down Expand Up @@ -162,15 +162,15 @@ library AssertBytes {
return returnBool;
}

function equalStorage(bytes storage _a, bytes memory _b, string message) internal returns (bool) {
function equalStorage(bytes storage _a, bytes memory _b, string memory message) internal returns (bool) {
bool returnBool = _equalStorage(_a, _b);

_report(returnBool, message);

return returnBool;
}

function notEqualStorage(bytes storage _a, bytes memory _b, string message) internal returns (bool) {
function notEqualStorage(bytes storage _a, bytes memory _b, string memory message) internal returns (bool) {
bool returnBool = _equalStorage(_a, _b);

_report(!returnBool, message);
Expand All @@ -191,10 +191,10 @@ library AssertBytes {
message (string) - The message that is sent if the assertion fails.
*/

function _report(bool result, string message) internal {
function _report(bool result, string memory message) internal {
if (result)
emit TestEvent(true, "");
else
emit TestEvent(false, message);
}
}
}
42 changes: 32 additions & 10 deletions contracts/BytesLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,18 @@
* The library lets you concatenate, slice and type cast bytes arrays both in memory and storage.
*/

pragma solidity ^0.4.19;
pragma solidity ^0.5.0;


library BytesLib {
function concat(bytes memory _preBytes, bytes memory _postBytes) internal pure returns (bytes) {
function concat(
bytes memory _preBytes,
bytes memory _postBytes
)
internal
pure
returns (bytes memory)
{
bytes memory tempBytes;

assembly {
Expand Down Expand Up @@ -218,7 +225,15 @@ library BytesLib {
}
}

function slice(bytes _bytes, uint _start, uint _length) internal pure returns (bytes) {
function slice(
bytes memory _bytes,
uint _start,
uint _length
)
internal
pure
returns (bytes memory)
{
require(_bytes.length >= (_start + _length));

bytes memory tempBytes;
Expand Down Expand Up @@ -275,7 +290,7 @@ library BytesLib {
return tempBytes;
}

function toAddress(bytes _bytes, uint _start) internal pure returns (address) {
function toAddress(bytes memory _bytes, uint _start) internal pure returns (address) {
require(_bytes.length >= (_start + 20));
address tempAddress;

Expand All @@ -286,7 +301,7 @@ library BytesLib {
return tempAddress;
}

function toUint8(bytes _bytes, uint _start) internal pure returns (uint8) {
function toUint8(bytes memory _bytes, uint _start) internal pure returns (uint8) {
require(_bytes.length >= (_start + 1));
uint8 tempUint;

Expand All @@ -297,7 +312,7 @@ library BytesLib {
return tempUint;
}

function toUint16(bytes _bytes, uint _start) internal pure returns (uint16) {
function toUint16(bytes memory _bytes, uint _start) internal pure returns (uint16) {
require(_bytes.length >= (_start + 2));
uint16 tempUint;

Expand All @@ -308,7 +323,7 @@ library BytesLib {
return tempUint;
}

function toUint32(bytes _bytes, uint _start) internal pure returns (uint32) {
function toUint32(bytes memory _bytes, uint _start) internal pure returns (uint32) {
require(_bytes.length >= (_start + 4));
uint32 tempUint;

Expand All @@ -319,7 +334,7 @@ library BytesLib {
return tempUint;
}

function toUint(bytes _bytes, uint _start) internal pure returns (uint256) {
function toUint(bytes memory _bytes, uint _start) internal pure returns (uint256) {
require(_bytes.length >= (_start + 32));
uint256 tempUint;

Expand All @@ -330,7 +345,7 @@ library BytesLib {
return tempUint;
}

function toBytes32(bytes _bytes, uint _start) internal pure returns (bytes32) {
function toBytes32(bytes memory _bytes, uint _start) internal pure returns (bytes32) {
require(_bytes.length >= (_start + 32));
bytes32 tempBytes32;

Expand Down Expand Up @@ -384,7 +399,14 @@ library BytesLib {
return success;
}

function equalStorage(bytes storage _preBytes, bytes memory _postBytes) internal view returns (bool) {
function equalStorage(
bytes storage _preBytes,
bytes memory _postBytes
)
internal
view
returns (bool)
{
bool success = true;

assembly {
Expand Down
4 changes: 2 additions & 2 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.4;
pragma solidity ^0.5.0;

contract Migrations {
address public owner;
Expand All @@ -8,7 +8,7 @@ contract Migrations {
if (msg.sender == owner) _;
}

function Migrations() public {
constructor () public {
owner = msg.sender;
}

Expand Down
2 changes: 1 addition & 1 deletion ethpm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"package_name": "bytes",
"version": "0.0.6",
"version": "0.0.7",
"description": "Solidity bytes tightly packed arrays utility library.",
"authors": [
"Gonçalo Sá <[email protected]>"
Expand Down
Loading

0 comments on commit 14ca2bd

Please sign in to comment.