C# pattern search -
i have set of hexadecimal numbers search file, each time matches stores last offset address of last byte found in pattern text file.
so example, pattern '04 00 03 00 00 00 04 00 04 00 00 00 08 00' each time found in file last byte in pattern '00' offset read in , stored in array.
any on please, has been driving me mad months now.
thanks
thanks svick did work indeed ... returned matches offsets needed find.
however, since added little bit of code in, stops on first match , doesn't cycle through... please point out wrong , why stopping @ first match found
many thanks
i'm assuming pattern string. should convert array of bytes.
if file search relatively small , efficiency not important starting search each byte in file , compare pattern byte byte. if go through whole pattern , bytes match, store position in result array. in code, go this:
byte[] pattern = …; byte[] file = …; var result = enumerable.range(0, file.length - pattern.length + 1) .where(i => pattern.select((b, j) => new { j, b }) .all(p => file[i + p.j] == p.b)) .select(i => + pattern.length - 1); the time complexity of solution o(hn), h length of file (“haystack”) , n length of pattern (“needle”).
if file big or if need fast, should use kmp algorithm, has complexity of o(h + n).
Comments
Post a Comment