-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsearch.c
557 lines (449 loc) · 14.8 KB
/
search.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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
// //////////////////////////////////////////////////////////
// search.c
// Copyright (c) 2014,2019 Stephan Brumme. All rights reserved.
// see http://create.stephan-brumme.com/disclaimer.html
//
// compiles with: gcc -Wall
// pretty much every decent C compiler should be able to eat this code, too
// note: some warnings about C++ comments and mixing declarations and code when enabling -pedantic
// but they all disappear in C99 mode
#include "search.h"
#include <string.h> // strlen
#include <stdlib.h> // malloc / free
/// naive approach (for C strings)
const char* searchSimpleString(const char* haystack, const char* needle)
{
// detect invalid input
if (!haystack || !needle)
return NULL;
// until the end of haystack (or a match, of course)
while (*haystack)
{
// compare current haystack to needle
size_t i = 0;
while (needle[i])
{
if (haystack[i] != needle[i])
break;
i++;
}
// needle fully matched (reached terminating NUL-byte)
if (!needle[i])
return haystack;
// no match, step forward
haystack++;
}
// not found
return NULL;
}
/// naive approach (for non-text data)
const char* searchSimple(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// match impossible if less than needleLength bytes left
haystackLength -= needleLength - 1;
// points beyond last considered start byte
const char* haystackEnd = haystack + haystackLength;
// until the end of haystack (or a match, of course ...)
while (haystack != haystackEnd)
{
// compare current haystack to needle
size_t i = 0;
for (; i < needleLength; i++)
if (haystack[i] != needle[i])
break;
// needle fully matched
if (i == needleLength)
return haystack;
// no match, step forward
haystack++;
}
// not found
return NULL;
}
// //////////////////////////////////////////////////////////
/// Knuth-Morris-Pratt algorithm (for C strings)
const char* searchKnuthMorrisPrattString(const char* haystack, const char* needle)
{
// detect invalid input
if (!haystack || !needle)
return NULL;
// empty needle matches everything
size_t needleLength = strlen(needle);
if (needleLength == 0)
return haystack;
// try to use stack instead of heap (avoid slow memory allocations if possible)
const size_t MaxLocalMemory = 256;
int localMemory[MaxLocalMemory];
int* skip = localMemory;
// stack too small => allocate heap
if (needleLength > MaxLocalMemory)
{
skip = (int*)malloc(needleLength * sizeof(int));
if (skip == NULL)
return NULL;
}
// prepare skip table
skip[0] = -1;
int i;
for (i = 0; needle[i]; i++)
{
skip[i + 1] = skip[i] + 1;
while (skip[i + 1] > 0 && needle[i] != needle[skip[i + 1] - 1])
skip[i + 1] = skip[skip[i + 1] - 1] + 1;
}
// assume no match
const char* result = NULL;
int shift = 0;
// search
while (*haystack)
{
// look for a matching character
while (shift >= 0 && *haystack != needle[shift])
shift = skip[shift];
// single step forward in needle and haystack
haystack++;
shift++;
// reached end of needle => hit
if (!needle[shift])
{
result = haystack - shift;
break;
}
}
// clean up heap (if used)
if (skip != localMemory)
free(skip);
// points to match position or NULL if not found
return result;
}
/// Knuth-Morris-Pratt algorithm (for non-text data)
const char* searchKnuthMorrisPratt(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// empty needle matches everything
if (needleLength == 0)
return haystack;
// try to use stack instead of heap (avoid slow memory allocations if possible)
const size_t MaxLocalMemory = 256;
int localMemory[MaxLocalMemory];
int* skip = localMemory;
// stack too small => allocate heap
if (needleLength > MaxLocalMemory)
{
skip = (int*)malloc(needleLength * sizeof(int));
if (skip == NULL)
return NULL;
}
// prepare skip table
skip[0] = -1;
size_t i;
for (i = 0; i < needleLength; i++)
{
skip[i + 1] = skip[i] + 1;
while (skip[i + 1] > 0 && needle[i] != needle[skip[i + 1] - 1])
skip[i + 1] = skip[skip[i + 1]-1] + 1;
}
// assume no match
const char* result = NULL;
const char* haystackEnd = haystack + haystackLength;
int shift = 0;
// search
while (haystack != haystackEnd)
{
// look for a matching character
while (shift >= 0 && *haystack != needle[shift])
shift = skip[shift];
// single step forward in needle and haystack
haystack++;
shift++;
// reached end of needle => hit
if ((size_t)shift == needleLength)
{
result = haystack - shift;
break;
}
}
// clean up heap (if used)
if (skip != localMemory)
free(skip);
// points to match position or NULL if not found
return result;
}
// //////////////////////////////////////////////////////////
/// Boyer-Moore-Horspool algorithm (for C strings)
const char* searchBoyerMooreHorspoolString(const char* haystack, const char* needle)
{
// detect invalid input
if (!haystack || !needle)
return NULL;
// call routine for non-text data
return searchBoyerMooreHorspool(haystack, strlen(haystack), needle, strlen(needle));
}
/// Boyer-Moore-Horspool algorithm (for non-text data)
const char* searchBoyerMooreHorspool(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// empty needle matches everything
if (needleLength == 0)
return haystack;
// find right-most position of each character
// and store its distance to the end of needle
// default value: when a character in haystack isn't in needle, then
// we can jump forward needleLength bytes
const size_t NumChar = 1 << (8 * sizeof(char));
size_t skip[NumChar];
size_t i;
for (i = 0; i < NumChar; i++)
skip[i] = needleLength;
// figure out for each character of the needle how much we can skip
// (if a character appears multiple times in needle, later occurrences
// overwrite previous ones, i.e. the value of skip[x] decreases)
const size_t lastPos = needleLength - 1;
size_t pos;
for (pos = 0; pos < lastPos; pos++)
skip[(unsigned char)needle[pos]] = lastPos - pos;
// now walk through the haystack
while (haystackLength >= needleLength)
{
// all characters match ?
for (i = lastPos; haystack[i] == needle[i]; i--)
if (i == 0)
return haystack;
// no match, jump ahead
unsigned char marker = (unsigned char) haystack[lastPos];
haystackLength -= skip[marker];
haystack += skip[marker];
}
// needle not found in haystack
return NULL;
}
// //////////////////////////////////////////////////////////
/// Bitap algorithm / Baeza-Yates-Gonnet algorithm (for C strings)
const char* searchBitapString(const char* haystack, const char* needle)
{
// detect invalid input
if (!haystack || !needle)
return NULL;
// empty needle matches everything
size_t needleLength = strlen(needle);
if (needleLength == 0)
return haystack;
// create bit masks for each possible byte / ASCII character
// each mask is as wide as needleLength
const size_t MaxBitWidth = 8 * sizeof(int) - 1;
// only if needleLength bits fit into an integer (minus 1), the algorithm will be fast
if (needleLength > MaxBitWidth)
return searchSimpleString(haystack, needle);
// one mask per allowed character (1 byte => 2^8 => 256)
// where all bits are set except those where the character is found in needle
const size_t AlphabetSize = 256;
unsigned int masks[AlphabetSize];
size_t i;
for (i = 0; i < AlphabetSize; i++)
masks[i] = ~0;
for (i = 0; i < needleLength; i++)
masks[(unsigned char)needle[i]] &= ~(1 << i);
// initial state mask has all bits set except the lowest one
unsigned int state = ~1;
const unsigned int FullMatch = 1 << needleLength;
while (*haystack)
{
// update the bit array
state |= masks[(unsigned char)*haystack];
state <<= 1;
// if an unset bit "bubbled up" we have a match
if ((state & FullMatch) == 0)
return (haystack - needleLength) + 1;
haystack++;
}
// needle not found in haystack
return NULL;
}
/// Bitap algorithm / Baeza-Yates-Gonnet algorithm (for C strings)
const char* searchBitap(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// empty needle matches everything
if (needleLength == 0)
return haystack;
// create bit masks for each possible byte / ASCII character
// each mask is as wide as needleLength
const size_t MaxBitWidth = 8 * sizeof(int) - 1;
// only if needleLength bits fit into an integer (minus 1), the algorithm will be fast
if (needleLength > MaxBitWidth)
return searchNative(haystack, haystackLength, needle, needleLength);
// one mask per allowed character (1 byte => 2^8 => 256)
// where all bits are set except those where the character is found in needle
const size_t AlphabetSize = 256;
unsigned int masks[AlphabetSize];
size_t i;
for (i = 0; i < AlphabetSize; i++)
masks[i] = ~0;
for (i = 0; i < needleLength; i++)
masks[(unsigned char)needle[i]] &= ~(1 << i);
// points beyond last considered byte
const char* haystackEnd = haystack + haystackLength;
// initial state mask has all bits set except the lowest one
unsigned int state = ~1;
const unsigned int FullMatch = 1 << needleLength;
while (haystack != haystackEnd)
{
// update the bit array
state |= masks[(unsigned char)*haystack];
state <<= 1;
// if an unset bit "bubbled up" we have a match
if ((state & FullMatch) == 0)
return (haystack - needleLength) + 1;
haystack++;
}
// needle not found in haystack
return NULL;
}
// //////////////////////////////////////////////////////////
/// Rabin-Karp algorithm
/** based on simple hash proposed by Raphael Javaux **/
const char* searchRabinKarpString(const char* haystack, const char* needle)
{
// detect invalid input
if (!haystack || !needle)
return NULL;
// empty needle matches everything
if (!*needle)
return haystack;
// find first match of the first letter
haystack = strchr(haystack, *needle);
if (!haystack)
return NULL;
// now first letter of haystack and needle is identical
// let's compute the sum of all characters of needle
unsigned int hashNeedle = *needle;
unsigned int hashHaystack = *haystack;
const char* scanNeedle = needle + 1;
const char* scanHaystack = haystack + 1;
while (*scanNeedle && *scanHaystack)
{
hashNeedle += *scanNeedle++;
hashHaystack += *scanHaystack++;
}
// if scanNeedle doesn't point to zero, then we have too little haystack
if (*scanNeedle)
return NULL;
// length of needle
const size_t needleLength = scanNeedle - needle;
// walk through haystack and roll the hash
for (;;)
{
// identical hash ?
if (hashHaystack == hashNeedle)
{
// can be a false positive, therefore must check all characters again
if (memcmp(haystack, needle, needleLength) == 0)
return haystack;
}
// no more bytes left ?
if (!*scanHaystack)
break;
// update hash
hashHaystack -= *haystack++;
hashHaystack += *scanHaystack++;
}
// needle not found in haystack
return NULL;
}
/// Rabin-Karp algorithm
/** based on simple hash proposed by Raphael Javaux **/
const char* searchRabinKarp(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// empty needle matches everything
if (needleLength == 0)
return haystack;
// one byte beyond last position where a match can begin
const char* haystackEnd = haystack + haystackLength - needleLength + 1;
// find first match of the first letter
haystack = (const char*)memchr(haystack, *needle, haystackLength);
if (!haystack)
return NULL;
// now first letter of haystack and needle is identical
// let's compute the sum of all characters of needle
unsigned int hashNeedle = 0;
unsigned int hashHaystack = 0;
size_t i;
for (i = 0; i < needleLength; i++)
{
// not enough haystack left ?
if (!haystack[i])
return NULL;
hashNeedle += needle [i];
hashHaystack += haystack[i];
}
// walk through haystack and roll the hash computation
while (haystack != haystackEnd)
{
// identical hash ?
if (hashHaystack == hashNeedle)
{
// can be a false positive, therefore must check all characters again
if (memcmp(haystack, needle, needleLength) == 0)
return haystack;
}
// update hash
hashHaystack += *(haystack + needleLength);
hashHaystack -= *haystack++;
}
// needle not found in haystack
return NULL;
}
// //////////////////////////////////////////////////////////
/// super-fast for short strings (less than about 8 bytes), else use searchSimple or searchBoyerMooreHorspool
const char* searchNative(const char* haystack, size_t haystackLength,
const char* needle, size_t needleLength)
{
// uses memchr() for the first byte, then memcmp to verify it's a valid match
// detect invalid input
if (!haystack || !needle || haystackLength < needleLength)
return NULL;
// empty needle matches everything
if (needleLength == 0)
return haystack;
// shorter code for just one character
if (needleLength == 1)
return (const char*)memchr(haystack, *needle, haystackLength);
haystackLength -= needleLength - 1;
// points beyond last considered byte
const char* haystackEnd = haystack + haystackLength;
// look for first byte
while ((haystack = (const char*)memchr(haystack, *needle, haystackLength)) != NULL)
{
// does last byte match, too ?
if (haystack[needleLength - 1] == needle[needleLength - 1])
// okay, perform full comparison, skip first and last byte (if just 2 bytes => already finished)
if (needleLength == 2 || memcmp(haystack + 1, needle + 1, needleLength - 2) == 0)
return haystack;
// compute number of remaining bytes
haystackLength = haystackEnd - haystack;
if (haystackLength == 0)
return NULL;
// keep going
haystack++;
haystackLength--;
}
// needle not found in haystack
return NULL;
}