forked from Bush2021/chrome_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastsearch.h
62 lines (48 loc) · 1.11 KB
/
fastsearch.h
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
#ifndef FASTSEARCH_H_
#define FASTSEARCH_H_
#include <stdint.h>
static const uint8_t* ForceSearch(const uint8_t* s, int n, const uint8_t* p) {
int i;
for (i = 0; i < n; ++i) {
if (*(s + i) == *p) {
return s + i;
}
}
return nullptr;
}
static const uint8_t* SundaySearch(const uint8_t* s,
int n,
const uint8_t* p,
int m) {
int i, j;
size_t skip[256];
for (i = 0; i < 256; ++i) {
skip[i] = m + 1;
}
for (i = 0; i < m; ++i) {
skip[p[i]] = m - i;
}
i = 0;
while (i <= n - m) {
j = 0;
while (s[i + j] == p[j]) {
++j;
if (j >= m) {
return s + i;
}
}
i += (int)skip[s[i + m]];
}
return nullptr;
}
const uint8_t* FastSearch(const uint8_t* s, int n, const uint8_t* p, int m) {
if (!s || !p || n < m)
return nullptr;
if (m == 0) {
return s;
} else if (m == 1) {
return ForceSearch(s, n, p);
}
return SundaySearch(s, n, p, m);
}
#endif // FASTSEARCH_H_