Skip to content

Commit 47de64e

Browse files
committed
update to src & tests to sol 0.8
1 parent 5c9ab99 commit 47de64e

File tree

4 files changed

+42
-30
lines changed

4 files changed

+42
-30
lines changed

.github/workflows/ci.yml

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
name: "Test"
1+
name: "CI"
22
on:
3-
pull_request:
43
push:
54
jobs:
65
tests:
@@ -10,5 +9,9 @@ jobs:
109
- uses: cachix/install-nix-action@v13
1110
with:
1211
nix_path: nixpkgs=channel:nixos-unstable
13-
- run: nix-env -iA dapp -f $(curl -sS https://api.github.com/repos/dapphub/dapptools/releases/latest | jq -r .tarball_url)
14-
- run: make test
12+
- name: Install dapp
13+
run: nix-env -iA dapp -f $(curl -sS https://api.github.com/repos/dapphub/dapptools/releases/latest | jq -r .tarball_url)
14+
- name: Fetch submodules
15+
run: git submodule update --init
16+
- name: Run tests
17+
run: make test

src/strings.sol

+19-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ library strings {
5353
}
5454

5555
// Copy remaining bytes
56-
uint mask = 256 ** (32 - len) - 1;
56+
uint mask = type(uint).max;
57+
if (len > 0) {
58+
mask = 256 ** (32 - len) - 1;
59+
}
5760
assembly {
5861
let srcpart := and(mload(src), not(mask))
5962
let destpart := and(mload(dest), mask)
@@ -83,23 +86,23 @@ library strings {
8386
uint ret;
8487
if (self == 0)
8588
return 0;
86-
if (uint(self) & 0xffffffffffffffffffffffffffffffff == 0) {
89+
if (uint(self) & type(uint128).max == 0) {
8790
ret += 16;
8891
self = bytes32(uint(self) / 0x100000000000000000000000000000000);
8992
}
90-
if (uint(self) & 0xffffffffffffffff == 0) {
93+
if (uint(self) & type(uint64).max == 0) {
9194
ret += 8;
9295
self = bytes32(uint(self) / 0x10000000000000000);
9396
}
94-
if (uint(self) & 0xffffffff == 0) {
97+
if (uint(self) & type(uint32).max == 0) {
9598
ret += 4;
9699
self = bytes32(uint(self) / 0x100000000);
97100
}
98-
if (uint(self) & 0xffff == 0) {
101+
if (uint(self) & type(uint16).max == 0) {
99102
ret += 2;
100103
self = bytes32(uint(self) / 0x10000);
101104
}
102-
if (uint(self) & 0xff == 0) {
105+
if (uint(self) & type(uint8).max == 0) {
103106
ret += 1;
104107
}
105108
return 32 - ret;
@@ -211,12 +214,12 @@ library strings {
211214
}
212215
if (a != b) {
213216
// Mask out irrelevant bytes and check again
214-
uint256 mask = type(uint256).max; // 0xffff...
217+
uint mask = type(uint).max; // 0xffff...
215218
if(shortest < 32) {
216219
mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
217220
}
218221
unchecked {
219-
uint256 diff = (a & mask) - (b & mask);
222+
uint diff = (a & mask) - (b & mask);
220223
if (diff != 0)
221224
return int(diff);
222225
}
@@ -469,7 +472,10 @@ library strings {
469472

470473
if (needlelen <= selflen) {
471474
if (needlelen <= 32) {
472-
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
475+
bytes32 mask;
476+
if (needlelen > 0) {
477+
mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
478+
}
473479

474480
bytes32 needledata;
475481
assembly { needledata := and(mload(needleptr), mask) }
@@ -509,7 +515,10 @@ library strings {
509515

510516
if (needlelen <= selflen) {
511517
if (needlelen <= 32) {
512-
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
518+
bytes32 mask;
519+
if (needlelen > 0) {
520+
mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
521+
}
513522

514523
bytes32 needledata;
515524
assembly { needledata := and(mload(needleptr), mask) }

src/strings.t.sol

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
pragma solidity ^0.4.21;
1+
pragma solidity ^0.8.0;
22

33
import 'ds-test/test.sol';
44
import './strings.sol';
@@ -17,15 +17,15 @@ contract StringsTest is DSTest {
1717
return x == 0 ? int(0) : (x < 0 ? -1 : int(1));
1818
}
1919

20-
function assertEq0(string a, string b) internal {
20+
function assertEq0(string memory a, string memory b) internal {
2121
assertEq0(bytes(a), bytes(b));
2222
}
2323

2424
function assertEq0(strings.slice memory a, strings.slice memory b) internal {
2525
assertEq0(a.toString(), b.toString());
2626
}
2727

28-
function assertEq0(strings.slice memory a, string b) internal {
28+
function assertEq0(strings.slice memory a, string memory b) internal {
2929
assertEq0(a.toString(), b);
3030
}
3131

@@ -58,8 +58,8 @@ contract StringsTest is DSTest {
5858
function testLen() public {
5959
assertEq("".toSlice().len(), 0);
6060
assertEq("Hello, world!".toSlice().len(), 13);
61-
assertEq("naïve".toSlice().len(), 5);
62-
assertEq("こんにちは".toSlice().len(), 5);
61+
assertEq(unicode"naïve".toSlice().len(), 5);
62+
assertEq(unicode"こんにちは".toSlice().len(), 5);
6363
}
6464

6565
function testEmpty() public {
@@ -74,23 +74,23 @@ contract StringsTest is DSTest {
7474
}
7575

7676
function testNextRune() public {
77-
strings.slice memory s = "a¡ࠀ𐀡".toSlice();
77+
strings.slice memory s = unicode"a¡ࠀ𐀡".toSlice();
7878
assertEq0(s.nextRune(), "a");
79-
assertEq0(s, "¡ࠀ𐀡");
80-
assertEq0(s.nextRune(), "¡");
81-
assertEq0(s, "ࠀ𐀡");
82-
assertEq0(s.nextRune(), "");
83-
assertEq0(s, "𐀡");
84-
assertEq0(s.nextRune(), "𐀡");
79+
assertEq0(s, unicode"¡ࠀ𐀡");
80+
assertEq0(s.nextRune(), unicode"¡");
81+
assertEq0(s, unicode"ࠀ𐀡");
82+
assertEq0(s.nextRune(), unicode"");
83+
assertEq0(s, unicode"𐀡");
84+
assertEq0(s.nextRune(), unicode"𐀡");
8585
assertEq0(s, "");
8686
assertEq0(s.nextRune(), "");
8787
}
8888

8989
function testOrd() public {
9090
assertEq("a".toSlice().ord(), 0x61);
91-
assertEq("¡".toSlice().ord(), 0xA1);
92-
assertEq("".toSlice().ord(), 0x800);
93-
assertEq("𐀡".toSlice().ord(), 0x10021);
91+
assertEq(unicode"¡".toSlice().ord(), 0xA1);
92+
assertEq(unicode"".toSlice().ord(), 0x800);
93+
assertEq(unicode"𐀡".toSlice().ord(), 0x10021);
9494
}
9595

9696
function testCompare() public {

0 commit comments

Comments
 (0)