Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Strings

String operations.

State Variables

HEX_DIGITS

bytes16 private constant HEX_DIGITS = "0123456789abcdef";

ADDRESS_LENGTH

uint8 private constant ADDRESS_LENGTH = 20;

SPECIAL_CHARS_LOOKUP

uint256 private constant SPECIAL_CHARS_LOOKUP =
    (1 << 0x08) | (1 << 0x09) | (1 << 0x0a) | (1 << 0x0c) | (1 << 0x0d) | (1 << 0x22) | (1 << 0x5c);

ABS_MIN_INT256

uint256 private constant ABS_MIN_INT256 = 2 ** 255;

Functions

toString

Converts a uint256 to its ASCII string decimal representation.

function toString(uint256 value) internal pure returns (string memory);

toStringSigned

Converts a int256 to its ASCII string decimal representation.

function toStringSigned(int256 value) internal pure returns (string memory);

toHexString

Converts a uint256 to its ASCII string hexadecimal representation.

function toHexString(uint256 value) internal pure returns (string memory);

toHexString

Converts a uint256 to its ASCII string hexadecimal representation with fixed length.

function toHexString(uint256 value, uint256 length) internal pure returns (string memory);

toHexString

Converts an address with fixed length of 20 bytes to its not checksummed ASCII string hexadecimal representation.

function toHexString(address addr) internal pure returns (string memory);

toChecksumHexString

Converts an address with fixed length of 20 bytes to its checksummed ASCII string hexadecimal representation, according to EIP-55.

function toChecksumHexString(address addr) internal pure returns (string memory);

equal

Returns true if the two strings are equal.

function equal(string memory a, string memory b) internal pure returns (bool);

parseUint

*Parse a decimal string and returns the value as a uint256. Requirements:

  • The string must be formatted as [0-9]*
  • The result must fit into an uint256 type*
function parseUint(string memory input) internal pure returns (uint256);

parseUint

*Variant of {parseUint-string} that parses a substring of input located between position begin (included) and end (excluded). Requirements:

  • The substring must be formatted as [0-9]*
  • The result must fit into an uint256 type*
function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256);

tryParseUint

Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character. NOTE: This function will revert if the result does not fit in a uint256.

function tryParseUint(string memory input) internal pure returns (bool success, uint256 value);

tryParseUint

Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid character. NOTE: This function will revert if the result does not fit in a uint256.

function tryParseUint(string memory input, uint256 begin, uint256 end)
    internal
    pure
    returns (bool success, uint256 value);

_tryParseUintUncheckedBounds

Implementation of {tryParseUint-string-uint256-uint256} that does not check bounds. Caller should make sure that begin <= end <= input.length. Other inputs would result in undefined behavior.

function _tryParseUintUncheckedBounds(string memory input, uint256 begin, uint256 end)
    private
    pure
    returns (bool success, uint256 value);

parseInt

*Parse a decimal string and returns the value as a int256. Requirements:

  • The string must be formatted as [-+]?[0-9]*
  • The result must fit in an int256 type.*
function parseInt(string memory input) internal pure returns (int256);

parseInt

*Variant of {parseInt-string} that parses a substring of input located between position begin (included) and end (excluded). Requirements:

  • The substring must be formatted as [-+]?[0-9]*
  • The result must fit in an int256 type.*
function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256);

tryParseInt

Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if the result does not fit in a int256. NOTE: This function will revert if the absolute value of the result does not fit in a uint256.

function tryParseInt(string memory input) internal pure returns (bool success, int256 value);

tryParseInt

Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid character or if the result does not fit in a int256. NOTE: This function will revert if the absolute value of the result does not fit in a uint256.

function tryParseInt(string memory input, uint256 begin, uint256 end)
    internal
    pure
    returns (bool success, int256 value);

_tryParseIntUncheckedBounds

Implementation of {tryParseInt-string-uint256-uint256} that does not check bounds. Caller should make sure that begin <= end <= input.length. Other inputs would result in undefined behavior.

function _tryParseIntUncheckedBounds(string memory input, uint256 begin, uint256 end)
    private
    pure
    returns (bool success, int256 value);

parseHexUint

*Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a uint256. Requirements:

  • The string must be formatted as (0x)?[0-9a-fA-F]*
  • The result must fit in an uint256 type.*
function parseHexUint(string memory input) internal pure returns (uint256);

parseHexUint

*Variant of {parseHexUint-string} that parses a substring of input located between position begin (included) and end (excluded). Requirements:

  • The substring must be formatted as (0x)?[0-9a-fA-F]*
  • The result must fit in an uint256 type.*
function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256);

tryParseHexUint

Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character. NOTE: This function will revert if the result does not fit in a uint256.

function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value);

tryParseHexUint

Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid character. NOTE: This function will revert if the result does not fit in a uint256.

function tryParseHexUint(string memory input, uint256 begin, uint256 end)
    internal
    pure
    returns (bool success, uint256 value);

_tryParseHexUintUncheckedBounds

Implementation of {tryParseHexUint-string-uint256-uint256} that does not check bounds. Caller should make sure that begin <= end <= input.length. Other inputs would result in undefined behavior.

function _tryParseHexUintUncheckedBounds(string memory input, uint256 begin, uint256 end)
    private
    pure
    returns (bool success, uint256 value);

parseAddress

*Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an address. Requirements:

  • The string must be formatted as (0x)?[0-9a-fA-F]{40}*
function parseAddress(string memory input) internal pure returns (address);

parseAddress

*Variant of {parseAddress-string} that parses a substring of input located between position begin (included) and end (excluded). Requirements:

  • The substring must be formatted as (0x)?[0-9a-fA-F]{40}*
function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address);

tryParseAddress

Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly formatted address. See {parseAddress-string} requirements.

function tryParseAddress(string memory input) internal pure returns (bool success, address value);

tryParseAddress

Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly formatted address. See {parseAddress-string-uint256-uint256} requirements.

function tryParseAddress(string memory input, uint256 begin, uint256 end)
    internal
    pure
    returns (bool success, address value);

_tryParseChr

function _tryParseChr(bytes1 chr) private pure returns (uint8);

escapeJSON

Escape special characters in JSON strings. This can be useful to prevent JSON injection in NFT metadata. WARNING: This function should only be used in double quoted JSON strings. Single quotes are not escaped. NOTE: This function escapes all unicode characters, and not just the ones in ranges defined in section 2.5 of RFC-4627 (U+0000 to U+001F, U+0022 and U+005C). ECMAScript's JSON.parse does recover escaped unicode characters that are not in this range, but other tooling may provide different results.

function escapeJSON(string memory input) internal pure returns (string memory);

_unsafeReadBytesOffset

Reads a bytes32 from a bytes array without bounds checking. NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the assembly block as such would prevent some optimizations.

function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value);

Errors

StringsInsufficientHexLength

The value string doesn't fit in the specified length.

error StringsInsufficientHexLength(uint256 value, uint256 length);

StringsInvalidChar

The string being parsed contains characters that are not in scope of the given base.

error StringsInvalidChar();

StringsInvalidAddressFormat

The string being parsed is not a properly formatted address.

error StringsInvalidAddressFormat();