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