ptrn => 模式串P
PLen => 模式串P长度 skip => 坏字符表 shift => 好后缀表 返回:
int - 1表示成功(文本串包含模式串),0表示失败(文本串不包含模式串)。 ****************************************************************/ int BMSearch(char *buf, int blen, char *ptrn, int plen, int *skip, int *shift) {
int b_idx = plen; if (plen == 0) return 1;
while (b_idx <= blen)//计算字符串是否匹配到了尽头 {
int p_idx = plen, skip_stride, shift_stride;
while (buf[--b_idx] == ptrn[--p_idx])//开始匹配 {
if (b_idx < 0) return 0; if (p_idx == 0) return 1; }
skip_stride = skip[(unsigned char)buf[b_idx]];//根据坏字符规则计算跳 跃的距离 shift_stride = shift[p_idx];//根据好后缀规则计算跳跃的距离
b_idx += (skip_stride > shift_stride) ? skip_stride : shift_stride;//取大者 }
return 0; }
int main(int argc, char* argv[]) {
//char test[] = \ //char find[] = \ //printf(\/* char test[] = \\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\\x90\ char find[] = \*/ char test[] = \ char find[] = \ // int i; // int toks; int *shift; int *skip;
shift=MakeShift(find,sizeof(find)-1);
skip=makeskip(find,sizeof(find)-1);
int ret = BMSearch(test, sizeof(test)-1, find, sizeof(find)-1, skip,shift); printf (\ printf (\ printf (\ if(ret ==0)
printf(\ if (ret == 1)
printf(\ getchar(); return 0; }