Strings.sol 3.9 KB

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