Skip to content

Commit 719f0aa

Browse files
committed
Migrate app from dapple to dapp and fix simple warnings.
Couldn't run unit tests under dapple due to boost error. Since dapple is deprecated anyway, makes sense to migrate to dapp. Warnings fixed are: - Use of 'var' deprecated - Local variable 'len' shadows contract function 'len' The warnings that remain are: - use of labels deprecated warning (findPtr, rFindPtr) - Jump instructions can lead to incorrect stack access (findPtr, rFindPtr) - Function state mutability can be restricted to pure (various) Last warning could be fixed easily, but it is easier to apply to all functions at once once findPtr, rFindPtr can be declared pure. Some functions (toSlice, compare) have been overloaded to accept either strings or slices, calling on strings will cause "<name> not visible after argument dependent lookup in literal_string" errors using compiler version 0.4.21. Remainder of functions to be updated in a later commit -- testCompare is failing with a BadInstruction error, possibly OOB error when using mload.
1 parent c76bd82 commit 719f0aa

6 files changed

+260
-253
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
**/chain_db//out
2+
out

src/SolidityStringutils.sol

-4
This file was deleted.

src/SolidityStringutils.t.sol

-21
This file was deleted.

strings.sol renamed to src/strings.sol

+37-21
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,18 @@ library strings {
138138
* @return A newly allocated string containing the slice's text.
139139
*/
140140
function toString(slice self) internal returns (string) {
141-
var ret = new string(self._len);
141+
string memory ret = new string(self._len);
142142
uint retptr;
143143
assembly { retptr := add(ret, 32) }
144144

145145
memcpy(retptr, self._ptr, self._len);
146146
return ret;
147147
}
148148

149+
function len(string self) internal returns (uint l) {
150+
return len(toSlice(self));
151+
}
152+
149153
/*
150154
* @dev Returns the length in runes of the slice. Note that this operation
151155
* takes time proportional to the length of the slice; avoid using it
@@ -156,8 +160,8 @@ library strings {
156160
*/
157161
function len(slice self) internal returns (uint l) {
158162
// Starting at ptr-31 means the LSB will be the byte we care about
159-
var ptr = self._ptr - 31;
160-
var end = ptr + self._len;
163+
uint ptr = self._ptr - 31;
164+
uint end = ptr + self._len;
161165
for (l = 0; ptr < end; l++) {
162166
uint8 b;
163167
assembly { b := and(mload(ptr), 0xFF) }
@@ -186,6 +190,18 @@ library strings {
186190
return self._len == 0;
187191
}
188192

193+
function compare(string self, string other) internal returns (int) {
194+
return compare(toSlice(self), toSlice(other));
195+
}
196+
197+
function compare(string self, slice other) internal returns (int) {
198+
return compare(toSlice(self), other);
199+
}
200+
201+
function compare(slice self, string other) internal returns (int) {
202+
return compare(self, toSlice(other));
203+
}
204+
189205
/*
190206
* @dev Returns a positive number if `other` comes lexicographically after
191207
* `self`, a negative number if it comes before, or zero if the
@@ -200,8 +216,8 @@ library strings {
200216
if (other._len < self._len)
201217
shortest = other._len;
202218

203-
var selfptr = self._ptr;
204-
var otherptr = other._ptr;
219+
uint selfptr = self._ptr;
220+
uint otherptr = other._ptr;
205221
for (uint idx = 0; idx < shortest; idx += 32) {
206222
uint a;
207223
uint b;
@@ -211,8 +227,8 @@ library strings {
211227
}
212228
if (a != b) {
213229
// Mask out irrelevant bytes and check again
214-
uint mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
215-
var diff = (a & mask) - (b & mask);
230+
uint256 mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
231+
uint256 diff = (a & mask) - (b & mask);
216232
if (diff != 0)
217233
return int(diff);
218234
}
@@ -247,31 +263,31 @@ library strings {
247263
return rune;
248264
}
249265

250-
uint len;
266+
uint l;
251267
uint b;
252268
// Load the first byte of the rune into the LSBs of b
253269
assembly { b := and(mload(sub(mload(add(self, 32)), 31)), 0xFF) }
254270
if (b < 0x80) {
255-
len = 1;
271+
l = 1;
256272
} else if(b < 0xE0) {
257-
len = 2;
273+
l = 2;
258274
} else if(b < 0xF0) {
259-
len = 3;
275+
l = 3;
260276
} else {
261-
len = 4;
277+
l = 4;
262278
}
263279

264280
// Check for truncated codepoints
265-
if (len > self._len) {
281+
if (l > self._len) {
266282
rune._len = self._len;
267283
self._ptr += self._len;
268284
self._len = 0;
269285
return rune;
270286
}
271287

272-
self._ptr += len;
273-
self._len -= len;
274-
rune._len = len;
288+
self._ptr += l;
289+
self._len -= l;
290+
rune._len = l;
275291
return rune;
276292
}
277293

@@ -301,7 +317,7 @@ library strings {
301317

302318
// Load the rune into the MSBs of b
303319
assembly { word:= mload(mload(add(self, 32))) }
304-
var b = word / divisor;
320+
uint b = word / divisor;
305321
if (b < 0x80) {
306322
ret = b;
307323
length = 1;
@@ -411,7 +427,7 @@ library strings {
411427
return false;
412428
}
413429

414-
var selfptr = self._ptr + self._len - needle._len;
430+
uint selfptr = self._ptr + self._len - needle._len;
415431

416432
if (selfptr == needle._ptr) {
417433
return true;
@@ -439,7 +455,7 @@ library strings {
439455
return self;
440456
}
441457

442-
var selfptr = self._ptr + self._len - needle._len;
458+
uint selfptr = self._ptr + self._len - needle._len;
443459
bool equal = true;
444460
if (selfptr != needle._ptr) {
445461
assembly {
@@ -669,7 +685,7 @@ library strings {
669685
* @return The concatenation of the two strings.
670686
*/
671687
function concat(slice self, slice other) internal returns (string) {
672-
var ret = new string(self._len + other._len);
688+
string memory ret = new string(self._len + other._len);
673689
uint retptr;
674690
assembly { retptr := add(ret, 32) }
675691
memcpy(retptr, self._ptr, self._len);
@@ -693,7 +709,7 @@ library strings {
693709
for(uint i = 0; i < parts.length; i++)
694710
length += parts[i]._len;
695711

696-
var ret = new string(length);
712+
string memory ret = new string(length);
697713
uint retptr;
698714
assembly { retptr := add(ret, 32) }
699715

0 commit comments

Comments
 (0)