Anyone know a good and effective way to search/match for a byte pattern in an byte[] array and then return the positions.
For example
byte[] pattern = new byte[] {12,3,5,76,8,0,6,125};
byte[] toBeSearched = new byte[] {23,36,43,76,125,56,34,234,12,3,5,76,8,0,6,125,234,56,211,122,22,4,7,89,76,64,12,3,5,76,8,0,6,125}
Use the efficient Boyer-Moore algorithm.
It's designed to find strings withing strings but you need little imagination to project this to byte arrays.
In general the best answer is: use any string searching algorithm that you like :).
These are the simplest and fastest methods you can use, and there wouldn't be anything faster than these. It is unsafe but thats what we use pointers for is speed. So here I offer you my extension methods that I use search for a single, and a list of indexes of the occurances. I would like to say this is the cleanest code here.
Benchmarked with Locate, it is 1.2-1.4x faster
Just another answer that is easy to follow and pretty efficient for a O(n) type of operation without using unsafe code or copying parts of the source arrays.
Be sure to test. Some of the suggestions found on this topic are susceptible to gotta situations.
May I suggest something that doesn't involve creating strings, copying arrays or unsafe code:
Edit (by IAbstract) - moving contents of post here since it is not an answer
Out of curiosity, I've created a small benchmark with the different answers.
Here are the results for a million iterations:
thanks for taking the time...
This is the code that i was using/testing with before I asked my question... The reason I ask this question was that I´m certain of that I´m not using the optimal code to do this... so thanks again for taking the time!
Anyone that see any errors in my code? Is this considered as an hackish approach? I have tried almost every sample you guys have posted and I seems to get some variations in the match results. I have been running my tests with ~10Mb byte array as my toBeSearched array.
You can use ORegex:
Will be found two matches:
Complexity: O(n*m) in worst case, in real life it is O(n) because of internal state machine. It is faster than .NET Regex in some cases. It is compact, fast and designed especialy for array pattern matching.