We want to improve solidity basic skill and we also create a basic library for learning and practice. You can see how to use as below:
pragma solidity ^0.4.16;
import "github.com/20Scoops-CNX/solidity-utils/lib/ListInteger.sol";
import "github.com/20Scoops-CNX/solidity-utils/lib/Strings.sol";
import "github.com/20Scoops-CNX/solidity-utils/lib/Integers.sol";
contract ExampleContract {
using ListInteger for *;
using Strings for string;
using Integers for uint;
uint[] items;
}
- add(uint)
- remove()
- removeByValue(uint)
- removeByIndex(uint)
- find(uint) : uint
- sort() : uint[]
- sortDESC() : uint[]
- getSize() : uint
- sum() : uint
Add unsigned integer to array
function example() public view returns (uint[]) {
items.add(99);
return items;
}
Remove item last index like function pop() in array
function example() public view returns (uint[]) {
items.remove();
return items;
}
Remove item by value in array
function example() public view returns (uint[]) {
items.removeByValue(99);
return items;
}
Remove item by index in array start at 0
function example() public view returns (uint[]) {
items.removeByIndex(0);
return items;
}
Find index by value in array
function example() public view returns (uint) {
uint index = items.find(99);
return index;
}
Sort ascending unsigned integer in array
function example() public view returns (uint[]) {
return items.sort();
}
Sort descending unsigned integer in array
function example() public view returns (uint[]) {
return items.sortDESC();
}
Get size array
function example() public view returns (uint) {
uint size = items.getSize();
return size;
}
Sum unsigned integer in array
function example() public view returns (uint) {
uint result = items.sum();
return result;
}
- concat(string)
- length() : uint
- replaceAll(bytes1, bytes1) : string
- replace(bytes1, bytes1) : string
- compareTo(string) : bool
Join two strings
function example() {
string memory str = "20scoops";
str = str.concat(" ").concat("CNX");
}
Get the length of a string
function example() returns (uint) {
string memory str = "20scoops CNX";
return str.length();
}
Replace all character in string
function example() public view returns (string) {
string memory str = "20scoops CNX ";
return str.replaceAll(" ", "");
}
Replace character in string
function example() public view returns (string) {
string memory str = "20scoops CNX ";
return str.replace(" ", "");
}
Compare two string
function example() public view returns (bool) {
string memory str = "20scoops CNX";
return str.compareTo("20scoops");
}
- parseInt(string) : uint
- toString() : uint
- plus(uint) : uint
- minus(uint) : uint
- divide(uint) : uint
- multiply(uint) : uint
- mod(uint) : uint
Convert string to unsigned integer
function example() public view returns (uint) {
return Integers.parseInt("99.00");
}
Convert unsigned integer to string
function example(uint value) public view returns (string) {
return value.toString();
}
Plus unsigned integer
function example() public view returns (uint) {
uint myInt = 5;
return myInt.plus(5);
}
Minus unsigned integer
function example() public view returns (string) {
uint myInt = 5;
return myInt.minus(5);
}
Divide unsigned integer
function example(uint value) public view returns (uint) {
uint myInt = 5;
return myInt.divide(5);
}
Multiply unsigned integer to string
function example() public view returns (uint) {
uint myInt = 5;
return myInt.multiply(5);
}
Modular unsigned integer
function example() public view returns (string) {
uint myInt = 500;
return myInt.mod(6);
}