Skip to content
Lattice

ShortStrings

Git Source

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

NameTypeDescription
strstringThe string to pack.

Returns

NameTypeDescription
<none>ShortStringThe 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

NameTypeDescription
sstrShortStringThe ShortString to unpack.

Returns

NameTypeDescription
<none>stringThe 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

NameTypeDescription
sstrShortStringThe ShortString.

Returns

NameTypeDescription
<none>uint256The 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

NameTypeDescription
valuestringThe string to convert.
storestringA storage reference to use for strings that are too long.

Returns

NameTypeDescription
<none>ShortStringThe 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

NameTypeDescription
valueShortStringThe ShortString to convert.
storestringThe storage fallback that may contain the full string.

Returns

NameTypeDescription
<none>stringThe 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

NameTypeDescription
storestring

Returns

NameTypeDescription
ptrStringSlot

_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

NameTypeDescription
bstrbytes

Returns

NameTypeDescription
resultbytes32

Errors

StringTooLong

error StringTooLong(string str);

Parameters

NameTypeDescription
strstring

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

NameTypeDescription
valuestring