123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol)
- pragma solidity ^0.8.0;
- /**
- * @dev String operations.
- */
- library Strings {
- bytes16 private constant _SYMBOLS = "0123456789abcdef";
- uint8 private constant _ADDRESS_LENGTH = 20;
- /**
- * @dev Converts a `uint256` to its ASCII `string` decimal representation.
- */
- function toString(uint256 value) internal pure returns (string memory) {
- unchecked {
- uint256 length = 1;
- // compute log10(value), and add it to length
- uint256 valueCopy = value;
- if (valueCopy >= 10**64) {
- valueCopy /= 10**64;
- length += 64;
- }
- if (valueCopy >= 10**32) {
- valueCopy /= 10**32;
- length += 32;
- }
- if (valueCopy >= 10**16) {
- valueCopy /= 10**16;
- length += 16;
- }
- if (valueCopy >= 10**8) {
- valueCopy /= 10**8;
- length += 8;
- }
- if (valueCopy >= 10**4) {
- valueCopy /= 10**4;
- length += 4;
- }
- if (valueCopy >= 10**2) {
- valueCopy /= 10**2;
- length += 2;
- }
- if (valueCopy >= 10**1) {
- length += 1;
- }
- // now, length is log10(value) + 1
- string memory buffer = new string(length);
- uint256 ptr;
- /// @solidity memory-safe-assembly
- assembly {
- ptr := add(buffer, add(32, length))
- }
- while (true) {
- ptr--;
- /// @solidity memory-safe-assembly
- assembly {
- mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
- }
- value /= 10;
- if (value == 0) break;
- }
- return buffer;
- }
- }
- /**
- * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
- */
- function toHexString(uint256 value) internal pure returns (string memory) {
- unchecked {
- uint256 length = 1;
- // compute log256(value), and add it to length
- uint256 valueCopy = value;
- if (valueCopy >= 1 << 128) {
- valueCopy >>= 128;
- length += 16;
- }
- if (valueCopy >= 1 << 64) {
- valueCopy >>= 64;
- length += 8;
- }
- if (valueCopy >= 1 << 32) {
- valueCopy >>= 32;
- length += 4;
- }
- if (valueCopy >= 1 << 16) {
- valueCopy >>= 16;
- length += 2;
- }
- if (valueCopy >= 1 << 8) {
- valueCopy >>= 8;
- length += 1;
- }
- // now, length is log256(value) + 1
- return toHexString(value, length);
- }
- }
- /**
- * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
- */
- function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
- bytes memory buffer = new bytes(2 * length + 2);
- buffer[0] = "0";
- buffer[1] = "x";
- for (uint256 i = 2 * length + 1; i > 1; --i) {
- buffer[i] = _SYMBOLS[value & 0xf];
- value >>= 4;
- }
- require(value == 0, "Strings: hex length insufficient");
- return string(buffer);
- }
- /**
- * @dev 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) {
- return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
- }
- }
|