memchr icon indicating copy to clipboard operation
memchr copied to clipboard

[feature request] memchr_inv?

Open cyphar opened this issue 4 months ago • 1 comments

Sometimes it's necessary to search for the first occurrence that is not a particular byte (the most obvious example is checking if a buffer is all NUL-bytes and returning the first non-NUL byte). For instance, the Linux kernel has a memchr_inv function for this purpose.

The usecase I have at the moment is that I'm writing a C API for libpathrs that I would like to design to mirror a Linux kernel API, and memchr_inv is needed to check whether a passed buffer is zeroed after a point. I have hand-rolled memchr_inv but I suspect it is far less efficient than something that this crate could provide.

(I guess memchr already handles this, but it's probably worth mentioning that the buffer is probably not going to be 4-byte or 8-byte aligned because offset from where we have to check the buffer could be any value and so the slice could start at any offset.)

I guess this can be seen as a duplicate of #157, but with a far more restricted scope (just != as opposed to all comparison operations). But I guess the other option would be to just adapt https://github.com/BurntSushi/bstr/blob/e223ec63cab2de544625b8946e372e6d50ab53d2/src/ascii.rs as suggested in that thread...

cyphar avatar Oct 11 '24 08:10 cyphar