From 7c5beafa308bb7b80a8fabf105ce4cd35415c558 Mon Sep 17 00:00:00 2001 From: dil Date: Sun, 18 Jul 2021 00:31:55 +0100 Subject: [PATCH 1/2] Support solidity 0.8.0 https://docs.soliditylang.org/en/breaking/080-breaking-changes.html --- src/strings.sol | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/strings.sol b/src/strings.sol index b449645a..4ad389b6 100644 --- a/src/strings.sol +++ b/src/strings.sol @@ -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 { @@ -83,23 +83,23 @@ library strings { uint ret; if (self == 0) return 0; - if (self & 0xffffffffffffffffffffffffffffffff == 0) { + if (uint(self) & 0xffffffffffffffffffffffffffffffff == 0) { ret += 16; self = bytes32(uint(self) / 0x100000000000000000000000000000000); } - if (self & 0xffffffffffffffff == 0) { + if (uint(self) & 0xffffffffffffffff == 0) { ret += 8; self = bytes32(uint(self) / 0x10000000000000000); } - if (self & 0xffffffff == 0) { + if (uint(self) & 0xffffffff == 0) { ret += 4; self = bytes32(uint(self) / 0x100000000); } - if (self & 0xffff == 0) { + if (uint(self) & 0xffff == 0) { ret += 2; self = bytes32(uint(self) / 0x10000); } - if (self & 0xff == 0) { + if (uint(self) & 0xff == 0) { ret += 1; } return 32 - ret; @@ -211,7 +211,7 @@ library strings { } if (a != b) { // Mask out irrelevant bytes and check again - uint256 mask = uint256(-1); // 0xffff... + uint256 mask = type(uint256).max; // 0xffff... if(shortest < 32) { mask = ~(2 ** (8 * (32 - shortest + idx)) - 1); } @@ -702,7 +702,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) { From d6f6961f22a0a15ac74aa43851cf3d8ce54af1f2 Mon Sep 17 00:00:00 2001 From: dil Date: Sun, 18 Jul 2021 00:53:57 +0100 Subject: [PATCH 2/2] unchecked arithmetic to preserve previous behavior --- src/strings.sol | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/strings.sol b/src/strings.sol index 4ad389b6..cc47f3fc 100644 --- a/src/strings.sol +++ b/src/strings.sol @@ -215,9 +215,11 @@ library strings { if(shortest < 32) { mask = ~(2 ** (8 * (32 - shortest + idx)) - 1); } - uint256 diff = (a & mask) - (b & mask); - if (diff != 0) - return int(diff); + unchecked { + uint256 diff = (a & mask) - (b & mask); + if (diff != 0) + return int(diff); + } } selfptr += 32; otherptr += 32;