Strings.sol 19 KB

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