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
|
/* Boyer-Moore string search as found on the internet using Google */
#include <pthread.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "bm.h"
#define MAX(a,b) ((a) < (b) ? (b) : (a))
static void
preBmBc(unsigned char *x,
int m,
int bmBc[])
{
int i;
for (i = 0; i < BM_ASIZE; ++i)
bmBc[i] = m;
for (i = 0; i < m - 1; ++i)
bmBc[x[i]] = m - i - 1;
}
static void
suffixes(unsigned char *x,
int m,
int *suff)
{
int f, g, i;
f = 0;
suff[m - 1] = m;
g = m - 1;
for (i = m - 2; i >= 0; --i)
{
if (i > g && suff[i + m - 1 - f] < i - g)
suff[i] = suff[i + m - 1 - f];
else
{
if (i < g)
g = i;
f = i;
while (g >= 0 && x[g] == x[g + m - 1 - f])
--g;
suff[i] = f - g;
}
}
}
static int
preBmGs(unsigned char *x, int m, int bmGs[])
{
int i, j, *suff;
suff = (int *) calloc(sizeof(int), m);
if (suff == NULL)
return -1;
suffixes(x, m, suff);
for (i = 0; i < m; ++i)
bmGs[i] = m;
j = 0;
for (i = m - 1; i >= -1; --i)
if (i == -1 || suff[i] == i + 1)
for (; j < m - 1 - i; ++j)
if (bmGs[j] == m)
bmGs[j] = m - 1 - i;
for (i = 0; i <= m - 2; ++i)
bmGs[m - 1 - suff[i]] = m - 1 - i;
free(suff);
return 0;
}
int
bm_init(BM *bmp,
unsigned char *x,
int m,
int icase)
{
int i;
memset(bmp, 0, sizeof(bmp));
bmp->icase = icase;
bmp->bmGs = (int *) calloc(sizeof(int), m);
if (bmp->bmGs == NULL)
return -1;
bmp->saved_m = m;
bmp->saved_x = (unsigned char *) malloc(m);
if (bmp->saved_x == NULL)
return -2;
for (i = 0; i < m; i++)
bmp->saved_x[i] = icase ? tolower(x[i]) : x[i];
/* Preprocessing */
if (preBmGs(bmp->saved_x, m, bmp->bmGs) < 0)
return -3;
preBmBc((unsigned char *) bmp->saved_x, m, bmp->bmBc);
return 0;
}
void
bm_destroy(BM *bmp)
{
if (bmp->bmGs)
free(bmp->bmGs);
if (bmp->saved_x)
free(bmp->saved_x);
}
/* Search for matches
**
** If mfun is defined, then call this function for each match.
** If mfun returns anything else but 0 abort the search. If the
** returned value is < 0 then return this value, else return the
** number of matches (so far).
**
** If mfun is NULL then stop at first match and return the position
*/
int
bm_search(BM *bmp,
unsigned char *y,
int n,
int (*mfun)(void *buf, int n, int pos))
{
int i, j, c;
int nm = 0;
/* Searching */
j = 0;
while (j <= n - bmp->saved_m)
{
for (i = bmp->saved_m - 1;
i >= 0 && bmp->saved_x[i] == (bmp->icase ? tolower(y[i + j]) : y[i + j]);
--i)
;
if (i < 0)
{
if (mfun)
{
++nm;
c = mfun(y, n, j);
if (c)
return (c < 0 ? c : nm);
j += bmp->bmGs[0];
}
else
return j;
}
else
{
unsigned char c = (bmp->icase ? tolower(y[i + j]) : y[i + j]);
j += MAX(bmp->bmGs[i], bmp->bmBc[c] - bmp->saved_m + 1 + i);
}
}
return mfun == NULL ? -1 : nm;
}
#if 0
int
main(int argc,
char *argv[])
{
int pos;
bm_setup(argv[1], strlen(argv[1]));
pos = bm_search(argv[2], strlen(argv[2]));
printf("Match at pos %d\n", pos);
exit(0);
}
#endif
|