```\nfunction toShortString(string memory str) internal pure returns (ShortString) {\n    bytes memory bstr = bytes(str);\n    if (bstr.length > 31) {\n        revert StringTooLong(str);\n    }\n    return ShortString.wrap(bytes32(uint256(bytes32(bstr)) | bstr.length));\n}\n```\n