-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearch.rs
386 lines (304 loc) · 10.4 KB
/
search.rs
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// An implementation of the Boyer-Moore search algorithm in Rust (0.2 pre)
export
// brute force
simple_search,
// boyer-moore
boyer_moore_search,
boyer_moore_unmatched_chars,
boyer_moore_largest_suffixes,
boyer_moore_matching_suffixes,
// boyer-moore-horspool
boyer_moore_horspool_search;
#[doc = "
Returns up to `nn` byte positions of matched substrings
between `start` and `end`
(using a naive search algorithm)
"]
fn simple_search (haystack: str, needle: str,
nn: uint,
start: uint, end: uint) -> [uint] {
let mut results = [];
let nlen = str::len(needle);
assert start <= end;
assert end <= str::len(haystack);
let hlen = end - start;
// empty needle
if nlen == 0u {
ret [start];
}
// haystack empty, or smaller than needle
if hlen == 0u || hlen < nlen {
ret [];
}
let mut ii = start, match_start = 0u, match_i = 0u;
while ii < end {
if haystack[ii] == needle[match_i] {
if match_i == 0u { match_start = ii; }
match_i += 1u;
// Found a match
if match_i == nlen {
vec::push(results, match_start);
match_i = 0u;
if vec::len(results) >= nn { ret results; }
}
ii += 1u;
} else {
// Failed match, backtrack
if match_i > 0u {
match_i = 0u;
ii = match_start + 1u;
} else {
ii += 1u;
}
}
}
ret results;
}
#[doc = "
Returns up to `nn` byte positions of matched substrings
between `start` and `end`
(using Boyer-Moore)
"]
fn boyer_moore_search (haystack: str, needle: str,
nn: uint,
start: uint, end: uint) -> [uint] {
let mut results = [];
let nlen = str::len(needle);
assert start <= end;
assert end <= str::len(haystack);
let hlen = end - start;
// empty needle
if nlen == 0u {
ret [start];
}
// haystack empty, or smaller than needle
if hlen == 0u || hlen < nlen {
ret [];
}
// generate the tables
let ct = boyer_moore_unmatched_chars(needle);
let pt = boyer_moore_matching_suffixes(needle);
// query both tables based on position
// within the needle and character in haystack
let getShift = fn@(pos: uint, ch: u8) -> uint {
let matchedSoFar = nlen - 1u - pos;
let rawCharShift = ct[ch as uint];
let prefShift = pt[matchedSoFar];
if rawCharShift >= matchedSoFar {
let adjCharShift = rawCharShift - matchedSoFar;
if adjCharShift > prefShift {
ret adjCharShift;
}
}
ret prefShift;
};
// step up through the haystack
let mut outerii = start;
while outerii + nlen <= end {
// step back through needle
// (checking outer range again)
let mut windowii = nlen;
while 0u < windowii {
windowii -= 1u;
// matching byte?
if needle[windowii] == haystack[outerii+windowii] {
// needle fully matched?
// note: last decremented windowii
if windowii == 0u {
vec::push(results, outerii);
if vec::len(results) >= nn { ret results; }
outerii += nlen;
}
// if not fully matched, leave outerii alone
// but decrement the windowii
} else {
// no match or a partial match
outerii += getShift(windowii, haystack[outerii+windowii]);
break;
}
}
}
ret results;
}
// compute the table used to choose a shift based on
// an unmatched character's possible position within the search string
// (a.k.a. the bad-character table)
fn boyer_moore_unmatched_chars(needle: str) -> [uint] {
let len = str::len(needle);
let deltas = vec::to_mut(vec::from_elem(255u, len));
assert 0u < len;
let mut jj = len - 1u; // drop the last byte
// from last-1 to first
while jj > 0u {
jj -= 1u;
let key = needle[jj] as uint;
// if we haven't set it yet, set it now
// (besides default)
if deltas[key] == len {
deltas[key] = len - 1u - jj;
}
}
ret vec::from_mut(deltas);
}
// for each prefix of the search string
// find the largest suffix which is a suffix of the search string
fn boyer_moore_largest_suffixes(needle: str) -> [uint] {
let len = str::len(needle);
if len == 0u { ret []; }
let mut suffs = vec::to_mut(vec::from_elem(len, 0u));
suffs[len - 1u] = len;
let mut ii = len - 1u;
let mut head = len; // index starting the previous found suffix
let mut tail = len; // index after the previous found suffix
// loop through each smaller prefix,
// keeping track of the last suffix of a prefix
// which was found to be a suffix of the needle
while 0u < ii {
ii -= 1u;
if head < ii + 1u
&& suffs[(len - 1u) - ((tail - 1u) - ii)] + head < ii + 1u
{
// The needle is a suffix of itself, stored before this loop,
// so each prefix of that is matched
// with its largest possible suffix...
//
// So (bear with me) when considering prefixes
// of another matched prefix (i.e., when head <= ii < tail)
// if the corresponding maximum prefix's match is
// smaller than the space left within the current match,
// then we know this prefix's matching suffix is the same.
// Consider:
// 01234567
// heyyheyy
// ^ ^
//
// When testing i=2, a match from 0-3 has already been found
// ("heyy"), and the match at i=6 ("y") fits
// in the remaining space within the current match,
// we know that suffs[2]=sufs[6].
//
// If, however, sufs[6] was much larger, we'd have to work more.
suffs[ii] = suffs[(len - 1u) - ((tail-1u) - ii)];
} else {
// Here, find the largest suffix of the needle which matches
// the prefix ending at ii.
// move the head left
//
// Note that if the head is already further left,
// we've already explored that far and eliminated the possibility
// of smaller match, above.
if ii + 1u <= head {
head = ii + 1u;
}
// put the tail here (the ending of this suffix)
tail = ii + 1u;
// move the head left until it is before the matching suffix
while 1u <= head
&& needle[head-1u] == needle[(len - 1u) - (tail - head)]
{
head -= 1u;
}
// store the length of this suffix
suffs[ii] = tail - head;
}
}
ret vec::from_mut(suffs);
}
// compute the table used to choose a shift based on
// a partially matched suffix of the search string
// (a.k.a. the good-suffix table)
fn boyer_moore_matching_suffixes(needle: str) -> [uint] {
let len = str::len(needle);
// compute the largest suffix of each prefix
let suffs = boyer_moore_largest_suffixes(needle);
// (1) initialize deltas
let deltas = vec::to_mut(vec::from_elem(len, len));
// (2) step to smaller suffixes ending with ii, and
// if a whole prefix is a suffix
// set all the deltas for indexes smaller than length - 1 - ii
// to length - 1 - ii
let mut ii = len;
let mut jj = 0u;
while 0u < ii {
ii -= 1u;
if suffs[ii] == ii + 1u {
// do not reset jj, only do this once
while ii < len - 1u - jj {
if deltas[len - 1u - jj] == len {
deltas[len - 1u - jj] = len - 1u - ii;
}
jj += 1u;
}
}
}
// (3) then for each different matched suffix size, set the delta
let mut kk = 0u;
while 2u <= len && kk <= len - 2u {
deltas[suffs[kk]] = len - 1u - kk;
kk += 1u;
}
ret vec::from_mut(deltas);
}
#[doc = "the same, but Boyer-Moore-Horspool"]
fn boyer_moore_horspool_search (haystack: str, needle: str,
nn: uint,
start: uint, end: uint) -> [uint] {
let mut results = [];
let nlen = str::len(needle);
assert start <= end;
assert end <= str::len(haystack);
let hlen = end - start;
// empty needle
if nlen == 0u {
ret [start];
}
// haystack empty, or smaller than needle
if hlen == 0u || hlen < nlen {
ret [];
}
// generate the tables
let ct = boyer_moore_unmatched_chars(needle);
// let pt = boyer_moore_matching_suffixes(needle);
// query both tables based on position
// within the needle and character in haystack
let getShift = fn@(pos: uint, ch: u8) -> uint {
let matchedSoFar = nlen - 1u - pos;
let rawCharShift = ct[ch as uint];
// let prefShift = pt[matchedSoFar];
if rawCharShift >= matchedSoFar {
let adjCharShift = rawCharShift - matchedSoFar;
// if adjCharShift > prefShift {
ret adjCharShift;
// }
}
// ret prefShift;
ret 1u;
};
// step up through the haystack
let mut outerii = start;
while outerii + nlen <= end {
// step back through needle
// (checking outer range again)
let mut windowii = nlen;
while 0u < windowii {
windowii -= 1u;
// matching byte?
if needle[windowii] == haystack[outerii+windowii] {
// needle fully matched?
// note: last decremented windowii
if windowii == 0u {
vec::push(results, outerii);
if vec::len(results) >= nn { ret results; }
outerii += nlen;
}
// if not fully matched, leave outerii alone
// but decrement the windowii
} else {
// no match or a partial match
outerii += getShift(windowii, haystack[outerii+windowii]);
break;
}
}
}
ret results;
}