C语言字符串匹配函数,保存有需要时可以用:
1 #include2 #include 3 #include 4 #include 5 #include 6 7 /* 8 pattern: 9 pos: 10 */ 11 12 static int badShift[256]; 13 14 15 static int goodPostfixLastPos(const char *pattern,int pos) 16 { 17 #define _break(flag) if(flag){ break;} 18 19 int flag = 0; 20 int len = strlen(pattern); 21 int postFix_len = len - pos; 22 int postFix_position = pos; 23 int initStart = pos - postFix_len; 24 int last_start = 0; 25 while(postFix_len) 26 { 27 last_start = (postFix_position == pos) ?initStart:0; 28 int postFix_start = postFix_position; 29 for(;last_start>=0 && postFix_start = 0) 38 { 39 initStart--; 40 } 41 else 42 { 43 postFix_position++; 44 postFix_len--; 45 } 46 } 47 48 return flag?last_start-1:-1; 49 } 50 51 static int *calc_goodPostfixShift(const char *pattern,int *goodShift) 52 { 53 int len = strlen(pattern); 54 for(int i=0;i =0;pos_pattern--,start--) 94 { 95 if(str[start] != pattern[pos_pattern]) 96 { 97 break; 98 } 99 }100 if(pos_pattern < 0)101 {102 return start + 1;103 }104 105 if(pos_pattern == (len2 - 1))106 {107 i += badShift[str[start]];108 }109 else110 {111 i += goodShift[pos_pattern + 1];112 }113 }114 115 return -1;116 }