Finding position of a sequence in a UInt8 array?

I think @Vasily_Pisarev was suggesting implementing KMP in native Julia. But probably what you really want is something that is already implemented.

As it turns out, searching for subsequences of byte arrays is already implemented in Julia Base in order to implement substring searching. You can call it via the undocumented function:

Base._searchindex(a, [0x90,0xea,0x00], 1)

to search for the starting index of the subsequence 0x90,0xea,0x00 in a byte array a starting at the beginning of the array, for example. It returns 0 if the subsequence was not found.

It might be nice to add a high-level interface for this via findfirst and findnext — should be a pretty easy PR since the main code is already implemented.

6 Likes