forked from xaxaxa/workspace
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbitap.H
More file actions
31 lines (26 loc) · 744 Bytes
/
bitap.H
File metadata and controls
31 lines (26 loc) · 744 Bytes
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
#ifndef BITAP_H_
#define BITAP_H_
#include <string.h>
#include <limits.h>
inline void *bitap_bitwise_search(const void *text, int textLen, const void *pattern, int patternLen) {
int m = patternLen;
unsigned long R;
unsigned long pattern_mask[CHAR_MAX + 1];
int i;
if (m > 31) return NULL;
/* Initialize the bit array R */
R = ~1;
/* Initialize the pattern bitmasks */
for (i = 0; i <= CHAR_MAX; ++i)
pattern_mask[i] = ~0;
for (i = 0; i < m; ++i)
pattern_mask[int(((char*) pattern)[i])] &= ~(1UL << i);
for (i = 0; i < textLen; ++i) {
/* Update the bit array */
R |= pattern_mask[int(((char*) text)[i])];
R <<= 1;
if (0 == (R & (1UL << m))) return ((char*) text + i - m) + 1;
}
return NULL;
}
#endif /* BITAP_H_ */