-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathbm.c
165 lines (151 loc) · 3.47 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/**
* @file bm.c
* @author hutusi ([email protected])
* @brief Refer to bm.h
* @date 2019-08-10
*
* @copyright Copyright (c) 2019, hutusi.com
*
*/
#include "bm.h"
#include "def.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
/**
* @brief Calculate bad charactors.
*
* @param pattern
* @param len
* @param bad_chars
* @return STATIC bm_calculate_bad_chars
*/
STATIC void bm_calculate_bad_chars(const char *pattern, int len, int *bad_chars)
{
for (int i = 0; i < 256; ++i) {
bad_chars[i] = -1;
}
for (int i = len - 1; i >= 0; --i) {
int ch = pattern[i];
if (bad_chars[ch] < 0) {
bad_chars[ch] = i;
}
}
}
/**
* @brief Calculate good suffixes
*
* @param pattern
* @param len
* @param good_suffixes
* @return STATIC bm_calculate_good_suffixes
*/
STATIC void
bm_calculate_good_suffixes(const char *pattern, int len, int *good_suffixes)
{
for (int i = 0; i <= len; ++i) {
good_suffixes[i] = -1;
}
for (int i = 0; i < len - 1; ++i) {
int j = i;
int k = 0;
while (j >= 0 && pattern[j] == pattern[len - 1 + j - i]) {
++k;
good_suffixes[k] = j;
--j;
}
}
}
STATIC int bm_move_by_good_suffixes(int *good_suffixes, int len, int good_len)
{
if (good_len == 0) {
return 1;
} else {
return len - good_suffixes[good_len] - good_len;
}
}
#define MAX(a, b) ((a) < (b) ? (b) : (a))
/**
* @brief BM algorithm function
*
* There are two rules in BM algorithm.
*
* rule 1: bad charactor.
*
* compare begin with last charactor:
* !
* abceaebabcde
* abcd
*
* 'e' is a bad charactor, 'e' is not in 'abcd', move length 4('abcd'):
* !
* abceaebabcde
* abcd
*
* now 'a' is a bad charactor, 'a' in 'abcd', move to make 'a' align with 'a':
*
* !
* abceaebabcde
* abcd
*
*
* rule 2: good suffix.
*
* compare begin with last charactor:
* !
* abceaabccabef
* aabccab
*
* step to left: 'ab' is good suffix, 'a', 'c' not match:
* *!!
* abceaabccabef
* aabccab
*
* move length 4 to make 'ab' align with 'ab':
*
* !!
* abceaabccabef
* aabccab
*
*
* @param text
* @param pattern
* @return int
*/
int bm_text_match(const char *text,
unsigned int text_len,
const char *pattern,
unsigned int pat_len)
{
int bad_chars[256];
bm_calculate_bad_chars(pattern, pat_len, bad_chars);
int *good_suffixes = (int *)malloc((pat_len + 1) * sizeof(int));
bool *prefixes = (bool *)malloc(pat_len * sizeof(bool));
bm_calculate_good_suffixes(pattern, pat_len, good_suffixes);
int index = -1;
int cursor = 0;
while (cursor <= text_len - pat_len) {
int i = 0;
for (; i < pat_len; ++i) {
if (text[cursor + pat_len - 1 - i] == pattern[pat_len - 1 - i]) {
} else {
int bad = pat_len - i - 1 -
bad_chars[(int)text[cursor + pat_len - 1 - i]];
int good = bm_move_by_good_suffixes(good_suffixes, pat_len, i);
cursor += MAX(bad, good);
break;
}
}
if (i == pat_len) { // found it!
index = cursor;
break;
}
}
free(good_suffixes);
free(prefixes);
return index;
}
int bm_string_match(const char *text, const char *pattern)
{
return bm_text_match(text, strlen(text), pattern, strlen(pattern));
}