Writing a parser. Thought of this. Why keep track of position twice if the language already does it for you? Only problem is there's currently no way to get that position back. For the most part, "position", as an offset from the start of the string, is uninteresting, but "address" is useful... except in error messages and other user-facing locations, where "position" is more useful. But well, you can easily get a position if you have two addresses and you know that one of them is contained within the range of the other. So it'd be nice to have something like that for &str and for &[T].
One thing to note is that this has some interesting edge-cases around &s[0..0] etc. Not all empty strings are created the same. But it's not really a problem as that's already exposed with std::ptr::eq.
// or do you mean the arguments the other way around?
// or something entirely different?
fn is_substr(this: &str, pattern: &str) -> Option<usize> {
this.find(pattern)
}
I thought about it. I don’t feel like I need to have this as a crate of mine. I don’t feel like the current naming is optimal and I’m also too lazy to create the missing assertions and add proper documentation. If you think this is something you need, feel free to use and modify the code from my playground however you like.
Damn that RFC proposed implementation is full of UB ... I mean, I guess the “empty slice right at the start of the next allocation” issue was already called out over there, but it’s also sound to obtain pointers to &[[T; 2]] sub-slices that aren’t properly aligned, e.g. a subslice [[2,3]] of [[1,2],[3,4]]. This is for example possible with the bytemuck crate.
And offset_from needs
The distance between the pointers, in bytes, must be an exact multiple of the size of T .
for safety.
Also @Soni, in case you were wondering, this kind of setting is why there’s a offset % size == 0 check in my playground.