diff options
author | Remco Bloemen <remco@wicked.ventures> | 2018-06-13 23:46:46 +0800 |
---|---|---|
committer | Remco Bloemen <remco@wicked.ventures> | 2018-06-23 19:53:38 +0800 |
commit | 425af46f98c83e2796aaf0b946d250193393754c (patch) | |
tree | 9b37ea0adcd9c2873ebe4a1901d52b733a695123 /packages/contracts/src | |
parent | c83ee04662177407f1d484175d8a76dddf3c4e85 (diff) | |
download | dexon-0x-contracts-425af46f98c83e2796aaf0b946d250193393754c.tar.gz dexon-0x-contracts-425af46f98c83e2796aaf0b946d250193393754c.tar.zst dexon-0x-contracts-425af46f98c83e2796aaf0b946d250193393754c.zip |
Rename bytes.equals
Diffstat (limited to 'packages/contracts/src')
-rw-r--r-- | packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol | 4 | ||||
-rw-r--r-- | packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol | 35 |
2 files changed, 37 insertions, 2 deletions
diff --git a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol index 299452c96..60ee3db7f 100644 --- a/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol +++ b/packages/contracts/src/contracts/current/test/TestLibBytes/TestLibBytes.sol @@ -53,12 +53,12 @@ contract TestLibBytes { /// @param lhs First byte array to compare. /// @param rhs Second byte array to compare. /// @return True if arrays are the same. False otherwise. - function publicAreBytesEqual(bytes memory lhs, bytes memory rhs) + function publicEquals(bytes memory lhs, bytes memory rhs) public pure returns (bool equal) { - equal = lhs.areBytesEqual(rhs); + equal = lhs.equals(rhs); return equal; } diff --git a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol index 58ce31c70..2862ee3dd 100644 --- a/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol +++ b/packages/contracts/src/contracts/current/utils/LibBytes/LibBytes.sol @@ -258,6 +258,41 @@ library LibBytes { return result; } + /// @dev Tests equality of two byte arrays. + /// @param lhs First byte array to compare. + /// @param rhs Second byte array to compare. + /// @return True if arrays are the same. False otherwise. + function equals( + bytes memory lhs, + bytes memory rhs + ) + internal + pure + returns (bool equal) + { + assembly { + // Get the number of words occupied by <lhs> + let lenFullWords := div(add(mload(lhs), 0x1F), 0x20) + + // Add 1 to the number of words, to account for the length field + lenFullWords := add(lenFullWords, 0x1) + + // Test equality word-by-word. + // Terminates early if there is a mismatch. + for {let i := 0} lt(i, lenFullWords) {i := add(i, 1)} { + let lhsWord := mload(add(lhs, mul(i, 0x20))) + let rhsWord := mload(add(rhs, mul(i, 0x20))) + equal := eq(lhsWord, rhsWord) + if eq(equal, 0) { + // Break + i := lenFullWords + } + } + } + + return equal; + } + /// @dev Reads an address from a position in a byte array. /// @param b Byte array containing an address. /// @param index Index in byte array of address. |