-
Notifications
You must be signed in to change notification settings - Fork 519
/
StringEqualityComparisonGasExample.sol
39 lines (33 loc) · 1.16 KB
/
StringEqualityComparisonGasExample.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
pragma solidity ^0.4.19;
contract StringEqualityComparisonGasExample {
function hashCompareInternal(string a, string b) internal returns (bool) {
return keccak256(a) == keccak256(b);
}
function utilCompareInternal(string a, string b) internal returns (bool) {
if (bytes(a).length != bytes(b).length) {
return false;
}
for (uint i = 0; i < bytes(a).length; i ++) {
if(bytes(a)[i] != bytes(b)[i]) {
return false;
}
}
return true;
}
function hashCompareWithLengthCheckInternal(string a, string b) internal returns (bool) {
if (bytes(a).length != bytes(b).length) {
return false;
} else {
return keccak256(a) == keccak256(b);
}
}
function hashCompare(string a, string b) public returns (bool) {
return hashCompareInternal(a, b);
}
function utilCompare(string a, string b) public returns (bool) {
return utilCompareInternal(a, b);
}
function hashCompareWithLengthCheck(string a, string b) public returns (bool) {
return hashCompareWithLengthCheckInternal(a, b);
}
}