Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/Addresses.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

/**
* Addresses Library
Expand All @@ -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)
}
Expand Down
26 changes: 15 additions & 11 deletions lib/Integers.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

/**
* Integers Library
Expand All @@ -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
*
Expand All @@ -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--);
Expand All @@ -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)
Expand All @@ -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)
Expand Down
90 changes: 53 additions & 37 deletions lib/Strings.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

/**
* Strings Library
Expand All @@ -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];
}

Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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) {
Expand All @@ -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);
}

Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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) {
Expand All @@ -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;
}
}
Expand All @@ -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]);
Expand All @@ -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]);
Expand All @@ -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;
Expand All @@ -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;
}
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion tests/AddressesTests.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

import "lib/Addresses.sol";

Expand Down
2 changes: 1 addition & 1 deletion tests/IntegerTests.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

import "lib/Strings.sol";
import "lib/Integers.sol";
Expand Down
2 changes: 1 addition & 1 deletion tests/StringsTest.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.0;
pragma solidity ^0.5.0;

import "lib/Strings.sol";

Expand Down