2021-04-24 00:21:46 +00:00
|
|
|
==== ExternalSource: _stringutils/stringutils.sol ====
|
|
|
|
==== Source: strings.sol ====
|
|
|
|
pragma abicoder v2;
|
|
|
|
import "_stringutils/stringutils.sol";
|
|
|
|
|
|
|
|
contract test {
|
|
|
|
using strings for bytes32;
|
|
|
|
using strings for string;
|
|
|
|
using strings for strings.slice;
|
|
|
|
|
|
|
|
function toSlice(string memory a) external pure returns (strings.slice memory) {
|
|
|
|
return a.toSlice();
|
|
|
|
}
|
|
|
|
|
|
|
|
function roundtrip(string memory a) external pure returns (string memory) {
|
|
|
|
return a.toSlice().toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function utf8len(string memory a) external pure returns (uint) {
|
|
|
|
return a.toSlice().len();
|
|
|
|
}
|
|
|
|
|
|
|
|
function multiconcat(string memory a, uint count) public pure returns (string memory) {
|
|
|
|
strings.slice memory s = a.toSlice();
|
|
|
|
for (uint i = 0; i < count; i++) {
|
|
|
|
s = s.concat(s).toSlice();
|
|
|
|
}
|
|
|
|
return s.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
function benchmark(string memory text, bytes32 seed) external pure returns (uint) {
|
|
|
|
// Grow text.
|
|
|
|
text = multiconcat(text, 10);
|
|
|
|
|
|
|
|
strings.slice memory a = text.toSlice();
|
|
|
|
strings.slice memory b = seed.toSliceB32();
|
|
|
|
|
|
|
|
// Some heavy computation.
|
|
|
|
bool c = b.equals(a) || b.startsWith(a);
|
|
|
|
|
|
|
|
// Join as a list.
|
|
|
|
strings.slice memory delim = c ? string(",").toSlice() : string(";").toSlice();
|
|
|
|
strings.slice[] memory parts = new strings.slice[](2);
|
|
|
|
parts[0] = a;
|
|
|
|
parts[1] = b;
|
|
|
|
string memory d = delim.join(parts);
|
|
|
|
return d.toSlice().len();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// ----
|
|
|
|
// constructor()
|
2023-04-26 11:03:17 +00:00
|
|
|
// gas irOptimized: 636074
|
|
|
|
// gas legacy: 1065857
|
|
|
|
// gas legacyOptimized: 725207
|
2021-04-24 00:21:46 +00:00
|
|
|
// toSlice(string): 0x20, 11, "hello world" -> 11, 0xa0
|
2021-09-15 15:01:40 +00:00
|
|
|
// gas irOptimized: 22660
|
2021-04-24 00:21:46 +00:00
|
|
|
// gas legacy: 23190
|
|
|
|
// gas legacyOptimized: 22508
|
|
|
|
// roundtrip(string): 0x20, 11, "hello world" -> 0x20, 11, "hello world"
|
2021-09-15 15:01:40 +00:00
|
|
|
// gas irOptimized: 23408
|
2021-04-24 00:22:13 +00:00
|
|
|
// gas legacy: 23820
|
|
|
|
// gas legacyOptimized: 23123
|
2021-04-24 00:21:46 +00:00
|
|
|
// utf8len(string): 0x20, 16, "\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83\xf0\x9f\x98\x83" -> 4 # Input: "😃😃😃😃" #
|
2021-09-15 15:01:40 +00:00
|
|
|
// gas irOptimized: 24026
|
2021-04-24 00:21:46 +00:00
|
|
|
// gas legacy: 25716
|
|
|
|
// gas legacyOptimized: 24115
|
|
|
|
// multiconcat(string,uint256): 0x40, 3, 11, "hello world" -> 0x20, 0x58, 0x68656c6c6f20776f726c6468656c6c6f20776f726c6468656c6c6f20776f726c, 0x6468656c6c6f20776f726c6468656c6c6f20776f726c6468656c6c6f20776f72, 49027192869463622675296414541903001712009715982962058146354235762728281047040 # concatenating 3 times #
|
2021-09-15 15:01:40 +00:00
|
|
|
// gas irOptimized: 28440
|
2021-04-24 00:21:46 +00:00
|
|
|
// gas legacy: 31621
|
2021-04-24 00:22:13 +00:00
|
|
|
// gas legacyOptimized: 27914
|
|
|
|
// benchmark(string,bytes32): 0x40, 0x0842021, 8, "solidity" -> 0x2020
|
2023-04-26 11:03:17 +00:00
|
|
|
// gas irOptimized: 1981693
|
|
|
|
// gas legacy: 4235651
|
|
|
|
// gas legacyOptimized: 2319622
|