Skip to content

Commit 000a42d

Browse files
authored
Merge pull request #7 from hubiinetwork/feature/solc-latest
Upgrade to solc@^0.5.0
2 parents 25efb27 + a82e770 commit 000a42d

File tree

9 files changed

+78
-58
lines changed

9 files changed

+78
-58
lines changed

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Solidity Utils Contrubutions
1+
# Solidity Utils Contributions
22

3-
When contributing to this repository please try and adhear to the following before making a pull request:
3+
When contributing to this repository please try and adhere to the following before making a pull request:
44
- Keep styling and naming conventions consistent with the pre-existing solutions
55
- Be mindful of gas consumption and highlight high gas costs
66
- Clearly document the changes or additions

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ npm install willitscale/solidity-util
99

1010
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:
1111
```javascript
12-
pragma solidity ^0.4.0;
12+
pragma solidity ^0.5.0;
1313

1414
import "solidity-util/lib/Strings.sol";
1515
import "solidity-util/lib/Integers.sol";
@@ -19,6 +19,7 @@ contract MyContract {
1919
using Strings for string;
2020
using Integers for uint;
2121
using Addresses for address;
22+
using Addresses for address payable;
2223
}
2324
```
2425
This will then allow the use of the functionality listed below.

lib/Addresses.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
/**
44
* Addresses Library
@@ -17,7 +17,7 @@ library Addresses {
1717
* @param _base The address on the network to check if it is a contract
1818
* @return bool Returns true if it is a valid contract
1919
*/
20-
function isContract(address _base) returns (bool _r) {
20+
function isContract(address _base) internal view returns (bool _r) {
2121
assembly {
2222
_r := gt(extcodesize(_base), 0)
2323
}

lib/Integers.sol

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
/**
44
* Integers Library
@@ -18,18 +18,19 @@ library Integers {
1818
* @param _value The ASCII string to be converted to an unsigned integer
1919
* @return uint The unsigned value of the ASCII string
2020
*/
21-
function parseInt(string _value)
21+
function parseInt(string memory _value)
2222
public
23+
pure
2324
returns (uint _ret) {
2425
bytes memory _bytesValue = bytes(_value);
2526
uint j = 1;
2627
for(uint i = _bytesValue.length-1; i >= 0 && i < _bytesValue.length; i--) {
27-
assert(_bytesValue[i] >= 48 && _bytesValue[i] <= 57);
28-
_ret += (uint(_bytesValue[i]) - 48)*j;
28+
assert(uint8(_bytesValue[i]) >= 48 && uint8(_bytesValue[i]) <= 57);
29+
_ret += (uint8(_bytesValue[i]) - 48)*j;
2930
j*=10;
3031
}
3132
}
32-
33+
3334
/**
3435
* To String
3536
*
@@ -38,13 +39,14 @@ library Integers {
3839
* @param _base The unsigned integer to be converted to a string
3940
* @return string The resulting ASCII string value
4041
*/
41-
function toString(uint _base)
42+
function toString(uint _base)
4243
internal
43-
returns (string) {
44+
pure
45+
returns (string memory) {
4446
bytes memory _tmp = new bytes(32);
4547
uint i;
4648
for(i = 0;_base > 0;i++) {
47-
_tmp[i] = byte((_base % 10) + 48);
49+
_tmp[i] = byte(uint8((_base % 10) + 48));
4850
_base /= 10;
4951
}
5052
bytes memory _real = new bytes(i--);
@@ -62,8 +64,9 @@ library Integers {
6264
* @param _base The 8 bit unsigned integer
6365
* @return byte The byte equivalent
6466
*/
65-
function toByte(uint8 _base)
67+
function toByte(uint8 _base)
6668
public
69+
pure
6770
returns (byte _ret) {
6871
assembly {
6972
let m_alloc := add(msize(),0x1)
@@ -80,9 +83,10 @@ library Integers {
8083
* @param _base The integer to be converted to bytes
8184
* @return bytes The bytes equivalent
8285
*/
83-
function toBytes(uint _base)
86+
function toBytes(uint _base)
8487
internal
85-
returns (bytes _ret) {
88+
pure
89+
returns (bytes memory _ret) {
8690
assembly {
8791
let m_alloc := add(msize(),0x1)
8892
_ret := mload(m_alloc)

lib/Strings.sol

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
/**
44
* Strings Library
@@ -24,27 +24,27 @@ library Strings {
2424
* @param _value The value to be the concatenated suffix
2525
* @return string The resulting string from combinging the base and value
2626
*/
27-
function concat(string _base, string _value)
27+
function concat(string memory _base, string memory _value)
2828
internal
2929
pure
30-
returns (string) {
30+
returns (string memory) {
3131
bytes memory _baseBytes = bytes(_base);
3232
bytes memory _valueBytes = bytes(_value);
3333

3434
assert(_valueBytes.length > 0);
3535

36-
string memory _tmpValue = new string(_baseBytes.length +
36+
string memory _tmpValue = new string(_baseBytes.length +
3737
_valueBytes.length);
3838
bytes memory _newValue = bytes(_tmpValue);
3939

4040
uint i;
4141
uint j;
4242

43-
for(i = 0; i < _baseBytes.length; i++) {
43+
for (i = 0; i < _baseBytes.length; i++) {
4444
_newValue[j++] = _baseBytes[i];
4545
}
4646

47-
for(i = 0; i<_valueBytes.length; i++) {
47+
for (i = 0; i < _valueBytes.length; i++) {
4848
_newValue[j++] = _valueBytes[i];
4949
}
5050

@@ -64,7 +64,7 @@ library Strings {
6464
* @return int The position of the needle starting from 0 and returning -1
6565
* in the case of no matches found
6666
*/
67-
function indexOf(string _base, string _value)
67+
function indexOf(string memory _base, string memory _value)
6868
internal
6969
pure
7070
returns (int) {
@@ -87,7 +87,7 @@ library Strings {
8787
* @return int The position of the needle starting from 0 and returning -1
8888
* in the case of no matches found
8989
*/
90-
function _indexOf(string _base, string _value, uint _offset)
90+
function _indexOf(string memory _base, string memory _value, uint _offset)
9191
internal
9292
pure
9393
returns (int) {
@@ -96,7 +96,7 @@ library Strings {
9696

9797
assert(_valueBytes.length == 1);
9898

99-
for(uint i = _offset; i < _baseBytes.length; i++) {
99+
for (uint i = _offset; i < _baseBytes.length; i++) {
100100
if (_baseBytes[i] == _valueBytes[0]) {
101101
return int(i);
102102
}
@@ -114,7 +114,7 @@ library Strings {
114114
* otherwise this is the string to be measured
115115
* @return uint The length of the passed string
116116
*/
117-
function length(string _base)
117+
function length(string memory _base)
118118
internal
119119
pure
120120
returns (uint) {
@@ -133,10 +133,10 @@ library Strings {
133133
* @param _length The length of the sub string to be extracted from the base
134134
* @return string The extracted sub string
135135
*/
136-
function substring(string _base, int _length)
136+
function substring(string memory _base, int _length)
137137
internal
138138
pure
139-
returns (string) {
139+
returns (string memory) {
140140
return _substring(_base, _length, 0);
141141
}
142142

@@ -153,20 +153,20 @@ library Strings {
153153
* @param _offset The starting point to extract the sub string from
154154
* @return string The extracted sub string
155155
*/
156-
function _substring(string _base, int _length, int _offset)
156+
function _substring(string memory _base, int _length, int _offset)
157157
internal
158158
pure
159-
returns (string) {
159+
returns (string memory) {
160160
bytes memory _baseBytes = bytes(_base);
161161

162-
assert(uint(_offset+_length) <= _baseBytes.length);
162+
assert(uint(_offset + _length) <= _baseBytes.length);
163163

164164
string memory _tmp = new string(uint(_length));
165165
bytes memory _tmpBytes = bytes(_tmp);
166166

167167
uint j = 0;
168-
for(uint i = uint(_offset); i < uint(_offset+_length); i++) {
169-
_tmpBytes[j++] = _baseBytes[i];
168+
for (uint i = uint(_offset); i < uint(_offset + _length); i++) {
169+
_tmpBytes[j++] = _baseBytes[i];
170170
}
171171

172172
return string(_tmpBytes);
@@ -186,28 +186,44 @@ library Strings {
186186
* @return string[] An array of values split based off the delimiter, but
187187
* do not container the delimiter.
188188
*/
189-
function split(string _base, string _value)
189+
function split(string memory _base, string memory _value)
190190
internal
191-
returns (string[] storage splitArr) {
191+
pure
192+
returns (string[] memory splitArr) {
192193
bytes memory _baseBytes = bytes(_base);
194+
193195
uint _offset = 0;
196+
uint _splitsCount = 1;
197+
while (_offset < _baseBytes.length - 1) {
198+
int _limit = _indexOf(_base, _value, _offset);
199+
if (_limit == -1)
200+
break;
201+
else {
202+
_splitsCount++;
203+
_offset = uint(_limit) + 1;
204+
}
205+
}
194206

195-
while(_offset < _baseBytes.length-1) {
207+
splitArr = new string[](_splitsCount);
208+
209+
_offset = 0;
210+
_splitsCount = 0;
211+
while (_offset < _baseBytes.length - 1) {
196212

197213
int _limit = _indexOf(_base, _value, _offset);
198-
if (_limit == -1) {
214+
if (_limit == - 1) {
199215
_limit = int(_baseBytes.length);
200216
}
201217

202-
string memory _tmp = new string(uint(_limit)-_offset);
218+
string memory _tmp = new string(uint(_limit) - _offset);
203219
bytes memory _tmpBytes = bytes(_tmp);
204220

205221
uint j = 0;
206-
for(uint i = _offset; i < uint(_limit); i++) {
222+
for (uint i = _offset; i < uint(_limit); i++) {
207223
_tmpBytes[j++] = _baseBytes[i];
208224
}
209225
_offset = uint(_limit) + 1;
210-
splitArr.push(string(_tmpBytes));
226+
splitArr[_splitsCount++] = string(_tmpBytes);
211227
}
212228
return splitArr;
213229
}
@@ -223,7 +239,7 @@ library Strings {
223239
* @param _value The string the base is being compared to
224240
* @return bool Simply notates if the two string have an equivalent
225241
*/
226-
function compareTo(string _base, string _value)
242+
function compareTo(string memory _base, string memory _value)
227243
internal
228244
pure
229245
returns (bool) {
@@ -234,7 +250,7 @@ library Strings {
234250
return false;
235251
}
236252

237-
for(uint i = 0; i < _baseBytes.length; i++) {
253+
for (uint i = 0; i < _baseBytes.length; i++) {
238254
if (_baseBytes[i] != _valueBytes[i]) {
239255
return false;
240256
}
@@ -256,7 +272,7 @@ library Strings {
256272
* @return bool Simply notates if the two string have an equivalent value
257273
* discarding case
258274
*/
259-
function compareToIgnoreCase(string _base, string _value)
275+
function compareToIgnoreCase(string memory _base, string memory _value)
260276
internal
261277
pure
262278
returns (bool) {
@@ -267,9 +283,9 @@ library Strings {
267283
return false;
268284
}
269285

270-
for(uint i = 0; i < _baseBytes.length; i++) {
271-
if (_baseBytes[i] != _valueBytes[i] &&
272-
_upper(_baseBytes[i]) != _upper(_valueBytes[i])) {
286+
for (uint i = 0; i < _baseBytes.length; i++) {
287+
if (_baseBytes[i] != _valueBytes[i] &&
288+
_upper(_baseBytes[i]) != _upper(_valueBytes[i])) {
273289
return false;
274290
}
275291
}
@@ -287,10 +303,10 @@ library Strings {
287303
* otherwise this is the string base to convert to upper case
288304
* @return string
289305
*/
290-
function upper(string _base)
306+
function upper(string memory _base)
291307
internal
292308
pure
293-
returns (string) {
309+
returns (string memory) {
294310
bytes memory _baseBytes = bytes(_base);
295311
for (uint i = 0; i < _baseBytes.length; i++) {
296312
_baseBytes[i] = _upper(_baseBytes[i]);
@@ -308,10 +324,10 @@ library Strings {
308324
* otherwise this is the string base to convert to lower case
309325
* @return string
310326
*/
311-
function lower(string _base)
327+
function lower(string memory _base)
312328
internal
313329
pure
314-
returns (string) {
330+
returns (string memory) {
315331
bytes memory _baseBytes = bytes(_base);
316332
for (uint i = 0; i < _baseBytes.length; i++) {
317333
_baseBytes[i] = _lower(_baseBytes[i]);
@@ -335,7 +351,7 @@ library Strings {
335351
returns (bytes1) {
336352

337353
if (_b1 >= 0x61 && _b1 <= 0x7A) {
338-
return bytes1(uint8(_b1)-32);
354+
return bytes1(uint8(_b1) - 32);
339355
}
340356

341357
return _b1;
@@ -357,9 +373,9 @@ library Strings {
357373
returns (bytes1) {
358374

359375
if (_b1 >= 0x41 && _b1 <= 0x5A) {
360-
return bytes1(uint8(_b1)+32);
376+
return bytes1(uint8(_b1) + 32);
361377
}
362-
378+
363379
return _b1;
364380
}
365381
}

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
{
22
"name": "solidity-util",
3-
"version": "1.0.0",
3+
"version": "2.0.0",
44
"description": "Solidity utils for string, uint and address types",
5-
"main": "index.js",
65
"directories": {
76
"lib": "lib",
87
"test": "tests"

tests/AddressesTests.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
import "lib/Addresses.sol";
44

tests/IntegerTests.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
import "lib/Strings.sol";
44
import "lib/Integers.sol";

tests/StringsTest.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.0;
1+
pragma solidity ^0.5.0;
22

33
import "lib/Strings.sol";
44

0 commit comments

Comments
 (0)