BytesParsing - change handling of calldata
slicing and checkLength
#76
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Two changes:
First, the various calldata versions of
slice
now returnbytes calldata
instead ofbytes memory
.This rectifies a previous mistake of mine where I didn't realize that I could use assembly to implement an unchecked version of
slice
that does what I want. I figured that creating abytes memory
is efficient enough since it can use CALLDATACOPY but failed to consider that pushing things from thecalldata
domain to thememory
domain makes for ugly and inefficient code if subsequent parsing is necessary.Second,
checkLength
now takes the encoded length as the first argument instead of the encoded bytes directly. The rationale is that it's way too easy to writesomeCalldataBytes.checkLength(expectedLength);
instead of using the appropriatecheckLengthCd
version, which would quietly allocate memory and copy the entiresomeCalldataBytes
just to do the length check. Additionally, it's also more consistent with howcheckBound
works.It is now recommended to also have a
using {BytesParsing.checkLength} for uint;
line at the beginning of the contract (along withusing BytesParsing for bytes;
) and check viaencoded.length.checkLength(expectedLength);
independent of whetherencoded
hasmemory
orcalldata
location.