-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e809402
commit 45aa01d
Showing
12 changed files
with
183 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.23; | ||
|
||
/** | ||
* @title A library to convert between string and bytes32 (assuming 32 characters or less). | ||
* @author M^0 Labs | ||
*/ | ||
library Bytes32String { | ||
function toBytes32(string memory input_) internal pure returns (bytes32) { | ||
return bytes32(abi.encodePacked(input_)); | ||
} | ||
|
||
function toString(bytes32 input_) internal pure returns (string memory) { | ||
uint256 length_; | ||
|
||
while (length_ < 32 && uint8(input_[length_]) != 0) { | ||
++length_; | ||
} | ||
|
||
bytes memory name_ = new bytes(length_); | ||
|
||
for (uint256 index_; index_ < length_; ++index_) { | ||
name_[index_] = input_[index_]; | ||
} | ||
|
||
return string(name_); | ||
} | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
// SPDX-License-Identifier: UNLICENSED | ||
|
||
pragma solidity 0.8.23; | ||
|
||
import { Test } from "../lib/forge-std/src/Test.sol"; | ||
|
||
import { Bytes32StringHarness } from "./utils/Bytes32StringHarness.sol"; | ||
|
||
contract Bytes32StringTests is Test { | ||
Bytes32StringHarness internal _bytes32String; | ||
|
||
function setUp() external { | ||
_bytes32String = new Bytes32StringHarness(); | ||
} | ||
|
||
function test_full() external { | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("")), ""); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("T")), "T"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Te")), "Te"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Tes")), "Tes"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test")), "Test"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1")), "Test1"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-")), "Test1-"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-T")), "Test1-T"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Te")), "Test1-Te"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Tes")), "Test1-Tes"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test")), "Test1-Test"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2")), "Test1-Test2"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-")), "Test1-Test2-"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-T")), "Test1-Test2-T"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Te")), "Test1-Test2-Te"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Tes")), "Test1-Test2-Tes"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test")), "Test1-Test2-Test"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3")), "Test1-Test2-Test3"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-")), "Test1-Test2-Test3-"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-T")), "Test1-Test2-Test3-T"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Te")), "Test1-Test2-Test3-Te"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Tes")), "Test1-Test2-Test3-Tes"); | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test")), "Test1-Test2-Test3-Test"); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4")), | ||
"Test1-Test2-Test3-Test4" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-")), | ||
"Test1-Test2-Test3-Test4-" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-T")), | ||
"Test1-Test2-Test3-Test4-T" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Te")), | ||
"Test1-Test2-Test3-Test4-Te" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Tes")), | ||
"Test1-Test2-Test3-Test4-Tes" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test")), | ||
"Test1-Test2-Test3-Test4-Test" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5")), | ||
"Test1-Test2-Test3-Test4-Test5" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-")), | ||
"Test1-Test2-Test3-Test4-Test5-" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-T")), | ||
"Test1-Test2-Test3-Test4-Test5-T" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-Te")), | ||
"Test1-Test2-Test3-Test4-Test5-Te" | ||
); | ||
|
||
// Beyond this point, input is larger than 32 bytes, so it is truncated. | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-Tes")), | ||
"Test1-Test2-Test3-Test4-Test5-Te" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-Test")), | ||
"Test1-Test2-Test3-Test4-Test5-Te" | ||
); | ||
assertEq( | ||
_bytes32String.toString(_bytes32String.toBytes32("Test1-Test2-Test3-Test4-Test5-Test6")), | ||
"Test1-Test2-Test3-Test4-Test5-Te" | ||
); | ||
} | ||
|
||
function testFuzz_full(string memory input_) external { | ||
assertEq(_bytes32String.toString(_bytes32String.toBytes32(input_)), _truncate32(input_)); | ||
} | ||
|
||
function _truncate32(string memory input_) internal pure returns (string memory) { | ||
bytes memory bytesIn_ = bytes(input_); | ||
|
||
if (bytesIn_.length <= 32) return input_; | ||
|
||
bytes memory bytesOut_ = new bytes(32); | ||
|
||
for (uint256 index_; index_ < 32; ++index_) { | ||
bytesOut_[index_] = bytesIn_[index_]; | ||
} | ||
|
||
return string(bytesOut_); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: GPL-3.0 | ||
|
||
pragma solidity 0.8.23; | ||
|
||
import { Bytes32String } from "../../src/libs/Bytes32String.sol"; | ||
|
||
/// @title Bytes32String harness used to correctly display test coverage. | ||
contract Bytes32StringHarness { | ||
function toBytes32(string memory input_) external pure returns (bytes32) { | ||
return Bytes32String.toBytes32(input_); | ||
} | ||
|
||
function toString(bytes32 input_) external pure returns (string memory) { | ||
return Bytes32String.toString(input_); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters