Revisions
-
frangio revised this gist
Dec 29, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ pragma solidity ^0.8.9; // Unlike the string type, ShortString is a value type that can be made immutable. // It supports strings of at most 32 bytes and assumes they don't contain null bytes. type ShortString is bytes32; -
frangio revised this gist
Dec 29, 2021 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,9 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.9; // Unlike the string type, ShortString is a value type that can be made immutable. // It supports strings of at most 32 bytes and assumes they are null-terminated. type ShortString is bytes32; error StringTooLong(string s); -
frangio revised this gist
Dec 29, 2021 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,10 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.9; type ShortString is bytes32; error StringTooLong(string s); function toShortString(string memory s) pure returns (ShortString) { bytes memory b = bytes(s); if (b.length > 32) { -
frangio created this gist
Dec 29, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,36 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.9; error StringTooLong(string s); type ShortString is bytes32; function toShortString(string memory s) pure returns (ShortString) { bytes memory b = bytes(s); if (b.length > 32) { revert StringTooLong(s); } return ShortString.wrap(bytes32(b)); } function shortStringLength(ShortString s) pure returns (uint) { uint x = uint(ShortString.unwrap(s)); uint len = 0; for (uint m = 0xff << 248; m != 0; m >>= 8) { if (x & m == 0) { break; } len += 1; } return len; } function toString(ShortString s) pure returns (string memory) { bytes memory b = new bytes(32); uint len = shortStringLength(s); assembly { mstore(b, len) mstore(add(b, 32), s) } return string(b); } This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.9; import './ShortString.sol'; contract TestShortString { ShortString immutable $name; constructor(string memory _name) { $name = toShortString(_name); } function name() public view returns (string memory) { return toString($name); } }