|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// OpenZeppelin Contracts (last updated v5.1.0) (utils/Strings.sol) |
| 3 | + |
| 4 | +pragma solidity ^0.8.20; |
| 5 | + |
| 6 | +import {Math} from "./math/Math.sol"; |
| 7 | +import {SignedMath} from "./math/SignedMath.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @dev String operations. |
| 11 | + */ |
| 12 | +library Strings { |
| 13 | + bytes16 private constant HEX_DIGITS = "0123456789abcdef"; |
| 14 | + uint8 private constant ADDRESS_LENGTH = 20; |
| 15 | + |
| 16 | + /** |
| 17 | + * @dev The `value` string doesn't fit in the specified `length`. |
| 18 | + */ |
| 19 | + error StringsInsufficientHexLength(uint256 value, uint256 length); |
| 20 | + |
| 21 | + /** |
| 22 | + * @dev Converts a `uint256` to its ASCII `string` decimal representation. |
| 23 | + */ |
| 24 | + function toString(uint256 value) internal pure returns (string memory) { |
| 25 | + unchecked { |
| 26 | + uint256 length = Math.log10(value) + 1; |
| 27 | + string memory buffer = new string(length); |
| 28 | + uint256 ptr; |
| 29 | + assembly ("memory-safe") { |
| 30 | + ptr := add(buffer, add(32, length)) |
| 31 | + } |
| 32 | + while (true) { |
| 33 | + ptr--; |
| 34 | + assembly ("memory-safe") { |
| 35 | + mstore8(ptr, byte(mod(value, 10), HEX_DIGITS)) |
| 36 | + } |
| 37 | + value /= 10; |
| 38 | + if (value == 0) break; |
| 39 | + } |
| 40 | + return buffer; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @dev Converts a `int256` to its ASCII `string` decimal representation. |
| 46 | + */ |
| 47 | + function toStringSigned(int256 value) internal pure returns (string memory) { |
| 48 | + return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value))); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation. |
| 53 | + */ |
| 54 | + function toHexString(uint256 value) internal pure returns (string memory) { |
| 55 | + unchecked { |
| 56 | + return toHexString(value, Math.log256(value) + 1); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length. |
| 62 | + */ |
| 63 | + function toHexString(uint256 value, uint256 length) internal pure returns (string memory) { |
| 64 | + uint256 localValue = value; |
| 65 | + bytes memory buffer = new bytes(2 * length + 2); |
| 66 | + buffer[0] = "0"; |
| 67 | + buffer[1] = "x"; |
| 68 | + for (uint256 i = 2 * length + 1; i > 1; --i) { |
| 69 | + buffer[i] = HEX_DIGITS[localValue & 0xf]; |
| 70 | + localValue >>= 4; |
| 71 | + } |
| 72 | + if (localValue != 0) { |
| 73 | + revert StringsInsufficientHexLength(value, length); |
| 74 | + } |
| 75 | + return string(buffer); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal |
| 80 | + * representation. |
| 81 | + */ |
| 82 | + function toHexString(address addr) internal pure returns (string memory) { |
| 83 | + return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal |
| 88 | + * representation, according to EIP-55. |
| 89 | + */ |
| 90 | + function toChecksumHexString(address addr) internal pure returns (string memory) { |
| 91 | + bytes memory buffer = bytes(toHexString(addr)); |
| 92 | + |
| 93 | + // hash the hex part of buffer (skip length + 2 bytes, length 40) |
| 94 | + uint256 hashValue; |
| 95 | + assembly ("memory-safe") { |
| 96 | + hashValue := shr(96, keccak256(add(buffer, 0x22), 40)) |
| 97 | + } |
| 98 | + |
| 99 | + for (uint256 i = 41; i > 1; --i) { |
| 100 | + // possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f) |
| 101 | + if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) { |
| 102 | + // case shift by xoring with 0x20 |
| 103 | + buffer[i] ^= 0x20; |
| 104 | + } |
| 105 | + hashValue >>= 4; |
| 106 | + } |
| 107 | + return string(buffer); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * @dev Returns true if the two strings are equal. |
| 112 | + */ |
| 113 | + function equal(string memory a, string memory b) internal pure returns (bool) { |
| 114 | + return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b)); |
| 115 | + } |
| 116 | +} |
0 commit comments