Ian Pilcher wrote:
> I'm looking for an API that will search an array of bytes for a
> specified byte pattern -- basically the same thing as
> String.indexOf(String).
>
> Does such a method exist?
Oops, I meant to convert the code before sending to use byte[] instead
of String. Here's a conversion based on the code from the page I
mentioned earlier (and some API changes to make it more like indexOf):
/**
* Knuth-Morris-Pratt Algorithm for Pattern Matching
*/
public class KMPMatch {
/**
* Finds the first occurrence of the pattern in the text.
*/
public boolean indexOf(byte[] data, byte[] pattern) {
int[] failure = computeFailure(pattern);
int j = 0;
if (data.length == 0) return false;
for (int i = 0; i < data.length; i++) {
while (j > 0 && pattern[j] != data[i]) {
j = failure[j - 1];
}
if (pattern[j] == data[i]) { j++; }
if (j == pattern.length) {
return i - pattern.length + 1;
}
}
return -1;
}
/**
* Computes the failure function using a boot-strapping process,
* where the pattern is matched against itself.
*/
private int[] computeFailure(byte[] pattern) {
int[] failure = new int[pattern.length];
int j = 0;
for (int i = 1; i < pattern.length; i++) {
while (j > 0 && pattern[j] != pattern[i]) {
j = failure[j - 1];
}
if (pattern[j] == pattern[i]) {
j++;
}
failure[i] = j;
}
return failure;
}
}
--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation