7 static int* prefix = NULL;
8 static int max_size = 0;
10 static void GetPrefixValue(const char* strPattern, int iPatternLen)
12 if (iPatternLen>max_size) {
13 prefix = (int*)realloc(prefix, iPatternLen*sizeof(int));
14 max_size = iPatternLen;
17 int i, j; /* i runs through the string, j counts the hits*/
23 if(strPattern[i] == strPattern[j])
37 static int KMPStringMatch(const char* strPattern, int iPatternLen, const char* strTarget, int iTargetLen)
41 for (i=0;i<iPatternLen;i++) {
42 while ((strPattern[i] != strTarget[j]) && (j > 0))
44 if (strPattern[i] == strTarget[j])
52 int KMP(const char* strPattern, int len, const char* strTarget)
54 GetPrefixValue(strPattern, len);
55 return KMPStringMatch(strPattern, len, strTarget, strlen(strTarget));