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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
|
//
// StringMatch.cc
//
// StringMatch: This class provides an interface to a fairly specialized string
// lookup facility. It is intended to be used as a replace for any
// regualr expression matching when the pattern string is in the form:
//
// <string1>|<string2>|<string3>|...
//
// Just like regular expression routines, the pattern needs to be
// compiled before it can be used. This is done using the Pattern()
// member function. Once the pattern has been compiled, the member
// function Find() can be used to search for the pattern in a string.
// If a string has been found, the "which" and "length" parameters
// will be set to the string index and string length respectively.
// (The string index is counted starting from 0) The return value of
// Find() is the position at which the string was found or -1 if no
// strings could be found. If a case insensitive match needs to be
// performed, call the IgnoreCase() member function before calling
// Pattern(). This function will setup a character translation table
// which will convert all uppercase characters to lowercase. If some
// other translation is required, the TranslationTable() member
// function can be called to provide a custom table. This table needs
// to be 256 characters.
//
// Part of the ht://Dig package <http://www.htdig.org/>
// Copyright (c) 1999-2004 The ht://Dig Group
// For copyright details, see the file COPYING in your distribution
// or the GNU Library General Public License (LGPL) version 2 or later
// <http://www.gnu.org/copyleft/lgpl.html>
//
// $Id: StringMatch.cc,v 1.18 2004/05/28 13:15:21 lha Exp $
//
#ifdef HAVE_CONFIG_H
#include "htconfig.h"
#endif /* HAVE_CONFIG_H */
#include "StringMatch.h"
#include <string.h>
#include <ctype.h>
#ifdef HAVE_STD
#include <fstream>
#ifdef HAVE_NAMESPACES
using namespace std;
#endif
#else
#include <fstream.h>
#endif /* HAVE_STD */
//
// Entries in the state table can either be normal or final.
// Final states have an match index encoded in them. This number
// is shifted left by INDEX_SHIFT bits.
//
#define MATCH_INDEX_MASK 0xffff0000
#define STATE_MASK 0x0000ffff
#define INDEX_SHIFT 16
//*****************************************************************************
// StringMatch::StringMatch()
//
StringMatch::StringMatch()
{
//
// Clear out the state table pointers
//
for (int i = 0; i < 256; i++)
table[i] = 0;
local_alloc = 0;
trans = 0;
}
//*****************************************************************************
// StringMatch::~StringMatch()
//
StringMatch::~StringMatch()
{
for (int i = 0; i < 256; i++)
delete [] table[i];
if (local_alloc)
delete [] trans;
}
//*****************************************************************************
// void StringMatch::Pattern(char *pattern)
// Compile the given pattern into a state transition table
//
void
StringMatch::Pattern(char *pattern, char sep)
{
if (!pattern || !*pattern)
{
//
// No pattern to compile...
//
return;
}
//
// Allocate enough space in the state table to hold the worst case
// patterns...
//
int n = strlen(pattern);
// ...but since the state table does not need an extra state
// for each string in the pattern, we can subtract the number
// of separators. Wins for small but numerous strings in
// the pattern.
char *tmpstr;
for (tmpstr = pattern;
(tmpstr = strchr(tmpstr, sep)) != NULL;
tmpstr++) // Pass the separator.
n--;
int i;
for (i = 0; i < 256; i++)
{
table[i] = new int[n];
memset((unsigned char *) table[i], 0, n * sizeof(int));
}
for (i = 0; i < n; i++)
table[0][i] = i; // "no-op" states for null char, to be ignored
//
// Set up a standard case translation table if needed.
//
if (!trans)
{
trans = new unsigned char[256];
for (i = 0; i < 256; i++)
{
trans[i] = (unsigned char)i;
}
local_alloc = 1;
}
//
// Go though each of the patterns and build entries in the table.
//
int state = 0;
int totalStates = 0;
unsigned char previous = 0;
int previousState = 0;
int previousValue = 0;
int index = 1;
unsigned char chr;
while ((unsigned char)*pattern)
{
#if 0
if (totalStates > n)
{
cerr << "Fatal! Miscalculation of number of states"
<< endl;
exit (2);
}
#endif
chr = trans[(unsigned char)*pattern];
if (chr == 0)
{
pattern++;
continue;
}
if (chr == sep)
{
//
// Next pattern
//
table[previous][previousState] =
previousValue | (index << INDEX_SHIFT);
index++;
state = 0;
// totalStates--;
}
else
{
previousValue = table[chr][state];
previousState = state;
if (previousValue)
{
if (previousValue & MATCH_INDEX_MASK)
{
if (previousValue & STATE_MASK)
{
state = previousValue & STATE_MASK;
}
else
{
table[chr][state] |= ++totalStates;
state = totalStates;
}
}
else
{
state = previousValue & STATE_MASK;
}
}
else
{
table[chr][state] = ++totalStates;
state = totalStates;
}
}
previous = chr;
pattern++;
}
table[previous][previousState] =
previousValue | (index << INDEX_SHIFT);
}
//*****************************************************************************
// int StringMatch::FindFirst(const char *string, int &which, int &length)
// Attempt to find the first occurance of the previous compiled patterns.
//
int StringMatch::FindFirst(const char *string, int &which, int &length)
{
which = -1;
length = -1;
if (!table[0])
return 0;
int state = 0, new_state = 0;
int pos = 0;
int start_pos = 0;
while ((unsigned char)string[pos])
{
new_state = table[trans[(unsigned char)string[pos] & 0xff]][state];
if (new_state)
{
if (state == 0)
{
//
// Keep track of where we started comparing so that we can
// come back to this point later if we didn't match anything
//
start_pos = pos;
}
}
else
{
//
// We came back to 0 state. This means we didn't match anything.
//
if (state)
{
// But we may already have a match, and are just being greedy.
if (which != -1)
return start_pos;
pos = start_pos + 1;
}
else
pos++;
state = 0;
continue;
}
state = new_state;
if (state & MATCH_INDEX_MASK)
{
//
// Matched one of the patterns.
// Determine which and return.
//
which = ((unsigned int) (state & MATCH_INDEX_MASK)
>> INDEX_SHIFT) - 1;
length = pos - start_pos + 1;
state &= STATE_MASK;
// Continue to find the longest, if there is one.
if (state == 0)
return start_pos;
}
pos++;
}
// Maybe we were too greedy.
if (which != -1)
return start_pos;
return -1;
}
//*****************************************************************************
// int StringMatch::Compare(const char *string, int &which, int &length)
//
int StringMatch::Compare(const char *string, int &which, int &length)
{
which = -1;
length = -1;
if (!table[0])
return 0;
int state = 0, new_state = 0;
int pos = 0;
int start_pos = 0;
//
// Skip to at least the start of a word.
//
while ((unsigned char)string[pos])
{
new_state = table[trans[string[pos]]][state];
if (new_state)
{
if (state == 0)
{
start_pos = pos;
}
}
else
{
// We may already have a match, and are just being greedy.
if (which != -1)
return 1;
return 0;
}
state = new_state;
if (state & MATCH_INDEX_MASK)
{
//
// Matched one of the patterns.
//
which = ((unsigned int) (state & MATCH_INDEX_MASK)
>> INDEX_SHIFT) - 1;
length = pos - start_pos + 1;
// Continue to find the longest, if there is one.
state &= STATE_MASK;
if (state == 0)
return 1;
}
pos++;
}
// Maybe we were too greedy.
if (which != -1)
return 1;
return 0;
}
//*****************************************************************************
// int StringMatch::FindFirstWord(char *string)
//
int StringMatch::FindFirstWord(const char *string)
{
int dummy;
return FindFirstWord(string, dummy, dummy);
}
//*****************************************************************************
// int StringMatch::CompareWord(const char *string)
//
int StringMatch::CompareWord(const char *string)
{
int dummy;
return CompareWord(string, dummy, dummy);
}
//*****************************************************************************
// int StringMatch::FindFirstWord(char *string, int &which, int &length)
// Attempt to find the first occurance of the previous compiled patterns.
//
int StringMatch::FindFirstWord(const char *string, int &which, int &length)
{
which = -1;
length = -1;
int state = 0, new_state = 0;
int pos = 0;
int start_pos = 0;
int is_word = 1;
//
// Skip to at least the start of a word.
//
while ((unsigned char)string[pos])
{
new_state = table[trans[(unsigned char)string[pos]]][state];
if (new_state)
{
if (state == 0)
{
start_pos = pos;
}
}
else
{
//
// We came back to 0 state. This means we didn't match anything.
//
if (state)
{
pos = start_pos + 1;
}
else
pos++;
state = 0;
continue;
}
state = new_state;
if (state & MATCH_INDEX_MASK)
{
//
// Matched one of the patterns.
//
is_word = 1;
if (start_pos != 0)
{
if (HtIsStrictWordChar((unsigned char)string[start_pos - 1]))
is_word = 0;
}
if (HtIsStrictWordChar((unsigned char)string[pos + 1]))
is_word = 0;
if (is_word)
{
//
// Determine which and return.
//
which = ((unsigned int) (state & MATCH_INDEX_MASK)
>> INDEX_SHIFT) - 1;
length = pos - start_pos + 1;
return start_pos;
}
else
{
//
// Not at the end of word. Continue searching.
//
if (state & STATE_MASK)
{
state &= STATE_MASK;
}
else
{
pos = start_pos + 1;
state = 0;
}
}
}
pos++;
}
return -1;
}
//*****************************************************************************
// int StringMatch::CompareWord(const char *string, int &which, int &length)
//
int StringMatch::CompareWord(const char *string, int &which, int &length)
{
which = -1;
length = -1;
if (!table[0])
return 0;
int state = 0;
int position = 0;
//
// Skip to at least the start of a word.
//
while ((unsigned char)string[position])
{
state = table[trans[(unsigned char)string[position]]][state];
if (state == 0)
{
return 0;
}
if (state & MATCH_INDEX_MASK)
{
//
// Matched one of the patterns. See if it is a word.
//
int isWord = 1;
if ((unsigned char)string[position + 1])
{
if (HtIsStrictWordChar((unsigned char)string[position + 1]))
isWord = 0;
}
if (isWord)
{
which = ((unsigned int) (state & MATCH_INDEX_MASK)
>> INDEX_SHIFT) - 1;
length = position + 1;
return 1;
}
else
{
//
// Not at the end of a word. Continue searching.
//
if ((state & STATE_MASK) != 0)
{
state &= STATE_MASK;
}
else
{
return 0;
}
}
}
position++;
}
return 0;
}
//*****************************************************************************
// void StringMatch::TranslationTable(char *table)
//
void StringMatch::TranslationTable(char *table)
{
if (local_alloc)
delete [] trans;
trans = (unsigned char *) table;
local_alloc = 0;
}
//*****************************************************************************
// void StringMatch::IgnoreCase()
// Set up the case translation table to convert uppercase to lowercase
//
void StringMatch::IgnoreCase()
{
if (!local_alloc || !trans)
{
trans = new unsigned char[256];
for (int i = 0; i < 256; i++)
trans[i] = (unsigned char)i;
local_alloc = 1;
}
for (int i = 0; i < 256; i++)
if (isupper((unsigned char)i))
trans[i] = tolower((unsigned char)i);
}
//*****************************************************************************
// void StringMatch::IgnorePunct(char *punct)
// Set up the character translation table to ignore punctuation
//
void StringMatch::IgnorePunct(char *punct)
{
if (!local_alloc || !trans)
{
trans = new unsigned char[256];
for (int i = 0; i < 256; i++)
trans[i] = (unsigned char)i;
local_alloc = 1;
}
if (punct)
for (int i = 0; punct[i]; i++)
trans[(unsigned char)punct[i]] = 0;
else
for (int i = 0; i < 256; i++)
if (HtIsWordChar(i) && !HtIsStrictWordChar(i))
trans[i] = 0;
}
//*****************************************************************************
// int StringMatch::FindFirst(const char *source)
//
int StringMatch::FindFirst(const char *source)
{
int dummy;
return FindFirst(source, dummy, dummy);
}
//*****************************************************************************
// int StringMatch::Compare(const char *source)
//
int StringMatch::Compare(const char *source)
{
int dummy;
return Compare(source, dummy, dummy);
}
|