Searching for a string within a string is extremely well supported in .NET but what do you do when the data you need to search isn't a string?
I have binary data arriving in regular chunks via a NetworkStream. Packets are binary but they all start with a signature sequence of bytes. I accumulate the chunks into a larger buffer and look for the start-of-packet signature.
What I'm really looking for is the byte[]
equivalent to the String.IndexOf(ss)
method. I have a nasty feeling I'm going to have to implement this myself with a loop and a state machine.
Any suggestions? Over to you!
As suggested, Array.IndexOf(byte) will at least save me an explicit loop. Since posting, it occurred to me to find the first signature byte, then probe ahead for a match where the last signature byte should be, then if they both match try a brute-force comparison for the rest of the string. This approach has the advantage of rejecting false matches cheaply and allowing me to cheaply reject when I have a partial signature pending another chunk.
Google reveals that the above brilliant plan is a degenerate case of "KMP" or Knuth-Morris-Pratt algorithm. On the bright side if Knuth put his name on it it's probably greased lightning, on the downside why is it that whenever I have a good idea Donald Knuth thought of it 25 years ago?
Since I can't award the points to Donald Knuth I guess they go to Nelson.