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/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. 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..9d9c34e 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) + function concat(string memory _base, string memory _value) internal pure - returns (string) { + 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,7 +64,7 @@ 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) + function indexOf(string memory _base, string memory _value) internal pure returns (int) { @@ -87,7 +87,7 @@ 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) + function _indexOf(string memory _base, string memory _value, uint _offset) internal pure returns (int) { @@ -96,7 +96,7 @@ library Strings { 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); } @@ -114,7 +114,7 @@ library Strings { * otherwise this is the string to be measured * @return uint The length of the passed string */ - function length(string _base) + function length(string memory _base) internal pure returns (uint) { @@ -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) + function substring(string memory _base, int _length) internal pure - returns (string) { + 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) + function _substring(string memory _base, int _length, int _offset) internal pure - returns (string) { + 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) + function split(string memory _base, string memory _value) internal - returns (string[] storage splitArr) { + 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,7 +239,7 @@ 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) + function compareTo(string memory _base, string memory _value) internal pure returns (bool) { @@ -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,7 +272,7 @@ library Strings { * @return bool Simply notates if the two string have an equivalent value * discarding case */ - function compareToIgnoreCase(string _base, string _value) + function compareToIgnoreCase(string memory _base, string memory _value) internal pure returns (bool) { @@ -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) + function upper(string memory _base) internal pure - returns (string) { + 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) + function lower(string memory _base) internal pure - returns (string) { + returns (string memory) { bytes memory _baseBytes = bytes(_base); for (uint i = 0; i < _baseBytes.length; i++) { _baseBytes[i] = _lower(_baseBytes[i]); @@ -335,7 +351,7 @@ library Strings { returns (bytes1) { if (_b1 >= 0x61 && _b1 <= 0x7A) { - return bytes1(uint8(_b1)-32); + return bytes1(uint8(_b1) - 32); } return _b1; @@ -357,9 +373,9 @@ library Strings { 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";