Sitelet https://github.com/Vectorized/solady/pull/1425
Skip to content

✨ LibBytes indexOfByte - #1425

Merged
Vectorized merged 8 commits into
Vectorized:index-of-bytefrom
Hyodar:index-of-byte
Jun 6, 2025
Merged

Vectorized merged 8 commits into
Vectorized:index-of-bytefrom
Hyodar:index-of-byte

Conversation

@Hyodar

@Hyodar Hyodar commented Jun 5, 2025 •

Copy link
Copy Markdown
Contributor

Description

Adds function indexOfByte(bytes memory subject, bytes1 needle, uint256 from) and offset-less shortcut to LibBytes and LibString.

This is a considerably more optimal version of indexOf in the case only a byte needle is used as we can apply some learnings from bithacks and keep the gas cost per 'missed' chunk quite low.

The reason for naming it indexOfByte versus keeping it as indexOf is because Solidity also tries to cast literal strings to bytes1, so if there's an implementation for bytes and one for bytes1, the user always has to be explicit on the type, which can get annoying.

Reproduced the clz implementation inside of it to avoid creating a dependency just for that and for a very small optimization, but it would be okay to do otherwise as well.

(Keeping it as draft for a review soon, and possibly some more tests.)

Checklist

Ensure you completed all of the steps below before submitting your pull request:

  • Ran forge fmt?
  • Ran forge test?

@Vectorized
Vectorized marked this pull request as ready for review June 6, 2025 03:02
@Vectorized

Copy link
Copy Markdown
Owner

looks good. I might cache the mask in a variable to make the bytecode smaller.

@Vectorized
Vectorized changed the base branch from main to index-of-byte June 6, 2025 03:17
@Vectorized
Vectorized merged commit 52c28a0 into Vectorized:index-of-byte Jun 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants