-
Notifications
You must be signed in to change notification settings - Fork 224
/
bm.c
68 lines (58 loc) · 1.48 KB
/
bm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <ctype.h>
const char * boyer_moore(const char *haystack, const char *needle) {
size_t plen = strlen(needle), slen = strlen(haystack);
if (plen > slen) {
return NULL;
}
int skip[UCHAR_MAX + 1];
int i, j, k, * next;
/* calc skip table („bad rule“) */
for (i = 0; i <= UCHAR_MAX; i++) {
skip[i] = plen;
}
for (i = 0; i < plen; i++) {
skip[tolower((unsigned char)needle[i])] = plen - i - 1;
}
/* calc next table („good rule“) */
next = (int*)malloc((plen + 1) * sizeof(int));
for (j = 0; j <= plen; j++) {
for (i = plen - 1; i >= 1; i--) {
for (k = 1; k <= j; k++) {
if (i - k < 0) {
break;
}
if (tolower((unsigned char)needle[plen - k]) != tolower((unsigned char)needle[i - k])) {
goto nexttry;
}
}
goto matched;
nexttry:
;
}
matched:
next[j] = plen - i;
}
plen--;
i = plen; /* position of last p letter in s */
while (i < slen) {
j = 0; /* matched letter count */
while (j <= plen) {
if (tolower((unsigned char)haystack[i - j]) == tolower((unsigned char)needle[plen - j])) {
j++;
} else {
i += skip[tolower((unsigned char)haystack[i - j])] > next[j] ? skip[tolower((unsigned char)haystack[i - j])] - j : next[j];
goto newi;
}
}
free(next);
return haystack + i - plen;
newi:
;
}
free(next);
return NULL;
}