Skip to content

Commit 4c36b49

Browse files
authored
Merge pull request Arachnid#17 from chriseth/patch-1
Update strings.sol according to latest compiler version
2 parents 4a06920 + 2f6ca9a commit 4c36b49

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

strings.sol

+30-28
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@
3333
* `s.splitNew('.')` leaves s unmodified, and returns two values
3434
* corresponding to the left and right parts of the string.
3535
*/
36+
37+
pragma solidity ^0.4.14;
38+
3639
library strings {
3740
struct slice {
3841
uint _len;
@@ -151,11 +154,11 @@ library strings {
151154
* @param self The slice to operate on.
152155
* @return The length of the slice in runes.
153156
*/
154-
function len(slice self) internal returns (uint) {
157+
function len(slice self) internal returns (uint l) {
155158
// Starting at ptr-31 means the LSB will be the byte we care about
156159
var ptr = self._ptr - 31;
157160
var end = ptr + self._len;
158-
for (uint len = 0; ptr < end; len++) {
161+
for (l = 0; ptr < end; l++) {
159162
uint8 b;
160163
assembly { b := and(mload(ptr), 0xFF) }
161164
if (b < 0x80) {
@@ -172,7 +175,6 @@ library strings {
172175
ptr += 6;
173176
}
174177
}
175-
return len;
176178
}
177179

178180
/*
@@ -294,34 +296,34 @@ library strings {
294296
}
295297

296298
uint word;
297-
uint len;
298-
uint div = 2 ** 248;
299+
uint length;
300+
uint divisor = 2 ** 248;
299301

300302
// Load the rune into the MSBs of b
301303
assembly { word:= mload(mload(add(self, 32))) }
302-
var b = word / div;
304+
var b = word / divisor;
303305
if (b < 0x80) {
304306
ret = b;
305-
len = 1;
307+
length = 1;
306308
} else if(b < 0xE0) {
307309
ret = b & 0x1F;
308-
len = 2;
310+
length = 2;
309311
} else if(b < 0xF0) {
310312
ret = b & 0x0F;
311-
len = 3;
313+
length = 3;
312314
} else {
313315
ret = b & 0x07;
314-
len = 4;
316+
length = 4;
315317
}
316318

317319
// Check for truncated codepoints
318-
if (len > self._len) {
320+
if (length > self._len) {
319321
return 0;
320322
}
321323

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;
325327
if (b & 0xC0 != 0x80) {
326328
// Invalid UTF-8 sequence
327329
return 0;
@@ -339,7 +341,7 @@ library strings {
339341
*/
340342
function keccak(slice self) internal returns (bytes32 ret) {
341343
assembly {
342-
ret := sha3(mload(add(self, 32)), mload(self))
344+
ret := keccak256(mload(add(self, 32)), mload(self))
343345
}
344346
}
345347

@@ -360,10 +362,10 @@ library strings {
360362

361363
bool equal;
362364
assembly {
363-
let len := mload(needle)
365+
let length := mload(needle)
364366
let selfptr := mload(add(self, 0x20))
365367
let needleptr := mload(add(needle, 0x20))
366-
equal := eq(sha3(selfptr, len), sha3(needleptr, len))
368+
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
367369
}
368370
return equal;
369371
}
@@ -383,10 +385,10 @@ library strings {
383385
bool equal = true;
384386
if (self._ptr != needle._ptr) {
385387
assembly {
386-
let len := mload(needle)
388+
let length := mload(needle)
387389
let selfptr := mload(add(self, 0x20))
388390
let needleptr := mload(add(needle, 0x20))
389-
equal := eq(sha3(selfptr, len), sha3(needleptr, len))
391+
equal := eq(sha3(selfptr, length), sha3(needleptr, length))
390392
}
391393
}
392394

@@ -417,9 +419,9 @@ library strings {
417419

418420
bool equal;
419421
assembly {
420-
let len := mload(needle)
422+
let length := mload(needle)
421423
let needleptr := mload(add(needle, 0x20))
422-
equal := eq(sha3(selfptr, len), sha3(needleptr, len))
424+
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
423425
}
424426

425427
return equal;
@@ -441,9 +443,9 @@ library strings {
441443
bool equal = true;
442444
if (selfptr != needle._ptr) {
443445
assembly {
444-
let len := mload(needle)
446+
let length := mload(needle)
445447
let needleptr := mload(add(needle, 0x20))
446-
equal := eq(sha3(selfptr, len), sha3(needleptr, len))
448+
equal := eq(keccak256(selfptr, length), keccak256(needleptr, length))
447449
}
448450
}
449451

@@ -641,10 +643,10 @@ library strings {
641643
* @param needle The text to search for in `self`.
642644
* @return The number of occurrences of `needle` found in `self`.
643645
*/
644-
function count(slice self, slice needle) internal returns (uint count) {
646+
function count(slice self, slice needle) internal returns (uint cnt) {
645647
uint ptr = findPtr(self._len, self._ptr, needle._len, needle._ptr) + needle._len;
646648
while (ptr <= self._ptr + self._len) {
647-
count++;
649+
cnt++;
648650
ptr = findPtr(self._len - (ptr - self._ptr), ptr, needle._len, needle._ptr) + needle._len;
649651
}
650652
}
@@ -687,11 +689,11 @@ library strings {
687689
if (parts.length == 0)
688690
return "";
689691

690-
uint len = self._len * (parts.length - 1);
692+
uint length = self._len * (parts.length - 1);
691693
for(uint i = 0; i < parts.length; i++)
692-
len += parts[i]._len;
694+
length += parts[i]._len;
693695

694-
var ret = new string(len);
696+
var ret = new string(length);
695697
uint retptr;
696698
assembly { retptr := add(ret, 32) }
697699

0 commit comments

Comments
 (0)