Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Strings.equal #3774

Merged
merged 10 commits into from
Dec 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* `ReentrancyGuard`: Add a `_reentrancyGuardEntered` function to expose the guard status. ([#3714](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3714))
* `ERC20Votes`: optimize by using unchecked arithmetic. ([#3748](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3748))
* `Strings`: add `compare` method. ([#3774](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3774))

## Unreleased

Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/StringsMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ contract StringsMock {
function toHexString(address addr) public pure returns (string memory) {
return Strings.toHexString(addr);
}

function compare(string memory a, string memory b) public pure returns (bool) {
return Strings.compare(a, b);
}
}
8 changes: 8 additions & 0 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,12 @@ library Strings {
function toHexString(address addr) internal pure returns (string memory) {
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
}

/**
* @dev Returns true if the two strings are equal.
*/
function compare(string memory a, string memory b) internal pure returns (bool) {
0xCaso marked this conversation as resolved.
Show resolved Hide resolved
if (bytes(a).length != bytes(b).length) return false;
return keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(b));
0xCaso marked this conversation as resolved.
Show resolved Hide resolved
}
}
21 changes: 21 additions & 0 deletions test/utils/Strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,25 @@ contract('Strings', function (accounts) {
expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr);
});
});

describe('compare', function () {
it('compares two equal strings', async function () {
expect(await this.strings.methods['compare(string,string)']('a', 'a')).to.equal(true);
});

it('compares two different strings', async function () {
expect(await this.strings.methods['compare(string,string)']('a', 'b')).to.equal(false);
});

it('compares two different strings of different lengths', async function () {
expect(await this.strings.methods['compare(string,string)']('a', 'aa')).to.equal(false);
expect(await this.strings.methods['compare(string,string)']('aa', 'a')).to.equal(false);
});

it('compares two different strings of different (big) lengths', async function () {
const str1 = 'a'.repeat(201);
const str2 = 'a'.repeat(200) + 'b';
expect(await this.strings.methods['compare(string,string)'](str1, str2)).to.equal(false);
});
});
});