ShortStrings
Title: ShortStrings
Author: Modified from OpenZeppelin (https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/ShortStrings.sol)
Pack strings of up to 31 bytes into a single bytes32 word.
Mirrors OpenZeppelin's ShortStrings. Strings longer than 31 bytes
fall back to a string storage variable held by the caller.
The sentinel value for a fallback ShortString is bytes32(type(uint256).max)
(all 0xff), which cannot be a valid packed short string since the length
byte would be 255 — greater than 31.
Constants
FALLBACK_SENTINEL
Sentinel for a ShortString stored in the fallback slot.
bytes32 private constant FALLBACK_SENTINEL = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;Functions
toShortString
Packs a string of up to 31 bytes into a ShortString.
Reverts with StringTooLong if the string is 32 bytes or longer.
function toShortString(string memory str) internal pure returns (ShortString);Parameters
| Name | Type | Description |
|---|---|---|
| str | string | The string to pack. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | ShortString | The packed ShortString. |
toString
Unpacks a ShortString into a string.
Reverts with InvalidShortString if the ShortString is the fallback sentinel.
function toString(ShortString sstr) internal pure returns (string memory);Parameters
| Name | Type | Description |
|---|---|---|
| sstr | ShortString | The ShortString to unpack. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | string | The original string. |
byteLength
Returns the byte length of a ShortString.
The length is stored in the lowest byte of the packed word. Reverts with InvalidShortString if the ShortString is the fallback sentinel.
function byteLength(ShortString sstr) internal pure returns (uint256);Parameters
| Name | Type | Description |
|---|---|---|
| sstr | ShortString | The ShortString. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | uint256 | The byte length. |
toShortStringWithFallback
Converts a string to a ShortString, falling back to storage for long strings.
If the string is longer than 31 bytes, it is stored in store and the
fallback sentinel is returned. Otherwise, the packed ShortString is returned
and store is left unchanged (but is assigned to empty to satisfy the compiler).
function toShortStringWithFallback(string memory value, string storage store) internal returns (ShortString);Parameters
| Name | Type | Description |
|---|---|---|
| value | string | The string to convert. |
| store | string | A storage reference to use for strings that are too long. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | ShortString | The ShortString (may be the fallback sentinel). |
toStringWithFallback
Converts a ShortString back to a string, using storage fallback if needed.
function toStringWithFallback(ShortString value, string storage store) internal pure returns (string memory);Parameters
| Name | Type | Description |
|---|---|---|
| value | ShortString | The ShortString to convert. |
| store | string | The storage fallback that may contain the full string. |
Returns
| Name | Type | Description |
|---|---|---|
| <none> | string | The original string. |
_stringSlot
Returns a StringSlot pointing at the same storage slot as store.
function _stringSlot(string storage store) private pure returns (StringSlot storage ptr);Parameters
| Name | Type | Description |
|---|---|---|
| store | string |
Returns
| Name | Type | Description |
|---|---|---|
| ptr | StringSlot |
_pack
Packs bytes into the upper bytes of a bytes32 word, length in the lowest byte.
function _pack(bytes memory bstr) private pure returns (bytes32 result);Parameters
| Name | Type | Description |
|---|---|---|
| bstr | bytes |
Returns
| Name | Type | Description |
|---|---|---|
| result | bytes32 |
Errors
StringTooLong
error StringTooLong(string str);Parameters
| Name | Type | Description |
|---|---|---|
| str | string |
InvalidShortString
error InvalidShortString();Structs
StringSlot
Mirror of OpenZeppelin's StorageSlot.StringSlot. Wrapping a slot in this struct lets
us copy a memory string into a string storage reference via a native assignment.
struct StringSlot {
string value;
}Properties
| Name | Type | Description |
|---|---|---|
| value | string |