Bytes.sol 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(add(buffer, 0x20), start), sub(end, start))
  88. }
  89. return result;
  90. }
  91. /**
  92. * @dev Reverses the byte order of a bytes32 value, converting between little-endian and big-endian.
  93. * Inspired in https://graphics.stanford.edu/~seander/bithacks.html#ReverseParallel[Reverse Parallel]
  94. */
  95. function reverseBytes32(bytes32 value) internal pure returns (bytes32) {
  96. value = // swap bytes
  97. ((value >> 8) & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) |
  98. ((value & 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
  99. value = // swap 2-byte long pairs
  100. ((value >> 16) & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) |
  101. ((value & 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
  102. value = // swap 4-byte long pairs
  103. ((value >> 32) & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) |
  104. ((value & 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF) << 32);
  105. value = // swap 8-byte long pairs
  106. ((value >> 64) & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) |
  107. ((value & 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF) << 64);
  108. return (value >> 128) | (value << 128); // swap 16-byte long pairs
  109. }
  110. /// @dev Same as {reverseBytes32} but optimized for 128-bit values.
  111. function reverseBytes16(bytes16 value) internal pure returns (bytes16) {
  112. value = // swap bytes
  113. ((value & 0xFF00FF00FF00FF00FF00FF00FF00FF00) >> 8) |
  114. ((value & 0x00FF00FF00FF00FF00FF00FF00FF00FF) << 8);
  115. value = // swap 2-byte long pairs
  116. ((value & 0xFFFF0000FFFF0000FFFF0000FFFF0000) >> 16) |
  117. ((value & 0x0000FFFF0000FFFF0000FFFF0000FFFF) << 16);
  118. value = // swap 4-byte long pairs
  119. ((value & 0xFFFFFFFF00000000FFFFFFFF00000000) >> 32) |
  120. ((value & 0x00000000FFFFFFFF00000000FFFFFFFF) << 32);
  121. return (value >> 64) | (value << 64); // swap 8-byte long pairs
  122. }
  123. /// @dev Same as {reverseBytes32} but optimized for 64-bit values.
  124. function reverseBytes8(bytes8 value) internal pure returns (bytes8) {
  125. value = ((value & 0xFF00FF00FF00FF00) >> 8) | ((value & 0x00FF00FF00FF00FF) << 8); // swap bytes
  126. value = ((value & 0xFFFF0000FFFF0000) >> 16) | ((value & 0x0000FFFF0000FFFF) << 16); // swap 2-byte long pairs
  127. return (value >> 32) | (value << 32); // swap 4-byte long pairs
  128. }
  129. /// @dev Same as {reverseBytes32} but optimized for 32-bit values.
  130. function reverseBytes4(bytes4 value) internal pure returns (bytes4) {
  131. value = ((value & 0xFF00FF00) >> 8) | ((value & 0x00FF00FF) << 8); // swap bytes
  132. return (value >> 16) | (value << 16); // swap 2-byte long pairs
  133. }
  134. /// @dev Same as {reverseBytes32} but optimized for 16-bit values.
  135. function reverseBytes2(bytes2 value) internal pure returns (bytes2) {
  136. return (value >> 8) | (value << 8);
  137. }
  138. /**
  139. * @dev Reads a bytes32 from a bytes array without bounds checking.
  140. *
  141. * NOTE: making this function internal would mean it could be used with memory unsafe offset, and marking the
  142. * assembly block as such would prevent some optimizations.
  143. */
  144. function _unsafeReadBytesOffset(bytes memory buffer, uint256 offset) private pure returns (bytes32 value) {
  145. // This is not memory safe in the general case, but all calls to this private function are within bounds.
  146. assembly ("memory-safe") {
  147. value := mload(add(add(buffer, 0x20), offset))
  148. }
  149. }
  150. }