-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmsearch.c
138 lines (124 loc) · 3.99 KB
/
bmsearch.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
/* bmsearch.c - Boyer-Moore search functions
* Copyright (C) 2015 Sean MacLennan <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "buff.h"
#define NUMASCII 256 /* number of ascii chars */
#define buff() (*buff->curcptr)
#define buffint() ((uint8_t)buff())
/** This is an implementation of the Boyer-Moore Search.
* It uses the delta1 only with the fast/slow loops.
* It searches for the string 'str' starting at the current buffer location.
* If sensitive is false, then the match is case insensitive.
* The point is left at the byte after the search str.
*/
bool bm_search(struct buff *buff, const char *str, bool sensitive)
{
int delta[NUMASCII], len, i, shift;
len = strlen(str) - 1;
/* Init the delta table to str length. For each char in the
* str, store the offset from the str start in the delta
* table. If we are in case insensitive mode - lower case the
* match string and mark both the upper case version and the
* lower case version of the match string chars in the delta
* array.
*/
for (i = 0; i < NUMASCII; ++i)
delta[i] = len ? len : 1;
if (sensitive)
for (i = 0; i <= len; ++i)
delta[(uint8_t)str[i]] = len - i;
else
for (i = 0; i <= len; ++i) {
delta[toupper(str[i])] = len - i;
delta[tolower(str[i])] = len - i;
}
/* search forward*/
while (!bisend(buff)) {
/* fast loop - delta will be 0 if matched */
while (!bisend(buff) && delta[buffint()])
bmove(buff, delta[buffint()]);
/* slow loop */
for (i = len;
buff() == str[i] ||
(!sensitive && tolower(buff()) == tolower(str[i]));
bmove(buff, -1), --i)
if (i == 0) {
bmove(buff, len + 1);
return true;
}
/* compute shift. shift must be forward! */
if (i + delta[buffint()] > len)
shift = delta[buffint()];
else
shift = len - i + 1;
bmove(buff, shift);
}
return false;
}
/** This is an implementation of the Boyer-Moore Search that searches backwards.
* It uses the delta1 only with the fast/slow loops.
* It searches for the string 'str' starting at the current buffer location.
* If sensitive is false, then the match is case insensitive.
* The point is left at the start of the search str.
*/
bool bm_rsearch(struct buff *buff, const char *str, bool sensitive)
{
int delta[NUMASCII], len, i;
len = strlen(str) - 1;
/* Init the delta table to str length. For each char in the
* str, store the negative offset from the str start in the
* delta table.
*/
for (i = 0; i < NUMASCII; ++i)
delta[i] = len ? -len : -1;
if (sensitive)
for (i = len; i >= 0; --i)
delta[(uint8_t)str[i]] = -i;
else
for (i = len; i >= 0; --i) {
delta[toupper(str[i])] = -i;
delta[tolower(str[i])] = -i;
}
/* reverse search */
bmove(buff, -len);
while (!bisstart(buff)) {
/* fast loop - delta will be 0 if matched */
while (delta[buffint()] && !bisstart(buff))
bmove(buff, delta[buffint()]);
/* slow loop */
for (i = 0;
i <= len &&
((char)buff() == str[i] ||
(!sensitive &&
tolower(buff()) == tolower(str[i])));
++i, bmove1(buff))
;
if (i > len) {
/* we matched! */
bmove(buff, -len - 1);
return true;
}
/* compute shift. shift must be backward! */
bmove(buff, delta[buffint()] + i < 0 ? delta[buffint()] : -i - 1);
}
return false;
}