Sitelet https://github.com/Vectorized/solady/pull/1427/files
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/utils/libbytes.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,32 @@ Returns the byte index of the first location of `needle` in `subject`,
needleing from left to right, starting from `from`.
Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.

### indexOfByte(bytes,bytes1,uint256)

```solidity
function indexOfByte(bytes memory subject, bytes1 needle, uint256 from)
internal
pure
returns (uint256 result)
```

Returns the byte index of the first location of `needle` in `subject`,
needleing from left to right, starting from `from`. Optimized for byte needles.
Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.

### indexOfByte(bytes,bytes1)

```solidity
function indexOfByte(bytes memory subject, bytes1 needle)
internal
pure
returns (uint256 result)
```

Returns the byte index of the first location of `needle` in `subject`,
needleing from left to right. Optimized for byte needles.
Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.

### indexOf(bytes,bytes)

```solidity
Expand Down
50 changes: 50 additions & 0 deletions src/utils/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,56 @@ library LibBytes {
}
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right, starting from `from`. Optimized for byte needles.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
function indexOfByte(bytes memory subject, bytes1 needle, uint256 from)
internal
pure
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
result := not(0) // Initialize to `NOT_FOUND`.
if gt(mload(subject), from) {
let start := add(subject, 0x20)
let end := add(start, mload(subject))
let m := div(not(0), 255) // `0x0101 ... `.
let h := mul(byte(0, needle), m) // Replicating needle mask.
m := not(shl(7, m)) // `0x7f7f ... `.
for { let i := add(start, from) } 1 {} {
let c := xor(mload(i), h) // Load 32-byte chunk and xor with mask.
c := not(or(or(add(and(c, m), m), c), m)) // Each needle byte will be `0x80`.
if c {
c := and(not(shr(shl(3, sub(end, i)), not(0))), c) // Truncate bytes past the end.
if c {
let r := shl(7, lt(0x8421084210842108cc6318c6db6d54be, c)) // Save bytecode.
r := or(shl(6, lt(0xffffffffffffffff, shr(r, c))), r)
// forgefmt: disable-next-item
result := add(sub(i, start), shr(3, xor(byte(and(0x1f, shr(byte(24,
mul(0x02040810204081, shr(r, c))), 0x8421084210842108cc6318c6db6d54be)),
0xc0c8c8d0c8e8d0d8c8e8e0e8d0d8e0f0c8d0e8d0e0e0d8f0d0d0e0d8f8f8f8f8), r)))
break
}
}
i := add(i, 0x20)
if iszero(lt(i, end)) { break }
}
}
}
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right. Optimized for byte needles.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
function indexOfByte(bytes memory subject, bytes1 needle)
internal
pure
returns (uint256 result)
{
return indexOfByte(subject, needle, 0);
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
Expand Down
50 changes: 50 additions & 0 deletions src/utils/g/LibBytes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,56 @@ library LibBytes {
}
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right, starting from `from`. Optimized for byte needles.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
function indexOfByte(bytes memory subject, bytes1 needle, uint256 from)
internal
pure
returns (uint256 result)
{
/// @solidity memory-safe-assembly
assembly {
result := not(0) // Initialize to `NOT_FOUND`.
if gt(mload(subject), from) {
let start := add(subject, 0x20)
let end := add(start, mload(subject))
let m := div(not(0), 255) // `0x0101 ... `.
let h := mul(byte(0, needle), m) // Replicating needle mask.
m := not(shl(7, m)) // `0x7f7f ... `.
for { let i := add(start, from) } 1 {} {
let c := xor(mload(i), h) // Load 32-byte chunk and xor with mask.
c := not(or(or(add(and(c, m), m), c), m)) // Each needle byte will be `0x80`.
if c {
c := and(not(shr(shl(3, sub(end, i)), not(0))), c) // Truncate bytes past the end.
if c {
let r := shl(7, lt(0x8421084210842108cc6318c6db6d54be, c)) // Save bytecode.
r := or(shl(6, lt(0xffffffffffffffff, shr(r, c))), r)
// forgefmt: disable-next-item
result := add(sub(i, start), shr(3, xor(byte(and(0x1f, shr(byte(24,
mul(0x02040810204081, shr(r, c))), 0x8421084210842108cc6318c6db6d54be)),
0xc0c8c8d0c8e8d0d8c8e8e0e8d0d8e0f0c8d0e8d0e0e0d8f0d0d0e0d8f8f8f8f8), r)))
break
}
}
i := add(i, 0x20)
if iszero(lt(i, end)) { break }
}
}
}
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right. Optimized for byte needles.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
function indexOfByte(bytes memory subject, bytes1 needle)
internal
pure
returns (uint256 result)
{
return indexOfByte(subject, needle, 0);
}

/// @dev Returns the byte index of the first location of `needle` in `subject`,
/// needleing from left to right.
/// Returns `NOT_FOUND` (i.e. `type(uint256).max`) if the `needle` is not found.
Expand Down
46 changes: 46 additions & 0 deletions test/LibBytes.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -320,4 +320,50 @@ contract LibBytesTest is SoladyTest {
if (a.length > b.length) return 1;
return 0;
}

function testIndexOfByteDifferential(bytes memory subject, bytes1 needle, uint256 from)
public
{
if (_randomChance(2)) _brutalizeMemory();
if (_randomChance(2)) _misalignFreeMemoryPointer();
if (_randomChance(2)) {
bytes memory empty;
subject = empty;
}
from = _bound(from, 0, subject.length * 2);
uint256 computed = LibBytes.indexOfByte(subject, needle, from);
uint256 expected = _indexOfByteOriginal(subject, needle, from);
assertEq(computed, expected);
}

function testIndexOfByte() public {
bytes memory subject = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
assertEq(LibBytes.indexOfByte("", "a"), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte("", "a", 1), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "a"), 0);
assertEq(LibBytes.indexOfByte(subject, "a", 1), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "b"), 1);
assertEq(LibBytes.indexOfByte(subject, "X"), 49);
assertEq(LibBytes.indexOfByte(subject, "q"), 16);
assertEq(LibBytes.indexOfByte(subject, "q", 16), 16);
assertEq(LibBytes.indexOfByte(subject, "q", 17), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte(subject, "q", 17), LibBytes.NOT_FOUND);
assertEq(LibBytes.indexOfByte("abcabcabc", "a", 0), 0);
assertEq(LibBytes.indexOfByte("abcabcabc", "a", 1), 3);
}

function _indexOfByteOriginal(bytes memory subject, bytes1 needle, uint256 from)
internal
pure
returns (uint256)
{
unchecked {
for (uint256 i; i < subject.length; ++i) {
if (i >= from) {
if (subject[i] == needle) return i;
}
}
return type(uint256).max;
}
}
}