Strings.sol 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 Converts a `bytes` buffer to its ASCII `string` hexadecimal representation.
  117. */
  118. function toHexString(bytes memory input) internal pure returns (string memory) {
  119. unchecked {
  120. bytes memory buffer = new bytes(2 * input.length + 2);
  121. buffer[0] = "0";
  122. buffer[1] = "x";
  123. for (uint256 i = 0; i < input.length; ++i) {
  124. uint8 v = uint8(input[i]);
  125. buffer[2 * i + 2] = HEX_DIGITS[v >> 4];
  126. buffer[2 * i + 3] = HEX_DIGITS[v & 0xf];
  127. }
  128. return string(buffer);
  129. }
  130. }
  131. /**
  132. * @dev Returns true if the two strings are equal.
  133. */
  134. function equal(string memory a, string memory b) internal pure returns (bool) {
  135. return bytes(a).length == bytes(b).length && keccak256(bytes(a)) == keccak256(bytes(b));
  136. }
  137. /**
  138. * @dev Parse a decimal string and returns the value as a `uint256`.
  139. *
  140. * Requirements:
  141. * - The string must be formatted as `[0-9]*`
  142. * - The result must fit into an `uint256` type
  143. */
  144. function parseUint(string memory input) internal pure returns (uint256) {
  145. return parseUint(input, 0, bytes(input).length);
  146. }
  147. /**
  148. * @dev Variant of {parseUint-string} that parses a substring of `input` located between position `begin` (included) and
  149. * `end` (excluded).
  150. *
  151. * Requirements:
  152. * - The substring must be formatted as `[0-9]*`
  153. * - The result must fit into an `uint256` type
  154. */
  155. function parseUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
  156. (bool success, uint256 value) = tryParseUint(input, begin, end);
  157. if (!success) revert StringsInvalidChar();
  158. return value;
  159. }
  160. /**
  161. * @dev Variant of {parseUint-string} that returns false if the parsing fails because of an invalid character.
  162. *
  163. * NOTE: This function will revert if the result does not fit in a `uint256`.
  164. */
  165. function tryParseUint(string memory input) internal pure returns (bool success, uint256 value) {
  166. return _tryParseUintUncheckedBounds(input, 0, bytes(input).length);
  167. }
  168. /**
  169. * @dev Variant of {parseUint-string-uint256-uint256} that returns false if the parsing fails because of an invalid
  170. * character.
  171. *
  172. * NOTE: This function will revert if the result does not fit in a `uint256`.
  173. */
  174. function tryParseUint(
  175. string memory input,
  176. uint256 begin,
  177. uint256 end
  178. ) internal pure returns (bool success, uint256 value) {
  179. if (end > bytes(input).length || begin > end) return (false, 0);
  180. return _tryParseUintUncheckedBounds(input, begin, end);
  181. }
  182. /**
  183. * @dev Implementation of {tryParseUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
  184. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  185. */
  186. function _tryParseUintUncheckedBounds(
  187. string memory input,
  188. uint256 begin,
  189. uint256 end
  190. ) private pure returns (bool success, uint256 value) {
  191. bytes memory buffer = bytes(input);
  192. uint256 result = 0;
  193. for (uint256 i = begin; i < end; ++i) {
  194. uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
  195. if (chr > 9) return (false, 0);
  196. result *= 10;
  197. result += chr;
  198. }
  199. return (true, result);
  200. }
  201. /**
  202. * @dev Parse a decimal string and returns the value as a `int256`.
  203. *
  204. * Requirements:
  205. * - The string must be formatted as `[-+]?[0-9]*`
  206. * - The result must fit in an `int256` type.
  207. */
  208. function parseInt(string memory input) internal pure returns (int256) {
  209. return parseInt(input, 0, bytes(input).length);
  210. }
  211. /**
  212. * @dev Variant of {parseInt-string} that parses a substring of `input` located between position `begin` (included) and
  213. * `end` (excluded).
  214. *
  215. * Requirements:
  216. * - The substring must be formatted as `[-+]?[0-9]*`
  217. * - The result must fit in an `int256` type.
  218. */
  219. function parseInt(string memory input, uint256 begin, uint256 end) internal pure returns (int256) {
  220. (bool success, int256 value) = tryParseInt(input, begin, end);
  221. if (!success) revert StringsInvalidChar();
  222. return value;
  223. }
  224. /**
  225. * @dev Variant of {parseInt-string} that returns false if the parsing fails because of an invalid character or if
  226. * the result does not fit in a `int256`.
  227. *
  228. * NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
  229. */
  230. function tryParseInt(string memory input) internal pure returns (bool success, int256 value) {
  231. return _tryParseIntUncheckedBounds(input, 0, bytes(input).length);
  232. }
  233. uint256 private constant ABS_MIN_INT256 = 2 ** 255;
  234. /**
  235. * @dev Variant of {parseInt-string-uint256-uint256} that returns false if the parsing fails because of an invalid
  236. * character or if the result does not fit in a `int256`.
  237. *
  238. * NOTE: This function will revert if the absolute value of the result does not fit in a `uint256`.
  239. */
  240. function tryParseInt(
  241. string memory input,
  242. uint256 begin,
  243. uint256 end
  244. ) internal pure returns (bool success, int256 value) {
  245. if (end > bytes(input).length || begin > end) return (false, 0);
  246. return _tryParseIntUncheckedBounds(input, begin, end);
  247. }
  248. /**
  249. * @dev Implementation of {tryParseInt-string-uint256-uint256} that does not check bounds. Caller should make sure that
  250. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  251. */
  252. function _tryParseIntUncheckedBounds(
  253. string memory input,
  254. uint256 begin,
  255. uint256 end
  256. ) private pure returns (bool success, int256 value) {
  257. bytes memory buffer = bytes(input);
  258. // Check presence of a negative sign.
  259. bytes1 sign = begin == end ? bytes1(0) : bytes1(_unsafeReadBytesOffset(buffer, begin)); // don't do out-of-bound (possibly unsafe) read if sub-string is empty
  260. bool positiveSign = sign == bytes1("+");
  261. bool negativeSign = sign == bytes1("-");
  262. uint256 offset = (positiveSign || negativeSign).toUint();
  263. (bool absSuccess, uint256 absValue) = tryParseUint(input, begin + offset, end);
  264. if (absSuccess && absValue < ABS_MIN_INT256) {
  265. return (true, negativeSign ? -int256(absValue) : int256(absValue));
  266. } else if (absSuccess && negativeSign && absValue == ABS_MIN_INT256) {
  267. return (true, type(int256).min);
  268. } else return (false, 0);
  269. }
  270. /**
  271. * @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as a `uint256`.
  272. *
  273. * Requirements:
  274. * - The string must be formatted as `(0x)?[0-9a-fA-F]*`
  275. * - The result must fit in an `uint256` type.
  276. */
  277. function parseHexUint(string memory input) internal pure returns (uint256) {
  278. return parseHexUint(input, 0, bytes(input).length);
  279. }
  280. /**
  281. * @dev Variant of {parseHexUint-string} that parses a substring of `input` located between position `begin` (included) and
  282. * `end` (excluded).
  283. *
  284. * Requirements:
  285. * - The substring must be formatted as `(0x)?[0-9a-fA-F]*`
  286. * - The result must fit in an `uint256` type.
  287. */
  288. function parseHexUint(string memory input, uint256 begin, uint256 end) internal pure returns (uint256) {
  289. (bool success, uint256 value) = tryParseHexUint(input, begin, end);
  290. if (!success) revert StringsInvalidChar();
  291. return value;
  292. }
  293. /**
  294. * @dev Variant of {parseHexUint-string} that returns false if the parsing fails because of an invalid character.
  295. *
  296. * NOTE: This function will revert if the result does not fit in a `uint256`.
  297. */
  298. function tryParseHexUint(string memory input) internal pure returns (bool success, uint256 value) {
  299. return _tryParseHexUintUncheckedBounds(input, 0, bytes(input).length);
  300. }
  301. /**
  302. * @dev Variant of {parseHexUint-string-uint256-uint256} that returns false if the parsing fails because of an
  303. * invalid character.
  304. *
  305. * NOTE: This function will revert if the result does not fit in a `uint256`.
  306. */
  307. function tryParseHexUint(
  308. string memory input,
  309. uint256 begin,
  310. uint256 end
  311. ) internal pure returns (bool success, uint256 value) {
  312. if (end > bytes(input).length || begin > end) return (false, 0);
  313. return _tryParseHexUintUncheckedBounds(input, begin, end);
  314. }
  315. /**
  316. * @dev Implementation of {tryParseHexUint-string-uint256-uint256} that does not check bounds. Caller should make sure that
  317. * `begin <= end <= input.length`. Other inputs would result in undefined behavior.
  318. */
  319. function _tryParseHexUintUncheckedBounds(
  320. string memory input,
  321. uint256 begin,
  322. uint256 end
  323. ) private pure returns (bool success, uint256 value) {
  324. bytes memory buffer = bytes(input);
  325. // skip 0x prefix if present
  326. 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
  327. uint256 offset = hasPrefix.toUint() * 2;
  328. uint256 result = 0;
  329. for (uint256 i = begin + offset; i < end; ++i) {
  330. uint8 chr = _tryParseChr(bytes1(_unsafeReadBytesOffset(buffer, i)));
  331. if (chr > 15) return (false, 0);
  332. result *= 16;
  333. unchecked {
  334. // Multiplying by 16 is equivalent to a shift of 4 bits (with additional overflow check).
  335. // This guarantees that adding a value < 16 will not cause an overflow, hence the unchecked.
  336. result += chr;
  337. }
  338. }
  339. return (true, result);
  340. }
  341. /**
  342. * @dev Parse a hexadecimal string (with or without "0x" prefix), and returns the value as an `address`.
  343. *
  344. * Requirements:
  345. * - The string must be formatted as `(0x)?[0-9a-fA-F]{40}`
  346. */
  347. function parseAddress(string memory input) internal pure returns (address) {
  348. return parseAddress(input, 0, bytes(input).length);
  349. }
  350. /**
  351. * @dev Variant of {parseAddress-string} that parses a substring of `input` located between position `begin` (included) and
  352. * `end` (excluded).
  353. *
  354. * Requirements:
  355. * - The substring must be formatted as `(0x)?[0-9a-fA-F]{40}`
  356. */
  357. function parseAddress(string memory input, uint256 begin, uint256 end) internal pure returns (address) {
  358. (bool success, address value) = tryParseAddress(input, begin, end);
  359. if (!success) revert StringsInvalidAddressFormat();
  360. return value;
  361. }
  362. /**
  363. * @dev Variant of {parseAddress-string} that returns false if the parsing fails because the input is not a properly
  364. * formatted address. See {parseAddress-string} requirements.
  365. */
  366. function tryParseAddress(string memory input) internal pure returns (bool success, address value) {
  367. return tryParseAddress(input, 0, bytes(input).length);
  368. }
  369. /**
  370. * @dev Variant of {parseAddress-string-uint256-uint256} that returns false if the parsing fails because input is not a properly
  371. * formatted address. See {parseAddress-string-uint256-uint256} requirements.
  372. */
  373. function tryParseAddress(
  374. string memory input,
  375. uint256 begin,
  376. uint256 end
  377. ) internal pure returns (bool success, address value) {
  378. if (end > bytes(input).length || begin > end) return (false, address(0));
  379. 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
  380. uint256 expectedLength = 40 + hasPrefix.toUint() * 2;
  381. // check that input is the correct length
  382. if (end - begin == expectedLength) {
  383. // length guarantees that this does not overflow, and value is at most type(uint160).max
  384. (bool s, uint256 v) = _tryParseHexUintUncheckedBounds(input, begin, end);
  385. return (s, address(uint160(v)));
  386. } else {
  387. return (false, address(0));
  388. }
  389. }
  390. function _tryParseChr(bytes1 chr) private pure returns (uint8) {
  391. uint8 value = uint8(chr);
  392. // Try to parse `chr`:
  393. // - Case 1: [0-9]
  394. // - Case 2: [a-f]
  395. // - Case 3: [A-F]
  396. // - otherwise not supported
  397. unchecked {
  398. if (value > 47 && value < 58) value -= 48;
  399. else if (value > 96 && value < 103) value -= 87;
  400. else if (value > 64 && value < 71) value -= 55;
  401. else return type(uint8).max;
  402. }
  403. return value;
  404. }
  405. /**
  406. * @dev Escape special characters in JSON strings. This can be useful to prevent JSON injection in NFT metadata.
  407. *
  408. * WARNING: This function should only be used in double quoted JSON strings. Single quotes are not escaped.
  409. *
  410. * NOTE: This function escapes all unicode characters, and not just the ones in ranges defined in section 2.5 of
  411. * RFC-4627 (U+0000 to U+001F, U+0022 and U+005C). ECMAScript's `JSON.parse` does recover escaped unicode
  412. * characters that are not in this range, but other tooling may provide different results.
  413. */
  414. function escapeJSON(string memory input) internal pure returns (string memory) {
  415. bytes memory buffer = bytes(input);
  416. bytes memory output = new bytes(2 * buffer.length); // worst case scenario
  417. uint256 outputLength = 0;
  418. for (uint256 i; i < buffer.length; ++i) {
  419. bytes1 char = bytes1(_unsafeReadBytesOffset(buffer, i));
  420. if (((SPECIAL_CHARS_LOOKUP & (1 << uint8(char))) != 0)) {
  421. output[outputLength++] = "\\";
  422. if (char == 0x08) output[outputLength++] = "b";
  423. else if (char == 0x09) output[outputLength++] = "t";
  424. else if (char == 0x0a) output[outputLength++] = "n";
  425. else if (char == 0x0c) output[outputLength++] = "f";
  426. else if (char == 0x0d) output[outputLength++] = "r";
  427. else if (char == 0x5c) output[outputLength++] = "\\";
  428. else if (char == 0x22) {
  429. // solhint-disable-next-line quotes
  430. output[outputLength++] = '"';
  431. }
  432. } else {
  433. output[outputLength++] = char;
  434. }
  435. }
  436. // write the actual length and deallocate unused memory
  437. assembly ("memory-safe") {
  438. mstore(output, outputLength)
  439. mstore(0x40, add(output, shl(5, shr(5, add(outputLength, 63)))))
  440. }
  441. return string(output);
  442. }
  443. /**
  444. * @dev Reads a bytes32 from a bytes array without bounds checking.
  445. *
  446. * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
  447. * assembly block as such would prevent some optimizations.
  448. */
  449. function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
  450. // This is not memory safe in the general case, but all calls to this private function are within bounds.
  451. assembly ("memory-safe") {
  452. value := mload(add(add(buffer, 0x20), offset))
  453. }
  454. }
  455. }