-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.c
63 lines (53 loc) · 2.17 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
/*
+---------------------------------------------------------------------+
| php_templates |
+---------------------------------------------------------------------+
| Copyright (C) 2001, 2002 Maxim Poltarak |
+---------------------------------------------------------------------+
| This library is free software; you can redistribute it and/or |
| modify it under the terms of the version 2.1 of the GNU Lesser |
| General Public License as published by the Free Software Foundation.|
+---------------------------------------------------------------------+
| This library 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 |
| Lesser General Public License for more details. |
+---------------------------------------------------------------------+
| You should have received a copy of the GNU Lesser General Public |
| License along with this library; if not, write to the Free Software |
| Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| MA 02111-1307 USA |
+---------------------------------------------------------------------+
*/
#include <stdlib.h>
#include "search.h"
/* Chartable size */
#define ASIZE 256
static void preQsBc(uchar *x, ulong m, ulong qsBc[]) {
register ulong i;
for(i=0; i < ASIZE; ++i) qsBc[i] = m + 1;
for(i=0; i < m; ++i) qsBc[x[i]] = m - i;
}
/*
y - haystack
n - haystackLen
x - needle
m - needleLen
*/
unsigned char* search_qs(unsigned char* y, unsigned long n, unsigned char* x, unsigned long m) {
register ulong j;
register ulong cmp;
ulong qsBc[ASIZE];
if(n < m) return NULL;
preQsBc(x, m, qsBc);
j = 0;
while(j <= n-m) {
/* if(memcmp(x, y+j, m-1) == 0) return y + j; */
/* case insensitive search */
for(cmp = 0; cmp < m; cmp++)
if(tolower(x[cmp]) != tolower(y[j+cmp])) break;
if(cmp == m) return y+j;
j += qsBc[ y[j+m] ];
}
return NULL;
}