Bytes.sol 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.2.0) (utils/Bytes.sol)
  3. pragma solidity ^0.8.24;
  4. import {Math} from "./math/Math.sol";
  5. /**
  6. * @dev Bytes operations.
  7. */
  8. library Bytes {
  9. /**
  10. * @dev Forward search for `s` in `buffer`
  11. * * If `s` is present in the buffer, returns the index of the first instance
  12. * * If `s` is not present in the buffer, returns type(uint256).max
  13. *
  14. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
  15. */
  16. function indexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
  17. return indexOf(buffer, s, 0);
  18. }
  19. /**
  20. * @dev Forward search for `s` in `buffer` starting at position `pos`
  21. * * If `s` is present in the buffer (at or after `pos`), returns the index of the next instance
  22. * * If `s` is not present in the buffer (at or after `pos`), returns type(uint256).max
  23. *
  24. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf[Javascript's `Array.indexOf`]
  25. */
  26. function indexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
  27. uint256 length = buffer.length;
  28. for (uint256 i = pos; i < length; ++i) {
  29. if (bytes1(_unsafeReadBytesOffset(buffer, i)) == s) {
  30. return i;
  31. }
  32. }
  33. return type(uint256).max;
  34. }
  35. /**
  36. * @dev Backward search for `s` in `buffer`
  37. * * If `s` is present in the buffer, returns the index of the last instance
  38. * * If `s` is not present in the buffer, returns type(uint256).max
  39. *
  40. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
  41. */
  42. function lastIndexOf(bytes memory buffer, bytes1 s) internal pure returns (uint256) {
  43. return lastIndexOf(buffer, s, type(uint256).max);
  44. }
  45. /**
  46. * @dev Backward search for `s` in `buffer` starting at position `pos`
  47. * * If `s` is present in the buffer (at or before `pos`), returns the index of the previous instance
  48. * * If `s` is not present in the buffer (at or before `pos`), returns type(uint256).max
  49. *
  50. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/lastIndexOf[Javascript's `Array.lastIndexOf`]
  51. */
  52. function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
  53. unchecked {
  54. uint256 length = buffer.length;
  55. // NOTE here we cannot do `i = Math.min(pos + 1, length)` because `pos + 1` could overflow
  56. for (uint256 i = Math.min(pos, length - 1) + 1; i > 0; --i) {
  57. if (bytes1(_unsafeReadBytesOffset(buffer, i - 1)) == s) {
  58. return i - 1;
  59. }
  60. }
  61. return type(uint256).max;
  62. }
  63. }
  64. /**
  65. * @dev Copies the content of `buffer`, from `start` (included) to the end of `buffer` into a new bytes object in
  66. * memory.
  67. *
  68. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
  69. */
  70. function slice(bytes memory buffer, uint256 start) internal pure returns (bytes memory) {
  71. return slice(buffer, start, buffer.length);
  72. }
  73. /**
  74. * @dev Copies the content of `buffer`, from `start` (included) to `end` (excluded) into a new bytes object in
  75. * memory.
  76. *
  77. * NOTE: replicates the behavior of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice[Javascript's `Array.slice`]
  78. */
  79. function slice(bytes memory buffer, uint256 start, uint256 end) internal pure returns (bytes memory) {
  80. // sanitize
  81. uint256 length = buffer.length;
  82. end = Math.min(end, length);
  83. start = Math.min(start, end);
  84. // allocate and copy
  85. bytes memory result = new bytes(end - start);
  86. assembly ("memory-safe") {
  87. mcopy(add(result, 0x20), add(buffer, add(start, 0x20)), sub(end, start))
  88. }
  89. return result;
  90. }
  91. /**
  92. * @dev Reads a bytes32 from a bytes array without bounds checking.
  93. *
  94. * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
  95. * assembly block as such would prevent some optimizations.
  96. */
  97. function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
  98. // This is not memory safe in the general case, but all calls to this private function are within bounds.
  99. assembly ("memory-safe") {
  100. value := mload(add(buffer, add(0x20, offset)))
  101. }
  102. }
  103. }