Strings.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.1) (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. assembly ("memory-safe") {
  25. ptr := add(buffer, add(32, length))
  26. }
  27. while (true) {
  28. ptr--;
  29. assembly ("memory-safe") {
  30. mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
  31. }
  32. value /= 10;
  33. if (value == 0) break;
  34. }
  35. return buffer;
  36. }
  37. }
  38. /**
  39. * @dev Converts a `int256` to its ASCII `string` decimal representation.
  40. */
  41. function toStringSigned(int256 value) internal pure returns (string memory) {
  42. return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
  43. }
  44. /**
  45. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
  46. */
  47. function toHexString(uint256 value) internal pure returns (string memory) {
  48. unchecked {
  49. return toHexString(value, Math.log256(value) + 1);
  50. }
  51. }
  52. /**
  53. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
  54. */
  55. function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
  56. uint256 localValue = value;
  57. bytes memory buffer = new bytes(2 * length + 2);
  58. buffer[0] = "0";
  59. buffer[1] = "x";
  60. for (uint256 i = 2 * length + 1; i > 1; --i) {
  61. buffer[i] = HEX_DIGITS[localValue & 0xf];
  62. localValue >>= 4;
  63. }
  64. if (localValue != 0) {
  65. revert StringsInsufficientHexLength(value, length);
  66. }
  67. return string(buffer);
  68. }
  69. /**
  70. * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
  71. * representation.
  72. */
  73. function toHexString(address addr) internal pure returns (string memory) {
  74. return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
  75. }
  76. /**
  77. * @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal
  78. * representation, according to EIP-55.
  79. */
  80. function toChecksumHexString(address addr) internal pure returns (string memory) {
  81. bytes memory buffer = bytes(toHexString(addr));
  82. // hash the hex part of buffer (skip length + 2 bytes, length 40)
  83. uint256 hashValue;
  84. assembly ("memory-safe") {
  85. hashValue := shr(96, keccak256(add(buffer, 0x22), 40))
  86. }
  87. for (uint256 i = 41; i > 1; --i) {
  88. // possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f)
  89. if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) {
  90. // case shift by xoring with 0x20
  91. buffer[i] ^= 0x20;
  92. }
  93. hashValue >>= 4;
  94. }
  95. return string(buffer);
  96. }
  97. /**
  98. * @dev Returns true if the two strings are equal.
  99. */
  100. function equal(string memory a, string memory b) internal pure returns (bool) {
  101. return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
  102. }
  103. }