Преглед изворни кода

Add Strings.equal (#3774)

Co-authored-by: Francisco <fg@frang.io>
Matteo Casonato пре 2 година
родитељ
комит
7a6a9d1516
4 измењених фајлова са 37 додато и 0 уклоњено
  1. 1 0
      CHANGELOG.md
  2. 4 0
      contracts/mocks/StringsMock.sol
  3. 7 0
      contracts/utils/Strings.sol
  4. 25 0
      test/utils/Strings.test.js

+ 1 - 0
CHANGELOG.md

@@ -7,6 +7,7 @@
  * `ERC20Votes`: optimize by using unchecked arithmetic. ([#3748](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3748))
  * `Initializable`: optimize `_disableInitializers` by using `!=` instead of `<`. ([#3787](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3787))
  * `Math`: optimize `log256` rounding check. ([#3745](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3745))
+ * `Strings`: add `equal` method. ([#3774](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3774))
 
 ### Deprecations
 

+ 4 - 0
contracts/mocks/StringsMock.sol

@@ -20,4 +20,8 @@ contract StringsMock {
     function toHexString(address addr) public pure returns (string memory) {
         return Strings.toHexString(addr);
     }
+
+    function equal(string memory a, string memory b) public pure returns (bool) {
+        return Strings.equal(a, b);
+    }
 }

+ 7 - 0
contracts/utils/Strings.sol

@@ -67,4 +67,11 @@ 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 equal(string memory a, string memory b) internal pure returns (bool) {
+        return keccak256(bytes(a)) == keccak256(bytes(b));
+    }
 }

+ 25 - 0
test/utils/Strings.test.js

@@ -83,4 +83,29 @@ contract('Strings', function (accounts) {
       expect(await this.strings.methods['toHexString(address)'](addr)).to.equal(addr);
     });
   });
+
+  describe('equal', function () {
+    it('compares two empty strings', async function () {
+      expect(await this.strings.methods['equal(string,string)']('', '')).to.equal(true);
+    });
+
+    it('compares two equal strings', async function () {
+      expect(await this.strings.methods['equal(string,string)']('a', 'a')).to.equal(true);
+    });
+
+    it('compares two different strings', async function () {
+      expect(await this.strings.methods['equal(string,string)']('a', 'b')).to.equal(false);
+    });
+
+    it('compares two different strings of different lengths', async function () {
+      expect(await this.strings.methods['equal(string,string)']('a', 'aa')).to.equal(false);
+      expect(await this.strings.methods['equal(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['equal(string,string)'](str1, str2)).to.equal(false);
+    });
+  });
 });