Strings.sol 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.3.0) (utils/Strings.sol)
  3. pragma solidity ^0.8.20;
  4. import {Math} from "./math/Math.sol";
  5. import {SafeCast} from "./math/SafeCast.sol";
  6. import {SignedMath} from "./math/SignedMath.sol";
  7. /**
  8. * @dev String operations.
  9. */
  10. library Strings {
  11. using SafeCast for *;
  12. bytes16 private constant HEX_DIGITS = "0123456789abcdef";
  13. uint8 private constant ADDRESS_LENGTH = 20;
  14. uint256 private constant SPECIAL_CHARS_LOOKUP =
  15. (1 << 0x08) | // backspace
  16. (1 << 0x09) | // tab
  17. (1 << 0x0a) | // newline
  18. (1 << 0x0c) | // form feed
  19. (1 << 0x0d) | // carriage return
  20. (1 << 0x22) | // double quote
  21. (1 << 0x5c); // backslash
  22. /**
  23. * @dev The `value` string doesn't fit in the specified `length`.
  24. */
  25. error StringsInsufficientHexLength(uint256 value, uint256 length);
  26. /**
  27. * @dev The string being parsed contains characters that are not in scope of the given base.
  28. */
  29. error StringsInvalidChar();
  30. /**
  31. * @dev The string being parsed is not a properly formatted address.
  32. */
  33. error StringsInvalidAddressFormat();
  34. /**
  35. * @dev Converts a `uint256` to its ASCII `string` decimal representation.
  36. */
  37. function toString(uint256 value) internal pure returns (string memory) {
  38. unchecked {
  39. uint256 length = Math.log10(value) + 1;
  40. string memory buffer = new string(length);
  41. uint256 ptr;
  42. assembly ("memory-safe") {
  43. ptr := add(add(buffer, 0x20), length)
  44. }
  45. while (true) {
  46. ptr--;
  47. assembly ("memory-safe") {
  48. mstore8(ptr, byte(mod(value, 10), HEX_DIGITS))
  49. }
  50. value /= 10;
  51. if (value == 0) break;
  52. }
  53. return buffer;
  54. }
  55. }
  56. /**
  57. * @dev Converts a `int256` to its ASCII `string` decimal representation.
  58. */
  59. function toStringSigned(int256 value) internal pure returns (string memory) {
  60. return string.concat(value < 0 ? "-" : "", toString(SignedMath.abs(value)));
  61. }
  62. /**
  63. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
  64. */
  65. function toHexString(uint256 value) internal pure returns (string memory) {
  66. unchecked {
  67. return toHexString(value, Math.log256(value) + 1);
  68. }
  69. }
  70. /**
  71. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
  72. */
  73. function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
  74. uint256 localValue = value;
  75. bytes memory buffer = new bytes(2 * length + 2);
  76. buffer[0] = "0";
  77. buffer[1] = "x";
  78. for (uint256 i = 2 * length + 1; i > 1; --i) {
  79. buffer[i] = HEX_DIGITS[localValue & 0xf];
  80. localValue >>= 4;
  81. }
  82. if (localValue != 0) {
  83. revert StringsInsufficientHexLength(value, length);
  84. }
  85. return string(buffer);
  86. }
  87. /**
  88. * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal
  89. * representation.
  90. */
  91. function toHexString(address addr) internal pure returns (string memory) {
  92. return toHexString(uint256(uint160(addr)), ADDRESS_LENGTH);
  93. }
  94. /**
  95. * @dev Converts an `address` with fixed length of 20 bytes to its checksummed ASCII `string` hexadecimal
  96. * representation, according to EIP-55.
  97. */
  98. function toChecksumHexString(address addr) internal pure returns (string memory) {
  99. bytes memory buffer = bytes(toHexString(addr));
  100. // hash the hex part of buffer (skip length + 2 bytes, length 40)
  101. uint256 hashValue;
  102. assembly ("memory-safe") {
  103. hashValue := shr(96, keccak256(add(buffer, 0x22), 40))
  104. }
  105. for (uint256 i = 41; i > 1; --i) {
  106. // possible values for buffer[i] are 48 (0) to 57 (9) and 97 (a) to 102 (f)
  107. if (hashValue & 0xf > 7 && uint8(buffer[i]) > 96) {
  108. // case shift by xoring with 0x20
  109. buffer[i] ^= 0x20;
  110. }
  111. hashValue >>= 4;
  112. }
  113. return string(buffer);
  114. }
  115. /**
  116. * @dev Returns true if the two strings are equal.
  117. */
  118. function equal(string memory a, string memory b) internal pure returns (bool) {
  119. return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
  120. }
  121. /**
  122. * @dev Parse a decimal string and returns the value as a `uint256`.
  123. *
  124. * Requirements:
  125. * - The string must be formatted as `[0-9]*`
  126. * - The result must fit into an `uint256` type
  127. */
  128. function parseUint(string memory input) internal pure returns (uint256) {
  129. return parseUint(input, 0, bytes(input).length);
  130. }
  131. /**
  132. * @dev Variant of {parseUint-string} that parses a substring of `input` located between position `begin` (included) and
  133. * `end` (excluded).
  134. *
  135. * Requirements:
  136. * - The substring must be formatted as `[0-9]*`
  137. * - The result must fit into an `uint256` type
  138. */
  139. function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
  140. (bool success, uint256 value) = tryParseUint(input, begin, end);
  141. if (!success) revert StringsInvalidChar();
  142. return value;
  143. }
  144. /**
  145. * @dev Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character.
  146. *
  147. * NOTE: This function will revert if the result does not fit in a `uint256`.
  148. */
  149. function tryParseUint(string memory input) internal pure returns (bool success, uint256 value) {
  150. return _tryParseUintUncheckedBounds(input, 0, bytes(input).length);
  151. }
  152. /**
  153. * @dev Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid
  154. * character.
  155. *
  156. * NOTE: This function will revert if the result does not fit in a `uint256`.
  157. */
  158. function tryParseUint(
  159. string memory input,
  160. uint256 begin,
  161. uint256 end
  162. ) internal pure returns (bool success, uint256 value) {
  163. if (end > bytes(input).length || begin > end) return (false, 0);
  164. return _tryParseUintUncheckedBounds(input, begin, end);
  165. }
  166. /**
  167. * @dev Implementation of {tryParseUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
  168. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  169. */
  170. function _tryParseUintUncheckedBounds(
  171. string memory input,
  172. uint256 begin,
  173. uint256 end
  174. ) private pure returns (bool success, uint256 value) {
  175. bytes memory buffer = bytes(input);
  176. uint256 result = 0;
  177. for (uint256 i = begin; i < end; ++i) {
  178. uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
  179. if (chr > 9) return (false, 0);
  180. result *= 10;
  181. result += chr;
  182. }
  183. return (true, result);
  184. }
  185. /**
  186. * @dev Parse a decimal string and returns the value as a `int256`.
  187. *
  188. * Requirements:
  189. * - The string must be formatted as `[-+]?[0-9]*`
  190. * - The result must fit in an `int256` type.
  191. */
  192. function parseInt(string memory input) internal pure returns (int256) {
  193. return parseInt(input, 0, bytes(input).length);
  194. }
  195. /**
  196. * @dev Variant of {parseInt-string} that parses a substring of `input` located between position `begin` (included) and
  197. * `end` (excluded).
  198. *
  199. * Requirements:
  200. * - The substring must be formatted as `[-+]?[0-9]*`
  201. * - The result must fit in an `int256` type.
  202. */
  203. function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256) {
  204. (bool success, int256 value) = tryParseInt(input, begin, end);
  205. if (!success) revert StringsInvalidChar();
  206. return value;
  207. }
  208. /**
  209. * @dev Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if
  210. * the result does not fit in a `int256`.
  211. *
  212. * NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
  213. */
  214. function tryParseInt(string memory input) internal pure returns (bool success, int256 value) {
  215. return _tryParseIntUncheckedBounds(input, 0, bytes(input).length);
  216. }
  217. uint256 private constant ABS_MIN_INT256 = 2 ** 255;
  218. /**
  219. * @dev Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid
  220. * character or if the result does not fit in a `int256`.
  221. *
  222. * NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
  223. */
  224. function tryParseInt(
  225. string memory input,
  226. uint256 begin,
  227. uint256 end
  228. ) internal pure returns (bool success, int256 value) {
  229. if (end > bytes(input).length || begin > end) return (false, 0);
  230. return _tryParseIntUncheckedBounds(input, begin, end);
  231. }
  232. /**
  233. * @dev Implementation of {tryParseInt-string-uint256-uint256} that does not check bounds. Caller should make sure that
  234. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  235. */
  236. function _tryParseIntUncheckedBounds(
  237. string memory input,
  238. uint256 begin,
  239. uint256 end
  240. ) private pure returns (bool success, int256 value) {
  241. bytes memory buffer = bytes(input);
  242. // Check presence of a negative sign.
  243. bytes1 sign = begin == end ? bytes1(0) : bytes1(_unsafeReadBytesOffset(buffer, begin)); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
  244. bool positiveSign = sign == bytes1("+");
  245. bool negativeSign = sign == bytes1("-");
  246. uint256 offset = (positiveSign || negativeSign).toUint();
  247. (bool absSuccess, uint256 absValue) = tryParseUint(input, begin + offset, end);
  248. if (absSuccess && absValue < ABS_MIN_INT256) {
  249. return (true, negativeSign ? -int256(absValue) : int256(absValue));
  250. } else if (absSuccess && negativeSign && absValue == ABS_MIN_INT256) {
  251. return (true, type(int256).min);
  252. } else return (false, 0);
  253. }
  254. /**
  255. * @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a `uint256`.
  256. *
  257. * Requirements:
  258. * - The string must be formatted as `(0x)?[0-9a-fA-F]*`
  259. * - The result must fit in an `uint256` type.
  260. */
  261. function parseHexUint(string memory input) internal pure returns (uint256) {
  262. return parseHexUint(input, 0, bytes(input).length);
  263. }
  264. /**
  265. * @dev Variant of {parseHexUint-string} that parses a substring of `input` located between position `begin` (included) and
  266. * `end` (excluded).
  267. *
  268. * Requirements:
  269. * - The substring must be formatted as `(0x)?[0-9a-fA-F]*`
  270. * - The result must fit in an `uint256` type.
  271. */
  272. function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
  273. (bool success, uint256 value) = tryParseHexUint(input, begin, end);
  274. if (!success) revert StringsInvalidChar();
  275. return value;
  276. }
  277. /**
  278. * @dev Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character.
  279. *
  280. * NOTE: This function will revert if the result does not fit in a `uint256`.
  281. */
  282. function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value) {
  283. return _tryParseHexUintUncheckedBounds(input, 0, bytes(input).length);
  284. }
  285. /**
  286. * @dev Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an
  287. * invalid character.
  288. *
  289. * NOTE: This function will revert if the result does not fit in a `uint256`.
  290. */
  291. function tryParseHexUint(
  292. string memory input,
  293. uint256 begin,
  294. uint256 end
  295. ) internal pure returns (bool success, uint256 value) {
  296. if (end > bytes(input).length || begin > end) return (false, 0);
  297. return _tryParseHexUintUncheckedBounds(input, begin, end);
  298. }
  299. /**
  300. * @dev Implementation of {tryParseHexUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
  301. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  302. */
  303. function _tryParseHexUintUncheckedBounds(
  304. string memory input,
  305. uint256 begin,
  306. uint256 end
  307. ) private pure returns (bool success, uint256 value) {
  308. bytes memory buffer = bytes(input);
  309. // skip 0x prefix if present
  310. bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(buffer, begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
  311. uint256 offset = hasPrefix.toUint() * 2;
  312. uint256 result = 0;
  313. for (uint256 i = begin + offset; i < end; ++i) {
  314. uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
  315. if (chr > 15) return (false, 0);
  316. result *= 16;
  317. unchecked {
  318. // Multiplying by 16 is equivalent to a shift of 4 bits (with additional overflow check).
  319. // This guarantees that adding a value < 16 will not cause an overflow, hence the unchecked.
  320. result += chr;
  321. }
  322. }
  323. return (true, result);
  324. }
  325. /**
  326. * @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an `address`.
  327. *
  328. * Requirements:
  329. * - The string must be formatted as `(0x)?[0-9a-fA-F]{40}`
  330. */
  331. function parseAddress(string memory input) internal pure returns (address) {
  332. return parseAddress(input, 0, bytes(input).length);
  333. }
  334. /**
  335. * @dev Variant of {parseAddress-string} that parses a substring of `input` located between position `begin` (included) and
  336. * `end` (excluded).
  337. *
  338. * Requirements:
  339. * - The substring must be formatted as `(0x)?[0-9a-fA-F]{40}`
  340. */
  341. function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address) {
  342. (bool success, address value) = tryParseAddress(input, begin, end);
  343. if (!success) revert StringsInvalidAddressFormat();
  344. return value;
  345. }
  346. /**
  347. * @dev Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly
  348. * formatted address. See {parseAddress-string} requirements.
  349. */
  350. function tryParseAddress(string memory input) internal pure returns (bool success, address value) {
  351. return tryParseAddress(input, 0, bytes(input).length);
  352. }
  353. /**
  354. * @dev Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly
  355. * formatted address. See {parseAddress-string-uint256-uint256} requirements.
  356. */
  357. function tryParseAddress(
  358. string memory input,
  359. uint256 begin,
  360. uint256 end
  361. ) internal pure returns (bool success, address value) {
  362. if (end > bytes(input).length || begin > end) return (false, address(0));
  363. bool hasPrefix = (end > begin + 1) && bytes2(_unsafeReadBytesOffset(bytes(input), begin)) == bytes2("0x"); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
  364. uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
  365. // check that input is the correct length
  366. if (end - begin == expectedLength) {
  367. // length guarantees that this does not overflow, and value is at most type(uint160).max
  368. (bool s, uint256 v) = _tryParseHexUintUncheckedBounds(input, begin, end);
  369. return (s, address(uint160(v)));
  370. } else {
  371. return (false, address(0));
  372. }
  373. }
  374. function _tryParseChr(bytes1 chr) private pure returns (uint8) {
  375. uint8 value = uint8(chr);
  376. // Try to parse `chr`:
  377. // - Case 1: [0-9]
  378. // - Case 2: [a-f]
  379. // - Case 3: [A-F]
  380. // - otherwise not supported
  381. unchecked {
  382. if (value > 47 && value < 58) value -= 48;
  383. else if (value > 96 && value < 103) value -= 87;
  384. else if (value > 64 && value < 71) value -= 55;
  385. else return type(uint8).max;
  386. }
  387. return value;
  388. }
  389. /**
  390. * @dev Escape special characters in JSON strings. This can be useful to prevent JSON injection in NFT metadata.
  391. *
  392. * WARNING: This function should only be used in double quoted JSON strings. Single quotes are not escaped.
  393. *
  394. * NOTE: This function escapes all unicode characters, and not just the ones in ranges defined in section 2.5 of
  395. * RFC-4627 (U+0000 to U+001F, U+0022 and U+005C). ECMAScript's `JSON.parse` does recover escaped unicode
  396. * characters that are not in this range, but other tooling may provide different results.
  397. */
  398. function escapeJSON(string memory input) internal pure returns (string memory) {
  399. bytes memory buffer = bytes(input);
  400. bytes memory output = new bytes(2 * buffer.length); // worst case scenario
  401. uint256 outputLength = 0;
  402. for (uint256 i; i < buffer.length; ++i) {
  403. bytes1 char = bytes1(_unsafeReadBytesOffset(buffer, i));
  404. if (((SPECIAL_CHARS_LOOKUP & (1 << uint8(char))) != 0)) {
  405. output[outputLength++] = "\\";
  406. if (char == 0x08) output[outputLength++] = "b";
  407. else if (char == 0x09) output[outputLength++] = "t";
  408. else if (char == 0x0a) output[outputLength++] = "n";
  409. else if (char == 0x0c) output[outputLength++] = "f";
  410. else if (char == 0x0d) output[outputLength++] = "r";
  411. else if (char == 0x5c) output[outputLength++] = "\\";
  412. else if (char == 0x22) {
  413. // solhint-disable-next-line quotes
  414. output[outputLength++] = '"';
  415. }
  416. } else {
  417. output[outputLength++] = char;
  418. }
  419. }
  420. // write the actual length and deallocate unused memory
  421. assembly ("memory-safe") {
  422. mstore(output, outputLength)
  423. mstore(0x40, add(output, shl(5, shr(5, add(outputLength, 63)))))
  424. }
  425. return string(output);
  426. }
  427. /**
  428. * @dev Reads a bytes32 from a bytes array without bounds checking.
  429. *
  430. * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
  431. * assembly block as such would prevent some optimizations.
  432. */
  433. function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
  434. // This is not memory safe in the general case, but all calls to this private function are within bounds.
  435. assembly ("memory-safe") {
  436. value := mload(add(add(buffer, 0x20), offset))
  437. }
  438. }
  439. }