项目作者: 20Scoops-CNX

项目描述 :
string, integer and list integer utils for solidity
高级语言: Solidity
项目地址: git://github.com/20Scoops-CNX/solidity-utils.git
创建时间: 2018-07-19T14:41:20Z
项目社区:https://github.com/20Scoops-CNX/solidity-utils

开源协议:

下载


Solidity Utils

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:

  1. pragma solidity ^0.4.16;
  2. import "github.com/20Scoops-CNX/solidity-utils/lib/ListInteger.sol";
  3. import "github.com/20Scoops-CNX/solidity-utils/lib/Strings.sol";
  4. import "github.com/20Scoops-CNX/solidity-utils/lib/Integers.sol";
  5. contract ExampleContract {
  6. using ListInteger for *;
  7. using Strings for string;
  8. using Integers for uint;
  9. uint[] items;
  10. }

ListInteger

add(uint)

Add unsigned integer to array

  1. function example() public view returns (uint[]) {
  2. items.add(99);
  3. return items;
  4. }

remove()

Remove item last index like function pop() in array

  1. function example() public view returns (uint[]) {
  2. items.remove();
  3. return items;
  4. }

removeByValue(uint)

Remove item by value in array

  1. function example() public view returns (uint[]) {
  2. items.removeByValue(99);
  3. return items;
  4. }

removeByIndex(uint)

Remove item by index in array start at 0

  1. function example() public view returns (uint[]) {
  2. items.removeByIndex(0);
  3. return items;
  4. }

find(uint) : uint

Find index by value in array

  1. function example() public view returns (uint) {
  2. uint index = items.find(99);
  3. return index;
  4. }

sort() : uint[]

Sort ascending unsigned integer in array

  1. function example() public view returns (uint[]) {
  2. return items.sort();
  3. }

sortDESC() : uint[]

Sort descending unsigned integer in array

  1. function example() public view returns (uint[]) {
  2. return items.sortDESC();
  3. }

getSize() : uint

Get size array

  1. function example() public view returns (uint) {
  2. uint size = items.getSize();
  3. return size;
  4. }

sum() : uint

Sum unsigned integer in array

  1. function example() public view returns (uint) {
  2. uint result = items.sum();
  3. return result;
  4. }

Strings

concat(string)

Join two strings

  1. function example() {
  2. string memory str = "20scoops";
  3. str = str.concat(" ").concat("CNX");
  4. }

length() : uint

Get the length of a string

  1. function example() returns (uint) {
  2. string memory str = "20scoops CNX";
  3. return str.length();
  4. }

replaceAll(bytes1, btyes1) : string

Replace all character in string

  1. function example() public view returns (string) {
  2. string memory str = "20scoops CNX ";
  3. return str.replaceAll(" ", "");
  4. }

replace(bytes1, bytes1) : string

Replace character in string

  1. function example() public view returns (string) {
  2. string memory str = "20scoops CNX ";
  3. return str.replace(" ", "");
  4. }

compareTo(string) : bool

Compare two string

  1. function example() public view returns (bool) {
  2. string memory str = "20scoops CNX";
  3. return str.compareTo("20scoops");
  4. }

Intergers

parseInt(string) : uint

Convert string to unsigned integer

  1. function example() public view returns (uint) {
  2. return Integers.parseInt("99.00");
  3. }

toString() : uint

Convert unsigned integer to string

  1. function example(uint value) public view returns (string) {
  2. return value.toString();
  3. }

plus(uint) : uint

Plus unsigned integer

  1. function example() public view returns (uint) {
  2. uint myInt = 5;
  3. return myInt.plus(5);
  4. }

minus(uint) : uint

Minus unsigned integer

  1. function example() public view returns (string) {
  2. uint myInt = 5;
  3. return myInt.minus(5);
  4. }

divide(uint) : uint

Divide unsigned integer

  1. function example(uint value) public view returns (uint) {
  2. uint myInt = 5;
  3. return myInt.divide(5);
  4. }

multiply(uint) : uint

Multiply unsigned integer to string

  1. function example() public view returns (uint) {
  2. uint myInt = 5;
  3. return myInt.multiply(5);
  4. }

mod(uint) : uint

Modular unsigned integer

  1. function example() public view returns (string) {
  2. uint myInt = 500;
  3. return myInt.mod(6);
  4. }