-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregexec.c
3738 lines (3535 loc) · 96.8 KB
/
regexec.c
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
/* regexec.c
*/
/*
* "One Ring to rule them all, One Ring to find them..."
*/
/* NOTE: this is derived from Henry Spencer's regexp code, and should not
* confused with the original package (see point 3 below). Thanks, Henry!
*/
/* Additional note: this code is very heavily munged from Henry's version
* in places. In some spots I've traded clarity for efficiency, so don't
* blame Henry for some of the lack of readability.
*/
/* The names of the functions have been changed from regcomp and
* regexec to pregcomp and pregexec in order to avoid conflicts
* with the POSIX routines of the same names.
*/
#ifdef PERL_EXT_RE_BUILD
/* need to replace pregcomp et al, so enable that */
# ifndef PERL_IN_XSUB_RE
# define PERL_IN_XSUB_RE
# endif
/* need access to debugger hooks */
# if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
# define DEBUGGING
# endif
#endif
#ifdef PERL_IN_XSUB_RE
/* We *really* need to overwrite these symbols: */
# define Perl_regexec_flags my_regexec
# define Perl_regdump my_regdump
# define Perl_regprop my_regprop
# define Perl_re_intuit_start my_re_intuit_start
/* *These* symbols are masked to allow static link. */
# define Perl_pregexec my_pregexec
# define Perl_reginitcolors my_reginitcolors
# define PERL_NO_GET_CONTEXT
#endif
/*SUPPRESS 112*/
/*
* pregcomp and pregexec -- regsub and regerror are not used in perl
*
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
**** Alterations to Henry's code are...
****
**** Copyright (c) 1991-2000, Larry Wall
****
**** You may distribute under the terms of either the GNU General Public
**** License or the Artistic License, as specified in the README file.
*
* Beware that some of this code is subtly aware of the way operator
* precedence is structured in regular expressions. Serious changes in
* regular-expression syntax might require a total rethink.
*/
#include "EXTERN.h"
#define PERL_IN_REGEXEC_C
#include "perl.h"
#ifdef PERL_IN_XSUB_RE
# if defined(PERL_CAPI) || defined(PERL_OBJECT)
# include "XSUB.h"
# endif
#endif
#include "regcomp.h"
#define RF_tainted 1 /* tainted information used? */
#define RF_warned 2 /* warned about big count? */
#define RF_evaled 4 /* Did an EVAL with setting? */
#define RF_utf8 8 /* String contains multibyte chars? */
#define UTF (PL_reg_flags & RF_utf8)
#define RS_init 1 /* eval environment created */
#define RS_set 2 /* replsv value is set */
#ifndef STATIC
#define STATIC static
#endif
/*
* Forwards.
*/
#define REGINCLASS(p,c) (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
#define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
#define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
#define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
#define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
#define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
#define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
#define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
#define HOPc(pos,off) ((char*)HOP(pos,off))
#define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
static void restore_pos(pTHXo_ void *arg);
STATIC CHECKPOINT
S_regcppush(pTHX_ I32 parenfloor)
{
dTHR;
int retval = PL_savestack_ix;
int i = (PL_regsize - parenfloor) * 4;
int p;
SSCHECK(i + 5);
for (p = PL_regsize; p > parenfloor; p--) {
SSPUSHINT(PL_regendp[p]);
SSPUSHINT(PL_regstartp[p]);
SSPUSHPTR(PL_reg_start_tmp[p]);
SSPUSHINT(p);
}
SSPUSHINT(PL_regsize);
SSPUSHINT(*PL_reglastparen);
SSPUSHPTR(PL_reginput);
SSPUSHINT(i + 3);
SSPUSHINT(SAVEt_REGCONTEXT);
return retval;
}
/* These are needed since we do not localize EVAL nodes: */
# define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
" Setting an EVAL scope, savestack=%"IVdf"\n", \
(IV)PL_savestack_ix)); lastcp = PL_savestack_ix
# define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
PerlIO_printf(Perl_debug_log, \
" Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
(IV)lastcp, (IV)PL_savestack_ix) : 0); regcpblow(lastcp)
STATIC char *
S_regcppop(pTHX)
{
dTHR;
I32 i = SSPOPINT;
U32 paren = 0;
char *input;
I32 tmps;
assert(i == SAVEt_REGCONTEXT);
i = SSPOPINT;
input = (char *) SSPOPPTR;
*PL_reglastparen = SSPOPINT;
PL_regsize = SSPOPINT;
for (i -= 3; i > 0; i -= 4) {
paren = (U32)SSPOPINT;
PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
PL_regstartp[paren] = SSPOPINT;
tmps = SSPOPINT;
if (paren <= *PL_reglastparen)
PL_regendp[paren] = tmps;
DEBUG_r(
PerlIO_printf(Perl_debug_log,
" restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
(UV)paren, (IV)PL_regstartp[paren],
(IV)(PL_reg_start_tmp[paren] - PL_bostr),
(IV)PL_regendp[paren],
(paren > *PL_reglastparen ? "(no)" : ""));
);
}
DEBUG_r(
if (*PL_reglastparen + 1 <= PL_regnpar) {
PerlIO_printf(Perl_debug_log,
" restoring \\%"IVdf"..\\%"IVdf" to undef\n",
(IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
}
);
for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
if (paren > PL_regsize)
PL_regstartp[paren] = -1;
PL_regendp[paren] = -1;
}
return input;
}
STATIC char *
S_regcp_set_to(pTHX_ I32 ss)
{
dTHR;
I32 tmp = PL_savestack_ix;
PL_savestack_ix = ss;
regcppop();
PL_savestack_ix = tmp;
return Nullch;
}
typedef struct re_cc_state
{
I32 ss;
regnode *node;
struct re_cc_state *prev;
CURCUR *cc;
regexp *re;
} re_cc_state;
#define regcpblow(cp) LEAVE_SCOPE(cp)
#define TRYPAREN(paren, n, input) { \
if (paren) { \
if (n) { \
PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
PL_regendp[paren] = input - PL_bostr; \
} \
else \
PL_regendp[paren] = -1; \
} \
if (regmatch(next)) \
sayYES; \
if (paren && n) \
PL_regendp[paren] = -1; \
}
/*
* pregexec and friends
*/
/*
- pregexec - match a regexp against a string
*/
I32
Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
char *strbeg, I32 minend, SV *screamer, U32 nosave)
/* strend: pointer to null at end of string */
/* strbeg: real beginning of string */
/* minend: end of match must be >=minend after stringarg. */
/* nosave: For optimizations. */
{
return
regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
nosave ? 0 : REXEC_COPY_STR);
}
STATIC void
S_cache_re(pTHX_ regexp *prog)
{
dTHR;
PL_regprecomp = prog->precomp; /* Needed for FAIL. */
#ifdef DEBUGGING
PL_regprogram = prog->program;
#endif
PL_regnpar = prog->nparens;
PL_regdata = prog->data;
PL_reg_re = prog;
}
/*
* Need to implement the following flags for reg_anch:
*
* USE_INTUIT_NOML - Useful to call re_intuit_start() first
* USE_INTUIT_ML
* INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
* INTUIT_AUTORITATIVE_ML
* INTUIT_ONCE_NOML - Intuit can match in one location only.
* INTUIT_ONCE_ML
*
* Another flag for this function: SECOND_TIME (so that float substrs
* with giant delta may be not rechecked).
*/
/* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
/* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
Otherwise, only SvCUR(sv) is used to get strbeg. */
/* XXXX We assume that strpos is strbeg unless sv. */
/* XXXX Some places assume that there is a fixed substring.
An update may be needed if optimizer marks as "INTUITable"
RExen without fixed substrings. Similarly, it is assumed that
lengths of all the strings are no more than minlen, thus they
cannot come from lookahead.
(Or minlen should take into account lookahead.) */
/* A failure to find a constant substring means that there is no need to make
an expensive call to REx engine, thus we celebrate a failure. Similarly,
finding a substring too deep into the string means that less calls to
regtry() should be needed.
REx compiler's optimizer found 4 possible hints:
a) Anchored substring;
b) Fixed substring;
c) Whether we are anchored (beginning-of-line or \G);
d) First node (of those at offset 0) which may distingush positions;
We use a)b)d) and multiline-part of c), and try to find a position in the
string which does not contradict any of them.
*/
/* Most of decisions we do here should have been done at compile time.
The nodes of the REx which we used for the search should have been
deleted from the finite automaton. */
char *
Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
char *strend, U32 flags, re_scream_pos_data *data)
{
register I32 start_shift;
/* Should be nonnegative! */
register I32 end_shift;
register char *s;
register SV *check;
char *t;
I32 ml_anch;
char *tmp;
register char *other_last = Nullch; /* other substr checked before this */
char *check_at; /* check substr found at this pos */
#ifdef DEBUGGING
char *i_strpos = strpos;
#endif
DEBUG_r( if (!PL_colorset) reginitcolors() );
DEBUG_r(PerlIO_printf(Perl_debug_log,
"%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
PL_colors[4],PL_colors[5],PL_colors[0],
prog->precomp,
PL_colors[1],
(strlen(prog->precomp) > 60 ? "..." : ""),
PL_colors[0],
(int)(strend - strpos > 60 ? 60 : strend - strpos),
strpos, PL_colors[1],
(strend - strpos > 60 ? "..." : ""))
);
if (prog->minlen > strend - strpos) {
DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
goto fail;
}
check = prog->check_substr;
if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
|| ( (prog->reganch & ROPT_ANCH_BOL)
&& !PL_multiline ) ); /* Check after \n? */
if ((prog->check_offset_min == prog->check_offset_max) && !ml_anch) {
/* Substring at constant offset from beg-of-str... */
I32 slen;
if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
/* SvCUR is not set on references: SvRV and SvPVX overlap */
&& sv && !SvROK(sv)
&& (strpos + SvCUR(sv) != strend)) {
DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
goto fail;
}
PL_regeol = strend; /* Used in HOP() */
s = HOPc(strpos, prog->check_offset_min);
if (SvTAIL(check)) {
slen = SvCUR(check); /* >= 1 */
if ( strend - s > slen || strend - s < slen - 1
|| (strend - s == slen && strend[-1] != '\n')) {
DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
goto fail_finish;
}
/* Now should match s[0..slen-2] */
slen--;
if (slen && (*SvPVX(check) != *s
|| (slen > 1
&& memNE(SvPVX(check), s, slen)))) {
report_neq:
DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
goto fail_finish;
}
}
else if (*SvPVX(check) != *s
|| ((slen = SvCUR(check)) > 1
&& memNE(SvPVX(check), s, slen)))
goto report_neq;
goto success_at_start;
}
/* Match is anchored, but substr is not anchored wrt beg-of-str. */
s = strpos;
start_shift = prog->check_offset_min; /* okay to underestimate on CC */
end_shift = prog->minlen - start_shift -
CHR_SVLEN(check) + (SvTAIL(check) != 0);
if (!ml_anch) {
I32 end = prog->check_offset_max + CHR_SVLEN(check)
- (SvTAIL(check) != 0);
I32 eshift = strend - s - end;
if (end_shift < eshift)
end_shift = eshift;
}
}
else { /* Can match at random position */
ml_anch = 0;
s = strpos;
start_shift = prog->check_offset_min; /* okay to underestimate on CC */
/* Should be nonnegative! */
end_shift = prog->minlen - start_shift -
CHR_SVLEN(check) + (SvTAIL(check) != 0);
}
#ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
if (end_shift < 0)
Perl_croak(aTHX_ "panic: end_shift");
#endif
restart:
/* Find a possible match in the region s..strend by looking for
the "check" substring in the region corrected by start/end_shift. */
if (flags & REXEC_SCREAM) {
char *strbeg = SvPVX(sv); /* XXXX Assume PV_force() on SCREAM! */
I32 p = -1; /* Internal iterator of scream. */
I32 *pp = data ? data->scream_pos : &p;
if (PL_screamfirst[BmRARE(check)] >= 0
|| ( BmRARE(check) == '\n'
&& (BmPREVIOUS(check) == SvCUR(check) - 1)
&& SvTAIL(check) ))
s = screaminstr(sv, check,
start_shift + (s - strbeg), end_shift, pp, 0);
else
goto fail_finish;
if (data)
*data->scream_olds = s;
}
else
s = fbm_instr((unsigned char*)s + start_shift,
(unsigned char*)strend - end_shift,
check, PL_multiline ? FBMrf_MULTILINE : 0);
/* Update the count-of-usability, remove useless subpatterns,
unshift s. */
DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
(s ? "Found" : "Did not find"),
((check == prog->anchored_substr) ? "anchored" : "floating"),
PL_colors[0],
(int)(SvCUR(check) - (SvTAIL(check)!=0)),
SvPVX(check),
PL_colors[1], (SvTAIL(check) ? "$" : ""),
(s ? " at offset " : "...\n") ) );
if (!s)
goto fail_finish;
check_at = s;
/* Finish the diagnostic message */
DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
/* Got a candidate. Check MBOL anchoring, and the *other* substr.
Start with the other substr.
XXXX no SCREAM optimization yet - and a very coarse implementation
XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
*always* match. Probably should be marked during compile...
Probably it is right to do no SCREAM here...
*/
if (prog->float_substr && prog->anchored_substr) {
/* Take into account the "other" substring. */
/* XXXX May be hopelessly wrong for UTF... */
if (!other_last)
other_last = strpos;
if (check == prog->float_substr) {
do_other_anchored:
{
char *last = s - start_shift, *last1, *last2;
char *s1 = s;
tmp = PL_bostr;
t = s - prog->check_offset_max;
if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
&& (!(prog->reganch & ROPT_UTF8)
|| (PL_bostr = strpos, /* Used in regcopmaybe() */
(t = reghopmaybe_c(s, -(prog->check_offset_max)))
&& t > strpos)))
/* EMPTY */;
else
t = strpos;
t += prog->anchored_offset;
if (t < other_last) /* These positions already checked */
t = other_last;
PL_bostr = tmp;
last2 = last1 = strend - prog->minlen;
if (last < last1)
last1 = last;
/* XXXX It is not documented what units *_offsets are in. Assume bytes. */
/* On end-of-str: see comment below. */
s = fbm_instr((unsigned char*)t,
(unsigned char*)last1 + prog->anchored_offset
+ SvCUR(prog->anchored_substr)
- (SvTAIL(prog->anchored_substr)!=0),
prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
(s ? "Found" : "Contradicts"),
PL_colors[0],
(int)(SvCUR(prog->anchored_substr)
- (SvTAIL(prog->anchored_substr)!=0)),
SvPVX(prog->anchored_substr),
PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
if (!s) {
if (last1 >= last2) {
DEBUG_r(PerlIO_printf(Perl_debug_log,
", giving up...\n"));
goto fail_finish;
}
DEBUG_r(PerlIO_printf(Perl_debug_log,
", trying floating at offset %ld...\n",
(long)(s1 + 1 - i_strpos)));
PL_regeol = strend; /* Used in HOP() */
other_last = last1 + prog->anchored_offset + 1;
s = HOPc(last, 1);
goto restart;
}
else {
DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
(long)(s - i_strpos)));
t = s - prog->anchored_offset;
other_last = s + 1;
s = s1;
if (t == strpos)
goto try_at_start;
goto try_at_offset;
}
}
}
else { /* Take into account the floating substring. */
char *last, *last1;
char *s1 = s;
t = s - start_shift;
last1 = last = strend - prog->minlen + prog->float_min_offset;
if (last - t > prog->float_max_offset)
last = t + prog->float_max_offset;
s = t + prog->float_min_offset;
if (s < other_last)
s = other_last;
/* XXXX It is not documented what units *_offsets are in. Assume bytes. */
/* fbm_instr() takes into account exact value of end-of-str
if the check is SvTAIL(ed). Since false positives are OK,
and end-of-str is not later than strend we are OK. */
s = fbm_instr((unsigned char*)s,
(unsigned char*)last + SvCUR(prog->float_substr)
- (SvTAIL(prog->float_substr)!=0),
prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
(s ? "Found" : "Contradicts"),
PL_colors[0],
(int)(SvCUR(prog->float_substr)
- (SvTAIL(prog->float_substr)!=0)),
SvPVX(prog->float_substr),
PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
if (!s) {
if (last1 == last) {
DEBUG_r(PerlIO_printf(Perl_debug_log,
", giving up...\n"));
goto fail_finish;
}
DEBUG_r(PerlIO_printf(Perl_debug_log,
", trying anchored starting at offset %ld...\n",
(long)(s1 + 1 - i_strpos)));
other_last = last + 1;
PL_regeol = strend; /* Used in HOP() */
s = HOPc(t, 1);
goto restart;
}
else {
DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
(long)(s - i_strpos)));
other_last = s + 1;
s = s1;
if (t == strpos)
goto try_at_start;
goto try_at_offset;
}
}
}
t = s - prog->check_offset_max;
tmp = PL_bostr;
if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
&& (!(prog->reganch & ROPT_UTF8)
|| (PL_bostr = strpos, /* Used in regcopmaybe() */
((t = reghopmaybe_c(s, -(prog->check_offset_max)))
&& t > strpos)))) {
PL_bostr = tmp;
/* Fixed substring is found far enough so that the match
cannot start at strpos. */
try_at_offset:
if (ml_anch && t[-1] != '\n') {
/* Eventually fbm_*() should handle this, but often
anchored_offset is not 0, so this check will not be wasted. */
/* XXXX In the code below we prefer to look for "^" even in
presence of anchored substrings. And we search even
beyond the found float position. These pessimizations
are historical artefacts only. */
find_anchor:
while (t < strend - prog->minlen) {
if (*t == '\n') {
if (t < check_at - prog->check_offset_min) {
if (prog->anchored_substr) {
/* Since we moved from the found position,
we definitely contradict the found anchored
substr. Due to the above check we do not
contradict "check" substr.
Thus we can arrive here only if check substr
is float. Redo checking for "other"=="fixed".
*/
strpos = t + 1;
DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
goto do_other_anchored;
}
/* We don't contradict the found floating substring. */
/* XXXX Why not check for STCLASS? */
s = t + 1;
DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
goto set_useful;
}
/* Position contradicts check-string */
/* XXXX probably better to look for check-string
than for "\n", so one should lower the limit for t? */
DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
other_last = strpos = s = t + 1;
goto restart;
}
t++;
}
DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
PL_colors[0],PL_colors[1]));
goto fail_finish;
}
else {
DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
PL_colors[0],PL_colors[1]));
}
s = t;
set_useful:
++BmUSEFUL(prog->check_substr); /* hooray/5 */
}
else {
PL_bostr = tmp;
/* The found string does not prohibit matching at strpos,
- no optimization of calling REx engine can be performed,
unless it was an MBOL and we are not after MBOL,
or a future STCLASS check will fail this. */
try_at_start:
/* Even in this situation we may use MBOL flag if strpos is offset
wrt the start of the string. */
if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
&& (strpos + SvCUR(sv) != strend) && strpos[-1] != '\n'
/* May be due to an implicit anchor of m{.*foo} */
&& !(prog->reganch & ROPT_IMPLICIT))
{
t = strpos;
goto find_anchor;
}
DEBUG_r( if (ml_anch)
PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
(long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
);
success_at_start:
if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
&& prog->check_substr /* Could be deleted already */
&& --BmUSEFUL(prog->check_substr) < 0
&& prog->check_substr == prog->float_substr)
{
/* If flags & SOMETHING - do not do it many times on the same match */
DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
SvREFCNT_dec(prog->check_substr);
prog->check_substr = Nullsv; /* disable */
prog->float_substr = Nullsv; /* clear */
check = Nullsv; /* abort */
s = strpos;
/* XXXX This is a remnant of the old implementation. It
looks wasteful, since now INTUIT can use many
other heuristics. */
prog->reganch &= ~RE_USE_INTUIT;
}
else
s = strpos;
}
/* Last resort... */
/* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
if (prog->regstclass) {
/* minlen == 0 is possible if regstclass is \b or \B,
and the fixed substr is ''$.
Since minlen is already taken into account, s+1 is before strend;
accidentally, minlen >= 1 guaranties no false positives at s + 1
even for \b or \B. But (minlen? 1 : 0) below assumes that
regstclass does not come from lookahead... */
/* If regstclass takes bytelength more than 1: If charlength==1, OK.
This leaves EXACTF only, which is dealt with in find_byclass(). */
int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
? STR_LEN(prog->regstclass)
: 1);
char *endpos = (prog->anchored_substr || ml_anch)
? s + (prog->minlen? cl_l : 0)
: (prog->float_substr ? check_at - start_shift + cl_l
: strend) ;
char *startpos = sv && SvPOK(sv) ? strend - SvCUR(sv) : s;
t = s;
if (prog->reganch & ROPT_UTF8) {
PL_regdata = prog->data; /* Used by REGINCLASS UTF logic */
PL_bostr = startpos;
}
s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
if (!s) {
#ifdef DEBUGGING
char *what;
#endif
if (endpos == strend) {
DEBUG_r( PerlIO_printf(Perl_debug_log,
"Could not match STCLASS...\n") );
goto fail;
}
DEBUG_r( PerlIO_printf(Perl_debug_log,
"This position contradicts STCLASS...\n") );
if ((prog->reganch & ROPT_ANCH) && !ml_anch)
goto fail;
/* Contradict one of substrings */
if (prog->anchored_substr) {
if (prog->anchored_substr == check) {
DEBUG_r( what = "anchored" );
hop_and_restart:
PL_regeol = strend; /* Used in HOP() */
s = HOPc(t, 1);
if (s + start_shift + end_shift > strend) {
/* XXXX Should be taken into account earlier? */
DEBUG_r( PerlIO_printf(Perl_debug_log,
"Could not match STCLASS...\n") );
goto fail;
}
if (!check)
goto giveup;
DEBUG_r( PerlIO_printf(Perl_debug_log,
"Looking for %s substr starting at offset %ld...\n",
what, (long)(s + start_shift - i_strpos)) );
goto restart;
}
/* Have both, check_string is floating */
if (t + start_shift >= check_at) /* Contradicts floating=check */
goto retry_floating_check;
/* Recheck anchored substring, but not floating... */
s = check_at;
if (!check)
goto giveup;
DEBUG_r( PerlIO_printf(Perl_debug_log,
"Looking for anchored substr starting at offset %ld...\n",
(long)(other_last - i_strpos)) );
goto do_other_anchored;
}
/* Another way we could have checked stclass at the
current position only: */
if (ml_anch) {
s = t = t + 1;
if (!check)
goto giveup;
DEBUG_r( PerlIO_printf(Perl_debug_log,
"Looking for /%s^%s/m starting at offset %ld...\n",
PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
goto try_at_offset;
}
if (!prog->float_substr) /* Could have been deleted */
goto fail;
/* Check is floating subtring. */
retry_floating_check:
t = check_at - start_shift;
DEBUG_r( what = "floating" );
goto hop_and_restart;
}
DEBUG_r( if (t != s)
PerlIO_printf(Perl_debug_log,
"By STCLASS: moving %ld --> %ld\n",
(long)(t - i_strpos), (long)(s - i_strpos));
else
PerlIO_printf(Perl_debug_log,
"Does not contradict STCLASS...\n") );
}
giveup:
DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
PL_colors[4], (check ? "Guessed" : "Giving up"),
PL_colors[5], (long)(s - i_strpos)) );
return s;
fail_finish: /* Substring not found */
if (prog->check_substr) /* could be removed already */
BmUSEFUL(prog->check_substr) += 5; /* hooray */
fail:
DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
PL_colors[4],PL_colors[5]));
return Nullch;
}
/* We know what class REx starts with. Try to find this position... */
STATIC char *
S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
{
I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
char *m;
STRLEN ln;
unsigned int c1;
unsigned int c2;
char *e;
register I32 tmp = 1; /* Scratch variable? */
/* We know what class it must start with. */
switch (OP(c)) {
case ANYOFUTF8:
while (s < strend) {
if (REGINCLASSUTF8(c, (U8*)s)) {
if (tmp && (norun || regtry(prog, s)))
goto got_it;
else
tmp = doevery;
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case ANYOF:
while (s < strend) {
if (REGINCLASS(c, *(U8*)s)) {
if (tmp && (norun || regtry(prog, s)))
goto got_it;
else
tmp = doevery;
}
else
tmp = 1;
s++;
}
break;
case EXACTF:
m = STRING(c);
ln = STR_LEN(c);
c1 = *(U8*)m;
c2 = PL_fold[c1];
goto do_exactf;
case EXACTFL:
m = STRING(c);
ln = STR_LEN(c);
c1 = *(U8*)m;
c2 = PL_fold_locale[c1];
do_exactf:
e = strend - ln;
if (norun && e < s)
e = s; /* Due to minlen logic of intuit() */
/* Here it is NOT UTF! */
if (c1 == c2) {
while (s <= e) {
if ( *(U8*)s == c1
&& (ln == 1 || !(OP(c) == EXACTF
? ibcmp(s, m, ln)
: ibcmp_locale(s, m, ln)))
&& (norun || regtry(prog, s)) )
goto got_it;
s++;
}
} else {
while (s <= e) {
if ( (*(U8*)s == c1 || *(U8*)s == c2)
&& (ln == 1 || !(OP(c) == EXACTF
? ibcmp(s, m, ln)
: ibcmp_locale(s, m, ln)))
&& (norun || regtry(prog, s)) )
goto got_it;
s++;
}
}
break;
case BOUNDL:
PL_reg_flags |= RF_tainted;
/* FALL THROUGH */
case BOUND:
tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
while (s < strend) {
if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
tmp = !tmp;
if ((norun || regtry(prog, s)))
goto got_it;
}
s++;
}
if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
goto got_it;
break;
case BOUNDLUTF8:
PL_reg_flags |= RF_tainted;
/* FALL THROUGH */
case BOUNDUTF8:
tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
tmp = ((OP(c) == BOUNDUTF8 ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
while (s < strend) {
if (tmp == !(OP(c) == BOUNDUTF8 ?
swash_fetch(PL_utf8_alnum, (U8*)s) :
isALNUM_LC_utf8((U8*)s)))
{
tmp = !tmp;
if ((norun || regtry(prog, s)))
goto got_it;
}
s += UTF8SKIP(s);
}
if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
goto got_it;
break;
case NBOUNDL:
PL_reg_flags |= RF_tainted;
/* FALL THROUGH */
case NBOUND:
tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
while (s < strend) {
if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
tmp = !tmp;
else if ((norun || regtry(prog, s)))
goto got_it;
s++;
}
if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
goto got_it;
break;
case NBOUNDLUTF8:
PL_reg_flags |= RF_tainted;
/* FALL THROUGH */
case NBOUNDUTF8:
tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : '\n';
tmp = ((OP(c) == NBOUNDUTF8 ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
while (s < strend) {
if (tmp == !(OP(c) == NBOUNDUTF8 ?
swash_fetch(PL_utf8_alnum, (U8*)s) :
isALNUM_LC_utf8((U8*)s)))
tmp = !tmp;
else if ((norun || regtry(prog, s)))
goto got_it;
s += UTF8SKIP(s);
}
if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
goto got_it;
break;
case ALNUM:
while (s < strend) {
if (isALNUM(*s)) {
if (tmp && (norun || regtry(prog, s)))
goto got_it;
else
tmp = doevery;
}
else
tmp = 1;
s++;
}
break;
case ALNUMUTF8:
while (s < strend) {
if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
if (tmp && (norun || regtry(prog, s)))
goto got_it;
else
tmp = doevery;
}
else
tmp = 1;
s += UTF8SKIP(s);
}
break;
case ALNUML:
PL_reg_flags |= RF_tainted;
while (s < strend) {
if (isALNUM_LC(*s)) {
if (tmp && (norun || regtry(prog, s)))
goto got_it;
else
tmp = doevery;