Guest User

Untitled

a guest
Dec 10th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.31 KB | None | 0 0
  1. pragma solidity ^0.4.24;
  2.  
  3. /**
  4. * Strings Library
  5. *
  6. * In summary this is a simple library of string functions which make simple
  7. * string operations less tedious in solidity.
  8. *
  9. * Please be aware these functions can be quite gas heavy so use them only when
  10. * necessary not to clog the blockchain with expensive transactions.
  11. *
  12. * @author James Lockhart <james@n3tw0rk.co.uk>
  13. */
  14. library Strings {
  15.  
  16. /**
  17. * Concat (High gas cost)
  18. *
  19. * Appends two strings together and returns a new value
  20. *
  21. * @param _base When being used for a data type this is the extended object
  22. * otherwise this is the string which will be the concatenated
  23. * prefix
  24. * @param _value The value to be the concatenated suffix
  25. * @return string The resulting string from combinging the base and value
  26. */
  27. function concat(string _base, string _value)
  28. internal
  29. pure
  30. returns (string) {
  31. bytes memory _baseBytes = bytes(_base);
  32. bytes memory _valueBytes = bytes(_value);
  33.  
  34. assert(_valueBytes.length > 0);
  35.  
  36. string memory _tmpValue = new string(_baseBytes.length +
  37. _valueBytes.length);
  38. bytes memory _newValue = bytes(_tmpValue);
  39.  
  40. uint i;
  41. uint j;
  42.  
  43. for(i = 0; i < _baseBytes.length; i++) {
  44. _newValue[j++] = _baseBytes[i];
  45. }
  46.  
  47. for(i = 0; i<_valueBytes.length; i++) {
  48. _newValue[j++] = _valueBytes[i];
  49. }
  50.  
  51. return string(_newValue);
  52. }
  53.  
  54. /**
  55. * Index Of
  56. *
  57. * Locates and returns the position of a character within a string
  58. *
  59. * @param _base When being used for a data type this is the extended object
  60. * otherwise this is the string acting as the haystack to be
  61. * searched
  62. * @param _value The needle to search for, at present this is currently
  63. * limited to one character
  64. * @return int The position of the needle starting from 0 and returning -1
  65. * in the case of no matches found
  66. */
  67. function indexOf(string _base, string _value)
  68. internal
  69. pure
  70. returns (int) {
  71. return _indexOf(_base, _value, 0);
  72. }
  73.  
  74. /**
  75. * Index Of
  76. *
  77. * Locates and returns the position of a character within a string starting
  78. * from a defined offset
  79. *
  80. * @param _base When being used for a data type this is the extended object
  81. * otherwise this is the string acting as the haystack to be
  82. * searched
  83. * @param _value The needle to search for, at present this is currently
  84. * limited to one character
  85. * @param _offset The starting point to start searching from which can start
  86. * from 0, but must not exceed the length of the string
  87. * @return int The position of the needle starting from 0 and returning -1
  88. * in the case of no matches found
  89. */
  90. function _indexOf(string _base, string _value, uint _offset)
  91. internal
  92. pure
  93. returns (int) {
  94. bytes memory _baseBytes = bytes(_base);
  95. bytes memory _valueBytes = bytes(_value);
  96.  
  97. assert(_valueBytes.length == 1);
  98.  
  99. for(uint i = _offset; i < _baseBytes.length; i++) {
  100. if (_baseBytes[i] == _valueBytes[0]) {
  101. return int(i);
  102. }
  103. }
  104.  
  105. return -1;
  106. }
  107.  
  108. /**
  109. * Length
  110. *
  111. * Returns the length of the specified string
  112. *
  113. * @param _base When being used for a data type this is the extended object
  114. * otherwise this is the string to be measured
  115. * @return uint The length of the passed string
  116. */
  117. function length(string _base)
  118. internal
  119. pure
  120. returns (uint) {
  121. bytes memory _baseBytes = bytes(_base);
  122. return _baseBytes.length;
  123. }
  124.  
  125. /**
  126. * Sub String
  127. *
  128. * Extracts the beginning part of a string based on the desired length
  129. *
  130. * @param _base When being used for a data type this is the extended object
  131. * otherwise this is the string that will be used for
  132. * extracting the sub string from
  133. * @param _length The length of the sub string to be extracted from the base
  134. * @return string The extracted sub string
  135. */
  136. function substring(string _base, int _length)
  137. internal
  138. pure
  139. returns (string) {
  140. return _substring(_base, _length, 0);
  141. }
  142.  
  143. /**
  144. * Sub String
  145. *
  146. * Extracts the part of a string based on the desired length and offset. The
  147. * offset and length must not exceed the lenth of the base string.
  148. *
  149. * @param _base When being used for a data type this is the extended object
  150. * otherwise this is the string that will be used for
  151. * extracting the sub string from
  152. * @param _length The length of the sub string to be extracted from the base
  153. * @param _offset The starting point to extract the sub string from
  154. * @return string The extracted sub string
  155. */
  156. function _substring(string _base, int _length, int _offset)
  157. internal
  158. pure
  159. returns (string) {
  160. bytes memory _baseBytes = bytes(_base);
  161.  
  162. assert(uint(_offset+_length) <= _baseBytes.length);
  163.  
  164. string memory _tmp = new string(uint(_length));
  165. bytes memory _tmpBytes = bytes(_tmp);
  166.  
  167. uint j = 0;
  168. for(uint i = uint(_offset); i < uint(_offset+_length); i++) {
  169. _tmpBytes[j++] = _baseBytes[i];
  170. }
  171.  
  172. return string(_tmpBytes);
  173. }
  174.  
  175. /**
  176. * String Split (Very high gas cost)
  177. *
  178. * Splits a string into an array of strings based off the delimiter value.
  179. * Please note this can be quite a gas expensive function due to the use of
  180. * storage so only use if really required.
  181. *
  182. * @param _base When being used for a data type this is the extended object
  183. * otherwise this is the string value to be split.
  184. * @param _value The delimiter to split the string on which must be a single
  185. * character
  186. * @return string[] An array of values split based off the delimiter, but
  187. * do not container the delimiter.
  188. */
  189. function split(string _base, string _value)
  190. internal
  191. returns (string[] storage splitArr) {
  192. bytes memory _baseBytes = bytes(_base);
  193. uint _offset = 0;
  194.  
  195. while(_offset < _baseBytes.length-1) {
  196.  
  197. int _limit = _indexOf(_base, _value, _offset);
  198. if (_limit == -1) {
  199. _limit = int(_baseBytes.length);
  200. }
  201.  
  202. string memory _tmp = new string(uint(_limit)-_offset);
  203. bytes memory _tmpBytes = bytes(_tmp);
  204.  
  205. uint j = 0;
  206. for(uint i = _offset; i < uint(_limit); i++) {
  207. _tmpBytes[j++] = _baseBytes[i];
  208. }
  209. _offset = uint(_limit) + 1;
  210. splitArr.push(string(_tmpBytes));
  211. }
  212. return splitArr;
  213. }
  214.  
  215. /**
  216. * Compare To
  217. *
  218. * Compares the characters of two strings, to ensure that they have an
  219. * identical footprint
  220. *
  221. * @param _base When being used for a data type this is the extended object
  222. * otherwise this is the string base to compare against
  223. * @param _value The string the base is being compared to
  224. * @return bool Simply notates if the two string have an equivalent
  225. */
  226. function compareTo(string _base, string _value)
  227. internal
  228. pure
  229. returns (bool) {
  230. bytes memory _baseBytes = bytes(_base);
  231. bytes memory _valueBytes = bytes(_value);
  232.  
  233. if (_baseBytes.length != _valueBytes.length) {
  234. return false;
  235. }
  236.  
  237. for(uint i = 0; i < _baseBytes.length; i++) {
  238. if (_baseBytes[i] != _valueBytes[i]) {
  239. return false;
  240. }
  241. }
  242.  
  243. return true;
  244. }
  245.  
  246. /**
  247. * Compare To Ignore Case (High gas cost)
  248. *
  249. * Compares the characters of two strings, converting them to the same case
  250. * where applicable to alphabetic characters to distinguish if the values
  251. * match.
  252. *
  253. * @param _base When being used for a data type this is the extended object
  254. * otherwise this is the string base to compare against
  255. * @param _value The string the base is being compared to
  256. * @return bool Simply notates if the two string have an equivalent value
  257. * discarding case
  258. */
  259. function compareToIgnoreCase(string _base, string _value)
  260. internal
  261. pure
  262. returns (bool) {
  263. bytes memory _baseBytes = bytes(_base);
  264. bytes memory _valueBytes = bytes(_value);
  265.  
  266. if (_baseBytes.length != _valueBytes.length) {
  267. return false;
  268. }
  269.  
  270. for(uint i = 0; i < _baseBytes.length; i++) {
  271. if (_baseBytes[i] != _valueBytes[i] &&
  272. _upper(_baseBytes[i]) != _upper(_valueBytes[i])) {
  273. return false;
  274. }
  275. }
  276.  
  277. return true;
  278. }
  279.  
  280. /**
  281. * Upper
  282. *
  283. * Converts all the values of a string to their corresponding upper case
  284. * value.
  285. *
  286. * @param _base When being used for a data type this is the extended object
  287. * otherwise this is the string base to convert to upper case
  288. * @return string
  289. */
  290. function upper(string _base)
  291. internal
  292. pure
  293. returns (string) {
  294. bytes memory _baseBytes = bytes(_base);
  295. for (uint i = 0; i < _baseBytes.length; i++) {
  296. _baseBytes[i] = _upper(_baseBytes[i]);
  297. }
  298. return string(_baseBytes);
  299. }
  300.  
  301. /**
  302. * Lower
  303. *
  304. * Converts all the values of a string to their corresponding lower case
  305. * value.
  306. *
  307. * @param _base When being used for a data type this is the extended object
  308. * otherwise this is the string base to convert to lower case
  309. * @return string
  310. */
  311. function lower(string _base)
  312. internal
  313. pure
  314. returns (string) {
  315. bytes memory _baseBytes = bytes(_base);
  316. for (uint i = 0; i < _baseBytes.length; i++) {
  317. _baseBytes[i] = _lower(_baseBytes[i]);
  318. }
  319. return string(_baseBytes);
  320. }
  321.  
  322. /**
  323. * Upper
  324. *
  325. * Convert an alphabetic character to upper case and return the original
  326. * value when not alphabetic
  327. *
  328. * @param _b1 The byte to be converted to upper case
  329. * @return bytes1 The converted value if the passed value was alphabetic
  330. * and in a lower case otherwise returns the original value
  331. */
  332. function _upper(bytes1 _b1)
  333. private
  334. pure
  335. returns (bytes1) {
  336.  
  337. if (_b1 >= 0x61 && _b1 <= 0x7A) {
  338. return bytes1(uint8(_b1)-32);
  339. }
  340.  
  341. return _b1;
  342. }
  343.  
  344. /**
  345. * Lower
  346. *
  347. * Convert an alphabetic character to lower case and return the original
  348. * value when not alphabetic
  349. *
  350. * @param _b1 The byte to be converted to lower case
  351. * @return bytes1 The converted value if the passed value was alphabetic
  352. * and in a upper case otherwise returns the original value
  353. */
  354. function _lower(bytes1 _b1)
  355. private
  356. pure
  357. returns (bytes1) {
  358.  
  359. if (_b1 >= 0x41 && _b1 <= 0x5A) {
  360. return bytes1(uint8(_b1)+32);
  361. }
  362.  
  363. return _b1;
  364. }
  365. }
Add Comment
Please, Sign In to add comment