|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity ^0.8.8; |
| 4 | + |
| 5 | +import "./StorageSlot.sol"; |
| 6 | + |
| 7 | +type ShortString is bytes32; |
| 8 | + |
| 9 | +/** |
| 10 | + * @dev This library provides functions to convert short memory strings |
| 11 | + * into a `ShortString` type that can be used as an immutable variable. |
| 12 | + * Strings of arbitrary length can be optimized if they are short enough by |
| 13 | + * the addition of a storage variable used as fallback. |
| 14 | + * |
| 15 | + * Usage example: |
| 16 | + * |
| 17 | + * ```solidity |
| 18 | + * contract Named { |
| 19 | + * using ShortStrings for *; |
| 20 | + * |
| 21 | + * ShortString private immutable _name; |
| 22 | + * string private _nameFallback; |
| 23 | + * |
| 24 | + * constructor(string memory contractName) { |
| 25 | + * _name = contractName.toShortStringWithFallback(_nameFallback); |
| 26 | + * } |
| 27 | + * |
| 28 | + * function name() external view returns (string memory) { |
| 29 | + * return _name.toStringWithFallback(_nameFallback); |
| 30 | + * } |
| 31 | + * } |
| 32 | + * ``` |
| 33 | + */ |
| 34 | +library ShortStrings { |
| 35 | + error StringTooLong(string str); |
| 36 | + |
| 37 | + /** |
| 38 | + * @dev Encode a string of at most 31 chars into a `ShortString`. |
| 39 | + * |
| 40 | + * This will trigger a `StringTooLong` error is the input string is too long. |
| 41 | + */ |
| 42 | + function toShortString(string memory str) internal pure returns (ShortString) { |
| 43 | + bytes memory bstr = bytes(str); |
| 44 | + if (bstr.length > 31) { |
| 45 | + revert StringTooLong(str); |
| 46 | + } |
| 47 | + return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length)); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * @dev Decode a `ShortString` back to a "normal" string. |
| 52 | + */ |
| 53 | + function toString(ShortString sstr) internal pure returns (string memory) { |
| 54 | + uint256 len = length(sstr); |
| 55 | + // using `new string(len)` would work locally but is not memory safe. |
| 56 | + string memory str = new string(32); |
| 57 | + /// @solidity memory-safe-assembly |
| 58 | + assembly { |
| 59 | + mstore(str, len) |
| 60 | + mstore(add(str, 0x20), sstr) |
| 61 | + } |
| 62 | + return str; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @dev Return the length of a `ShortString`. |
| 67 | + */ |
| 68 | + function length(ShortString sstr) internal pure returns (uint256) { |
| 69 | + return uint256(ShortString.unwrap(sstr)) & 0xFF; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * @dev Encode a string into a `ShortString`, or write it to storage if it is too long. |
| 74 | + */ |
| 75 | + function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString) { |
| 76 | + if (bytes(value).length < 32) { |
| 77 | + return toShortString(value); |
| 78 | + } else { |
| 79 | + StorageSlot.getStringSlot(store).value = value; |
| 80 | + return ShortString.wrap(0); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @dev Decode a string that was encoded to `ShortString` or written to storage using {setWithFallback}. |
| 86 | + */ |
| 87 | + function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory) { |
| 88 | + if (length(value) > 0) { |
| 89 | + return toString(value); |
| 90 | + } else { |
| 91 | + return store; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments