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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
|
/*
* DFA routines
* This file is #included by regexec.c.
*
* Copyright (c) 1998, 1999 Henry Spencer. All rights reserved.
*
* Development of this software was funded, in part, by Cray Research Inc.,
* UUNET Communications Services Inc., Sun Microsystems Inc., and Scriptics
* Corporation, none of whom are responsible for the results. The author
* thanks all of them.
*
* Redistribution and use in source and binary forms -- with or without
* modification -- are permitted for any purpose, provided that
* redistributions in source form retain this entire copyright notice and
* indicate the origin and nature of any modifications.
*
* I'd appreciate being given credit for this package in the documentation
* of software which uses it, but that is not a requirement.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* HENRY SPENCER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* src/backend/regex/rege_dfa.c
*
*/
/*
* longest - longest-preferred matching engine
*
* On success, returns match endpoint address. Returns NULL on no match.
* Internal errors also return NULL, with v->err set.
*/
static chr *
longest(struct vars *v,
struct dfa *d,
chr *start, /* where the match should start */
chr *stop, /* match must end at or before here */
int *hitstopp) /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realstop = (stop == v->stop) ? stop : stop + 1;
color co;
struct sset *css;
struct sset *ss;
chr *post;
int i;
struct colormap *cm = d->cm;
/* prevent "uninitialized variable" warnings */
if (hitstopp != NULL)
*hitstopp = 0;
/* if this is a backref to a known string, just match against that */
if (d->backno >= 0)
{
assert((size_t) d->backno < v->nmatch);
if (v->pmatch[d->backno].rm_so >= 0)
{
cp = dfa_backref(v, d, start, start, stop, false);
if (cp == v->stop && stop == v->stop && hitstopp != NULL)
*hitstopp = 1;
return cp;
}
}
/* fast path for matchall NFAs */
if (d->cnfa->flags & MATCHALL)
{
size_t nchr = stop - start;
size_t maxmatchall = d->cnfa->maxmatchall;
if (nchr < d->cnfa->minmatchall)
return NULL;
if (maxmatchall == DUPINF)
{
if (stop == v->stop && hitstopp != NULL)
*hitstopp = 1;
}
else
{
if (stop == v->stop && nchr <= maxmatchall + 1 && hitstopp != NULL)
*hitstopp = 1;
if (nchr > maxmatchall)
return start + maxmatchall;
}
return stop;
}
/* initialize */
css = initialize(v, d, start);
if (css == NULL)
return NULL;
cp = start;
/* startup */
FDEBUG(("+++ startup +++\n"));
if (cp == v->start)
{
co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
}
else
{
co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
}
css = miss(v, d, css, co, cp, start);
if (css == NULL)
return NULL;
css->lastseen = cp;
/*
* This is the main text-scanning loop. It seems worth having two copies
* to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
* builds, when you're not actively tracing.
*/
#ifdef REG_DEBUG
if (v->eflags & REG_FTRACE)
{
while (cp < realstop)
{
FDEBUG(("+++ at c%d +++\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
else
#endif
{
while (cp < realstop)
{
co = GETCOLOR(cm, *cp);
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
if (ISERR())
return NULL;
/* shutdown */
FDEBUG(("+++ shutdown at c%d +++\n", (int) (css - d->ssets)));
if (cp == v->stop && stop == v->stop)
{
if (hitstopp != NULL)
*hitstopp = 1;
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, start);
if (ISERR())
return NULL;
/* special case: match ended at eol? */
if (ss != NULL && (ss->flags & POSTSTATE))
return cp;
else if (ss != NULL)
ss->lastseen = cp; /* to be tidy */
}
/* find last match, if any */
post = d->lastpost;
for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
if ((ss->flags & POSTSTATE) && post != ss->lastseen &&
(post == NULL || post < ss->lastseen))
post = ss->lastseen;
if (post != NULL) /* found one */
return post - 1;
return NULL;
}
/*
* shortest - shortest-preferred matching engine
*
* On success, returns match endpoint address. Returns NULL on no match.
* Internal errors also return NULL, with v->err set.
*/
static chr *
shortest(struct vars *v,
struct dfa *d,
chr *start, /* where the match should start */
chr *min, /* match must end at or after here */
chr *max, /* match must end at or before here */
chr **coldp, /* store coldstart pointer here, if non-NULL */
int *hitstopp) /* record whether hit v->stop, if non-NULL */
{
chr *cp;
chr *realmin = (min == v->stop) ? min : min + 1;
chr *realmax = (max == v->stop) ? max : max + 1;
color co;
struct sset *css;
struct sset *ss;
struct colormap *cm = d->cm;
/* prevent "uninitialized variable" warnings */
if (coldp != NULL)
*coldp = NULL;
if (hitstopp != NULL)
*hitstopp = 0;
/* if this is a backref to a known string, just match against that */
if (d->backno >= 0)
{
assert((size_t) d->backno < v->nmatch);
if (v->pmatch[d->backno].rm_so >= 0)
{
cp = dfa_backref(v, d, start, min, max, true);
if (cp != NULL && coldp != NULL)
*coldp = start;
/* there is no case where we should set *hitstopp */
return cp;
}
}
/* fast path for matchall NFAs */
if (d->cnfa->flags & MATCHALL)
{
size_t nchr = min - start;
if (d->cnfa->maxmatchall != DUPINF &&
nchr > d->cnfa->maxmatchall)
return NULL;
if ((max - start) < d->cnfa->minmatchall)
return NULL;
if (nchr < d->cnfa->minmatchall)
min = start + d->cnfa->minmatchall;
if (coldp != NULL)
*coldp = start;
/* there is no case where we should set *hitstopp */
return min;
}
/* initialize */
css = initialize(v, d, start);
if (css == NULL)
return NULL;
cp = start;
/* startup */
FDEBUG(("--- startup ---\n"));
if (cp == v->start)
{
co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
}
else
{
co = GETCOLOR(cm, *(cp - 1));
FDEBUG(("char %c, color %ld\n", (char) *(cp - 1), (long) co));
}
css = miss(v, d, css, co, cp, start);
if (css == NULL)
return NULL;
css->lastseen = cp;
ss = css;
/*
* This is the main text-scanning loop. It seems worth having two copies
* to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
* builds, when you're not actively tracing.
*/
#ifdef REG_DEBUG
if (v->eflags & REG_FTRACE)
{
while (cp < realmax)
{
FDEBUG(("--- at c%d ---\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
if ((ss->flags & POSTSTATE) && cp >= realmin)
break; /* NOTE BREAK OUT */
}
}
else
#endif
{
while (cp < realmax)
{
co = GETCOLOR(cm, *cp);
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
if ((ss->flags & POSTSTATE) && cp >= realmin)
break; /* NOTE BREAK OUT */
}
}
if (ss == NULL)
return NULL;
if (coldp != NULL) /* report last no-progress state set, if any */
*coldp = lastcold(v, d);
if ((ss->flags & POSTSTATE) && cp > min)
{
assert(cp >= realmin);
cp--;
}
else if (cp == v->stop && max == v->stop)
{
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, start);
/* match might have ended at eol */
if ((ss == NULL || !(ss->flags & POSTSTATE)) && hitstopp != NULL)
*hitstopp = 1;
}
if (ss == NULL || !(ss->flags & POSTSTATE))
return NULL;
return cp;
}
/*
* matchuntil - incremental matching engine
*
* This is meant for use with a search-style NFA (that is, the pattern is
* known to act as though it had a leading .*). We determine whether a
* match exists starting at v->start and ending at probe. Multiple calls
* require only O(N) time not O(N^2) so long as the probe values are
* nondecreasing. *lastcss and *lastcp must be initialized to NULL before
* starting a series of calls.
*
* Returns 1 if a match exists, 0 if not.
* Internal errors also return 0, with v->err set.
*/
static int
matchuntil(struct vars *v,
struct dfa *d,
chr *probe, /* we want to know if a match ends here */
struct sset **lastcss, /* state storage across calls */
chr **lastcp) /* state storage across calls */
{
chr *cp = *lastcp;
color co;
struct sset *css = *lastcss;
struct sset *ss;
struct colormap *cm = d->cm;
/* fast path for matchall NFAs */
if (d->cnfa->flags & MATCHALL)
{
size_t nchr = probe - v->start;
if (nchr < d->cnfa->minmatchall)
return 0;
/* maxmatchall will always be infinity, cf. makesearch() */
assert(d->cnfa->maxmatchall == DUPINF);
return 1;
}
/* initialize and startup, or restart, if necessary */
if (cp == NULL || cp > probe)
{
cp = v->start;
css = initialize(v, d, cp);
if (css == NULL)
return 0;
FDEBUG((">>> startup >>>\n"));
co = d->cnfa->bos[(v->eflags & REG_NOTBOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
css = miss(v, d, css, co, cp, v->start);
if (css == NULL)
return 0;
css->lastseen = cp;
}
else if (css == NULL)
{
/* we previously found that no match is possible beyond *lastcp */
return 0;
}
ss = css;
/*
* This is the main text-scanning loop. It seems worth having two copies
* to avoid the overhead of REG_FTRACE tests here, even in REG_DEBUG
* builds, when you're not actively tracing.
*/
#ifdef REG_DEBUG
if (v->eflags & REG_FTRACE)
{
while (cp < probe)
{
FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, v->start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
else
#endif
{
while (cp < probe)
{
co = GETCOLOR(cm, *cp);
ss = css->outs[co];
if (ss == NULL)
{
ss = miss(v, d, css, co, cp + 1, v->start);
if (ss == NULL)
break; /* NOTE BREAK OUT */
}
cp++;
ss->lastseen = cp;
css = ss;
}
}
*lastcss = ss;
*lastcp = cp;
if (ss == NULL)
return 0; /* impossible match, or internal error */
/* We need to process one more chr, or the EOS symbol, to check match */
if (cp < v->stop)
{
FDEBUG((">>> at c%d >>>\n", (int) (css - d->ssets)));
co = GETCOLOR(cm, *cp);
FDEBUG(("char %c, color %ld\n", (char) *cp, (long) co));
ss = css->outs[co];
if (ss == NULL)
ss = miss(v, d, css, co, cp + 1, v->start);
}
else
{
assert(cp == v->stop);
co = d->cnfa->eos[(v->eflags & REG_NOTEOL) ? 0 : 1];
FDEBUG(("color %ld\n", (long) co));
ss = miss(v, d, css, co, cp, v->start);
}
if (ss == NULL || !(ss->flags & POSTSTATE))
return 0;
return 1;
}
/*
* dfa_backref - find best match length for a known backref string
*
* When the backref's referent is already available, we can deliver an exact
* answer with considerably less work than running the backref node's NFA.
*
* Return match endpoint for longest or shortest valid repeated match,
* or NULL if there is no valid match.
*
* Should be in sync with cbrdissect(), although that has the different task
* of checking a match to a predetermined section of the string.
*/
static chr *
dfa_backref(struct vars *v,
struct dfa *d,
chr *start, /* where the match should start */
chr *min, /* match must end at or after here */
chr *max, /* match must end at or before here */
bool shortest)
{
int n = d->backno;
int backmin = d->backmin;
int backmax = d->backmax;
size_t numreps;
size_t minreps;
size_t maxreps;
size_t brlen;
chr *brstring;
chr *p;
/* get the backreferenced string (caller should have checked this) */
if (v->pmatch[n].rm_so == -1)
return NULL;
brstring = v->start + v->pmatch[n].rm_so;
brlen = v->pmatch[n].rm_eo - v->pmatch[n].rm_so;
/* special-case zero-length backreference to avoid divide by zero */
if (brlen == 0)
{
/*
* matches only a zero-length string, but any number of repetitions
* can be considered to be present
*/
if (min == start && backmin <= backmax)
return start;
return NULL;
}
/*
* convert min and max into numbers of possible repetitions of the backref
* string, rounding appropriately
*/
if (min <= start)
minreps = 0;
else
minreps = (min - start - 1) / brlen + 1;
maxreps = (max - start) / brlen;
/* apply bounds, then see if there is any allowed match length */
if (minreps < backmin)
minreps = backmin;
if (backmax != DUPINF && maxreps > backmax)
maxreps = backmax;
if (maxreps < minreps)
return NULL;
/* quick exit if zero-repetitions match is valid and preferred */
if (shortest && minreps == 0)
return start;
/* okay, compare the actual string contents */
p = start;
numreps = 0;
while (numreps < maxreps)
{
if ((*v->g->compare) (brstring, p, brlen) != 0)
break;
p += brlen;
numreps++;
if (shortest && numreps >= minreps)
break;
}
if (numreps >= minreps)
return p;
return NULL;
}
/*
* lastcold - determine last point at which no progress had been made
*/
static chr * /* endpoint, or NULL */
lastcold(struct vars *v,
struct dfa *d)
{
struct sset *ss;
chr *nopr;
int i;
nopr = d->lastnopr;
if (nopr == NULL)
nopr = v->start;
for (ss = d->ssets, i = d->nssused; i > 0; ss++, i--)
if ((ss->flags & NOPROGRESS) && nopr < ss->lastseen)
nopr = ss->lastseen;
return nopr;
}
/*
* newdfa - set up a fresh DFA
*
* Returns NULL (and sets v->err) on failure.
*/
static struct dfa *
newdfa(struct vars *v,
struct cnfa *cnfa,
struct colormap *cm,
struct smalldfa *sml) /* preallocated space, may be NULL */
{
struct dfa *d;
size_t nss = cnfa->nstates * 2;
int wordsper = (cnfa->nstates + UBITS - 1) / UBITS;
bool ismalloced = false;
assert(cnfa != NULL && cnfa->nstates != 0);
if (nss <= FEWSTATES && cnfa->ncolors <= FEWCOLORS)
{
assert(wordsper == 1);
if (sml == NULL)
{
sml = (struct smalldfa *) MALLOC(sizeof(struct smalldfa));
if (sml == NULL)
{
ERR(REG_ESPACE);
return NULL;
}
ismalloced = true;
}
d = &sml->dfa;
d->ssets = sml->ssets;
d->statesarea = sml->statesarea;
d->work = &d->statesarea[nss];
d->outsarea = sml->outsarea;
d->incarea = sml->incarea;
d->ismalloced = ismalloced;
d->arraysmalloced = false; /* not separately allocated, anyway */
}
else
{
d = (struct dfa *) MALLOC(sizeof(struct dfa));
if (d == NULL)
{
ERR(REG_ESPACE);
return NULL;
}
d->ssets = (struct sset *) MALLOC(nss * sizeof(struct sset));
d->statesarea = (unsigned *) MALLOC((nss + WORK) * wordsper *
sizeof(unsigned));
d->work = &d->statesarea[nss * wordsper];
d->outsarea = (struct sset **) MALLOC(nss * cnfa->ncolors *
sizeof(struct sset *));
d->incarea = (struct arcp *) MALLOC(nss * cnfa->ncolors *
sizeof(struct arcp));
d->ismalloced = true;
d->arraysmalloced = true;
/* now freedfa() will behave sanely */
if (d->ssets == NULL || d->statesarea == NULL ||
d->outsarea == NULL || d->incarea == NULL)
{
freedfa(d);
ERR(REG_ESPACE);
return NULL;
}
}
d->nssets = (v->eflags & REG_SMALL) ? 7 : nss;
d->nssused = 0;
d->nstates = cnfa->nstates;
d->ncolors = cnfa->ncolors;
d->wordsper = wordsper;
d->cnfa = cnfa;
d->cm = cm;
d->lastpost = NULL;
d->lastnopr = NULL;
d->search = d->ssets;
d->backno = -1; /* may be set by caller */
d->backmin = d->backmax = 0;
/* initialization of sset fields is done as needed */
return d;
}
/*
* freedfa - free a DFA
*/
static void
freedfa(struct dfa *d)
{
if (d->arraysmalloced)
{
if (d->ssets != NULL)
FREE(d->ssets);
if (d->statesarea != NULL)
FREE(d->statesarea);
if (d->outsarea != NULL)
FREE(d->outsarea);
if (d->incarea != NULL)
FREE(d->incarea);
}
if (d->ismalloced)
FREE(d);
}
/*
* hash - construct a hash code for a bitvector
*
* There are probably better ways, but they're more expensive.
*/
static unsigned
hash(unsigned *uv,
int n)
{
int i;
unsigned h;
h = 0;
for (i = 0; i < n; i++)
h ^= uv[i];
return h;
}
/*
* initialize - hand-craft a cache entry for startup, otherwise get ready
*/
static struct sset *
initialize(struct vars *v,
struct dfa *d,
chr *start)
{
struct sset *ss;
int i;
/* is previous one still there? */
if (d->nssused > 0 && (d->ssets[0].flags & STARTER))
ss = &d->ssets[0];
else
{ /* no, must (re)build it */
ss = getvacant(v, d, start, start);
if (ss == NULL)
return NULL;
for (i = 0; i < d->wordsper; i++)
ss->states[i] = 0;
BSET(ss->states, d->cnfa->pre);
ss->hash = HASH(ss->states, d->wordsper);
assert(d->cnfa->pre != d->cnfa->post);
ss->flags = STARTER | LOCKED | NOPROGRESS;
/* lastseen dealt with below */
}
for (i = 0; i < d->nssused; i++)
d->ssets[i].lastseen = NULL;
ss->lastseen = start; /* maybe untrue, but harmless */
d->lastpost = NULL;
d->lastnopr = NULL;
return ss;
}
/*
* miss - handle a stateset cache miss
*
* css is the current stateset, co is the color of the current input character,
* cp points to the character after that (which is where we may need to test
* LACONs). start does not affect matching behavior but is needed for pickss'
* heuristics about which stateset cache entry to replace.
*
* Ordinarily, returns the address of the next stateset (the one that is
* valid after consuming the input character). Returns NULL if no valid
* NFA states remain, ie we have a certain match failure.
* Internal errors also return NULL, with v->err set.
*/
static struct sset *
miss(struct vars *v,
struct dfa *d,
struct sset *css,
color co,
chr *cp, /* next chr */
chr *start) /* where the attempt got started */
{
struct cnfa *cnfa = d->cnfa;
int i;
unsigned h;
struct carc *ca;
struct sset *p;
int ispseudocolor;
int ispost;
int noprogress;
int gotstate;
int dolacons;
int sawlacons;
/* for convenience, we can be called even if it might not be a miss */
if (css->outs[co] != NULL)
{
FDEBUG(("hit\n"));
return css->outs[co];
}
FDEBUG(("miss\n"));
/*
* Checking for operation cancel in the inner text search loop seems
* unduly expensive. As a compromise, check during cache misses.
*/
if (CANCEL_REQUESTED(v->re))
{
ERR(REG_CANCEL);
return NULL;
}
/*
* What set of states would we end up in after consuming the co character?
* We first consider PLAIN arcs that consume the character, and then look
* to see what LACON arcs could be traversed after consuming it.
*/
for (i = 0; i < d->wordsper; i++)
d->work[i] = 0; /* build new stateset bitmap in d->work */
ispseudocolor = d->cm->cd[co].flags & PSEUDO;
ispost = 0;
noprogress = 1;
gotstate = 0;
for (i = 0; i < d->nstates; i++)
if (ISBSET(css->states, i))
for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
if (ca->co == co ||
(ca->co == RAINBOW && !ispseudocolor))
{
BSET(d->work, ca->to);
gotstate = 1;
if (ca->to == cnfa->post)
ispost = 1;
if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
noprogress = 0;
FDEBUG(("%d -> %d\n", i, ca->to));
}
if (!gotstate)
return NULL; /* character cannot reach any new state */
dolacons = (cnfa->flags & HASLACONS);
sawlacons = 0;
/* outer loop handles transitive closure of reachable-by-LACON states */
while (dolacons)
{
dolacons = 0;
for (i = 0; i < d->nstates; i++)
if (ISBSET(d->work, i))
for (ca = cnfa->states[i]; ca->co != COLORLESS; ca++)
{
if (ca->co < cnfa->ncolors)
continue; /* not a LACON arc */
if (ISBSET(d->work, ca->to))
continue; /* arc would be a no-op anyway */
sawlacons = 1; /* this LACON affects our result */
if (!lacon(v, cnfa, cp, ca->co))
{
if (ISERR())
return NULL;
continue; /* LACON arc cannot be traversed */
}
if (ISERR())
return NULL;
BSET(d->work, ca->to);
dolacons = 1;
if (ca->to == cnfa->post)
ispost = 1;
if (!(cnfa->stflags[ca->to] & CNFA_NOPROGRESS))
noprogress = 0;
FDEBUG(("%d :> %d\n", i, ca->to));
}
}
h = HASH(d->work, d->wordsper);
/* Is this stateset already in the cache? */
for (p = d->ssets, i = d->nssused; i > 0; p++, i--)
if (HIT(h, d->work, p, d->wordsper))
{
FDEBUG(("cached c%d\n", (int) (p - d->ssets)));
break; /* NOTE BREAK OUT */
}
if (i == 0)
{ /* nope, need a new cache entry */
p = getvacant(v, d, cp, start);
if (p == NULL)
return NULL;
assert(p != css);
for (i = 0; i < d->wordsper; i++)
p->states[i] = d->work[i];
p->hash = h;
p->flags = (ispost) ? POSTSTATE : 0;
if (noprogress)
p->flags |= NOPROGRESS;
/* lastseen to be dealt with by caller */
}
/*
* Link new stateset to old, unless a LACON affected the result, in which
* case we don't create the link. That forces future transitions across
* this same arc (same prior stateset and character color) to come through
* miss() again, so that we can recheck the LACON(s), which might or might
* not pass since context will be different.
*/
if (!sawlacons)
{
FDEBUG(("c%d[%d]->c%d\n",
(int) (css - d->ssets), co, (int) (p - d->ssets)));
css->outs[co] = p;
css->inchain[co] = p->ins;
p->ins.ss = css;
p->ins.co = co;
}
return p;
}
/*
* lacon - lookaround-constraint checker for miss()
*/
static int /* predicate: constraint satisfied? */
lacon(struct vars *v,
struct cnfa *pcnfa, /* parent cnfa */
chr *cp,
color co) /* "color" of the lookaround constraint */
{
int n;
struct subre *sub;
struct dfa *d;
chr *end;
int satisfied;
/* Since this is recursive, it could be driven to stack overflow */
if (STACK_TOO_DEEP(v->re))
{
ERR(REG_ETOOBIG);
return 0;
}
n = co - pcnfa->ncolors;
assert(n > 0 && n < v->g->nlacons && v->g->lacons != NULL);
FDEBUG(("=== testing lacon %d\n", n));
sub = &v->g->lacons[n];
d = getladfa(v, n);
if (d == NULL)
return 0;
if (LATYPE_IS_AHEAD(sub->latype))
{
/* used to use longest() here, but shortest() could be much cheaper */
end = shortest(v, d, cp, cp, v->stop,
(chr **) NULL, (int *) NULL);
satisfied = LATYPE_IS_POS(sub->latype) ? (end != NULL) : (end == NULL);
}
else
{
/*
* To avoid doing O(N^2) work when repeatedly testing a lookbehind
* constraint in an N-character string, we use matchuntil() which can
* cache the DFA state across calls. We only need to restart if the
* probe point decreases, which is not common. The NFA we're using is
* a search NFA, so it doesn't mind scanning over stuff before the
* nominal match.
*/
satisfied = matchuntil(v, d, cp, &v->lblastcss[n], &v->lblastcp[n]);
if (!LATYPE_IS_POS(sub->latype))
satisfied = !satisfied;
}
FDEBUG(("=== lacon %d satisfied %d\n", n, satisfied));
return satisfied;
}
/*
* getvacant - get a vacant state set
*
* This routine clears out the inarcs and outarcs, but does not otherwise
* clear the innards of the state set -- that's up to the caller.
*/
static struct sset *
getvacant(struct vars *v,
struct dfa *d,
chr *cp,
chr *start)
{
int i;
struct sset *ss;
struct sset *p;
struct arcp ap;
color co;
ss = pickss(v, d, cp, start);
if (ss == NULL)
return NULL;
assert(!(ss->flags & LOCKED));
/* clear out its inarcs, including self-referential ones */
ap = ss->ins;
while ((p = ap.ss) != NULL)
{
co = ap.co;
FDEBUG(("zapping c%d's %ld outarc\n", (int) (p - d->ssets), (long) co));
p->outs[co] = NULL;
ap = p->inchain[co];
p->inchain[co].ss = NULL; /* paranoia */
}
ss->ins.ss = NULL;
/* take it off the inarc chains of the ssets reached by its outarcs */
for (i = 0; i < d->ncolors; i++)
{
p = ss->outs[i];
assert(p != ss); /* not self-referential */
if (p == NULL)
continue; /* NOTE CONTINUE */
FDEBUG(("del outarc %d from c%d's in chn\n", i, (int) (p - d->ssets)));
if (p->ins.ss == ss && p->ins.co == i)
p->ins = ss->inchain[i];
else
{
struct arcp lastap = {NULL, 0};
assert(p->ins.ss != NULL);
for (ap = p->ins; ap.ss != NULL &&
!(ap.ss == ss && ap.co == i);
ap = ap.ss->inchain[ap.co])
lastap = ap;
assert(ap.ss != NULL);
lastap.ss->inchain[lastap.co] = ss->inchain[i];
}
ss->outs[i] = NULL;
ss->inchain[i].ss = NULL;
}
/* if ss was a success state, may need to remember location */
if ((ss->flags & POSTSTATE) && ss->lastseen != d->lastpost &&
(d->lastpost == NULL || d->lastpost < ss->lastseen))
d->lastpost = ss->lastseen;
/* likewise for a no-progress state */
if ((ss->flags & NOPROGRESS) && ss->lastseen != d->lastnopr &&
(d->lastnopr == NULL || d->lastnopr < ss->lastseen))
d->lastnopr = ss->lastseen;
return ss;
}
/*
* pickss - pick the next stateset to be used
*/
static struct sset *
pickss(struct vars *v,
struct dfa *d,
chr *cp,
chr *start)
{
int i;
struct sset *ss;
struct sset *end;
chr *ancient;
/* shortcut for cases where cache isn't full */
if (d->nssused < d->nssets)
{
i = d->nssused;
d->nssused++;
ss = &d->ssets[i];
FDEBUG(("new c%d\n", i));
/* set up innards */
ss->states = &d->statesarea[i * d->wordsper];
ss->flags = 0;
ss->ins.ss = NULL;
ss->ins.co = WHITE; /* give it some value */
ss->outs = &d->outsarea[i * d->ncolors];
ss->inchain = &d->incarea[i * d->ncolors];
for (i = 0; i < d->ncolors; i++)
{
ss->outs[i] = NULL;
ss->inchain[i].ss = NULL;
}
return ss;
}
/* look for oldest, or old enough anyway */
if (cp - start > d->nssets * 2 / 3) /* oldest 33% are expendable */
ancient = cp - d->nssets * 2 / 3;
else
ancient = start;
for (ss = d->search, end = &d->ssets[d->nssets]; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED))
{
d->search = ss + 1;
FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
return ss;
}
for (ss = d->ssets, end = d->search; ss < end; ss++)
if ((ss->lastseen == NULL || ss->lastseen < ancient) &&
!(ss->flags & LOCKED))
{
d->search = ss + 1;
FDEBUG(("replacing c%d\n", (int) (ss - d->ssets)));
return ss;
}
/* nobody's old enough?!? -- something's really wrong */
FDEBUG(("cannot find victim to replace!\n"));
ERR(REG_ASSERT);
return NULL;
}
|