fix: change offset calculation order to work with prefix matches #1
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.
If a match was found exactly at the beginning of a haystack (see the new supplied test), the scanner would panic when trying to subtract
pat_len
frompos
, aspat_len
would always be larger thanpos
in this case.In case you're wondering "Why is someone randomly sending a PR to this 7 year old, seemingly unmaintained repo?": We are working on a Rust implementation of Nix, called Tvix. In a part of this project we have a potentially huge number of needles which are all equal length, very random strings (hashes of other data), and need to find their occurences in a set of other matches. The set of needles frequently changes and we need to do this scanning hundreds/thousands of times during a single run.
Especially the fact that the set changes quickly makes automaton-based solutions fairly inefficient, as we're doing expensive construction and throwing them away all the time. We tried the famous
aho-corasick
Rust implementation - it increased runtime by 5x, and ended up being most of what the program did (apart from disk IO). After reading some of the literature, I found that the Wu-Manber algorithm is likely a good fit for our problem space, and your crate was the only one implementing it already (I wasn't quite convinced enough to start a from-scratch implementation, though). It is significantly better performing for us already, but we had to squash this bug :)Thanks for publishing this!