33
33
* `s.splitNew('.')` leaves s unmodified, and returns two values
34
34
* corresponding to the left and right parts of the string.
35
35
*/
36
+
37
+ pragma solidity ^ 0.4.14 ;
38
+
36
39
library strings {
37
40
struct slice {
38
41
uint _len;
@@ -151,11 +154,11 @@ library strings {
151
154
* @param self The slice to operate on.
152
155
* @return The length of the slice in runes.
153
156
*/
154
- function len(slice self) internal returns (uint) {
157
+ function len (slice self ) internal returns (uint l ) {
155
158
// Starting at ptr-31 means the LSB will be the byte we care about
156
159
var ptr = self._ptr - 31 ;
157
160
var end = ptr + self._len;
158
- for (uint len = 0; ptr < end; len ++) {
161
+ for (l = 0 ; ptr < end; l ++ ) {
159
162
uint8 b;
160
163
assembly { b := and (mload (ptr), 0xFF ) }
161
164
if (b < 0x80 ) {
@@ -172,7 +175,6 @@ library strings {
172
175
ptr += 6 ;
173
176
}
174
177
}
175
- return len;
176
178
}
177
179
178
180
/*
@@ -294,34 +296,34 @@ library strings {
294
296
}
295
297
296
298
uint word;
297
- uint len ;
298
- uint div = 2 ** 248;
299
+ uint length ;
300
+ uint divisor = 2 ** 248 ;
299
301
300
302
// Load the rune into the MSBs of b
301
303
assembly { word:= mload (mload (add (self, 32 ))) }
302
- var b = word / div ;
304
+ var b = word / divisor ;
303
305
if (b < 0x80 ) {
304
306
ret = b;
305
- len = 1;
307
+ length = 1 ;
306
308
} else if (b < 0xE0 ) {
307
309
ret = b & 0x1F ;
308
- len = 2;
310
+ length = 2 ;
309
311
} else if (b < 0xF0 ) {
310
312
ret = b & 0x0F ;
311
- len = 3;
313
+ length = 3 ;
312
314
} else {
313
315
ret = b & 0x07 ;
314
- len = 4;
316
+ length = 4 ;
315
317
}
316
318
317
319
// Check for truncated codepoints
318
- if (len > self._len) {
320
+ if (length > self._len) {
319
321
return 0 ;
320
322
}
321
323
322
- for (uint i = 1; i < len ; i++) {
323
- div = div / 256;
324
- b = (word / div ) & 0xFF;
324
+ for (uint i = 1 ; i < length ; i++ ) {
325
+ divisor = divisor / 256 ;
326
+ b = (word / divisor ) & 0xFF ;
325
327
if (b & 0xC0 != 0x80 ) {
326
328
// Invalid UTF-8 sequence
327
329
return 0 ;
@@ -339,7 +341,7 @@ library strings {
339
341
*/
340
342
function keccak (slice self ) internal returns (bytes32 ret ) {
341
343
assembly {
342
- ret := sha3 (mload(add(self, 32)), mload(self))
344
+ ret := keccak256 (mload (add (self, 32 )), mload (self))
343
345
}
344
346
}
345
347
@@ -360,10 +362,10 @@ library strings {
360
362
361
363
bool equal;
362
364
assembly {
363
- let len := mload(needle)
365
+ let length := mload (needle)
364
366
let selfptr := mload (add (self, 0x20 ))
365
367
let needleptr := mload (add (needle, 0x20 ))
366
- equal := eq(sha3 (selfptr, len ), sha3 (needleptr, len ))
368
+ equal := eq (keccak256 (selfptr, length ), keccak256 (needleptr, length ))
367
369
}
368
370
return equal;
369
371
}
@@ -383,10 +385,10 @@ library strings {
383
385
bool equal = true ;
384
386
if (self._ptr != needle._ptr) {
385
387
assembly {
386
- let len := mload(needle)
388
+ let length := mload (needle)
387
389
let selfptr := mload (add (self, 0x20 ))
388
390
let needleptr := mload (add (needle, 0x20 ))
389
- equal := eq(sha3(selfptr, len ), sha3(needleptr, len ))
391
+ equal := eq (sha3 (selfptr, length ), sha3 (needleptr, length ))
390
392
}
391
393
}
392
394
@@ -417,9 +419,9 @@ library strings {
417
419
418
420
bool equal;
419
421
assembly {
420
- let len := mload(needle)
422
+ let length := mload (needle)
421
423
let needleptr := mload (add (needle, 0x20 ))
422
- equal := eq(sha3 (selfptr, len ), sha3 (needleptr, len ))
424
+ equal := eq (keccak256 (selfptr, length ), keccak256 (needleptr, length ))
423
425
}
424
426
425
427
return equal;
@@ -441,9 +443,9 @@ library strings {
441
443
bool equal = true ;
442
444
if (selfptr != needle._ptr) {
443
445
assembly {
444
- let len := mload(needle)
446
+ let length := mload (needle)
445
447
let needleptr := mload (add (needle, 0x20 ))
446
- equal := eq(sha3 (selfptr, len ), sha3 (needleptr, len ))
448
+ equal := eq (keccak256 (selfptr, length ), keccak256 (needleptr, length ))
447
449
}
448
450
}
449
451
@@ -641,10 +643,10 @@ library strings {
641
643
* @param needle The text to search for in `self`.
642
644
* @return The number of occurrences of `needle` found in `self`.
643
645
*/
644
- function count(slice self, slice needle) internal returns (uint count ) {
646
+ function count (slice self , slice needle ) internal returns (uint cnt ) {
645
647
uint ptr = findPtr (self._len, self._ptr, needle._len, needle._ptr) + needle._len;
646
648
while (ptr <= self._ptr + self._len) {
647
- count ++;
649
+ cnt ++ ;
648
650
ptr = findPtr (self._len - (ptr - self._ptr), ptr, needle._len, needle._ptr) + needle._len;
649
651
}
650
652
}
@@ -687,11 +689,11 @@ library strings {
687
689
if (parts.length == 0 )
688
690
return "" ;
689
691
690
- uint len = self._len * (parts.length - 1);
692
+ uint length = self._len * (parts.length - 1 );
691
693
for (uint i = 0 ; i < parts.length ; i++ )
692
- len += parts[i]._len;
694
+ length += parts[i]._len;
693
695
694
- var ret = new string(len );
696
+ var ret = new string (length );
695
697
uint retptr;
696
698
assembly { retptr := add (ret, 32 ) }
697
699
0 commit comments