5 * "One Ring to rule them all, One Ring to find them..."
8 /* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
12 /* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
17 /* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
22 #ifdef PERL_EXT_RE_BUILD
23 /* need to replace pregcomp et al, so enable that */
24 # ifndef PERL_IN_XSUB_RE
25 # define PERL_IN_XSUB_RE
27 /* need access to debugger hooks */
28 # if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
33 #ifdef PERL_IN_XSUB_RE
34 /* We *really* need to overwrite these symbols: */
35 # define Perl_regexec_flags my_regexec
36 # define Perl_regdump my_regdump
37 # define Perl_regprop my_regprop
38 # define Perl_re_intuit_start my_re_intuit_start
39 /* *These* symbols are masked to allow static link. */
40 # define Perl_pregexec my_pregexec
41 # define Perl_reginitcolors my_reginitcolors
42 # define Perl_regclass_swash my_regclass_swash
44 # define PERL_NO_GET_CONTEXT
49 * pregcomp and pregexec -- regsub and regerror are not used in perl
51 * Copyright (c) 1986 by University of Toronto.
52 * Written by Henry Spencer. Not derived from licensed software.
54 * Permission is granted to anyone to use this software for any
55 * purpose on any computer system, and to redistribute it freely,
56 * subject to the following restrictions:
58 * 1. The author is not responsible for the consequences of use of
59 * this software, no matter how awful, even if they arise
62 * 2. The origin of this software must not be misrepresented, either
63 * by explicit claim or by omission.
65 * 3. Altered versions must be plainly marked as such, and must not
66 * be misrepresented as being the original software.
68 **** Alterations to Henry's code are...
70 **** Copyright (c) 1991-2001, Larry Wall
72 **** You may distribute under the terms of either the GNU General Public
73 **** License or the Artistic License, as specified in the README file.
75 * Beware that some of this code is subtly aware of the way operator
76 * precedence is structured in regular expressions. Serious changes in
77 * regular-expression syntax might require a total rethink.
80 #define PERL_IN_REGEXEC_C
83 #ifdef PERL_IN_XSUB_RE
84 # if defined(PERL_CAPI) || defined(PERL_OBJECT)
91 #define RF_tainted 1 /* tainted information used? */
92 #define RF_warned 2 /* warned about big count? */
93 #define RF_evaled 4 /* Did an EVAL with setting? */
94 #define RF_utf8 8 /* String contains multibyte chars? */
96 #define UTF (PL_reg_flags & RF_utf8)
98 #define RS_init 1 /* eval environment created */
99 #define RS_set 2 /* replsv value is set */
102 #define STATIC static
109 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
110 #define CHR_DIST(a,b) (DO_UTF8(PL_reg_sv) ? utf8_distance(a,b) : a - b)
112 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
113 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
114 #define HOP(pos,off) (DO_UTF8(PL_reg_sv) ? reghop((U8*)pos, off) : (U8*)(pos + off))
115 #define HOPMAYBE(pos,off) (DO_UTF8(PL_reg_sv) ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
116 #define HOPc(pos,off) ((char*)HOP(pos,off))
117 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
119 #define reghop3_c(pos,off,lim) ((char*)reghop3((U8*)pos, off, (U8*)lim))
120 #define reghopmaybe3_c(pos,off,lim) ((char*)reghopmaybe3((U8*)pos, off, (U8*)lim))
121 #define HOP3(pos,off,lim) (DO_UTF8(PL_reg_sv) ? reghop3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
122 #define HOPMAYBE3(pos,off,lim) (DO_UTF8(PL_reg_sv) ? reghopmaybe3((U8*)pos, off, (U8*)lim) : (U8*)(pos + off))
123 #define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
124 #define HOPMAYBE3c(pos,off,lim) ((char*)HOPMAYBE3(pos,off,lim))
126 #define LOAD_UTF8_CHARCLASS(a,b) STMT_START { if (!CAT2(PL_utf8_,a)) (void)CAT2(is_utf8_, a)((U8*)b); } STMT_END
128 static void restore_pos(pTHXo_ void *arg);
131 S_regcppush(pTHX_ I32 parenfloor)
133 int retval = PL_savestack_ix;
134 #define REGCP_PAREN_ELEMS 4
135 int paren_elems_to_push = (PL_regsize - parenfloor) * REGCP_PAREN_ELEMS;
138 #define REGCP_OTHER_ELEMS 5
139 SSCHECK(paren_elems_to_push + REGCP_OTHER_ELEMS);
140 for (p = PL_regsize; p > parenfloor; p--) {
141 /* REGCP_PARENS_ELEMS are pushed per pairs of parentheses. */
142 SSPUSHINT(PL_regendp[p]);
143 SSPUSHINT(PL_regstartp[p]);
144 SSPUSHPTR(PL_reg_start_tmp[p]);
147 /* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
148 SSPUSHINT(PL_regsize);
149 SSPUSHINT(*PL_reglastparen);
150 SSPUSHPTR(PL_reginput);
151 #define REGCP_FRAME_ELEMS 2
152 /* REGCP_FRAME_ELEMS are part of the REGCP_OTHER_ELEMS and
153 * are needed for the regexp context stack bookkeeping. */
154 SSPUSHINT(paren_elems_to_push + REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
155 SSPUSHINT(SAVEt_REGCONTEXT); /* Magic cookie. */
160 /* These are needed since we do not localize EVAL nodes: */
161 # define REGCP_SET(cp) DEBUG_r(PerlIO_printf(Perl_debug_log, \
162 " Setting an EVAL scope, savestack=%"IVdf"\n", \
163 (IV)PL_savestack_ix)); cp = PL_savestack_ix
165 # define REGCP_UNWIND(cp) DEBUG_r(cp != PL_savestack_ix ? \
166 PerlIO_printf(Perl_debug_log, \
167 " Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
168 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
178 /* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
180 assert(i == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
181 i = SSPOPINT; /* Parentheses elements to pop. */
182 input = (char *) SSPOPPTR;
183 *PL_reglastparen = SSPOPINT;
184 PL_regsize = SSPOPINT;
186 /* Now restore the parentheses context. */
187 for (i -= (REGCP_OTHER_ELEMS - REGCP_FRAME_ELEMS);
188 i > 0; i -= REGCP_PAREN_ELEMS) {
189 paren = (U32)SSPOPINT;
190 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
191 PL_regstartp[paren] = SSPOPINT;
193 if (paren <= *PL_reglastparen)
194 PL_regendp[paren] = tmps;
196 PerlIO_printf(Perl_debug_log,
197 " restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
198 (UV)paren, (IV)PL_regstartp[paren],
199 (IV)(PL_reg_start_tmp[paren] - PL_bostr),
200 (IV)PL_regendp[paren],
201 (paren > *PL_reglastparen ? "(no)" : ""));
205 if (*PL_reglastparen + 1 <= PL_regnpar) {
206 PerlIO_printf(Perl_debug_log,
207 " restoring \\%"IVdf"..\\%"IVdf" to undef\n",
208 (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
212 /* It would seem that the similar code in regtry()
213 * already takes care of this, and in fact it is in
214 * a better location to since this code can #if 0-ed out
215 * but the code in regtry() is needed or otherwise tests
216 * requiring null fields (pat.t#187 and split.t#{13,14}
217 * (as of patchlevel 7877) will fail. Then again,
218 * this code seems to be necessary or otherwise
219 * building DynaLoader will fail:
220 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
222 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
223 if (paren > PL_regsize)
224 PL_regstartp[paren] = -1;
225 PL_regendp[paren] = -1;
232 S_regcp_set_to(pTHX_ I32 ss)
234 I32 tmp = PL_savestack_ix;
236 PL_savestack_ix = ss;
238 PL_savestack_ix = tmp;
242 typedef struct re_cc_state
246 struct re_cc_state *prev;
251 #define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
253 #define TRYPAREN(paren, n, input) { \
256 PL_regstartp[paren] = HOPc(input, -1) - PL_bostr; \
257 PL_regendp[paren] = input - PL_bostr; \
260 PL_regendp[paren] = -1; \
262 if (regmatch(next)) \
265 PL_regendp[paren] = -1; \
270 * pregexec and friends
274 - pregexec - match a regexp against a string
277 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
278 char *strbeg, I32 minend, SV *screamer, U32 nosave)
279 /* strend: pointer to null at end of string */
280 /* strbeg: real beginning of string */
281 /* minend: end of match must be >=minend after stringarg. */
282 /* nosave: For optimizations. */
285 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
286 nosave ? 0 : REXEC_COPY_STR);
290 S_cache_re(pTHX_ regexp *prog)
292 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
294 PL_regprogram = prog->program;
296 PL_regnpar = prog->nparens;
297 PL_regdata = prog->data;
302 * Need to implement the following flags for reg_anch:
304 * USE_INTUIT_NOML - Useful to call re_intuit_start() first
306 * INTUIT_AUTORITATIVE_NOML - Can trust a positive answer
307 * INTUIT_AUTORITATIVE_ML
308 * INTUIT_ONCE_NOML - Intuit can match in one location only.
311 * Another flag for this function: SECOND_TIME (so that float substrs
312 * with giant delta may be not rechecked).
315 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
317 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
318 Otherwise, only SvCUR(sv) is used to get strbeg. */
320 /* XXXX We assume that strpos is strbeg unless sv. */
322 /* XXXX Some places assume that there is a fixed substring.
323 An update may be needed if optimizer marks as "INTUITable"
324 RExen without fixed substrings. Similarly, it is assumed that
325 lengths of all the strings are no more than minlen, thus they
326 cannot come from lookahead.
327 (Or minlen should take into account lookahead.) */
329 /* A failure to find a constant substring means that there is no need to make
330 an expensive call to REx engine, thus we celebrate a failure. Similarly,
331 finding a substring too deep into the string means that less calls to
332 regtry() should be needed.
334 REx compiler's optimizer found 4 possible hints:
335 a) Anchored substring;
337 c) Whether we are anchored (beginning-of-line or \G);
338 d) First node (of those at offset 0) which may distingush positions;
339 We use a)b)d) and multiline-part of c), and try to find a position in the
340 string which does not contradict any of them.
343 /* Most of decisions we do here should have been done at compile time.
344 The nodes of the REx which we used for the search should have been
345 deleted from the finite automaton. */
348 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
349 char *strend, U32 flags, re_scream_pos_data *data)
351 register I32 start_shift;
352 /* Should be nonnegative! */
353 register I32 end_shift;
360 register char *other_last = Nullch; /* other substr checked before this */
361 char *check_at; /* check substr found at this pos */
363 char *i_strpos = strpos;
366 DEBUG_r( if (!PL_colorset) reginitcolors() );
367 DEBUG_r(PerlIO_printf(Perl_debug_log,
368 "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
369 PL_colors[4],PL_colors[5],PL_colors[0],
372 (strlen(prog->precomp) > 60 ? "..." : ""),
374 (int)(strend - strpos > 60 ? 60 : strend - strpos),
375 strpos, PL_colors[1],
376 (strend - strpos > 60 ? "..." : ""))
379 if (prog->reganch & ROPT_UTF8)
380 PL_reg_flags |= RF_utf8;
382 if (prog->minlen > CHR_DIST((U8*)strend, (U8*)strpos)) {
383 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
386 strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
388 check = prog->check_substr;
389 if (prog->reganch & ROPT_ANCH) { /* Match at beg-of-str or after \n */
390 ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
391 || ( (prog->reganch & ROPT_ANCH_BOL)
392 && !PL_multiline ) ); /* Check after \n? */
395 if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
396 /* SvCUR is not set on references: SvRV and SvPVX overlap */
398 && (strpos != strbeg)) {
399 DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
402 if (prog->check_offset_min == prog->check_offset_max &&
403 !(prog->reganch & ROPT_SANY_SEEN)) {
404 /* Substring at constant offset from beg-of-str... */
407 s = HOP3c(strpos, prog->check_offset_min, strend);
409 slen = SvCUR(check); /* >= 1 */
411 if ( strend - s > slen || strend - s < slen - 1
412 || (strend - s == slen && strend[-1] != '\n')) {
413 DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
416 /* Now should match s[0..slen-2] */
418 if (slen && (*SvPVX(check) != *s
420 && memNE(SvPVX(check), s, slen)))) {
422 DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
426 else if (*SvPVX(check) != *s
427 || ((slen = SvCUR(check)) > 1
428 && memNE(SvPVX(check), s, slen)))
430 goto success_at_start;
433 /* Match is anchored, but substr is not anchored wrt beg-of-str. */
435 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
436 end_shift = prog->minlen - start_shift -
437 CHR_SVLEN(check) + (SvTAIL(check) != 0);
439 I32 end = prog->check_offset_max + CHR_SVLEN(check)
440 - (SvTAIL(check) != 0);
441 I32 eshift = CHR_DIST((U8*)strend, (U8*)s) - end;
443 if (end_shift < eshift)
447 else { /* Can match at random position */
450 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
451 /* Should be nonnegative! */
452 end_shift = prog->minlen - start_shift -
453 CHR_SVLEN(check) + (SvTAIL(check) != 0);
456 #ifdef DEBUGGING /* 7/99: reports of failure (with the older version) */
458 Perl_croak(aTHX_ "panic: end_shift");
462 /* Find a possible match in the region s..strend by looking for
463 the "check" substring in the region corrected by start/end_shift. */
464 if (flags & REXEC_SCREAM) {
465 I32 p = -1; /* Internal iterator of scream. */
466 I32 *pp = data ? data->scream_pos : &p;
468 if (PL_screamfirst[BmRARE(check)] >= 0
469 || ( BmRARE(check) == '\n'
470 && (BmPREVIOUS(check) == SvCUR(check) - 1)
472 s = screaminstr(sv, check,
473 start_shift + (s - strbeg), end_shift, pp, 0);
477 *data->scream_olds = s;
479 else if (prog->reganch & ROPT_SANY_SEEN)
480 s = fbm_instr((U8*)(s + start_shift),
481 (U8*)(strend - end_shift),
482 check, PL_multiline ? FBMrf_MULTILINE : 0);
484 s = fbm_instr(HOP3(s, start_shift, strend),
485 HOP3(strend, -end_shift, strbeg),
486 check, PL_multiline ? FBMrf_MULTILINE : 0);
488 /* Update the count-of-usability, remove useless subpatterns,
491 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
492 (s ? "Found" : "Did not find"),
493 ((check == prog->anchored_substr) ? "anchored" : "floating"),
495 (int)(SvCUR(check) - (SvTAIL(check)!=0)),
497 PL_colors[1], (SvTAIL(check) ? "$" : ""),
498 (s ? " at offset " : "...\n") ) );
505 /* Finish the diagnostic message */
506 DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
508 /* Got a candidate. Check MBOL anchoring, and the *other* substr.
509 Start with the other substr.
510 XXXX no SCREAM optimization yet - and a very coarse implementation
511 XXXX /ttx+/ results in anchored=`ttx', floating=`x'. floating will
512 *always* match. Probably should be marked during compile...
513 Probably it is right to do no SCREAM here...
516 if (prog->float_substr && prog->anchored_substr) {
517 /* Take into account the "other" substring. */
518 /* XXXX May be hopelessly wrong for UTF... */
521 if (check == prog->float_substr) {
524 char *last = HOP3c(s, -start_shift, strbeg), *last1, *last2;
527 t = s - prog->check_offset_max;
528 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
529 && (!(prog->reganch & ROPT_UTF8)
530 || ((t = reghopmaybe3_c(s, -(prog->check_offset_max), strpos))
535 t = HOP3c(t, prog->anchored_offset, strend);
536 if (t < other_last) /* These positions already checked */
538 last2 = last1 = HOP3c(strend, -prog->minlen, strbeg);
541 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
542 /* On end-of-str: see comment below. */
543 s = fbm_instr((unsigned char*)t,
544 HOP3(HOP3(last1, prog->anchored_offset, strend)
545 + SvCUR(prog->anchored_substr),
546 -(SvTAIL(prog->anchored_substr)!=0), strbeg),
547 prog->anchored_substr,
548 PL_multiline ? FBMrf_MULTILINE : 0);
549 DEBUG_r(PerlIO_printf(Perl_debug_log,
550 "%s anchored substr `%s%.*s%s'%s",
551 (s ? "Found" : "Contradicts"),
553 (int)(SvCUR(prog->anchored_substr)
554 - (SvTAIL(prog->anchored_substr)!=0)),
555 SvPVX(prog->anchored_substr),
556 PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
558 if (last1 >= last2) {
559 DEBUG_r(PerlIO_printf(Perl_debug_log,
560 ", giving up...\n"));
563 DEBUG_r(PerlIO_printf(Perl_debug_log,
564 ", trying floating at offset %ld...\n",
565 (long)(HOP3c(s1, 1, strend) - i_strpos)));
566 other_last = HOP3c(last1, prog->anchored_offset+1, strend);
567 s = HOP3c(last, 1, strend);
571 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
572 (long)(s - i_strpos)));
573 t = HOP3c(s, -prog->anchored_offset, strbeg);
574 other_last = HOP3c(s, 1, strend);
582 else { /* Take into account the floating substring. */
586 t = HOP3c(s, -start_shift, strbeg);
588 HOP3c(strend, -prog->minlen + prog->float_min_offset, strbeg);
589 if (CHR_DIST((U8*)last, (U8*)t) > prog->float_max_offset)
590 last = HOP3c(t, prog->float_max_offset, strend);
591 s = HOP3c(t, prog->float_min_offset, strend);
594 /* XXXX It is not documented what units *_offsets are in. Assume bytes. */
595 /* fbm_instr() takes into account exact value of end-of-str
596 if the check is SvTAIL(ed). Since false positives are OK,
597 and end-of-str is not later than strend we are OK. */
598 s = fbm_instr((unsigned char*)s,
599 (unsigned char*)last + SvCUR(prog->float_substr)
600 - (SvTAIL(prog->float_substr)!=0),
601 prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
602 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
603 (s ? "Found" : "Contradicts"),
605 (int)(SvCUR(prog->float_substr)
606 - (SvTAIL(prog->float_substr)!=0)),
607 SvPVX(prog->float_substr),
608 PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
611 DEBUG_r(PerlIO_printf(Perl_debug_log,
612 ", giving up...\n"));
615 DEBUG_r(PerlIO_printf(Perl_debug_log,
616 ", trying anchored starting at offset %ld...\n",
617 (long)(s1 + 1 - i_strpos)));
619 s = HOP3c(t, 1, strend);
623 DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
624 (long)(s - i_strpos)));
625 other_last = s; /* Fix this later. --Hugo */
634 t = s - prog->check_offset_max;
635 if (s - strpos > prog->check_offset_max /* signed-corrected t > strpos */
636 && (!(prog->reganch & ROPT_UTF8)
637 || ((t = reghopmaybe3_c(s, -prog->check_offset_max, strpos))
639 /* Fixed substring is found far enough so that the match
640 cannot start at strpos. */
642 if (ml_anch && t[-1] != '\n') {
643 /* Eventually fbm_*() should handle this, but often
644 anchored_offset is not 0, so this check will not be wasted. */
645 /* XXXX In the code below we prefer to look for "^" even in
646 presence of anchored substrings. And we search even
647 beyond the found float position. These pessimizations
648 are historical artefacts only. */
650 while (t < strend - prog->minlen) {
652 if (t < check_at - prog->check_offset_min) {
653 if (prog->anchored_substr) {
654 /* Since we moved from the found position,
655 we definitely contradict the found anchored
656 substr. Due to the above check we do not
657 contradict "check" substr.
658 Thus we can arrive here only if check substr
659 is float. Redo checking for "other"=="fixed".
662 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
663 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
664 goto do_other_anchored;
666 /* We don't contradict the found floating substring. */
667 /* XXXX Why not check for STCLASS? */
669 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
670 PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
673 /* Position contradicts check-string */
674 /* XXXX probably better to look for check-string
675 than for "\n", so one should lower the limit for t? */
676 DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
677 PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
678 other_last = strpos = s = t + 1;
683 DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
684 PL_colors[0],PL_colors[1]));
688 DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
689 PL_colors[0],PL_colors[1]));
693 ++BmUSEFUL(prog->check_substr); /* hooray/5 */
696 /* The found string does not prohibit matching at strpos,
697 - no optimization of calling REx engine can be performed,
698 unless it was an MBOL and we are not after MBOL,
699 or a future STCLASS check will fail this. */
701 /* Even in this situation we may use MBOL flag if strpos is offset
702 wrt the start of the string. */
703 if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
704 && (strpos != strbeg) && strpos[-1] != '\n'
705 /* May be due to an implicit anchor of m{.*foo} */
706 && !(prog->reganch & ROPT_IMPLICIT))
711 DEBUG_r( if (ml_anch)
712 PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
713 (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
716 if (!(prog->reganch & ROPT_NAUGHTY) /* XXXX If strpos moved? */
717 && prog->check_substr /* Could be deleted already */
718 && --BmUSEFUL(prog->check_substr) < 0
719 && prog->check_substr == prog->float_substr)
721 /* If flags & SOMETHING - do not do it many times on the same match */
722 DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
723 SvREFCNT_dec(prog->check_substr);
724 prog->check_substr = Nullsv; /* disable */
725 prog->float_substr = Nullsv; /* clear */
726 check = Nullsv; /* abort */
728 /* XXXX This is a remnant of the old implementation. It
729 looks wasteful, since now INTUIT can use many
731 prog->reganch &= ~RE_USE_INTUIT;
738 /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
739 if (prog->regstclass) {
740 /* minlen == 0 is possible if regstclass is \b or \B,
741 and the fixed substr is ''$.
742 Since minlen is already taken into account, s+1 is before strend;
743 accidentally, minlen >= 1 guaranties no false positives at s + 1
744 even for \b or \B. But (minlen? 1 : 0) below assumes that
745 regstclass does not come from lookahead... */
746 /* If regstclass takes bytelength more than 1: If charlength==1, OK.
747 This leaves EXACTF only, which is dealt with in find_byclass(). */
748 U8* str = (U8*)STRING(prog->regstclass);
749 int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
750 ? CHR_DIST(str+STR_LEN(prog->regstclass), str)
752 char *endpos = (prog->anchored_substr || ml_anch)
753 ? HOP3c(s, (prog->minlen ? cl_l : 0), strend)
754 : (prog->float_substr
755 ? HOP3c(HOP3c(check_at, -start_shift, strbeg),
758 char *startpos = strbeg;
761 if (prog->reganch & ROPT_UTF8) {
762 PL_regdata = prog->data;
765 s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
770 if (endpos == strend) {
771 DEBUG_r( PerlIO_printf(Perl_debug_log,
772 "Could not match STCLASS...\n") );
775 DEBUG_r( PerlIO_printf(Perl_debug_log,
776 "This position contradicts STCLASS...\n") );
777 if ((prog->reganch & ROPT_ANCH) && !ml_anch)
779 /* Contradict one of substrings */
780 if (prog->anchored_substr) {
781 if (prog->anchored_substr == check) {
782 DEBUG_r( what = "anchored" );
784 s = HOP3c(t, 1, strend);
785 if (s + start_shift + end_shift > strend) {
786 /* XXXX Should be taken into account earlier? */
787 DEBUG_r( PerlIO_printf(Perl_debug_log,
788 "Could not match STCLASS...\n") );
793 DEBUG_r( PerlIO_printf(Perl_debug_log,
794 "Looking for %s substr starting at offset %ld...\n",
795 what, (long)(s + start_shift - i_strpos)) );
798 /* Have both, check_string is floating */
799 if (t + start_shift >= check_at) /* Contradicts floating=check */
800 goto retry_floating_check;
801 /* Recheck anchored substring, but not floating... */
805 DEBUG_r( PerlIO_printf(Perl_debug_log,
806 "Looking for anchored substr starting at offset %ld...\n",
807 (long)(other_last - i_strpos)) );
808 goto do_other_anchored;
810 /* Another way we could have checked stclass at the
811 current position only: */
816 DEBUG_r( PerlIO_printf(Perl_debug_log,
817 "Looking for /%s^%s/m starting at offset %ld...\n",
818 PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
821 if (!prog->float_substr) /* Could have been deleted */
823 /* Check is floating subtring. */
824 retry_floating_check:
825 t = check_at - start_shift;
826 DEBUG_r( what = "floating" );
827 goto hop_and_restart;
830 PerlIO_printf(Perl_debug_log,
831 "By STCLASS: moving %ld --> %ld\n",
832 (long)(t - i_strpos), (long)(s - i_strpos));
834 PerlIO_printf(Perl_debug_log,
835 "Does not contradict STCLASS...\n") );
838 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
839 PL_colors[4], (check ? "Guessed" : "Giving up"),
840 PL_colors[5], (long)(s - i_strpos)) );
843 fail_finish: /* Substring not found */
844 if (prog->check_substr) /* could be removed already */
845 BmUSEFUL(prog->check_substr) += 5; /* hooray */
847 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
848 PL_colors[4],PL_colors[5]));
852 /* We know what class REx starts with. Try to find this position... */
854 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
856 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
862 register I32 tmp = 1; /* Scratch variable? */
863 register bool do_utf8 = DO_UTF8(PL_reg_sv);
865 /* We know what class it must start with. */
869 if (reginclass(c, (U8*)s, do_utf8)) {
870 if (tmp && (norun || regtry(prog, s)))
877 s += do_utf8 ? UTF8SKIP(s) : 1;
884 c1 = to_utf8_lower((U8*)m);
885 c2 = to_utf8_upper((U8*)m);
896 c2 = PL_fold_locale[c1];
901 e = s; /* Due to minlen logic of intuit() */
907 if ( utf8_to_uvchr((U8*)s, &len) == c1
914 UV c = utf8_to_uvchr((U8*)s, &len);
915 if ( (c == c1 || c == c2) && regtry(prog, s) )
924 && (ln == 1 || !(OP(c) == EXACTF
926 : ibcmp_locale(s, m, ln)))
927 && (norun || regtry(prog, s)) )
933 if ( (*(U8*)s == c1 || *(U8*)s == c2)
934 && (ln == 1 || !(OP(c) == EXACTF
936 : ibcmp_locale(s, m, ln)))
937 && (norun || regtry(prog, s)) )
944 PL_reg_flags |= RF_tainted;
951 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
953 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
955 tmp = ((OP(c) == BOUND ?
956 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
957 LOAD_UTF8_CHARCLASS(alnum,"a");
959 if (tmp == !(OP(c) == BOUND ?
960 swash_fetch(PL_utf8_alnum, (U8*)s) :
961 isALNUM_LC_utf8((U8*)s)))
964 if ((norun || regtry(prog, s)))
971 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
972 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
975 !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
977 if ((norun || regtry(prog, s)))
983 if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
987 PL_reg_flags |= RF_tainted;
994 U8 *r = reghop3((U8*)s, -1, (U8*)startpos);
996 tmp = (I32)utf8n_to_uvchr(r, s - (char*)r, 0, 0);
998 tmp = ((OP(c) == NBOUND ?
999 isALNUM_uni(tmp) : isALNUM_LC_uvchr(UNI_TO_NATIVE(tmp))) != 0);
1000 LOAD_UTF8_CHARCLASS(alnum,"a");
1001 while (s < strend) {
1002 if (tmp == !(OP(c) == NBOUND ?
1003 swash_fetch(PL_utf8_alnum, (U8*)s) :
1004 isALNUM_LC_utf8((U8*)s)))
1006 else if ((norun || regtry(prog, s)))
1012 tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
1013 tmp = ((OP(c) == NBOUND ?
1014 isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
1015 while (s < strend) {
1017 !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
1019 else if ((norun || regtry(prog, s)))
1024 if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
1029 LOAD_UTF8_CHARCLASS(alnum,"a");
1030 while (s < strend) {
1031 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1032 if (tmp && (norun || regtry(prog, s)))
1043 while (s < strend) {
1045 if (tmp && (norun || regtry(prog, s)))
1057 PL_reg_flags |= RF_tainted;
1059 while (s < strend) {
1060 if (isALNUM_LC_utf8((U8*)s)) {
1061 if (tmp && (norun || regtry(prog, s)))
1072 while (s < strend) {
1073 if (isALNUM_LC(*s)) {
1074 if (tmp && (norun || regtry(prog, s)))
1087 LOAD_UTF8_CHARCLASS(alnum,"a");
1088 while (s < strend) {
1089 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1090 if (tmp && (norun || regtry(prog, s)))
1101 while (s < strend) {
1103 if (tmp && (norun || regtry(prog, s)))
1115 PL_reg_flags |= RF_tainted;
1117 while (s < strend) {
1118 if (!isALNUM_LC_utf8((U8*)s)) {
1119 if (tmp && (norun || regtry(prog, s)))
1130 while (s < strend) {
1131 if (!isALNUM_LC(*s)) {
1132 if (tmp && (norun || regtry(prog, s)))
1145 LOAD_UTF8_CHARCLASS(space," ");
1146 while (s < strend) {
1147 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1148 if (tmp && (norun || regtry(prog, s)))
1159 while (s < strend) {
1161 if (tmp && (norun || regtry(prog, s)))
1173 PL_reg_flags |= RF_tainted;
1175 while (s < strend) {
1176 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1177 if (tmp && (norun || regtry(prog, s)))
1188 while (s < strend) {
1189 if (isSPACE_LC(*s)) {
1190 if (tmp && (norun || regtry(prog, s)))
1203 LOAD_UTF8_CHARCLASS(space," ");
1204 while (s < strend) {
1205 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1206 if (tmp && (norun || regtry(prog, s)))
1217 while (s < strend) {
1219 if (tmp && (norun || regtry(prog, s)))
1231 PL_reg_flags |= RF_tainted;
1233 while (s < strend) {
1234 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1235 if (tmp && (norun || regtry(prog, s)))
1246 while (s < strend) {
1247 if (!isSPACE_LC(*s)) {
1248 if (tmp && (norun || regtry(prog, s)))
1261 LOAD_UTF8_CHARCLASS(digit,"0");
1262 while (s < strend) {
1263 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1264 if (tmp && (norun || regtry(prog, s)))
1275 while (s < strend) {
1277 if (tmp && (norun || regtry(prog, s)))
1289 PL_reg_flags |= RF_tainted;
1291 while (s < strend) {
1292 if (isDIGIT_LC_utf8((U8*)s)) {
1293 if (tmp && (norun || regtry(prog, s)))
1304 while (s < strend) {
1305 if (isDIGIT_LC(*s)) {
1306 if (tmp && (norun || regtry(prog, s)))
1319 LOAD_UTF8_CHARCLASS(digit,"0");
1320 while (s < strend) {
1321 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1322 if (tmp && (norun || regtry(prog, s)))
1333 while (s < strend) {
1335 if (tmp && (norun || regtry(prog, s)))
1347 PL_reg_flags |= RF_tainted;
1349 while (s < strend) {
1350 if (!isDIGIT_LC_utf8((U8*)s)) {
1351 if (tmp && (norun || regtry(prog, s)))
1362 while (s < strend) {
1363 if (!isDIGIT_LC(*s)) {
1364 if (tmp && (norun || regtry(prog, s)))
1376 Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1385 - regexec_flags - match a regexp against a string
1388 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1389 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1390 /* strend: pointer to null at end of string */
1391 /* strbeg: real beginning of string */
1392 /* minend: end of match must be >=minend after stringarg. */
1393 /* data: May be used for some additional optimizations. */
1394 /* nosave: For optimizations. */
1397 register regnode *c;
1398 register char *startpos = stringarg;
1399 I32 minlen; /* must match at least this many chars */
1400 I32 dontbother = 0; /* how many characters not to try at end */
1401 /* I32 start_shift = 0; */ /* Offset of the start to find
1402 constant substr. */ /* CC */
1403 I32 end_shift = 0; /* Same for the end. */ /* CC */
1404 I32 scream_pos = -1; /* Internal iterator of scream. */
1406 SV* oreplsv = GvSV(PL_replgv);
1407 bool do_utf8 = DO_UTF8(sv);
1413 PL_regnarrate = DEBUG_r_TEST;
1416 /* Be paranoid... */
1417 if (prog == NULL || startpos == NULL) {
1418 Perl_croak(aTHX_ "NULL regexp parameter");
1422 minlen = prog->minlen;
1424 if (!(prog->reganch & ROPT_SANY_SEEN))
1425 if (utf8_distance((U8*)strend, (U8*)startpos) < minlen) goto phooey;
1428 if (strend - startpos < minlen) goto phooey;
1431 if (startpos == strbeg) /* is ^ valid at stringarg? */
1434 if (prog->reganch & ROPT_UTF8 && do_utf8) {
1435 U8 *s = reghop3((U8*)stringarg, -1, (U8*)strbeg);
1436 PL_regprev = utf8n_to_uvchr(s, (U8*)stringarg - s, NULL, 0);
1439 PL_regprev = (U32)stringarg[-1];
1440 if (!PL_multiline && PL_regprev == '\n')
1441 PL_regprev = '\0'; /* force ^ to NOT match */
1444 /* Check validity of program. */
1445 if (UCHARAT(prog->program) != REG_MAGIC) {
1446 Perl_croak(aTHX_ "corrupted regexp program");
1450 PL_reg_eval_set = 0;
1453 if (prog->reganch & ROPT_UTF8)
1454 PL_reg_flags |= RF_utf8;
1456 /* Mark beginning of line for ^ and lookbehind. */
1457 PL_regbol = startpos;
1461 /* Mark end of line for $ (and such) */
1464 /* see how far we have to get to not match where we matched before */
1465 PL_regtill = startpos+minend;
1467 /* We start without call_cc context. */
1470 /* If there is a "must appear" string, look for it. */
1473 if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1476 if (flags & REXEC_IGNOREPOS) /* Means: check only at start */
1477 PL_reg_ganch = startpos;
1478 else if (sv && SvTYPE(sv) >= SVt_PVMG
1480 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1481 PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1482 if (prog->reganch & ROPT_ANCH_GPOS) {
1483 if (s > PL_reg_ganch)
1488 else /* pos() not defined */
1489 PL_reg_ganch = strbeg;
1492 if (do_utf8 == (UTF!=0) &&
1493 !(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1494 re_scream_pos_data d;
1496 d.scream_olds = &scream_olds;
1497 d.scream_pos = &scream_pos;
1498 s = re_intuit_start(prog, sv, s, strend, flags, &d);
1500 goto phooey; /* not present */
1503 DEBUG_r( if (!PL_colorset) reginitcolors() );
1504 DEBUG_r(PerlIO_printf(Perl_debug_log,
1505 "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1506 PL_colors[4],PL_colors[5],PL_colors[0],
1509 (strlen(prog->precomp) > 60 ? "..." : ""),
1511 (int)(strend - startpos > 60 ? 60 : strend - startpos),
1512 startpos, PL_colors[1],
1513 (strend - startpos > 60 ? "..." : ""))
1516 /* Simplest case: anchored match need be tried only once. */
1517 /* [unless only anchor is BOL and multiline is set] */
1518 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1519 if (s == startpos && regtry(prog, startpos))
1521 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1522 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1527 dontbother = minlen - 1;
1528 end = HOP3c(strend, -dontbother, strbeg) - 1;
1529 /* for multiline we only have to try after newlines */
1530 if (prog->check_substr) {
1534 if (regtry(prog, s))
1539 if (prog->reganch & RE_USE_INTUIT) {
1540 s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1551 if (*s++ == '\n') { /* don't need PL_utf8skip here */
1552 if (regtry(prog, s))
1559 } else if (prog->reganch & ROPT_ANCH_GPOS) {
1560 if (regtry(prog, PL_reg_ganch))
1565 /* Messy cases: unanchored match. */
1566 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
1567 /* we have /x+whatever/ */
1568 /* it must be a one character string (XXXX Except UTF?) */
1569 char ch = SvPVX(prog->anchored_substr)[0];
1575 while (s < strend) {
1577 DEBUG_r( did_match = 1 );
1578 if (regtry(prog, s)) goto got_it;
1580 while (s < strend && *s == ch)
1587 while (s < strend) {
1589 DEBUG_r( did_match = 1 );
1590 if (regtry(prog, s)) goto got_it;
1592 while (s < strend && *s == ch)
1598 DEBUG_r(did_match ||
1599 PerlIO_printf(Perl_debug_log,
1600 "Did not find anchored character...\n"));
1603 else if (do_utf8 == (UTF!=0) &&
1604 (prog->anchored_substr != Nullsv
1605 || (prog->float_substr != Nullsv
1606 && prog->float_max_offset < strend - s))) {
1607 SV *must = prog->anchored_substr
1608 ? prog->anchored_substr : prog->float_substr;
1610 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1612 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1613 char *last = HOP3c(strend, /* Cannot start after this */
1614 -(I32)(CHR_SVLEN(must)
1615 - (SvTAIL(must) != 0) + back_min), strbeg);
1616 char *last1; /* Last position checked before */
1622 last1 = HOPc(s, -1);
1624 last1 = s - 1; /* bogus */
1626 /* XXXX check_substr already used to find `s', can optimize if
1627 check_substr==must. */
1629 dontbother = end_shift;
1630 strend = HOPc(strend, -dontbother);
1631 while ( (s <= last) &&
1632 ((flags & REXEC_SCREAM)
1633 ? (s = screaminstr(sv, must, HOP3c(s, back_min, strend) - strbeg,
1634 end_shift, &scream_pos, 0))
1635 : (s = fbm_instr((unsigned char*)HOP3(s, back_min, strend),
1636 (unsigned char*)strend, must,
1637 PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1638 DEBUG_r( did_match = 1 );
1639 if (HOPc(s, -back_max) > last1) {
1640 last1 = HOPc(s, -back_min);
1641 s = HOPc(s, -back_max);
1644 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1646 last1 = HOPc(s, -back_min);
1650 while (s <= last1) {
1651 if (regtry(prog, s))
1657 while (s <= last1) {
1658 if (regtry(prog, s))
1664 DEBUG_r(did_match ||
1665 PerlIO_printf(Perl_debug_log, "Did not find %s substr `%s%.*s%s'%s...\n",
1666 ((must == prog->anchored_substr)
1667 ? "anchored" : "floating"),
1669 (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1671 PL_colors[1], (SvTAIL(must) ? "$" : "")));
1674 else if ((c = prog->regstclass)) {
1675 if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1676 /* don't bother with what can't match */
1677 strend = HOPc(strend, -(minlen - 1));
1679 SV *prop = sv_newmortal();
1681 PerlIO_printf(Perl_debug_log, "Matching stclass `%s' against `%s'\n", SvPVX(prop), s);
1683 if (find_byclass(prog, c, s, strend, startpos, 0))
1685 DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1689 if (prog->float_substr != Nullsv) { /* Trim the end. */
1692 if (flags & REXEC_SCREAM) {
1693 last = screaminstr(sv, prog->float_substr, s - strbeg,
1694 end_shift, &scream_pos, 1); /* last one */
1696 last = scream_olds; /* Only one occurrence. */
1700 char *little = SvPV(prog->float_substr, len);
1702 if (SvTAIL(prog->float_substr)) {
1703 if (memEQ(strend - len + 1, little, len - 1))
1704 last = strend - len + 1;
1705 else if (!PL_multiline)
1706 last = memEQ(strend - len, little, len)
1707 ? strend - len : Nullch;
1713 last = rninstr(s, strend, little, little + len);
1715 last = strend; /* matching `$' */
1719 DEBUG_r(PerlIO_printf(Perl_debug_log,
1720 "%sCan't trim the tail, match fails (should not happen)%s\n",
1721 PL_colors[4],PL_colors[5]));
1722 goto phooey; /* Should not happen! */
1724 dontbother = strend - last + prog->float_min_offset;
1726 if (minlen && (dontbother < minlen))
1727 dontbother = minlen - 1;
1728 strend -= dontbother; /* this one's always in bytes! */
1729 /* We don't know much -- general case. */
1732 if (regtry(prog, s))
1741 if (regtry(prog, s))
1743 } while (s++ < strend);
1751 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1753 if (PL_reg_eval_set) {
1754 /* Preserve the current value of $^R */
1755 if (oreplsv != GvSV(PL_replgv))
1756 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1757 restored, the value remains
1759 restore_pos(aTHXo_ 0);
1762 /* make sure $`, $&, $', and $digit will work later */
1763 if ( !(flags & REXEC_NOT_FIRST) ) {
1764 if (RX_MATCH_COPIED(prog)) {
1765 Safefree(prog->subbeg);
1766 RX_MATCH_COPIED_off(prog);
1768 if (flags & REXEC_COPY_STR) {
1769 I32 i = PL_regeol - startpos + (stringarg - strbeg);
1771 s = savepvn(strbeg, i);
1774 RX_MATCH_COPIED_on(prog);
1777 prog->subbeg = strbeg;
1778 prog->sublen = PL_regeol - strbeg; /* strend may have been modified */
1785 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1786 PL_colors[4],PL_colors[5]));
1787 if (PL_reg_eval_set)
1788 restore_pos(aTHXo_ 0);
1793 - regtry - try match at specific point
1795 STATIC I32 /* 0 failure, 1 success */
1796 S_regtry(pTHX_ regexp *prog, char *startpos)
1804 PL_regindent = 0; /* XXXX Not good when matches are reenterable... */
1806 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1809 PL_reg_eval_set = RS_init;
1811 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %"IVdf"\n",
1812 (IV)(PL_stack_sp - PL_stack_base));
1814 SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1815 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1816 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1818 /* Apparently this is not needed, judging by wantarray. */
1819 /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1820 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1823 /* Make $_ available to executed code. */
1824 if (PL_reg_sv != DEFSV) {
1825 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1830 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1831 && (mg = mg_find(PL_reg_sv, 'g')))) {
1832 /* prepare for quick setting of pos */
1833 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1834 mg = mg_find(PL_reg_sv, 'g');
1838 PL_reg_oldpos = mg->mg_len;
1839 SAVEDESTRUCTOR_X(restore_pos, 0);
1842 Newz(22,PL_reg_curpm, 1, PMOP);
1843 PL_reg_curpm->op_pmregexp = prog;
1844 PL_reg_oldcurpm = PL_curpm;
1845 PL_curpm = PL_reg_curpm;
1846 if (RX_MATCH_COPIED(prog)) {
1847 /* Here is a serious problem: we cannot rewrite subbeg,
1848 since it may be needed if this match fails. Thus
1849 $` inside (?{}) could fail... */
1850 PL_reg_oldsaved = prog->subbeg;
1851 PL_reg_oldsavedlen = prog->sublen;
1852 RX_MATCH_COPIED_off(prog);
1855 PL_reg_oldsaved = Nullch;
1856 prog->subbeg = PL_bostr;
1857 prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1859 prog->startp[0] = startpos - PL_bostr;
1860 PL_reginput = startpos;
1861 PL_regstartp = prog->startp;
1862 PL_regendp = prog->endp;
1863 PL_reglastparen = &prog->lastparen;
1864 prog->lastparen = 0;
1866 DEBUG_r(PL_reg_starttry = startpos);
1867 if (PL_reg_start_tmpl <= prog->nparens) {
1868 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1869 if(PL_reg_start_tmp)
1870 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1872 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1875 /* XXXX What this code is doing here?!!! There should be no need
1876 to do this again and again, PL_reglastparen should take care of
1879 /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1880 * Actually, the code in regcppop() (which Ilya may be meaning by
1881 * PL_reglastparen), is not needed at all by the test suite
1882 * (op/regexp, op/pat, op/split), but that code is needed, oddly
1883 * enough, for building DynaLoader, or otherwise this
1884 * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1885 * will happen. Meanwhile, this code *is* needed for the
1886 * above-mentioned test suite tests to succeed. The common theme
1887 * on those tests seems to be returning null fields from matches.
1892 if (prog->nparens) {
1893 for (i = prog->nparens; i > *PL_reglastparen; i--) {
1900 if (regmatch(prog->program + 1)) {
1901 prog->endp[0] = PL_reginput - PL_bostr;
1904 REGCP_UNWIND(lastcp);
1908 #define RE_UNWIND_BRANCH 1
1909 #define RE_UNWIND_BRANCHJ 2
1913 typedef struct { /* XX: makes sense to enlarge it... */
1917 } re_unwind_generic_t;
1930 } re_unwind_branch_t;
1932 typedef union re_unwind_t {
1934 re_unwind_generic_t generic;
1935 re_unwind_branch_t branch;
1939 - regmatch - main matching routine
1941 * Conceptually the strategy is simple: check to see whether the current
1942 * node matches, call self recursively to see whether the rest matches,
1943 * and then act accordingly. In practice we make some effort to avoid
1944 * recursion, in particular by going through "ordinary" nodes (that don't
1945 * need to know whether the rest of the match failed) by a loop instead of
1948 /* [lwall] I've hoisted the register declarations to the outer block in order to
1949 * maybe save a little bit of pushing and popping on the stack. It also takes
1950 * advantage of machines that use a register save mask on subroutine entry.
1952 STATIC I32 /* 0 failure, 1 success */
1953 S_regmatch(pTHX_ regnode *prog)
1955 register regnode *scan; /* Current node. */
1956 regnode *next; /* Next node. */
1957 regnode *inner; /* Next node in internal branch. */
1958 register I32 nextchr; /* renamed nextchr - nextchar colides with
1959 function of same name */
1960 register I32 n; /* no or next */
1961 register I32 ln; /* len or last */
1962 register char *s; /* operand or save */
1963 register char *locinput = PL_reginput;
1964 register I32 c1, c2, paren; /* case fold search, parenth */
1965 int minmod = 0, sw = 0, logical = 0;
1967 I32 firstcp = PL_savestack_ix;
1968 register bool do_utf8 = DO_UTF8(PL_reg_sv);
1974 /* Note that nextchr is a byte even in UTF */
1975 nextchr = UCHARAT(locinput);
1977 while (scan != NULL) {
1978 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1980 # define sayYES goto yes
1981 # define sayNO goto no
1982 # define sayYES_FINAL goto yes_final
1983 # define sayYES_LOUD goto yes_loud
1984 # define sayNO_FINAL goto no_final
1985 # define sayNO_SILENT goto do_no
1986 # define saySAME(x) if (x) goto yes; else goto no
1987 # define REPORT_CODE_OFF 24
1989 # define sayYES return 1
1990 # define sayNO return 0
1991 # define sayYES_FINAL return 1
1992 # define sayYES_LOUD return 1
1993 # define sayNO_FINAL return 0
1994 # define sayNO_SILENT return 0
1995 # define saySAME(x) return x
1998 SV *prop = sv_newmortal();
1999 int docolor = *PL_colors[0];
2000 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
2001 int l = (PL_regeol - locinput) > taill ? taill : (PL_regeol - locinput);
2002 /* The part of the string before starttry has one color
2003 (pref0_len chars), between starttry and current
2004 position another one (pref_len - pref0_len chars),
2005 after the current position the third one.
2006 We assume that pref0_len <= pref_len, otherwise we
2007 decrease pref0_len. */
2008 int pref_len = (locinput - PL_bostr) > (5 + taill) - l
2009 ? (5 + taill) - l : locinput - PL_bostr;
2012 while (UTF8_IS_CONTINUATION(*(U8*)(locinput - pref_len)))
2014 pref0_len = pref_len - (locinput - PL_reg_starttry);
2015 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
2016 l = ( PL_regeol - locinput > (5 + taill) - pref_len
2017 ? (5 + taill) - pref_len : PL_regeol - locinput);
2018 while (UTF8_IS_CONTINUATION(*(U8*)(locinput + l)))
2022 if (pref0_len > pref_len)
2023 pref0_len = pref_len;
2024 regprop(prop, scan);
2025 PerlIO_printf(Perl_debug_log,
2026 "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
2027 (IV)(locinput - PL_bostr),
2028 PL_colors[4], pref0_len,
2029 locinput - pref_len, PL_colors[5],
2030 PL_colors[2], pref_len - pref0_len,
2031 locinput - pref_len + pref0_len, PL_colors[3],
2032 (docolor ? "" : "> <"),
2033 PL_colors[0], l, locinput, PL_colors[1],
2034 15 - l - pref_len + 1,
2036 (IV)(scan - PL_regprogram), PL_regindent*2, "",
2040 next = scan + NEXT_OFF(scan);
2046 if (locinput == PL_bostr
2047 ? PL_regprev == '\n'
2049 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2051 /* regtill = regbol; */
2056 if (locinput == PL_bostr
2057 ? PL_regprev == '\n'
2058 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
2064 if (locinput == PL_bostr)
2068 if (locinput == PL_reg_ganch)
2078 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2083 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2085 if (PL_regeol - locinput > 1)
2089 if (PL_regeol != locinput)
2093 if (!nextchr && locinput >= PL_regeol)
2095 nextchr = UCHARAT(++locinput);
2098 if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2101 locinput += PL_utf8skip[nextchr];
2102 if (locinput > PL_regeol)
2104 nextchr = UCHARAT(locinput);
2107 nextchr = UCHARAT(++locinput);
2112 if (do_utf8 != (UTF!=0)) {
2120 if (*((U8*)s) != utf8_to_uvchr((U8*)l, &len))
2129 if (*((U8*)l) != utf8_to_uvchr((U8*)s, &len))
2135 nextchr = UCHARAT(locinput);
2138 /* Inline the first character, for speed. */
2139 if (UCHARAT(s) != nextchr)
2141 if (PL_regeol - locinput < ln)
2143 if (ln > 1 && memNE(s, locinput, ln))
2146 nextchr = UCHARAT(locinput);
2149 PL_reg_flags |= RF_tainted;
2159 c1 = OP(scan) == EXACTF;
2161 if (l >= PL_regeol) {
2164 if ((UTF ? utf8n_to_uvchr((U8*)s, e - s, 0, 0) : *((U8*)s)) !=
2165 (c1 ? toLOWER_utf8((U8*)l) : toLOWER_LC_utf8((U8*)l)))
2167 s += UTF ? UTF8SKIP(s) : 1;
2171 nextchr = UCHARAT(locinput);
2175 /* Inline the first character, for speed. */
2176 if (UCHARAT(s) != nextchr &&
2177 UCHARAT(s) != ((OP(scan) == EXACTF)
2178 ? PL_fold : PL_fold_locale)[nextchr])
2180 if (PL_regeol - locinput < ln)
2182 if (ln > 1 && (OP(scan) == EXACTF
2183 ? ibcmp(s, locinput, ln)
2184 : ibcmp_locale(s, locinput, ln)))
2187 nextchr = UCHARAT(locinput);
2191 if (!reginclass(scan, (U8*)locinput, do_utf8))
2193 if (locinput >= PL_regeol)
2195 locinput += PL_utf8skip[nextchr];
2196 nextchr = UCHARAT(locinput);
2200 nextchr = UCHARAT(locinput);
2201 if (!reginclass(scan, (U8*)locinput, do_utf8))
2203 if (!nextchr && locinput >= PL_regeol)
2205 nextchr = UCHARAT(++locinput);
2209 PL_reg_flags |= RF_tainted;
2215 if (!(OP(scan) == ALNUM
2216 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2217 : isALNUM_LC_utf8((U8*)locinput)))
2221 locinput += PL_utf8skip[nextchr];
2222 nextchr = UCHARAT(locinput);
2225 if (!(OP(scan) == ALNUM
2226 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2228 nextchr = UCHARAT(++locinput);
2231 PL_reg_flags |= RF_tainted;
2234 if (!nextchr && locinput >= PL_regeol)
2237 LOAD_UTF8_CHARCLASS(alnum,"a");
2238 if (OP(scan) == NALNUM
2239 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2240 : isALNUM_LC_utf8((U8*)locinput))
2244 locinput += PL_utf8skip[nextchr];
2245 nextchr = UCHARAT(locinput);
2248 if (OP(scan) == NALNUM
2249 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2251 nextchr = UCHARAT(++locinput);
2255 PL_reg_flags |= RF_tainted;
2259 /* was last char in word? */
2261 if (locinput == PL_regbol)
2264 U8 *r = reghop((U8*)locinput, -1);
2266 ln = utf8n_to_uvchr(r, s - (char*)r, 0, 0);
2268 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2269 ln = isALNUM_uni(ln);
2270 LOAD_UTF8_CHARCLASS(alnum,"a");
2271 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2274 ln = isALNUM_LC_uvchr(UNI_TO_NATIVE(ln));
2275 n = isALNUM_LC_utf8((U8*)locinput);
2279 ln = (locinput != PL_regbol) ?
2280 UCHARAT(locinput - 1) : PL_regprev;
2281 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2283 n = isALNUM(nextchr);
2286 ln = isALNUM_LC(ln);
2287 n = isALNUM_LC(nextchr);
2290 if (((!ln) == (!n)) == (OP(scan) == BOUND ||
2291 OP(scan) == BOUNDL))
2295 PL_reg_flags |= RF_tainted;
2301 if (UTF8_IS_CONTINUED(nextchr)) {
2302 LOAD_UTF8_CHARCLASS(space," ");
2303 if (!(OP(scan) == SPACE
2304 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2305 : isSPACE_LC_utf8((U8*)locinput)))
2309 locinput += PL_utf8skip[nextchr];
2310 nextchr = UCHARAT(locinput);
2313 if (!(OP(scan) == SPACE
2314 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2316 nextchr = UCHARAT(++locinput);
2319 if (!(OP(scan) == SPACE
2320 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2322 nextchr = UCHARAT(++locinput);
2326 PL_reg_flags |= RF_tainted;
2329 if (!nextchr && locinput >= PL_regeol)
2332 LOAD_UTF8_CHARCLASS(space," ");
2333 if (OP(scan) == NSPACE
2334 ? swash_fetch(PL_utf8_space, (U8*)locinput)
2335 : isSPACE_LC_utf8((U8*)locinput))
2339 locinput += PL_utf8skip[nextchr];
2340 nextchr = UCHARAT(locinput);
2343 if (OP(scan) == NSPACE
2344 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2346 nextchr = UCHARAT(++locinput);
2349 PL_reg_flags |= RF_tainted;
2355 LOAD_UTF8_CHARCLASS(digit,"0");
2356 if (!(OP(scan) == DIGIT
2357 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2358 : isDIGIT_LC_utf8((U8*)locinput)))
2362 locinput += PL_utf8skip[nextchr];
2363 nextchr = UCHARAT(locinput);
2366 if (!(OP(scan) == DIGIT
2367 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2369 nextchr = UCHARAT(++locinput);
2372 PL_reg_flags |= RF_tainted;
2375 if (!nextchr && locinput >= PL_regeol)
2378 LOAD_UTF8_CHARCLASS(digit,"0");
2379 if (OP(scan) == NDIGIT
2380 ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2381 : isDIGIT_LC_utf8((U8*)locinput))
2385 locinput += PL_utf8skip[nextchr];
2386 nextchr = UCHARAT(locinput);
2389 if (OP(scan) == NDIGIT
2390 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2392 nextchr = UCHARAT(++locinput);
2395 LOAD_UTF8_CHARCLASS(mark,"~");
2396 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2398 locinput += PL_utf8skip[nextchr];
2399 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2400 locinput += UTF8SKIP(locinput);
2401 if (locinput > PL_regeol)
2403 nextchr = UCHARAT(locinput);
2406 PL_reg_flags |= RF_tainted;
2410 n = ARG(scan); /* which paren pair */
2411 ln = PL_regstartp[n];
2412 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2413 if (*PL_reglastparen < n || ln == -1)
2414 sayNO; /* Do not match unless seen CLOSEn. */
2415 if (ln == PL_regendp[n])
2419 if (do_utf8 && OP(scan) != REF) { /* REF can do byte comparison */
2421 char *e = PL_bostr + PL_regendp[n];
2423 * Note that we can't do the "other character" lookup trick as
2424 * in the 8-bit case (no pun intended) because in Unicode we
2425 * have to map both upper and title case to lower case.
2427 if (OP(scan) == REFF) {
2431 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2441 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2448 nextchr = UCHARAT(locinput);
2452 /* Inline the first character, for speed. */
2453 if (UCHARAT(s) != nextchr &&
2455 (UCHARAT(s) != ((OP(scan) == REFF
2456 ? PL_fold : PL_fold_locale)[nextchr]))))
2458 ln = PL_regendp[n] - ln;
2459 if (locinput + ln > PL_regeol)
2461 if (ln > 1 && (OP(scan) == REF
2462 ? memNE(s, locinput, ln)
2464 ? ibcmp(s, locinput, ln)
2465 : ibcmp_locale(s, locinput, ln))))
2468 nextchr = UCHARAT(locinput);
2479 OP_4tree *oop = PL_op;
2480 COP *ocurcop = PL_curcop;
2481 SV **ocurpad = PL_curpad;
2485 PL_op = (OP_4tree*)PL_regdata->data[n];
2486 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2487 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2488 PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2490 CALLRUNOPS(aTHX); /* Scalar context. */
2496 PL_curpad = ocurpad;
2497 PL_curcop = ocurcop;
2499 if (logical == 2) { /* Postponed subexpression. */
2501 MAGIC *mg = Null(MAGIC*);
2503 CHECKPOINT cp, lastcp;
2505 if(SvROK(ret) || SvRMAGICAL(ret)) {
2506 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2509 mg = mg_find(sv, 'r');
2512 re = (regexp *)mg->mg_obj;
2513 (void)ReREFCNT_inc(re);
2517 char *t = SvPV(ret, len);
2519 char *oprecomp = PL_regprecomp;
2520 I32 osize = PL_regsize;
2521 I32 onpar = PL_regnpar;
2524 re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2526 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2527 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2528 PL_regprecomp = oprecomp;
2533 PerlIO_printf(Perl_debug_log,
2534 "Entering embedded `%s%.60s%s%s'\n",
2538 (strlen(re->precomp) > 60 ? "..." : ""))
2541 state.prev = PL_reg_call_cc;
2542 state.cc = PL_regcc;
2543 state.re = PL_reg_re;
2547 cp = regcppush(0); /* Save *all* the positions. */
2550 state.ss = PL_savestack_ix;
2551 *PL_reglastparen = 0;
2552 PL_reg_call_cc = &state;
2553 PL_reginput = locinput;
2555 /* XXXX This is too dramatic a measure... */
2558 if (regmatch(re->program + 1)) {
2559 /* Even though we succeeded, we need to restore
2560 global variables, since we may be wrapped inside
2561 SUSPEND, thus the match may be not finished yet. */
2563 /* XXXX Do this only if SUSPENDed? */
2564 PL_reg_call_cc = state.prev;
2565 PL_regcc = state.cc;
2566 PL_reg_re = state.re;
2567 cache_re(PL_reg_re);
2569 /* XXXX This is too dramatic a measure... */
2572 /* These are needed even if not SUSPEND. */
2578 REGCP_UNWIND(lastcp);
2580 PL_reg_call_cc = state.prev;
2581 PL_regcc = state.cc;
2582 PL_reg_re = state.re;
2583 cache_re(PL_reg_re);
2585 /* XXXX This is too dramatic a measure... */
2594 sv_setsv(save_scalar(PL_replgv), ret);
2598 n = ARG(scan); /* which paren pair */
2599 PL_reg_start_tmp[n] = locinput;
2604 n = ARG(scan); /* which paren pair */
2605 PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2606 PL_regendp[n] = locinput - PL_bostr;
2607 if (n > *PL_reglastparen)
2608 *PL_reglastparen = n;
2611 n = ARG(scan); /* which paren pair */
2612 sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2615 PL_reg_leftiter = PL_reg_maxiter; /* Void cache */
2617 next = NEXTOPER(NEXTOPER(scan));
2619 next = scan + ARG(scan);
2620 if (OP(next) == IFTHEN) /* Fake one. */
2621 next = NEXTOPER(NEXTOPER(next));
2625 logical = scan->flags;
2627 /*******************************************************************
2628 PL_regcc contains infoblock about the innermost (...)* loop, and
2629 a pointer to the next outer infoblock.
2631 Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2633 1) After matching X, regnode for CURLYX is processed;
2635 2) This regnode creates infoblock on the stack, and calls
2636 regmatch() recursively with the starting point at WHILEM node;
2638 3) Each hit of WHILEM node tries to match A and Z (in the order
2639 depending on the current iteration, min/max of {min,max} and
2640 greediness). The information about where are nodes for "A"
2641 and "Z" is read from the infoblock, as is info on how many times "A"
2642 was already matched, and greediness.
2644 4) After A matches, the same WHILEM node is hit again.
2646 5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2647 of the same pair. Thus when WHILEM tries to match Z, it temporarily
2648 resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2649 as in (Y(A)*Z)*. If Z matches, the automaton will hit the WHILEM node
2650 of the external loop.
2652 Currently present infoblocks form a tree with a stem formed by PL_curcc
2653 and whatever it mentions via ->next, and additional attached trees
2654 corresponding to temporarily unset infoblocks as in "5" above.
2656 In the following picture infoblocks for outer loop of
2657 (Y(A)*?Z)*?T are denoted O, for inner I. NULL starting block
2658 is denoted by x. The matched string is YAAZYAZT. Temporarily postponed
2659 infoblocks are drawn below the "reset" infoblock.
2661 In fact in the picture below we do not show failed matches for Z and T
2662 by WHILEM blocks. [We illustrate minimal matches, since for them it is
2663 more obvious *why* one needs to *temporary* unset infoblocks.]
2665 Matched REx position InfoBlocks Comment
2669 Y A)*?Z)*?T x <- O <- I
2670 YA )*?Z)*?T x <- O <- I
2671 YA A)*?Z)*?T x <- O <- I
2672 YAA )*?Z)*?T x <- O <- I
2673 YAA Z)*?T x <- O # Temporary unset I
2676 YAAZ Y(A)*?Z)*?T x <- O
2679 YAAZY (A)*?Z)*?T x <- O
2682 YAAZY A)*?Z)*?T x <- O <- I
2685 YAAZYA )*?Z)*?T x <- O <- I
2688 YAAZYA Z)*?T x <- O # Temporary unset I
2694 YAAZYAZ T x # Temporary unset O
2701 *******************************************************************/
2704 CHECKPOINT cp = PL_savestack_ix;
2705 /* No need to save/restore up to this paren */
2706 I32 parenfloor = scan->flags;
2708 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2710 cc.oldcc = PL_regcc;
2712 /* XXXX Probably it is better to teach regpush to support
2713 parenfloor > PL_regsize... */
2714 if (parenfloor > *PL_reglastparen)
2715 parenfloor = *PL_reglastparen; /* Pessimization... */
2716 cc.parenfloor = parenfloor;
2718 cc.min = ARG1(scan);
2719 cc.max = ARG2(scan);
2720 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2724 PL_reginput = locinput;
2725 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
2727 PL_regcc = cc.oldcc;
2733 * This is really hard to understand, because after we match
2734 * what we're trying to match, we must make sure the rest of
2735 * the REx is going to match for sure, and to do that we have
2736 * to go back UP the parse tree by recursing ever deeper. And
2737 * if it fails, we have to reset our parent's current state
2738 * that we can try again after backing off.
2741 CHECKPOINT cp, lastcp;
2742 CURCUR* cc = PL_regcc;
2743 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2745 n = cc->cur + 1; /* how many we know we matched */
2746 PL_reginput = locinput;
2749 PerlIO_printf(Perl_debug_log,
2750 "%*s %ld out of %ld..%ld cc=%lx\n",
2751 REPORT_CODE_OFF+PL_regindent*2, "",
2752 (long)n, (long)cc->min,
2753 (long)cc->max, (long)cc)
2756 /* If degenerate scan matches "", assume scan done. */
2758 if (locinput == cc->lastloc && n >= cc->min) {
2759 PL_regcc = cc->oldcc;
2763 PerlIO_printf(Perl_debug_log,
2764 "%*s empty match detected, try continuation...\n",
2765 REPORT_CODE_OFF+PL_regindent*2, "")
2767 if (regmatch(cc->next))
2775 /* First just match a string of min scans. */
2779 cc->lastloc = locinput;
2780 if (regmatch(cc->scan))
2783 cc->lastloc = lastloc;
2788 /* Check whether we already were at this position.
2789 Postpone detection until we know the match is not
2790 *that* much linear. */
2791 if (!PL_reg_maxiter) {
2792 PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2793 PL_reg_leftiter = PL_reg_maxiter;
2795 if (PL_reg_leftiter-- == 0) {
2796 I32 size = (PL_reg_maxiter + 7)/8;
2797 if (PL_reg_poscache) {
2798 if (PL_reg_poscache_size < size) {
2799 Renew(PL_reg_poscache, size, char);
2800 PL_reg_poscache_size = size;
2802 Zero(PL_reg_poscache, size, char);
2805 PL_reg_poscache_size = size;
2806 Newz(29, PL_reg_poscache, size, char);
2809 PerlIO_printf(Perl_debug_log,
2810 "%sDetected a super-linear match, switching on caching%s...\n",
2811 PL_colors[4], PL_colors[5])
2814 if (PL_reg_leftiter < 0) {
2815 I32 o = locinput - PL_bostr, b;
2817 o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2820 if (PL_reg_poscache[o] & (1<<b)) {
2822 PerlIO_printf(Perl_debug_log,
2823 "%*s already tried at this position...\n",
2824 REPORT_CODE_OFF+PL_regindent*2, "")
2828 PL_reg_poscache[o] |= (1<<b);
2832 /* Prefer next over scan for minimal matching. */
2835 PL_regcc = cc->oldcc;
2838 cp = regcppush(cc->parenfloor);
2840 if (regmatch(cc->next)) {
2842 sayYES; /* All done. */
2844 REGCP_UNWIND(lastcp);
2850 if (n >= cc->max) { /* Maximum greed exceeded? */
2851 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2852 && !(PL_reg_flags & RF_warned)) {
2853 PL_reg_flags |= RF_warned;
2854 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2855 "Complex regular subexpression recursion",
2862 PerlIO_printf(Perl_debug_log,
2863 "%*s trying longer...\n",
2864 REPORT_CODE_OFF+PL_regindent*2, "")
2866 /* Try scanning more and see if it helps. */
2867 PL_reginput = locinput;
2869 cc->lastloc = locinput;
2870 cp = regcppush(cc->parenfloor);
2872 if (regmatch(cc->scan)) {
2876 REGCP_UNWIND(lastcp);
2879 cc->lastloc = lastloc;
2883 /* Prefer scan over next for maximal matching. */
2885 if (n < cc->max) { /* More greed allowed? */
2886 cp = regcppush(cc->parenfloor);
2888 cc->lastloc = locinput;
2890 if (regmatch(cc->scan)) {
2894 REGCP_UNWIND(lastcp);
2895 regcppop(); /* Restore some previous $<digit>s? */
2896 PL_reginput = locinput;
2898 PerlIO_printf(Perl_debug_log,
2899 "%*s failed, try continuation...\n",
2900 REPORT_CODE_OFF+PL_regindent*2, "")
2903 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY
2904 && !(PL_reg_flags & RF_warned)) {
2905 PL_reg_flags |= RF_warned;
2906 Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2907 "Complex regular subexpression recursion",
2911 /* Failed deeper matches of scan, so see if this one works. */
2912 PL_regcc = cc->oldcc;
2915 if (regmatch(cc->next))
2921 cc->lastloc = lastloc;
2926 next = scan + ARG(scan);
2929 inner = NEXTOPER(NEXTOPER(scan));
2932 inner = NEXTOPER(scan);
2937 if (OP(next) != c1) /* No choice. */
2938 next = inner; /* Avoid recursion. */
2940 I32 lastparen = *PL_reglastparen;
2942 re_unwind_branch_t *uw;
2944 /* Put unwinding data on stack */
2945 unwind1 = SSNEWt(1,re_unwind_branch_t);
2946 uw = SSPTRt(unwind1,re_unwind_branch_t);
2949 uw->type = ((c1 == BRANCH)
2951 : RE_UNWIND_BRANCHJ);
2952 uw->lastparen = lastparen;
2954 uw->locinput = locinput;
2955 uw->nextchr = nextchr;
2957 uw->regindent = ++PL_regindent;
2960 REGCP_SET(uw->lastcp);
2962 /* Now go into the first branch */
2975 /* We suppose that the next guy does not need
2976 backtracking: in particular, it is of constant length,
2977 and has no parenths to influence future backrefs. */
2978 ln = ARG1(scan); /* min to match */
2979 n = ARG2(scan); /* max to match */
2980 paren = scan->flags;
2982 if (paren > PL_regsize)
2984 if (paren > *PL_reglastparen)
2985 *PL_reglastparen = paren;
2987 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2989 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2990 PL_reginput = locinput;
2993 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2995 if (ln && l == 0 && n >= ln
2996 /* In fact, this is tricky. If paren, then the
2997 fact that we did/didnot match may influence
2998 future execution. */
2999 && !(paren && ln == 0))
3001 locinput = PL_reginput;
3002 if (PL_regkind[(U8)OP(next)] == EXACT) {
3003 c1 = (U8)*STRING(next);
3004 if (OP(next) == EXACTF)
3006 else if (OP(next) == EXACTFL)
3007 c2 = PL_fold_locale[c1];
3014 /* This may be improved if l == 0. */
3015 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
3016 /* If it could work, try it. */
3018 UCHARAT(PL_reginput) == c1 ||
3019 UCHARAT(PL_reginput) == c2)
3023 PL_regstartp[paren] =
3024 HOPc(PL_reginput, -l) - PL_bostr;
3025 PL_regendp[paren] = PL_reginput - PL_bostr;
3028 PL_regendp[paren] = -1;
3032 REGCP_UNWIND(lastcp);
3034 /* Couldn't or didn't -- move forward. */
3035 PL_reginput = locinput;
3036 if (regrepeat_hard(scan, 1, &l)) {
3038 locinput = PL_reginput;
3045 n = regrepeat_hard(scan, n, &l);
3046 if (n != 0 && l == 0
3047 /* In fact, this is tricky. If paren, then the
3048 fact that we did/didnot match may influence
3049 future execution. */
3050 && !(paren && ln == 0))
3052 locinput = PL_reginput;
3054 PerlIO_printf(Perl_debug_log,
3055 "%*s matched %"IVdf" times, len=%"IVdf"...\n",
3056 (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3060 if (PL_regkind[(U8)OP(next)] == EXACT) {
3061 c1 = (U8)*STRING(next);
3062 if (OP(next) == EXACTF)
3064 else if (OP(next) == EXACTFL)
3065 c2 = PL_fold_locale[c1];
3074 /* If it could work, try it. */
3076 UCHARAT(PL_reginput) == c1 ||
3077 UCHARAT(PL_reginput) == c2)
3080 PerlIO_printf(Perl_debug_log,
3081 "%*s trying tail with n=%"IVdf"...\n",
3082 (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3086 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3087 PL_regendp[paren] = PL_reginput - PL_bostr;
3090 PL_regendp[paren] = -1;
3094 REGCP_UNWIND(lastcp);
3096 /* Couldn't or didn't -- back up. */
3098 locinput = HOPc(locinput, -l);
3099 PL_reginput = locinput;
3106 paren = scan->flags; /* Which paren to set */
3107 if (paren > PL_regsize)
3109 if (paren > *PL_reglastparen)
3110 *PL_reglastparen = paren;
3111 ln = ARG1(scan); /* min to match */
3112 n = ARG2(scan); /* max to match */
3113 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3117 ln = ARG1(scan); /* min to match */
3118 n = ARG2(scan); /* max to match */
3119 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3124 scan = NEXTOPER(scan);
3130 scan = NEXTOPER(scan);
3134 * Lookahead to avoid useless match attempts
3135 * when we know what character comes next.
3137 if (PL_regkind[(U8)OP(next)] == EXACT) {
3138 U8 *s = (U8*)STRING(next);
3141 if (OP(next) == EXACTF)
3143 else if (OP(next) == EXACTFL)
3144 c2 = PL_fold_locale[c1];
3147 if (OP(next) == EXACTF) {
3148 c1 = to_utf8_lower(s);
3149 c2 = to_utf8_upper(s);
3152 c2 = c1 = utf8_to_uvchr(s, NULL);
3158 PL_reginput = locinput;
3162 if (ln && regrepeat(scan, ln) < ln)
3164 locinput = PL_reginput;
3167 char *e; /* Should not check after this */
3168 char *old = locinput;
3170 if (n == REG_INFTY) {
3173 while (UTF8_IS_CONTINUATION(*(U8*)e))
3179 m >0 && e + UTF8SKIP(e) <= PL_regeol; m--)
3183 e = locinput + n - ln;
3189 /* Find place 'next' could work */
3192 while (locinput <= e && *locinput != c1)
3195 while (locinput <= e
3200 count = locinput - old;
3207 utf8_to_uvchr((U8*)locinput, &len) != c1;
3212 for (count = 0; locinput <= e; count++) {
3213 UV c = utf8_to_uvchr((U8*)locinput, &len);
3214 if (c == c1 || c == c2)
3222 /* PL_reginput == old now */
3223 if (locinput != old) {
3224 ln = 1; /* Did some */
3225 if (regrepeat(scan, count) < count)
3228 /* PL_reginput == locinput now */
3229 TRYPAREN(paren, ln, locinput);
3230 PL_reginput = locinput; /* Could be reset... */
3231 REGCP_UNWIND(lastcp);
3232 /* Couldn't or didn't -- move forward. */
3235 locinput += UTF8SKIP(locinput);
3241 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3245 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3247 c = UCHARAT(PL_reginput);
3249 /* If it could work, try it. */
3250 if (c1 == -1000 || c == c1 || c == c2)
3252 TRYPAREN(paren, n, PL_reginput);
3253 REGCP_UNWIND(lastcp);
3255 /* Couldn't or didn't -- move forward. */
3256 PL_reginput = locinput;
3257 if (regrepeat(scan, 1)) {
3259 locinput = PL_reginput;
3267 n = regrepeat(scan, n);
3268 locinput = PL_reginput;
3269 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3270 (!PL_multiline || OP(next) == SEOL || OP(next) == EOS)) {
3271 ln = n; /* why back off? */
3272 /* ...because $ and \Z can match before *and* after
3273 newline at the end. Consider "\n\n" =~ /\n+\Z\n/.
3274 We should back off by one in this case. */
3275 if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3284 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3286 c = UCHARAT(PL_reginput);
3288 /* If it could work, try it. */
3289 if (c1 == -1000 || c == c1 || c == c2)
3291 TRYPAREN(paren, n, PL_reginput);
3292 REGCP_UNWIND(lastcp);
3294 /* Couldn't or didn't -- back up. */
3296 PL_reginput = locinput = HOPc(locinput, -1);
3304 c = utf8_to_uvchr((U8*)PL_reginput, NULL);
3306 c = UCHARAT(PL_reginput);
3308 /* If it could work, try it. */
3309 if (c1 == -1000 || c == c1 || c == c2)
3311 TRYPAREN(paren, n, PL_reginput);
3312 REGCP_UNWIND(lastcp);
3314 /* Couldn't or didn't -- back up. */
3316 PL_reginput = locinput = HOPc(locinput, -1);
3323 if (PL_reg_call_cc) {
3324 re_cc_state *cur_call_cc = PL_reg_call_cc;
3325 CURCUR *cctmp = PL_regcc;
3326 regexp *re = PL_reg_re;
3327 CHECKPOINT cp, lastcp;
3329 cp = regcppush(0); /* Save *all* the positions. */
3331 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3333 PL_reginput = locinput; /* Make position available to
3335 cache_re(PL_reg_call_cc->re);
3336 PL_regcc = PL_reg_call_cc->cc;
3337 PL_reg_call_cc = PL_reg_call_cc->prev;
3338 if (regmatch(cur_call_cc->node)) {
3339 PL_reg_call_cc = cur_call_cc;
3343 REGCP_UNWIND(lastcp);
3345 PL_reg_call_cc = cur_call_cc;
3351 PerlIO_printf(Perl_debug_log,
3352 "%*s continuation failed...\n",
3353 REPORT_CODE_OFF+PL_regindent*2, "")
3357 if (locinput < PL_regtill) {
3358 DEBUG_r(PerlIO_printf(Perl_debug_log,
3359 "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3361 (long)(locinput - PL_reg_starttry),
3362 (long)(PL_regtill - PL_reg_starttry),
3364 sayNO_FINAL; /* Cannot match: too short. */
3366 PL_reginput = locinput; /* put where regtry can find it */
3367 sayYES_FINAL; /* Success! */
3369 PL_reginput = locinput; /* put where regtry can find it */
3370 sayYES_LOUD; /* Success! */
3373 PL_reginput = locinput;
3378 if (UTF) { /* XXXX This is absolutely
3379 broken, we read before
3381 s = HOPMAYBEc(locinput, -scan->flags);
3387 if (locinput < PL_bostr + scan->flags)
3389 PL_reginput = locinput - scan->flags;
3394 PL_reginput = locinput;
3399 if (UTF) { /* XXXX This is absolutely
3400 broken, we read before
3402 s = HOPMAYBEc(locinput, -scan->flags);
3403 if (!s || s < PL_bostr)
3408 if (locinput < PL_bostr + scan->flags)
3410 PL_reginput = locinput - scan->flags;
3415 PL_reginput = locinput;
3418 inner = NEXTOPER(NEXTOPER(scan));
3419 if (regmatch(inner) != n) {
3434 if (OP(scan) == SUSPEND) {
3435 locinput = PL_reginput;
3436 nextchr = UCHARAT(locinput);
3441 next = scan + ARG(scan);
3446 PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3447 PTR2UV(scan), OP(scan));
3448 Perl_croak(aTHX_ "regexp memory corruption");
3455 * We get here only if there's trouble -- normally "case END" is
3456 * the terminating point.
3458 Perl_croak(aTHX_ "corrupted regexp pointers");
3464 PerlIO_printf(Perl_debug_log,
3465 "%*s %scould match...%s\n",
3466 REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3470 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3471 PL_colors[4],PL_colors[5]));
3477 #if 0 /* Breaks $^R */
3485 PerlIO_printf(Perl_debug_log,
3486 "%*s %sfailed...%s\n",
3487 REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3493 re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3496 case RE_UNWIND_BRANCH:
3497 case RE_UNWIND_BRANCHJ:
3499 re_unwind_branch_t *uwb = &(uw->branch);
3500 I32 lastparen = uwb->lastparen;
3502 REGCP_UNWIND(uwb->lastcp);
3503 for (n = *PL_reglastparen; n > lastparen; n--)
3505 *PL_reglastparen = n;
3506 scan = next = uwb->next;
3508 OP(scan) != (uwb->type == RE_UNWIND_BRANCH
3509 ? BRANCH : BRANCHJ) ) { /* Failure */
3516 /* Have more choice yet. Reuse the same uwb. */
3518 if ((n = (uwb->type == RE_UNWIND_BRANCH
3519 ? NEXT_OFF(next) : ARG(next))))
3522 next = NULL; /* XXXX Needn't unwinding in this case... */
3524 next = NEXTOPER(scan);
3525 if (uwb->type == RE_UNWIND_BRANCHJ)
3526 next = NEXTOPER(next);
3527 locinput = uwb->locinput;
3528 nextchr = uwb->nextchr;
3530 PL_regindent = uwb->regindent;
3537 Perl_croak(aTHX_ "regexp unwind memory corruption");
3548 - regrepeat - repeatedly match something simple, report how many
3551 * [This routine now assumes that it will only match on things of length 1.
3552 * That was true before, but now we assume scan - reginput is the count,
3553 * rather than incrementing count on every character. [Er, except utf8.]]
3556 S_regrepeat(pTHX_ regnode *p, I32 max)
3558 register char *scan;
3560 register char *loceol = PL_regeol;
3561 register I32 hardcount = 0;
3562 register bool do_utf8 = DO_UTF8(PL_reg_sv);
3565 if (max != REG_INFTY && max < loceol - scan)
3566 loceol = scan + max;
3571 while (scan < loceol && hardcount < max && *scan != '\n') {
3572 scan += UTF8SKIP(scan);
3576 while (scan < loceol && *scan != '\n')
3583 case EXACT: /* length of string is 1 */
3585 while (scan < loceol && UCHARAT(scan) == c)
3588 case EXACTF: /* length of string is 1 */
3590 while (scan < loceol &&
3591 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3594 case EXACTFL: /* length of string is 1 */
3595 PL_reg_flags |= RF_tainted;
3597 while (scan < loceol &&
3598 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3604 while (hardcount < max && scan < loceol &&
3605 reginclass(p, (U8*)scan, do_utf8)) {
3606 scan += UTF8SKIP(scan);
3610 while (scan < loceol && reginclass(p, (U8*)scan, do_utf8))
3617 LOAD_UTF8_CHARCLASS(alnum,"a");
3618 while (hardcount < max && scan < loceol &&
3619 swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3620 scan += UTF8SKIP(scan);
3624 while (scan < loceol && isALNUM(*scan))
3629 PL_reg_flags |= RF_tainted;
3632 while (hardcount < max && scan < loceol &&
3633 isALNUM_LC_utf8((U8*)scan)) {
3634 scan += UTF8SKIP(scan);
3638 while (scan < loceol && isALNUM_LC(*scan))
3645 LOAD_UTF8_CHARCLASS(alnum,"a");
3646 while (hardcount < max && scan < loceol &&
3647 !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3648 scan += UTF8SKIP(scan);
3652 while (scan < loceol && !isALNUM(*scan))
3657 PL_reg_flags |= RF_tainted;
3660 while (hardcount < max && scan < loceol &&
3661 !isALNUM_LC_utf8((U8*)scan)) {
3662 scan += UTF8SKIP(scan);
3666 while (scan < loceol && !isALNUM_LC(*scan))
3673 LOAD_UTF8_CHARCLASS(space," ");
3674 while (hardcount < max && scan < loceol &&
3675 (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3676 scan += UTF8SKIP(scan);
3680 while (scan < loceol && isSPACE(*scan))
3685 PL_reg_flags |= RF_tainted;
3688 while (hardcount < max && scan < loceol &&
3689 (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3690 scan += UTF8SKIP(scan);
3694 while (scan < loceol && isSPACE_LC(*scan))
3701 LOAD_UTF8_CHARCLASS(space," ");
3702 while (hardcount < max && scan < loceol &&
3703 !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3704 scan += UTF8SKIP(scan);
3708 while (scan < loceol && !isSPACE(*scan))
3713 PL_reg_flags |= RF_tainted;
3716 while (hardcount < max && scan < loceol &&
3717 !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3718 scan += UTF8SKIP(scan);
3722 while (scan < loceol && !isSPACE_LC(*scan))
3729 LOAD_UTF8_CHARCLASS(digit,"0");
3730 while (hardcount < max && scan < loceol &&
3731 swash_fetch(PL_utf8_digit,(U8*)scan)) {
3732 scan += UTF8SKIP(scan);
3736 while (scan < loceol && isDIGIT(*scan))
3743 LOAD_UTF8_CHARCLASS(digit,"0");
3744 while (hardcount < max && scan < loceol &&
3745 !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3746 scan += UTF8SKIP(scan);
3750 while (scan < loceol && !isDIGIT(*scan))
3754 default: /* Called on something of 0 width. */
3755 break; /* So match right here or not at all. */
3761 c = scan - PL_reginput;
3766 SV *prop = sv_newmortal();
3769 PerlIO_printf(Perl_debug_log,
3770 "%*s %s can match %"IVdf" times out of %"IVdf"...\n",
3771 REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3778 - regrepeat_hard - repeatedly match something, report total lenth and length
3780 * The repeater is supposed to have constant length.
3784 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3786 register char *scan;
3787 register char *start;
3788 register char *loceol = PL_regeol;
3790 I32 count = 0, res = 1;
3795 start = PL_reginput;
3796 if (DO_UTF8(PL_reg_sv)) {
3797 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3800 while (start < PL_reginput) {
3802 start += UTF8SKIP(start);
3813 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3815 *lp = l = PL_reginput - start;
3816 if (max != REG_INFTY && l*max < loceol - scan)
3817 loceol = scan + l*max;
3830 - regclass_swash - prepare the utf8 swash
3834 Perl_regclass_swash(pTHX_ register regnode* node, bool doinit, SV** initsvp)
3839 if (PL_regdata && PL_regdata->count) {
3842 if (PL_regdata->what[n] == 's') {
3843 SV *rv = (SV*)PL_regdata->data[n];
3844 AV *av = (AV*)SvRV((SV*)rv);
3847 si = *av_fetch(av, 0, FALSE);
3848 a = av_fetch(av, 1, FALSE);
3852 else if (si && doinit) {
3853 sw = swash_init("utf8", "", si, 1, 0);
3854 (void)av_store(av, 1, sw);
3866 - reginclass - determine if a character falls into a character class
3870 S_reginclass(pTHX_ register regnode *n, register U8* p, register bool do_utf8)
3872 char flags = ANYOF_FLAGS(n);
3878 c = utf8_to_uvchr(p, &len);
3882 if (do_utf8 || (flags & ANYOF_UNICODE)) {
3883 if (do_utf8 && !ANYOF_RUNTIME(n)) {
3884 if (len != (STRLEN)-1 && c < 256 && ANYOF_BITMAP_TEST(n, c))
3887 if (do_utf8 && flags & ANYOF_UNICODE_ALL && c >= 256)
3890 SV *sw = regclass_swash(n, TRUE, 0);
3893 if (swash_fetch(sw, p))
3895 else if (flags & ANYOF_FOLD) {
3896 U8 tmpbuf[UTF8_MAXLEN+1];
3898 if (flags & ANYOF_LOCALE) {
3899 PL_reg_flags |= RF_tainted;
3900 uvchr_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3903 uvchr_to_utf8(tmpbuf, toLOWER_utf8(p));
3904 if (swash_fetch(sw, tmpbuf))
3910 if (!match && c < 256) {
3911 if (ANYOF_BITMAP_TEST(n, c))
3913 else if (flags & ANYOF_FOLD) {
3916 if (flags & ANYOF_LOCALE) {
3917 PL_reg_flags |= RF_tainted;
3918 f = PL_fold_locale[c];
3922 if (f != c && ANYOF_BITMAP_TEST(n, f))
3926 if (!match && (flags & ANYOF_CLASS)) {
3927 PL_reg_flags |= RF_tainted;
3929 (ANYOF_CLASS_TEST(n, ANYOF_ALNUM) && isALNUM_LC(c)) ||
3930 (ANYOF_CLASS_TEST(n, ANYOF_NALNUM) && !isALNUM_LC(c)) ||
3931 (ANYOF_CLASS_TEST(n, ANYOF_SPACE) && isSPACE_LC(c)) ||
3932 (ANYOF_CLASS_TEST(n, ANYOF_NSPACE) && !isSPACE_LC(c)) ||
3933 (ANYOF_CLASS_TEST(n, ANYOF_DIGIT) && isDIGIT_LC(c)) ||
3934 (ANYOF_CLASS_TEST(n, ANYOF_NDIGIT) && !isDIGIT_LC(c)) ||
3935 (ANYOF_CLASS_TEST(n, ANYOF_ALNUMC) && isALNUMC_LC(c)) ||
3936 (ANYOF_CLASS_TEST(n, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3937 (ANYOF_CLASS_TEST(n, ANYOF_ALPHA) && isALPHA_LC(c)) ||
3938 (ANYOF_CLASS_TEST(n, ANYOF_NALPHA) && !isALPHA_LC(c)) ||
3939 (ANYOF_CLASS_TEST(n, ANYOF_ASCII) && isASCII(c)) ||
3940 (ANYOF_CLASS_TEST(n, ANYOF_NASCII) && !isASCII(c)) ||
3941 (ANYOF_CLASS_TEST(n, ANYOF_CNTRL) && isCNTRL_LC(c)) ||
3942 (ANYOF_CLASS_TEST(n, ANYOF_NCNTRL) && !isCNTRL_LC(c)) ||
3943 (ANYOF_CLASS_TEST(n, ANYOF_GRAPH) && isGRAPH_LC(c)) ||
3944 (ANYOF_CLASS_TEST(n, ANYOF_NGRAPH) && !isGRAPH_LC(c)) ||
3945 (ANYOF_CLASS_TEST(n, ANYOF_LOWER) && isLOWER_LC(c)) ||
3946 (ANYOF_CLASS_TEST(n, ANYOF_NLOWER) && !isLOWER_LC(c)) ||
3947 (ANYOF_CLASS_TEST(n, ANYOF_PRINT) && isPRINT_LC(c)) ||
3948 (ANYOF_CLASS_TEST(n, ANYOF_NPRINT) && !isPRINT_LC(c)) ||
3949 (ANYOF_CLASS_TEST(n, ANYOF_PUNCT) && isPUNCT_LC(c)) ||
3950 (ANYOF_CLASS_TEST(n, ANYOF_NPUNCT) && !isPUNCT_LC(c)) ||
3951 (ANYOF_CLASS_TEST(n, ANYOF_UPPER) && isUPPER_LC(c)) ||
3952 (ANYOF_CLASS_TEST(n, ANYOF_NUPPER) && !isUPPER_LC(c)) ||
3953 (ANYOF_CLASS_TEST(n, ANYOF_XDIGIT) && isXDIGIT(c)) ||
3954 (ANYOF_CLASS_TEST(n, ANYOF_NXDIGIT) && !isXDIGIT(c)) ||
3955 (ANYOF_CLASS_TEST(n, ANYOF_PSXSPC) && isPSXSPC(c)) ||
3956 (ANYOF_CLASS_TEST(n, ANYOF_NPSXSPC) && !isPSXSPC(c)) ||
3957 (ANYOF_CLASS_TEST(n, ANYOF_BLANK) && isBLANK(c)) ||
3958 (ANYOF_CLASS_TEST(n, ANYOF_NBLANK) && !isBLANK(c))
3959 ) /* How's that for a conditional? */
3966 return (flags & ANYOF_INVERT) ? !match : match;
3970 S_reghop(pTHX_ U8 *s, I32 off)
3972 return S_reghop3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
3976 S_reghop3(pTHX_ U8 *s, I32 off, U8* lim)
3979 while (off-- && s < lim) {
3980 /* XXX could check well-formedness here */
3988 if (UTF8_IS_CONTINUED(*s)) {
3989 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
3992 /* XXX could check well-formedness here */
4000 S_reghopmaybe(pTHX_ U8 *s, I32 off)
4002 return S_reghopmaybe3(aTHX_ s, off, (U8*)(off >= 0 ? PL_regeol : PL_bostr));
4006 S_reghopmaybe3(pTHX_ U8* s, I32 off, U8* lim)
4009 while (off-- && s < lim) {
4010 /* XXX could check well-formedness here */
4020 if (UTF8_IS_CONTINUED(*s)) {
4021 while (s > (U8*)lim && UTF8_IS_CONTINUATION(*s))
4024 /* XXX could check well-formedness here */
4040 restore_pos(pTHXo_ void *arg)
4042 if (PL_reg_eval_set) {
4043 if (PL_reg_oldsaved) {
4044 PL_reg_re->subbeg = PL_reg_oldsaved;
4045 PL_reg_re->sublen = PL_reg_oldsavedlen;
4046 RX_MATCH_COPIED_on(PL_reg_re);
4048 PL_reg_magic->mg_len = PL_reg_oldpos;
4049 PL_reg_eval_set = 0;
4050 PL_curpm = PL_reg_oldcurpm;