std.mem
: add namespace for comparing sentinel-terminated pointers
#24683
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.
This adds a new namespace to
std.mem
for comparing sentinel-terminated pointers:std.mem.sentinel_terminated
.This includes both a function for comparing two such pointers and a function for comparing one to a slice (latter addresses #7848 (comment)).
Ordering is also provided, which also works if the sentinel is not the minimal possible value in the slice (copied from and closes #22618).
I also included
allEqual()
,min()
andmax()
functions.len()
andspan()
link to the implementation currently instd.mem
, though if this get accepted, moving the implementations and deprecating the original functions might be a possibility.The naming I chose isn't final, if a better one is suggested, I can change it.
Similarly, I am not quite sure on the explicitness of the sentinel value in the function calls, though it does follow a similar pattern as many functions in
std.mem
do currently.I tested the speed of
eql()
andeqlSlice()
. Both are faster than usingstd.mem.eql()
with sentinel-terminated pointer to slice conversion, especially at smaller lengths.If operating on sentinel-terminated slices it is actually sometimes faster to use
std.mem.sentinel_terminated.eql()
on the pointers than it is to pass the slices tostd.mem.eql()
, though more testing should be done before this is used.Lastly, if there are any more functions I should add, or any usecase in the standard library or the compiler where these functions could help, tell me and I will implement them to the best of my ability.