Hi. I am writing a program where I need to find a substring in a larger one, and I also need to look at the characters on the left and the right of the match.
For example, I have this large string : “xfooy bar ufoov” and I am looking for “foo”.
First match gives me: ‘x’, “foo”, ‘y’ at index 1
Second match gives me: ‘u’, “foo”, ‘v’ at index 11
I started writing an algorithm with a loop and calls to str::find but I cannot figure out how to make the find function start at a specific byte index: I want to re-inject the matched byte index to find the next match.
Something like str::find_start_at or str::find_from taking an additional argument (the starting byte index) would be perfect for my case, but I was very disappointed when I found an issue about it in a closed state, without an approval to create such a function : https://github.com/rust-lang/rust/issues/11986
I also looked at stackoverflow for a solution. The proposal is to slice the input string. I started doing that but in my case it becomes very difficult to track the previous character of the match. Indeed, I just found a bug in my code because it was becoming complicated trying to slice at the right place.
So, is there any hope that a str::find_start_at function may appear somehow? Meanwhile, what is the simplest way to iterate in a string for matches? (without doing memory allocations).
For now the only option I think of is to code a std::find_start_at function by myself, but it feels a bit like reinventing the wheel.