Here is my implementation of the BMH algorithm (it works like a charm):
public static Int64 IndexOf(this Byte[] value, Byte[] pattern) { if (value == null) throw new ArgumentNullException("value"); if (pattern == null) throw new ArgumentNullException("pattern"); Int64 valueLength = value.LongLength; Int64 patternLength = pattern.LongLength; if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength)) return -1; Int64[] badCharacters = new Int64[256]; for (Int64 i = 0; i < 256; ++i) badCharacters[i] = patternLength; Int64 lastPatternByte = patternLength - 1; for (Int64 i = 0; i < lastPatternByte; ++i) badCharacters[pattern[i]] = lastPatternByte - i;
I tried changing it to return all matches, not just the first index, but I get an IndexOutOfRangeException everywhere D:
Obviously, I am missing something important or I did not understand how it works. What am I doing wrong?
public static List<Int64> IndexesOf(this Byte[] value, Byte[] pattern) { if (value == null) throw new ArgumentNullException("value"); if (pattern == null) throw new ArgumentNullException("pattern"); Int64 valueLength = value.LongLength; Int64 patternLength = pattern.LongLength; if ((valueLength == 0) || (patternLength == 0) || (patternLength > valueLength)) return (new List<Int64>()); Int64[] badCharacters = new Int64[256]; for (Int64 i = 0; i < 256; ++i) badCharacters[i] = patternLength; Int64 lastPatternByte = patternLength - 1; for (Int64 i = 0; i < lastPatternByte; ++i) badCharacters[pattern[i]] = lastPatternByte - i;
source share