2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1995-10-09 16:18:32 adam
8 * Function dict_lookup_grep got extra client data parameter.
10 * Revision 1.5 1995/09/14 11:52:59 adam
11 * Grep handle function parameter info is const now.
13 * Revision 1.4 1995/01/24 16:01:02 adam
14 * Added -ansi to CFLAGS.
15 * Use new API of dfa module.
17 * Revision 1.3 1994/10/05 12:16:50 adam
18 * Pagesize is a resource now.
20 * Revision 1.2 1994/10/04 12:08:07 adam
21 * Some bug fixes and some optimizations.
23 * Revision 1.1 1994/10/03 17:23:04 adam
24 * First version of dictionary lookup with regular expressions and errors.
36 typedef unsigned MatchWord;
38 #define MAX_LENGTH 1024
41 int n; /* no of MatchWord needed */
42 int range; /* max no. of errors */
43 int fact; /* (range+1)*n */
44 MatchWord *match_mask; /* match_mask */
49 static INLINE void set_bit (MatchContext *mc, MatchWord *m, int ch, int state)
51 int off = state & (WORD_BITS-1);
52 int wno = state / WORD_BITS;
54 m[mc->n * ch + wno] |= 1<<off;
57 static INLINE MatchWord get_bit (MatchContext *mc, MatchWord *m, int ch,
60 int off = state & (WORD_BITS-1);
61 int wno = state / WORD_BITS;
63 return m[mc->n * ch + wno] & (1<<off);
66 static MatchContext *mk_MatchContext (struct DFA *dfa, int range)
68 MatchContext *mc = xmalloc (sizeof(*mc));
71 mc->n = (dfa->no_states+WORD_BITS) / WORD_BITS;
73 mc->fact = (range+1)*mc->n;
74 mc->match_mask = xcalloc (mc->n, sizeof(*mc->match_mask));
76 for (s = 0; s<dfa->no_states; s++)
77 if (dfa->states[s]->rule_no)
78 set_bit (mc, mc->match_mask, 0, s);
82 static void rm_MatchContext (MatchContext **mc)
84 xfree ((*mc)->match_mask);
89 static void mask_shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
90 struct DFA *dfa, int ch)
93 MatchWord *Rsrc_p = Rsrc, mask;
95 for (j = 0; j<mc->n; j++)
100 for (j = 0; j<WORD_BITS/4; j++)
106 struct DFA_state *state = dfa->states[s];
107 int i = state->tran_no;
109 if (ch >= state->trans[i].ch[0] &&
110 ch <= state->trans[i].ch[1])
111 set_bit (mc, Rdst, 0, state->trans[i].to);
115 struct DFA_state *state = dfa->states[s+1];
116 int i = state->tran_no;
118 if (ch >= state->trans[i].ch[0] &&
119 ch <= state->trans[i].ch[1])
120 set_bit (mc, Rdst, 0, state->trans[i].to);
124 struct DFA_state *state = dfa->states[s+2];
125 int i = state->tran_no;
127 if (ch >= state->trans[i].ch[0] &&
128 ch <= state->trans[i].ch[1])
129 set_bit (mc, Rdst, 0, state->trans[i].to);
133 struct DFA_state *state = dfa->states[s+3];
134 int i = state->tran_no;
136 if (ch >= state->trans[i].ch[0] &&
137 ch <= state->trans[i].ch[1])
138 set_bit (mc, Rdst, 0, state->trans[i].to);
142 if (s >= dfa->no_states)
149 static void shift (MatchContext *mc, MatchWord *Rdst, MatchWord *Rsrc,
153 MatchWord *Rsrc_p = Rsrc, mask;
154 for (j = 0; j<mc->n; j++)
159 for (j = 0; j<WORD_BITS/4; j++)
165 struct DFA_state *state = dfa->states[s];
166 int i = state->tran_no;
168 set_bit (mc, Rdst, 0, state->trans[i].to);
172 struct DFA_state *state = dfa->states[s+1];
173 int i = state->tran_no;
175 set_bit (mc, Rdst, 0, state->trans[i].to);
179 struct DFA_state *state = dfa->states[s+2];
180 int i = state->tran_no;
182 set_bit (mc, Rdst, 0, state->trans[i].to);
186 struct DFA_state *state = dfa->states[s+3];
187 int i = state->tran_no;
189 set_bit (mc, Rdst, 0, state->trans[i].to);
193 if (s >= dfa->no_states)
200 static void or (MatchContext *mc, MatchWord *Rdst,
201 MatchWord *Rsrc1, MatchWord *Rsrc2)
204 for (i = 0; i<mc->n; i++)
205 Rdst[i] = Rsrc1[i] | Rsrc2[i];
208 static INLINE int move (MatchContext *mc, MatchWord *Rj1, MatchWord *Rj,
209 Dict_char ch, struct DFA *dfa, MatchWord *Rtmp)
212 MatchWord *Rtmp_2 = Rtmp + mc->n;
214 mask_shift (mc, Rj1, Rj, dfa, ch);
215 for (d = 1; d <= mc->range; d++)
217 or (mc, Rtmp, Rj, Rj1); /* 2,3 */
219 shift (mc, Rtmp_2, Rtmp, dfa);
221 mask_shift (mc, Rtmp, Rj+mc->n, dfa, ch); /* 1 */
223 or (mc, Rtmp, Rtmp_2, Rtmp); /* 1,2,3*/
227 or (mc, Rj1, Rtmp, Rj); /* 1,2,3,4 */
236 static int dict_grep (Dict dict, Dict_ptr ptr, MatchContext *mc,
237 MatchWord *Rj, int pos, void *client,
238 int (*userfunc)(Dict_char *, const char *, void *),
239 Dict_char *prefix, struct DFA *dfa)
246 dict_bf_readp (dict->dbf, ptr, &p);
248 hi = DICT_nodir(p)-1;
249 indxp = (short*) ((char*) p+DICT_pagesize(dict)-sizeof(short));
255 /* string (Dict_char *) DICT_EOS terminated */
256 /* unsigned char length of information */
257 /* char * information */
260 info = (char*)p + indxp[-lo];
264 MatchWord *Rj0 = Rj + j *mc->fact;
265 MatchWord *Rj1 = Rj + (j+1)*mc->fact;
266 MatchWord *Rj_tmp = Rj + (j+2)*mc->fact;
268 memcpy (&ch, info+j*sizeof(Dict_char), sizeof(Dict_char));
273 (*userfunc)(prefix, info+(j+1)*sizeof(Dict_char),
277 move (mc, Rj1, Rj0, ch, dfa, Rj_tmp);
278 for (d = mc->n; --d >= 0; )
279 if (Rj1[mc->range*mc->n + d])
284 for (d = mc->n; --d >= 0; )
285 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
294 MatchWord *Rj1 = Rj+ mc->fact;
295 MatchWord *Rj_tmp = Rj+2*mc->fact;
298 /* Dict_ptr subptr */
299 /* Dict_char sub char */
300 /* unsigned char length of information */
301 /* char * information */
302 info = (char*)p - indxp[-lo];
303 memcpy (&ch, info+sizeof(Dict_ptr), sizeof(Dict_char));
306 move (mc, Rj1, Rj, ch, dfa, Rj_tmp);
307 for (d = mc->n; --d >= 0; )
308 if (Rj1[mc->range*mc->n + d])
313 if (info[sizeof(Dict_ptr)+sizeof(Dict_char)])
315 for (d = mc->n; --d >= 0; )
316 if (Rj1[mc->range*mc->n + d] & mc->match_mask[d])
318 prefix[pos+1] = DICT_EOS;
319 (*userfunc)(prefix, info+sizeof(Dict_ptr)+
320 sizeof(Dict_char), client);
324 memcpy (&subptr, info, sizeof(Dict_ptr));
327 dict_grep (dict, subptr, mc, Rj1, pos+1,
328 client, userfunc, prefix, dfa);
329 dict_bf_readp (dict->dbf, ptr, &p);
330 indxp = (short*) ((char*) p+DICT_pagesize(dict)
340 int dict_lookup_grep (Dict dict, Dict_char *pattern, int range, void *client,
341 int (*userfunc)(Dict_char *name, const char *info,
345 Dict_char prefix[MAX_LENGTH+1];
346 char *this_pattern = pattern;
348 struct DFA *dfa = dfa_init();
351 i = dfa_parse (dfa, &this_pattern);
352 if (i || *this_pattern)
359 mc = mk_MatchContext (dfa, range);
361 Rj = xcalloc ((MAX_LENGTH+1) * mc->n, sizeof(*Rj));
363 set_bit (mc, Rj, 0, 0);
364 for (d = 1; d<=mc->range; d++)
367 memcpy (Rj + mc->n * d, Rj + mc->n * (d-1), mc->n * sizeof(*Rj));
368 for (s = 0; s<dfa->no_states; s++)
370 if (get_bit (mc, Rj, d-1, s))
372 struct DFA_state *state = dfa->states[s];
373 int i = state->tran_no;
375 set_bit (mc, Rj, d, state->trans[i].to);
379 i = dict_grep (dict, 1, mc, Rj, 0, client, userfunc, prefix, dfa);
383 rm_MatchContext (&mc);