From d32f978c47bbeb6e56bd059286ddc9ba733c17b0 Mon Sep 17 00:00:00 2001 From: Jens Ivar Jordre Date: Mon, 13 May 2019 10:24:10 +0200 Subject: [PATCH 1/3] Add support for solc@^0.5.0 --- CONTRIBUTING.md | 4 +- lib/Addresses.sol | 4 +- lib/Integers.sol | 26 ++++--- lib/Strings.sol | 156 +++++++++++++++++++++------------------ package.json | 3 +- tests/AddressesTests.sol | 2 +- tests/IntegerTests.sol | 2 +- tests/StringsTest.sol | 2 +- 8 files changed, 109 insertions(+), 90 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 34c0a2a..0c3abc5 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -1,6 +1,6 @@ -# Solidity Utils Contrubutions +# Solidity Utils Contributions -When contributing to this repository please try and adhear to the following before making a pull request: +When contributing to this repository please try and adhere to the following before making a pull request: - Keep styling and naming conventions consistent with the pre-existing solutions - Be mindful of gas consumption and highlight high gas costs - Clearly document the changes or additions diff --git a/lib/Addresses.sol b/lib/Addresses.sol index fbd8c42..ea8f615 100644 --- a/lib/Addresses.sol +++ b/lib/Addresses.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; /** * Addresses Library @@ -17,7 +17,7 @@ library Addresses { * @param _base The address on the network to check if it is a contract * @return bool Returns true if it is a valid contract */ - function isContract(address _base) returns (bool _r) { + function isContract(address _base) internal view returns (bool _r) { assembly { _r := gt(extcodesize(_base), 0) } diff --git a/lib/Integers.sol b/lib/Integers.sol index 692311f..75a3221 100644 --- a/lib/Integers.sol +++ b/lib/Integers.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; /** * Integers Library @@ -18,18 +18,19 @@ library Integers { * @param _value The ASCII string to be converted to an unsigned integer * @return uint The unsigned value of the ASCII string */ - function parseInt(string _value) + function parseInt(string memory _value) public + pure returns (uint _ret) { bytes memory _bytesValue = bytes(_value); uint j = 1; for(uint i = _bytesValue.length-1; i >= 0 && i < _bytesValue.length; i--) { - assert(_bytesValue[i] >= 48 && _bytesValue[i] <= 57); - _ret += (uint(_bytesValue[i]) - 48)*j; + assert(uint8(_bytesValue[i]) >= 48 && uint8(_bytesValue[i]) <= 57); + _ret += (uint8(_bytesValue[i]) - 48)*j; j*=10; } } - + /** * To String * @@ -38,13 +39,14 @@ library Integers { * @param _base The unsigned integer to be converted to a string * @return string The resulting ASCII string value */ - function toString(uint _base) + function toString(uint _base) internal - returns (string) { + pure + returns (string memory) { bytes memory _tmp = new bytes(32); uint i; for(i = 0;_base > 0;i++) { - _tmp[i] = byte((_base % 10) + 48); + _tmp[i] = byte(uint8((_base % 10) + 48)); _base /= 10; } bytes memory _real = new bytes(i--); @@ -62,8 +64,9 @@ library Integers { * @param _base The 8 bit unsigned integer * @return byte The byte equivalent */ - function toByte(uint8 _base) + function toByte(uint8 _base) public + pure returns (byte _ret) { assembly { let m_alloc := add(msize(),0x1) @@ -80,9 +83,10 @@ library Integers { * @param _base The integer to be converted to bytes * @return bytes The bytes equivalent */ - function toBytes(uint _base) + function toBytes(uint _base) internal - returns (bytes _ret) { + pure + returns (bytes memory _ret) { assembly { let m_alloc := add(msize(),0x1) _ret := mload(m_alloc) diff --git a/lib/Strings.sol b/lib/Strings.sol index ecff76a..ef45147 100644 --- a/lib/Strings.sol +++ b/lib/Strings.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; /** * Strings Library @@ -24,27 +24,27 @@ library Strings { * @param _value The value to be the concatenated suffix * @return string The resulting string from combinging the base and value */ - function concat(string _base, string _value) - internal - pure - returns (string) { + function concat(string memory _base, string memory _value) + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); assert(_valueBytes.length > 0); - string memory _tmpValue = new string(_baseBytes.length + + string memory _tmpValue = new string(_baseBytes.length + _valueBytes.length); bytes memory _newValue = bytes(_tmpValue); uint i; uint j; - for(i = 0; i < _baseBytes.length; i++) { + for (i = 0; i < _baseBytes.length; i++) { _newValue[j++] = _baseBytes[i]; } - for(i = 0; i<_valueBytes.length; i++) { + for (i = 0; i < _valueBytes.length; i++) { _newValue[j++] = _valueBytes[i]; } @@ -64,10 +64,10 @@ library Strings { * @return int The position of the needle starting from 0 and returning -1 * in the case of no matches found */ - function indexOf(string _base, string _value) - internal - pure - returns (int) { + function indexOf(string memory _base, string memory _value) + internal + pure + returns (int) { return _indexOf(_base, _value, 0); } @@ -87,22 +87,22 @@ library Strings { * @return int The position of the needle starting from 0 and returning -1 * in the case of no matches found */ - function _indexOf(string _base, string _value, uint _offset) - internal - pure - returns (int) { + function _indexOf(string memory _base, string memory _value, uint _offset) + internal + pure + returns (int) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); assert(_valueBytes.length == 1); - for(uint i = _offset; i < _baseBytes.length; i++) { + for (uint i = _offset; i < _baseBytes.length; i++) { if (_baseBytes[i] == _valueBytes[0]) { return int(i); } } - return -1; + return - 1; } /** @@ -114,10 +114,10 @@ library Strings { * otherwise this is the string to be measured * @return uint The length of the passed string */ - function length(string _base) - internal - pure - returns (uint) { + function length(string memory _base) + internal + pure + returns (uint) { bytes memory _baseBytes = bytes(_base); return _baseBytes.length; } @@ -133,10 +133,10 @@ library Strings { * @param _length The length of the sub string to be extracted from the base * @return string The extracted sub string */ - function substring(string _base, int _length) - internal - pure - returns (string) { + function substring(string memory _base, int _length) + internal + pure + returns (string memory) { return _substring(_base, _length, 0); } @@ -153,20 +153,20 @@ library Strings { * @param _offset The starting point to extract the sub string from * @return string The extracted sub string */ - function _substring(string _base, int _length, int _offset) - internal - pure - returns (string) { + function _substring(string memory _base, int _length, int _offset) + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); - assert(uint(_offset+_length) <= _baseBytes.length); + assert(uint(_offset + _length) <= _baseBytes.length); string memory _tmp = new string(uint(_length)); bytes memory _tmpBytes = bytes(_tmp); uint j = 0; - for(uint i = uint(_offset); i < uint(_offset+_length); i++) { - _tmpBytes[j++] = _baseBytes[i]; + for (uint i = uint(_offset); i < uint(_offset + _length); i++) { + _tmpBytes[j++] = _baseBytes[i]; } return string(_tmpBytes); @@ -186,28 +186,44 @@ library Strings { * @return string[] An array of values split based off the delimiter, but * do not container the delimiter. */ - function split(string _base, string _value) - internal - returns (string[] storage splitArr) { + function split(string memory _base, string memory _value) + internal + pure + returns (string[] memory splitArr) { bytes memory _baseBytes = bytes(_base); + uint _offset = 0; + uint _splitsCount = 1; + while (_offset < _baseBytes.length - 1) { + int _limit = _indexOf(_base, _value, _offset); + if (_limit == -1) + break; + else { + _splitsCount++; + _offset = uint(_limit) + 1; + } + } - while(_offset < _baseBytes.length-1) { + splitArr = new string[](_splitsCount); + + _offset = 0; + _splitsCount = 0; + while (_offset < _baseBytes.length - 1) { int _limit = _indexOf(_base, _value, _offset); - if (_limit == -1) { + if (_limit == - 1) { _limit = int(_baseBytes.length); } - string memory _tmp = new string(uint(_limit)-_offset); + string memory _tmp = new string(uint(_limit) - _offset); bytes memory _tmpBytes = bytes(_tmp); uint j = 0; - for(uint i = _offset; i < uint(_limit); i++) { + for (uint i = _offset; i < uint(_limit); i++) { _tmpBytes[j++] = _baseBytes[i]; } _offset = uint(_limit) + 1; - splitArr.push(string(_tmpBytes)); + splitArr[_splitsCount++] = string(_tmpBytes); } return splitArr; } @@ -223,10 +239,10 @@ library Strings { * @param _value The string the base is being compared to * @return bool Simply notates if the two string have an equivalent */ - function compareTo(string _base, string _value) - internal - pure - returns (bool) { + function compareTo(string memory _base, string memory _value) + internal + pure + returns (bool) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -234,7 +250,7 @@ library Strings { return false; } - for(uint i = 0; i < _baseBytes.length; i++) { + for (uint i = 0; i < _baseBytes.length; i++) { if (_baseBytes[i] != _valueBytes[i]) { return false; } @@ -256,10 +272,10 @@ library Strings { * @return bool Simply notates if the two string have an equivalent value * discarding case */ - function compareToIgnoreCase(string _base, string _value) - internal - pure - returns (bool) { + function compareToIgnoreCase(string memory _base, string memory _value) + internal + pure + returns (bool) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -267,9 +283,9 @@ library Strings { return false; } - for(uint i = 0; i < _baseBytes.length; i++) { - if (_baseBytes[i] != _valueBytes[i] && - _upper(_baseBytes[i]) != _upper(_valueBytes[i])) { + for (uint i = 0; i < _baseBytes.length; i++) { + if (_baseBytes[i] != _valueBytes[i] && + _upper(_baseBytes[i]) != _upper(_valueBytes[i])) { return false; } } @@ -287,10 +303,10 @@ library Strings { * otherwise this is the string base to convert to upper case * @return string */ - function upper(string _base) - internal - pure - returns (string) { + function upper(string memory _base) + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); for (uint i = 0; i < _baseBytes.length; i++) { _baseBytes[i] = _upper(_baseBytes[i]); @@ -308,10 +324,10 @@ library Strings { * otherwise this is the string base to convert to lower case * @return string */ - function lower(string _base) - internal - pure - returns (string) { + function lower(string memory _base) + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); for (uint i = 0; i < _baseBytes.length; i++) { _baseBytes[i] = _lower(_baseBytes[i]); @@ -330,12 +346,12 @@ library Strings { * and in a lower case otherwise returns the original value */ function _upper(bytes1 _b1) - private - pure - returns (bytes1) { + private + pure + returns (bytes1) { if (_b1 >= 0x61 && _b1 <= 0x7A) { - return bytes1(uint8(_b1)-32); + return bytes1(uint8(_b1) - 32); } return _b1; @@ -352,14 +368,14 @@ library Strings { * and in a upper case otherwise returns the original value */ function _lower(bytes1 _b1) - private - pure - returns (bytes1) { + private + pure + returns (bytes1) { if (_b1 >= 0x41 && _b1 <= 0x5A) { - return bytes1(uint8(_b1)+32); + return bytes1(uint8(_b1) + 32); } - + return _b1; } } diff --git a/package.json b/package.json index 83b8ad6..a8af1e9 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,7 @@ { "name": "solidity-util", - "version": "1.0.0", + "version": "2.0.0", "description": "Solidity utils for string, uint and address types", - "main": "index.js", "directories": { "lib": "lib", "test": "tests" diff --git a/tests/AddressesTests.sol b/tests/AddressesTests.sol index a5c5e3f..5e7c98b 100644 --- a/tests/AddressesTests.sol +++ b/tests/AddressesTests.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; import "lib/Addresses.sol"; diff --git a/tests/IntegerTests.sol b/tests/IntegerTests.sol index 63d43ea..76c2c1a 100644 --- a/tests/IntegerTests.sol +++ b/tests/IntegerTests.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; import "lib/Strings.sol"; import "lib/Integers.sol"; diff --git a/tests/StringsTest.sol b/tests/StringsTest.sol index 1d37414..184cd79 100644 --- a/tests/StringsTest.sol +++ b/tests/StringsTest.sol @@ -1,4 +1,4 @@ -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; import "lib/Strings.sol"; From 53d4c00814d0ea587ccd2544759bf046d2a4a11d Mon Sep 17 00:00:00 2001 From: Jens Ivar Jordre Date: Sun, 19 May 2019 18:29:44 +0200 Subject: [PATCH 2/3] Update README --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c566e51..c6d20a5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ npm install willitscale/solidity-util In a project based on [Truffle framework](https://truffleframework.com/) you may then import and bind the libraries to the appropriate data types as seen below: ```javascript -pragma solidity ^0.4.0; +pragma solidity ^0.5.0; import "solidity-util/lib/Strings.sol"; import "solidity-util/lib/Integers.sol"; @@ -19,6 +19,7 @@ contract MyContract { using Strings for string; using Integers for uint; using Addresses for address; + using Addresses for address payable; } ``` This will then allow the use of the functionality listed below. From a82e770260fb6ded4e86994023b9e80bdec696f3 Mon Sep 17 00:00:00 2001 From: Jens Ivar Jordre Date: Mon, 20 May 2019 10:26:25 +0200 Subject: [PATCH 3/3] Update indentation --- lib/Strings.sol | 80 ++++++++++++++++++++++++------------------------- 1 file changed, 40 insertions(+), 40 deletions(-) diff --git a/lib/Strings.sol b/lib/Strings.sol index ef45147..9d9c34e 100644 --- a/lib/Strings.sol +++ b/lib/Strings.sol @@ -25,9 +25,9 @@ library Strings { * @return string The resulting string from combinging the base and value */ function concat(string memory _base, string memory _value) - internal - pure - returns (string memory) { + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -65,9 +65,9 @@ library Strings { * in the case of no matches found */ function indexOf(string memory _base, string memory _value) - internal - pure - returns (int) { + internal + pure + returns (int) { return _indexOf(_base, _value, 0); } @@ -88,9 +88,9 @@ library Strings { * in the case of no matches found */ function _indexOf(string memory _base, string memory _value, uint _offset) - internal - pure - returns (int) { + internal + pure + returns (int) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -102,7 +102,7 @@ library Strings { } } - return - 1; + return -1; } /** @@ -115,9 +115,9 @@ library Strings { * @return uint The length of the passed string */ function length(string memory _base) - internal - pure - returns (uint) { + internal + pure + returns (uint) { bytes memory _baseBytes = bytes(_base); return _baseBytes.length; } @@ -134,9 +134,9 @@ library Strings { * @return string The extracted sub string */ function substring(string memory _base, int _length) - internal - pure - returns (string memory) { + internal + pure + returns (string memory) { return _substring(_base, _length, 0); } @@ -154,9 +154,9 @@ library Strings { * @return string The extracted sub string */ function _substring(string memory _base, int _length, int _offset) - internal - pure - returns (string memory) { + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); assert(uint(_offset + _length) <= _baseBytes.length); @@ -187,9 +187,9 @@ library Strings { * do not container the delimiter. */ function split(string memory _base, string memory _value) - internal - pure - returns (string[] memory splitArr) { + internal + pure + returns (string[] memory splitArr) { bytes memory _baseBytes = bytes(_base); uint _offset = 0; @@ -240,9 +240,9 @@ library Strings { * @return bool Simply notates if the two string have an equivalent */ function compareTo(string memory _base, string memory _value) - internal - pure - returns (bool) { + internal + pure + returns (bool) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -273,9 +273,9 @@ library Strings { * discarding case */ function compareToIgnoreCase(string memory _base, string memory _value) - internal - pure - returns (bool) { + internal + pure + returns (bool) { bytes memory _baseBytes = bytes(_base); bytes memory _valueBytes = bytes(_value); @@ -304,9 +304,9 @@ library Strings { * @return string */ function upper(string memory _base) - internal - pure - returns (string memory) { + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); for (uint i = 0; i < _baseBytes.length; i++) { _baseBytes[i] = _upper(_baseBytes[i]); @@ -325,9 +325,9 @@ library Strings { * @return string */ function lower(string memory _base) - internal - pure - returns (string memory) { + internal + pure + returns (string memory) { bytes memory _baseBytes = bytes(_base); for (uint i = 0; i < _baseBytes.length; i++) { _baseBytes[i] = _lower(_baseBytes[i]); @@ -346,9 +346,9 @@ library Strings { * and in a lower case otherwise returns the original value */ function _upper(bytes1 _b1) - private - pure - returns (bytes1) { + private + pure + returns (bytes1) { if (_b1 >= 0x61 && _b1 <= 0x7A) { return bytes1(uint8(_b1) - 32); @@ -368,9 +368,9 @@ library Strings { * and in a upper case otherwise returns the original value */ function _lower(bytes1 _b1) - private - pure - returns (bytes1) { + private + pure + returns (bytes1) { if (_b1 >= 0x41 && _b1 <= 0x5A) { return bytes1(uint8(_b1) + 32);