-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathTextbookBoyerMoore.cpp
473 lines (432 loc) · 15.8 KB
/
TextbookBoyerMoore.cpp
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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "ParserPch.h"
namespace UnifiedRegex
{
template <typename C>
void TextbookBoyerMooreSetup<C>::Init()
{
Assert(patLen > 0);
for (uint i = 0; i < MaxCharMapLinearChars; i++)
{
lastOcc[i] = -1;
}
numLinearChars = 1;
// Always put the last character in the first index
linearChar[0] = pat[patLen - 1];
for (CharCount i = 0; i < patLen; i++)
{
if (numLinearChars <= MaxCharMapLinearChars)
{
uint j = 0;
for (; j < numLinearChars; j++)
{
if (linearChar[j] == pat[i])
{
lastOcc[j] = i;
break;
}
}
if (j == numLinearChars)
{
if (numLinearChars < MaxCharMapLinearChars)
{
linearChar[numLinearChars] = pat[i];
lastOcc[numLinearChars] = i;
}
numLinearChars++;
}
}
if (numLinearChars > MaxCharMapLinearChars)
{
break;
}
}
if (numLinearChars <= MaxCharMapLinearChars)
{
scheme = LinearScheme;
}
else
{
scheme = DefaultScheme;
}
}
template <typename C>
void TextbookBoyerMoore<C>::Setup(ArenaAllocator* allocator, TextbookBoyerMooreSetup<C> const& info)
{
Assert(info.GetScheme() == TextbookBoyerMooreSetup<C>::DefaultScheme);
this->Setup(allocator, info.pat, info.patLen, 1);
}
template <typename C>
void TextbookBoyerMoore<C>::Setup(ArenaAllocator * allocator, const Char * pat, CharCount patLen, int skip)
{
// character c |-> index of last occurrence of c in pat, otherwise -1
for (CharCount i = 0; i < patLen; i++)
{
for (int j = 0; j < skip; j++)
lastOccurrence.Set(allocator, pat[i * skip + j], i);
}
goodSuffix = TextbookBoyerMooreSetup<C>::GetGoodSuffix(allocator, pat, patLen, skip);
}
template <typename C>
int32 * TextbookBoyerMooreSetup<C>::GetGoodSuffix(ArenaAllocator* allocator, const Char * pat, CharCount patLen, int skip)
{
// pat offset q |-> longest prefix of pat which is a proper suffix of pat[0..q]
// (thanks to equivalence classes being in canonical order we only need to look at the first
// character of each skip grouping in the pattern)
int32* prefix = AnewArray(allocator, int32, patLen);
prefix[0] = 0;
int32 k = 0;
for (CharCount q = 1; q < patLen; q++)
{
while (k > 0 && pat[k * skip] != pat[q * skip])
k = prefix[k - 1];
if (pat[k * skip] == pat[q * skip])
k++;
prefix[q] = k;
}
// As above, but for rev(pat)
int32* revPrefix = AnewArray(allocator, int32, patLen);
revPrefix[0] = 0;
k = 0;
for (CharCount q = 1; q < patLen; q++)
{
while (k > 0 && pat[(patLen - k - 1) * skip] != pat[(patLen - q - 1) * skip])
k = revPrefix[k - 1];
if (pat[(patLen - k - 1) * skip] == pat[(patLen - q - 1) * skip])
k++;
revPrefix[q] = k;
}
// pat prefix length l |-> least shift s.t. pat[0..l-1] is not mismatched
int32 * goodSuffix = AnewArray(allocator, int32, patLen + 1);
for (CharCount j = 0; j <= patLen; j++)
goodSuffix[j] = patLen - prefix[patLen - 1];
for (CharCount l = 1; l <= patLen; l++)
{
CharCount j = patLen - revPrefix[l - 1];
int32 s = l - revPrefix[l - 1];
if (goodSuffix[j] > s)
goodSuffix[j] = s;
}
// shift above one to the left
for (CharCount j = 0; j < patLen; j++)
goodSuffix[j] = goodSuffix[j + 1];
AdeleteArray(allocator, patLen, prefix);
AdeleteArray(allocator, patLen, revPrefix);
return goodSuffix;
}
template <typename C>
void TextbookBoyerMoore<C>::FreeBody(ArenaAllocator* allocator, CharCount patLen)
{
if(goodSuffix)
{
AdeleteArray(allocator, patLen + 1, PointerValue(goodSuffix));
#if DBG
goodSuffix = nullptr;
#endif
}
lastOccurrence.FreeBody(allocator);
}
template <uint equivClassSize, uint compareCount>
static bool MatchPatternAt(uint inputChar, char16 const * pat, CharCount index);
template <>
bool MatchPatternAt<1, 1>(uint inputChar, char16 const* pat, CharCount index)
{
return inputChar == pat[index];
}
template <>
bool MatchPatternAt<CaseInsensitive::EquivClassSize, CaseInsensitive::EquivClassSize>(uint inputChar, char16 const * pat, CharCount index)
{
CompileAssert(CaseInsensitive::EquivClassSize == 4);
return inputChar == pat[index * CaseInsensitive::EquivClassSize]
|| inputChar == pat[index * CaseInsensitive::EquivClassSize + 1]
|| inputChar == pat[index * CaseInsensitive::EquivClassSize + 2]
|| inputChar == pat[index * CaseInsensitive::EquivClassSize + 3];
}
template <>
bool MatchPatternAt<CaseInsensitive::EquivClassSize, 1>(uint inputChar, char16 const * pat, CharCount index)
{
CompileAssert(CaseInsensitive::EquivClassSize == 4);
return inputChar == pat[index * 4];
}
template <typename C>
template <uint equivClassSize, uint lastPatCharEquivClass>
bool TextbookBoyerMoore<C>::Match
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const
{
Assert(input != 0);
Assert(inputOffset <= inputLength);
if (inputLength < patLen)
return false;
CharCount offset = inputOffset;
const CharCount endOffset = inputLength - (patLen - 1);
const int32* const localGoodSuffix = goodSuffix;
const LastOccMap* const localLastOccurrence = &lastOccurrence;
const CharCount lastPatCharIndex = (patLen - 1);
while (offset < endOffset)
{
// A separate tight loop to find the last character
while (true)
{
uint inputChar = Chars<Char>::CTU(input[offset + lastPatCharIndex]);
if (MatchPatternAt<equivClassSize, lastPatCharEquivClass>(inputChar, pat, lastPatCharIndex))
{
// Found a match. Break out of this loop and go to the match pattern loop
break;
}
// Negative case is more common,
// Write the checks so that we have a super tight loop
int lastOcc;
if (inputChar < localLastOccurrence->GetDirectMapSize())
{
if (!localLastOccurrence->IsInDirectMap(inputChar))
{
offset += patLen;
if (offset >= endOffset)
{
return false;
}
continue;
}
lastOcc = localLastOccurrence->GetDirectMap(inputChar);
}
else if (!localLastOccurrence->GetNonDirect(inputChar, lastOcc))
{
offset += patLen;
if (offset >= endOffset)
{
return false;
}
continue;
}
Assert((int)lastPatCharIndex - lastOcc >= localGoodSuffix[lastPatCharIndex]);
offset += lastPatCharIndex - lastOcc;
if (offset >= endOffset)
{
return false;
}
}
// CONSIDER: we can remove this check if we stop using TextbookBoyerMoore for one char pattern
if (lastPatCharIndex == 0)
{
inputOffset = offset;
return true;
}
// Match the rest of the pattern
int32 j = lastPatCharIndex - 1;
while (true)
{
#if ENABLE_REGEX_CONFIG_OPTIONS
if (stats != 0)
stats->numCompares++;
#endif
uint inputChar = Chars<Char>::CTU(input[offset + j]);
if (!MatchPatternAt<equivClassSize, equivClassSize>(inputChar, pat, j))
{
const int32 e = j - localLastOccurrence->Get((Char)inputChar);
offset += e > localGoodSuffix[j] ? e : localGoodSuffix[j];
break;
}
if (--j < 0)
{
inputOffset = offset;
return true;
}
}
}
return false;
}
// Specialized linear char map version
template <typename C>
void TextbookBoyerMooreWithLinearMap<C>::Setup(ArenaAllocator * allocator, TextbookBoyerMooreSetup<C> const& setup)
{
Assert(setup.GetScheme() == TextbookBoyerMooreSetup<C>::LinearScheme);
lastOccurrence.Set(setup.numLinearChars, setup.linearChar, setup.lastOcc);
goodSuffix = TextbookBoyerMooreSetup<C>::GetGoodSuffix(allocator, setup.pat, setup.patLen);
}
template <typename C>
void TextbookBoyerMooreWithLinearMap<C>::FreeBody(ArenaAllocator* allocator, CharCount patLen)
{
if(goodSuffix)
{
AdeleteArray(allocator, patLen + 1, goodSuffix);
#if DBG
goodSuffix = 0;
#endif
}
}
template <typename C>
template <uint equivClassSize>
bool TextbookBoyerMooreWithLinearMap<C>::Match
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const
{
CompileAssert(equivClassSize == 1);
Assert(input != 0);
Assert(inputOffset <= inputLength);
if (inputLength < patLen)
return false;
const int32* const localGoodSuffix = goodSuffix;
const LastOccMap* const localLastOccurrence = &lastOccurrence;
CharCount offset = inputOffset;
const CharCount lastPatCharIndex = (patLen - 1);
const CharCount endOffset = inputLength - lastPatCharIndex;
// Using int size instead of Char value is faster
const uint lastPatChar = pat[lastPatCharIndex];
Assert(lastPatChar == localLastOccurrence->GetChar(0));
while (offset < endOffset)
{
// A separate tight loop to find the last character
while (true)
{
#if ENABLE_REGEX_CONFIG_OPTIONS
if (stats != 0)
stats->numCompares++;
#endif
uint inputChar = Chars<Char>::CTU(input[offset + lastPatCharIndex]);
if (inputChar == lastPatChar)
{
// Found a match. Break out of this loop and go to the match pattern loop
break;
}
// Negative case is more common,
// Write the checks so that we have a super tight loop
Assert(inputChar != localLastOccurrence->GetChar(0));
int32 lastOcc;
if (localLastOccurrence->GetChar(1) != inputChar)
{
if (localLastOccurrence->GetChar(2) != inputChar)
{
if (localLastOccurrence->GetChar(3) != inputChar)
{
offset += patLen;
if (offset >= endOffset)
{
return false;
}
continue;
}
lastOcc = localLastOccurrence->GetLastOcc(3);
}
else
{
lastOcc = localLastOccurrence->GetLastOcc(2);
}
}
else
{
lastOcc = localLastOccurrence->GetLastOcc(1);
}
Assert((int)lastPatCharIndex - lastOcc >= localGoodSuffix[lastPatCharIndex]);
offset += lastPatCharIndex - lastOcc;
if (offset >= endOffset)
{
return false;
}
}
// CONSIDER: we can remove this check if we stop using
// TextbookBoyerMoore for one char pattern
if (lastPatCharIndex == 0)
{
inputOffset = offset;
return true;
}
// Match the rest of the pattern
int32 j = lastPatCharIndex - 1;
while (true)
{
#if ENABLE_REGEX_CONFIG_OPTIONS
if (stats != 0)
stats->numCompares++;
#endif
uint inputChar = Chars<Char>::CTU(input[offset + j]);
if (inputChar != pat[j])
{
int goodSuffix = localGoodSuffix[j];
Assert(patLen <= MaxCharCount);
if (goodSuffix == (int)patLen)
{
offset += patLen;
}
else
{
const int32 e = j - localLastOccurrence->Get(inputChar);
offset += e > goodSuffix ? e : goodSuffix;
}
break;
}
if (--j < 0)
{
inputOffset = offset;
return true;
}
}
}
return false;
}
// explicit instantiation
template struct TextbookBoyerMooreSetup<char16>;
template class TextbookBoyerMoore<char16>;
template class TextbookBoyerMooreWithLinearMap<char16>;
template
bool TextbookBoyerMoore<char16>::Match<1>
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const;
template
bool TextbookBoyerMoore<char16>::Match<CaseInsensitive::EquivClassSize>
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const;
template
bool TextbookBoyerMoore<char16>::Match<CaseInsensitive::EquivClassSize, 1>
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const;
template
bool TextbookBoyerMooreWithLinearMap<char16>::Match<1>
( const Char *const input
, const CharCount inputLength
, CharCount& inputOffset
, const Char* pat
, const CharCount patLen
#if ENABLE_REGEX_CONFIG_OPTIONS
, RegexStats* stats
#endif
) const;
}