We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent aa1cdb8 commit 29b1b35Copy full SHA for 29b1b35
contracts/utils/Bytes.sol
@@ -58,12 +58,10 @@ library Bytes {
58
function lastIndexOf(bytes memory buffer, bytes1 s, uint256 pos) internal pure returns (uint256) {
59
unchecked {
60
uint256 length = buffer.length;
61
- if (length > 0) {
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
- }
+ uint256 end = Math.ternary(pos == type(uint256).max, length, Math.min(pos + 1, length));
+ for (uint256 i = end; i > 0; --i) {
+ if (bytes1(_unsafeReadBytesOffset(buffer, i - 1)) == s) {
+ return i - 1;
67
}
68
69
return type(uint256).max;
0 commit comments