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}
0 commit comments