Skip to content

Commit

Permalink
Merge pull request #1 from smartcontractkit/sol-0.8
Browse files Browse the repository at this point in the history
Sol 0.8
  • Loading branch information
RyanRHall committed Oct 20, 2021
2 parents 01e955c + 47de64e commit d60bcd2
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "CI"
on:
push:
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/[email protected]
- uses: cachix/install-nix-action@v13
with:
nix_path: nixpkgs=channel:nixos-unstable
- name: Install dapp
run: nix-env -iA dapp -f $(curl -sS https://api.github.com/repos/dapphub/dapptools/releases/latest | jq -r .tarball_url)
- name: Fetch submodules
run: git submodule update --init
- name: Run tests
run: make test
2 changes: 1 addition & 1 deletion lib/ds-test
Submodule ds-test updated 5 files
+0 −6 Dappfile
+13 −1 Makefile
+4 −0 default.nix
+223 −0 demo/demo.sol
+352 −57 src/test.sol
39 changes: 25 additions & 14 deletions src/strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
* corresponding to the left and right parts of the string.
*/

pragma solidity ^0.4.14;
pragma solidity ^0.8.0;

library strings {
struct slice {
Expand All @@ -53,7 +53,10 @@ library strings {
}

// Copy remaining bytes
uint mask = 256 ** (32 - len) - 1;
uint mask = type(uint).max;
if (len > 0) {
mask = 256 ** (32 - len) - 1;
}
assembly {
let srcpart := and(mload(src), not(mask))
let destpart := and(mload(dest), mask)
Expand Down Expand Up @@ -83,23 +86,23 @@ library strings {
uint ret;
if (self == 0)
return 0;
if (self & 0xffffffffffffffffffffffffffffffff == 0) {
if (uint(self) & type(uint128).max == 0) {
ret += 16;
self = bytes32(uint(self) / 0x100000000000000000000000000000000);
}
if (self & 0xffffffffffffffff == 0) {
if (uint(self) & type(uint64).max == 0) {
ret += 8;
self = bytes32(uint(self) / 0x10000000000000000);
}
if (self & 0xffffffff == 0) {
if (uint(self) & type(uint32).max == 0) {
ret += 4;
self = bytes32(uint(self) / 0x100000000);
}
if (self & 0xffff == 0) {
if (uint(self) & type(uint16).max == 0) {
ret += 2;
self = bytes32(uint(self) / 0x10000);
}
if (self & 0xff == 0) {
if (uint(self) & type(uint8).max == 0) {
ret += 1;
}
return 32 - ret;
Expand Down Expand Up @@ -211,13 +214,15 @@ library strings {
}
if (a != b) {
// Mask out irrelevant bytes and check again
uint256 mask = uint256(-1); // 0xffff...
uint mask = type(uint).max; // 0xffff...
if(shortest < 32) {
mask = ~(2 ** (8 * (32 - shortest + idx)) - 1);
}
uint256 diff = (a & mask) - (b & mask);
if (diff != 0)
return int(diff);
unchecked {
uint diff = (a & mask) - (b & mask);
if (diff != 0)
return int(diff);
}
}
selfptr += 32;
otherptr += 32;
Expand Down Expand Up @@ -467,7 +472,10 @@ library strings {

if (needlelen <= selflen) {
if (needlelen <= 32) {
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
bytes32 mask;
if (needlelen > 0) {
mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
}

bytes32 needledata;
assembly { needledata := and(mload(needleptr), mask) }
Expand Down Expand Up @@ -507,7 +515,10 @@ library strings {

if (needlelen <= selflen) {
if (needlelen <= 32) {
bytes32 mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
bytes32 mask;
if (needlelen > 0) {
mask = bytes32(~(2 ** (8 * (32 - needlelen)) - 1));
}

bytes32 needledata;
assembly { needledata := and(mload(needleptr), mask) }
Expand Down Expand Up @@ -702,7 +713,7 @@ library strings {
uint retptr;
assembly { retptr := add(ret, 32) }

for(i = 0; i < parts.length; i++) {
for(uint i = 0; i < parts.length; i++) {
memcpy(retptr, parts[i]._ptr, parts[i]._len);
retptr += parts[i]._len;
if (i < parts.length - 1) {
Expand Down
30 changes: 15 additions & 15 deletions src/strings.t.sol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pragma solidity ^0.4.21;
pragma solidity ^0.8.0;

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

function assertEq0(string a, string b) internal {
function assertEq0(string memory a, string memory b) internal {
assertEq0(bytes(a), bytes(b));
}

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

function assertEq0(strings.slice memory a, string b) internal {
function assertEq0(strings.slice memory a, string memory b) internal {
assertEq0(a.toString(), b);
}

Expand Down Expand Up @@ -58,8 +58,8 @@ contract StringsTest is DSTest {
function testLen() public {
assertEq("".toSlice().len(), 0);
assertEq("Hello, world!".toSlice().len(), 13);
assertEq("naïve".toSlice().len(), 5);
assertEq("こんにちは".toSlice().len(), 5);
assertEq(unicode"naïve".toSlice().len(), 5);
assertEq(unicode"こんにちは".toSlice().len(), 5);
}

function testEmpty() public {
Expand All @@ -74,23 +74,23 @@ contract StringsTest is DSTest {
}

function testNextRune() public {
strings.slice memory s = "a¡ࠀ𐀡".toSlice();
strings.slice memory s = unicode"a¡ࠀ𐀡".toSlice();
assertEq0(s.nextRune(), "a");
assertEq0(s, "¡ࠀ𐀡");
assertEq0(s.nextRune(), "¡");
assertEq0(s, "ࠀ𐀡");
assertEq0(s.nextRune(), "");
assertEq0(s, "𐀡");
assertEq0(s.nextRune(), "𐀡");
assertEq0(s, unicode"¡ࠀ𐀡");
assertEq0(s.nextRune(), unicode"¡");
assertEq0(s, unicode"ࠀ𐀡");
assertEq0(s.nextRune(), unicode"");
assertEq0(s, unicode"𐀡");
assertEq0(s.nextRune(), unicode"𐀡");
assertEq0(s, "");
assertEq0(s.nextRune(), "");
}

function testOrd() public {
assertEq("a".toSlice().ord(), 0x61);
assertEq("¡".toSlice().ord(), 0xA1);
assertEq("".toSlice().ord(), 0x800);
assertEq("𐀡".toSlice().ord(), 0x10021);
assertEq(unicode"¡".toSlice().ord(), 0xA1);
assertEq(unicode"".toSlice().ord(), 0x800);
assertEq(unicode"𐀡".toSlice().ord(), 0x10021);
}

function testCompare() public {
Expand Down

0 comments on commit d60bcd2

Please sign in to comment.