Skip to content

Commit ec9bd79

Browse files
authored
add Bytes from OZ (#1160)
1 parent 6b7a8a4 commit ec9bd79

File tree

1 file changed

+4
-32
lines changed

1 file changed

+4
-32
lines changed

contracts/external/libraries/BytesLib.sol

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// SPDX-License-Identifier: BUSL-1.1
22
pragma solidity ^0.8.0;
33

4+
import { Bytes } from "@openzeppelin/contracts-v5/utils/Bytes.sol";
5+
46
library BytesLib {
57
/**************************************
68
* ERRORS *
@@ -85,43 +87,13 @@ library BytesLib {
8587

8688
/**
8789
* @notice Reads a bytes array from a bytes array at a given start index and length
88-
* Source: https://github.com/Vectorized/solady/blob/21202175063a0010826bf42697b5aa2ff0c27f9f/src/utils/LibBytes.sol#L369C5-L396C6
89-
* Code was copied, was not modified
90+
* Source: OpenZeppelin Contracts v5 (utils/Bytes.sol)
9091
* @param _bytes The bytes array to convert
9192
* @param _start The start index of the bytes array
9293
* @param _end The end index of the bytes array
9394
* @return result The bytes array result
9495
*/
9596
function slice(bytes memory _bytes, uint256 _start, uint256 _end) internal pure returns (bytes memory result) {
96-
// solhint-disable-next-line no-inline-assembly
97-
assembly {
98-
let l := mload(_bytes) // _bytes length.
99-
if iszero(gt(l, _end)) {
100-
_end := l
101-
}
102-
if iszero(gt(l, _start)) {
103-
_start := l
104-
}
105-
if lt(_start, _end) {
106-
result := mload(0x40)
107-
let n := sub(_end, _start)
108-
let i := add(_bytes, _start)
109-
let w := not(0x1f)
110-
// Copy the `_bytes` one word at a time, backwards.
111-
for {
112-
let j := and(add(n, 0x1f), w)
113-
} 1 {} {
114-
mstore(add(result, j), mload(add(i, j)))
115-
j := add(j, w) // `sub(j, 0x20)`.
116-
if iszero(j) {
117-
break
118-
}
119-
}
120-
let o := add(add(result, 0x20), n)
121-
mstore(o, 0) // Zeroize the slot after the bytes.
122-
mstore(0x40, add(o, 0x20)) // Allocate memory.
123-
mstore(result, n) // Store the length.
124-
}
125-
}
97+
return Bytes.slice(_bytes, _start, _end);
12698
}
12799
}

0 commit comments

Comments
 (0)