I have two byte[]
and I want to find the first occurrence of the second byte[]
in the first byte[]
(or a range in it).
I don't want to use strings for efficiency (translating the first byte[]
to a string
will be inefficient).
Basically I believe that's what strstr()
does in C.
What is the best way to do that (so it be efficient and easy to use)?
This is how it should look like:
int GetOffsetOfArrayInArray(byte[] bigArray, int bigArrayOffset, int bigArrayCount, byte[] smallArray);
Thanks!
UPDATE:
I want a solution that would be more efficient than a simple search. This means that using the fact that comparing buffers can be more efficient should be used - memcmp() is more efficient than iterating over the bytes.
Also, I know there are algorithms that optimize scenarios like this one:
I don't have any code for you but the name of the fastest solution you will find is the Boyer-Moore algorithm. It can do better than O(n).
Here is an implementation for strings on CodeProject. Looks like a conversion to byte[]
should not be too difficult.