Solidity is still very primitive and doing basic operations can be quite tedious and off-putting to newer developers. I've put together a very basic library of functions to help improve this.
The easiest way to use this library is to install it with npm as
npm install willitscale/solidity-utilIn a project based on Truffle framework you may then import and bind the libraries to the appropriate data types as seen below:
pragmasolidity^0.5.0;import"solidity-util/lib/Strings.sol";import"solidity-util/lib/Integers.sol";import"solidity-util/lib/Addresses.sol";contractMyContract{usingStringsforstring;usingIntegersforuint;usingAddressesforaddress;usingAddressesforaddresspayable;}This will then allow the use of the functionality listed below.
Other frameworks may require slightly different approaches than the description above.
The functionality of this library is to extend the existing functionality of an address:
Check to see if the subject address is a contract on the Ethereum network
functionisContract(address_addr)public{if(_addr.isContract()){// Do contract specific stuff}}The functionality of this library is based loosely around the Java implementation:
Convert an ASCII string to its unsigned integer equivalent
functionparseInt(){if(321==Integers.parseInt("321")){// Matches the uint value}}Convert an unsigned integer to its ASCII string equivalent
functiontoString(uint_value)returns(string){return_value.toString();}Convert an unsigned integer to a bytes equivalent
functiontoString(uint_value)returns(bytes){return_value.toBytes();}Convert an 8-bit unsigned integer to its byte equivalent
functiontoByte(uint8_value){if(0x1==Integer.toByte(_value)){// Matching byte}}Please be aware that some of these functions can be quite gas heavy so use appropriately!
The functionality of this library is based loosely around the Java implementation:
- concat(string) : string
- indexOf(string) : int
- length() : uint
- substring(uint) : string
- split(string) : string[]
- compareTo(string) : bool
- compareToIgnoreCase(string) : bool
- upper(string) : string
- lower(string) : string
Concatenate two strings together.
functionconcat(){stringmemorymyVal="firstname";myVal=myVal.concat(" ").concat("lastname");}Find the position of a character.
functionindexOf(){stringmemorymyVal="haystack";uintpositionOfFirstA=myVal.indexOf("a");uintpositionOfSecondA=myVal._indexOf("a",positionOfFirstA+1);}Get the length of a string.
functionlength(){stringmemorymyVal="length";if(myVal.length()==6){// Length is 6!}}Get a partial section of the string.
functionsubstring(){stringmemorymyVal="sub string example";stringmemoryfirstWord=myVal.substring(3);stringmemorysecondWord=myVal._substring(6,4);}Splits a string into an array of smaller strings based off a delimiter.
functionsplit(){stringmemorymyVal="my example split";string[]storagesplit=myVal.split(" ");if(3==split.length){if(split[1].compareTo("example")){// Valid split length and second word}}}Compare two strings.
functioncompareTo(){stringmemorymyVal="my example split";if(myVal.compareTo("my example split")){// They match!}}Compare two strings discarding alphabetic case.
functioncompareToIgnoreCase(){stringmemorymyVal="my example split";if(myVal.compareTo("mY Example Split")){// They match regardless of case!}}Converts a string to use upper alphabetic case.
functionupper(){stringmemorymyVal="lower";if(myVal.upper().compareTo("LOWER")){// It's now upper!}}Converts a string to use lower alphabetic case.
functionlower(){stringmemorymyVal="UPPER";if(myVal.lower().compareTo("upper")){// It's now lower!}}Feel free to contribute!