This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [ID 20001129.009] Not OK: perl v5.7.0 +DEVEL7928 on os2-64int-ld 2.30 (UNINSTALLED)
[perl5.git] / regexec.c
1 /*    regexec.c
2  */
3
4 /*
5  * "One Ring to rule them all, One Ring to find them..."
6  */
7
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!
10  */
11
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.
15  */
16
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.
20 */
21
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
26 #  endif
27 /* need access to debugger hooks */
28 #  if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
29 #    define DEBUGGING
30 #  endif
31 #endif
32
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
43 #  define PERL_NO_GET_CONTEXT
44 #endif 
45
46 /*SUPPRESS 112*/
47 /*
48  * pregcomp and pregexec -- regsub and regerror are not used in perl
49  *
50  *      Copyright (c) 1986 by University of Toronto.
51  *      Written by Henry Spencer.  Not derived from licensed software.
52  *
53  *      Permission is granted to anyone to use this software for any
54  *      purpose on any computer system, and to redistribute it freely,
55  *      subject to the following restrictions:
56  *
57  *      1. The author is not responsible for the consequences of use of
58  *              this software, no matter how awful, even if they arise
59  *              from defects in it.
60  *
61  *      2. The origin of this software must not be misrepresented, either
62  *              by explicit claim or by omission.
63  *
64  *      3. Altered versions must be plainly marked as such, and must not
65  *              be misrepresented as being the original software.
66  *
67  ****    Alterations to Henry's code are...
68  ****
69  ****    Copyright (c) 1991-2000, Larry Wall
70  ****
71  ****    You may distribute under the terms of either the GNU General Public
72  ****    License or the Artistic License, as specified in the README file.
73  *
74  * Beware that some of this code is subtly aware of the way operator
75  * precedence is structured in regular expressions.  Serious changes in
76  * regular-expression syntax might require a total rethink.
77  */
78 #include "EXTERN.h"
79 #define PERL_IN_REGEXEC_C
80 #include "perl.h"
81
82 #ifdef PERL_IN_XSUB_RE
83 #  if defined(PERL_CAPI) || defined(PERL_OBJECT)
84 #    include "XSUB.h"
85 #  endif
86 #endif
87
88 #include "regcomp.h"
89
90 #define RF_tainted      1               /* tainted information used? */
91 #define RF_warned       2               /* warned about big count? */
92 #define RF_evaled       4               /* Did an EVAL with setting? */
93 #define RF_utf8         8               /* String contains multibyte chars? */
94
95 #define UTF (PL_reg_flags & RF_utf8)
96
97 #define RS_init         1               /* eval environment created */
98 #define RS_set          2               /* replsv value is set */
99
100 #ifndef STATIC
101 #define STATIC  static
102 #endif
103
104 /*
105  * Forwards.
106  */
107
108 #define REGINCLASS(p,c)  (ANYOF_FLAGS(p) ? reginclass(p,c) : ANYOF_BITMAP_TEST(p,c))
109 #ifdef DEBUGGING
110 #   define REGINCLASSUTF8(f,p)  (ARG1(f) ? reginclassutf8(f,p) : swash_fetch(*av_fetch((AV*)SvRV((SV*)PL_regdata->data[ARG2(f)]),0,FALSE),p))
111 #else
112 #   define REGINCLASSUTF8(f,p)  (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
113 #endif
114
115 #define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
116 #define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
117
118 #define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
119 #define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
120 #define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
121 #define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
122 #define HOPc(pos,off) ((char*)HOP(pos,off))
123 #define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
124
125 static void restore_pos(pTHXo_ void *arg);
126
127
128 STATIC CHECKPOINT
129 S_regcppush(pTHX_ I32 parenfloor)
130 {
131     dTHR;
132     int retval = PL_savestack_ix;
133     int i = (PL_regsize - parenfloor) * 4;
134     int p;
135
136     SSCHECK(i + 5);
137     for (p = PL_regsize; p > parenfloor; p--) {
138         SSPUSHINT(PL_regendp[p]);
139         SSPUSHINT(PL_regstartp[p]);
140         SSPUSHPTR(PL_reg_start_tmp[p]);
141         SSPUSHINT(p);
142     }
143     SSPUSHINT(PL_regsize);
144     SSPUSHINT(*PL_reglastparen);
145     SSPUSHPTR(PL_reginput);
146     SSPUSHINT(i + 3);
147     SSPUSHINT(SAVEt_REGCONTEXT);
148     return retval;
149 }
150
151 /* These are needed since we do not localize EVAL nodes: */
152 #  define REGCP_SET(cp)  DEBUG_r(PerlIO_printf(Perl_debug_log,          \
153                              "  Setting an EVAL scope, savestack=%"IVdf"\n",    \
154                              (IV)PL_savestack_ix)); cp = PL_savestack_ix
155
156 #  define REGCP_UNWIND(cp)  DEBUG_r(cp != PL_savestack_ix ?             \
157                                 PerlIO_printf(Perl_debug_log,           \
158                                 "  Clearing an EVAL scope, savestack=%"IVdf"..%"IVdf"\n", \
159                                 (IV)(cp), (IV)PL_savestack_ix) : 0); regcpblow(cp)
160
161 STATIC char *
162 S_regcppop(pTHX)
163 {
164     dTHR;
165     I32 i = SSPOPINT;
166     U32 paren = 0;
167     char *input;
168     I32 tmps;
169     assert(i == SAVEt_REGCONTEXT);
170     i = SSPOPINT;
171     input = (char *) SSPOPPTR;
172     *PL_reglastparen = SSPOPINT;
173     PL_regsize = SSPOPINT;
174     for (i -= 3; i > 0; i -= 4) {
175         paren = (U32)SSPOPINT;
176         PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
177         PL_regstartp[paren] = SSPOPINT;
178         tmps = SSPOPINT;
179         if (paren <= *PL_reglastparen)
180             PL_regendp[paren] = tmps;
181         DEBUG_r(
182             PerlIO_printf(Perl_debug_log,
183                           "     restoring \\%"UVuf" to %"IVdf"(%"IVdf")..%"IVdf"%s\n",
184                           (UV)paren, (IV)PL_regstartp[paren], 
185                           (IV)(PL_reg_start_tmp[paren] - PL_bostr),
186                           (IV)PL_regendp[paren], 
187                           (paren > *PL_reglastparen ? "(no)" : ""));
188         );
189     }
190     DEBUG_r(
191         if (*PL_reglastparen + 1 <= PL_regnpar) {
192             PerlIO_printf(Perl_debug_log,
193                           "     restoring \\%"IVdf"..\\%"IVdf" to undef\n",
194                           (IV)(*PL_reglastparen + 1), (IV)PL_regnpar);
195         }
196     );
197 #if 1
198     /* It would seem that the similar code in regtry()
199      * already takes care of this, and in fact it is in
200      * a better location to since this code can #if 0-ed out
201      * but the code in regtry() is needed or otherwise tests
202      * requiring null fields (pat.t#187 and split.t#{13,14}
203      * (as of patchlevel 7877)  will fail.  Then again,
204      * this code seems to be necessary or otherwise
205      * building DynaLoader will fail:
206      * "Error: '*' not in typemap in DynaLoader.xs, line 164"
207      * --jhi */
208     for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
209         if (paren > PL_regsize)
210             PL_regstartp[paren] = -1;
211         PL_regendp[paren] = -1;
212     }
213 #endif
214     return input;
215 }
216
217 STATIC char *
218 S_regcp_set_to(pTHX_ I32 ss)
219 {
220     dTHR;
221     I32 tmp = PL_savestack_ix;
222
223     PL_savestack_ix = ss;
224     regcppop();
225     PL_savestack_ix = tmp;
226     return Nullch;
227 }
228
229 typedef struct re_cc_state
230 {
231     I32 ss;
232     regnode *node;
233     struct re_cc_state *prev;
234     CURCUR *cc;
235     regexp *re;
236 } re_cc_state;
237
238 #define regcpblow(cp) LEAVE_SCOPE(cp)   /* Ignores regcppush()ed data. */
239
240 #define TRYPAREN(paren, n, input) {                             \
241     if (paren) {                                                \
242         if (n) {                                                \
243             PL_regstartp[paren] = HOPc(input, -1) - PL_bostr;   \
244             PL_regendp[paren] = input - PL_bostr;               \
245         }                                                       \
246         else                                                    \
247             PL_regendp[paren] = -1;                             \
248     }                                                           \
249     if (regmatch(next))                                         \
250         sayYES;                                                 \
251     if (paren && n)                                             \
252         PL_regendp[paren] = -1;                                 \
253 }
254
255
256 /*
257  * pregexec and friends
258  */
259
260 /*
261  - pregexec - match a regexp against a string
262  */
263 I32
264 Perl_pregexec(pTHX_ register regexp *prog, char *stringarg, register char *strend,
265          char *strbeg, I32 minend, SV *screamer, U32 nosave)
266 /* strend: pointer to null at end of string */
267 /* strbeg: real beginning of string */
268 /* minend: end of match must be >=minend after stringarg. */
269 /* nosave: For optimizations. */
270 {
271     return
272         regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL, 
273                       nosave ? 0 : REXEC_COPY_STR);
274 }
275
276 STATIC void
277 S_cache_re(pTHX_ regexp *prog)
278 {
279     dTHR;
280     PL_regprecomp = prog->precomp;              /* Needed for FAIL. */
281 #ifdef DEBUGGING
282     PL_regprogram = prog->program;
283 #endif
284     PL_regnpar = prog->nparens;
285     PL_regdata = prog->data;    
286     PL_reg_re = prog;    
287 }
288
289 /* 
290  * Need to implement the following flags for reg_anch:
291  *
292  * USE_INTUIT_NOML              - Useful to call re_intuit_start() first
293  * USE_INTUIT_ML
294  * INTUIT_AUTORITATIVE_NOML     - Can trust a positive answer
295  * INTUIT_AUTORITATIVE_ML
296  * INTUIT_ONCE_NOML             - Intuit can match in one location only.
297  * INTUIT_ONCE_ML
298  *
299  * Another flag for this function: SECOND_TIME (so that float substrs
300  * with giant delta may be not rechecked).
301  */
302
303 /* Assumptions: if ANCH_GPOS, then strpos is anchored. XXXX Check GPOS logic */
304
305 /* If SCREAM, then SvPVX(sv) should be compatible with strpos and strend.
306    Otherwise, only SvCUR(sv) is used to get strbeg. */
307
308 /* XXXX We assume that strpos is strbeg unless sv. */
309
310 /* XXXX Some places assume that there is a fixed substring.
311         An update may be needed if optimizer marks as "INTUITable"
312         RExen without fixed substrings.  Similarly, it is assumed that
313         lengths of all the strings are no more than minlen, thus they
314         cannot come from lookahead.
315         (Or minlen should take into account lookahead.) */
316
317 /* A failure to find a constant substring means that there is no need to make
318    an expensive call to REx engine, thus we celebrate a failure.  Similarly,
319    finding a substring too deep into the string means that less calls to
320    regtry() should be needed.
321
322    REx compiler's optimizer found 4 possible hints:
323         a) Anchored substring;
324         b) Fixed substring;
325         c) Whether we are anchored (beginning-of-line or \G);
326         d) First node (of those at offset 0) which may distingush positions;
327    We use a)b)d) and multiline-part of c), and try to find a position in the
328    string which does not contradict any of them.
329  */
330
331 /* Most of decisions we do here should have been done at compile time.
332    The nodes of the REx which we used for the search should have been
333    deleted from the finite automaton. */
334
335 char *
336 Perl_re_intuit_start(pTHX_ regexp *prog, SV *sv, char *strpos,
337                      char *strend, U32 flags, re_scream_pos_data *data)
338 {
339     register I32 start_shift;
340     /* Should be nonnegative! */
341     register I32 end_shift;
342     register char *s;
343     register SV *check;
344     char *strbeg;
345     char *t;
346     I32 ml_anch;
347     char *tmp;
348     register char *other_last = Nullch; /* other substr checked before this */
349     char *check_at;                     /* check substr found at this pos */
350 #ifdef DEBUGGING
351     char *i_strpos = strpos;
352 #endif
353
354     DEBUG_r( if (!PL_colorset) reginitcolors() );
355     DEBUG_r(PerlIO_printf(Perl_debug_log,
356                       "%sGuessing start of match, REx%s `%s%.60s%s%s' against `%s%.*s%s%s'...\n",
357                       PL_colors[4],PL_colors[5],PL_colors[0],
358                       prog->precomp,
359                       PL_colors[1],
360                       (strlen(prog->precomp) > 60 ? "..." : ""),
361                       PL_colors[0],
362                       (int)(strend - strpos > 60 ? 60 : strend - strpos),
363                       strpos, PL_colors[1],
364                       (strend - strpos > 60 ? "..." : ""))
365         );
366
367     if (prog->minlen > strend - strpos) {
368         DEBUG_r(PerlIO_printf(Perl_debug_log, "String too short...\n"));
369         goto fail;
370     }
371     strbeg = (sv && SvPOK(sv)) ? strend - SvCUR(sv) : strpos;
372     check = prog->check_substr;
373     if (prog->reganch & ROPT_ANCH) {    /* Match at beg-of-str or after \n */
374         ml_anch = !( (prog->reganch & ROPT_ANCH_SINGLE)
375                      || ( (prog->reganch & ROPT_ANCH_BOL)
376                           && !PL_multiline ) ); /* Check after \n? */
377
378         if (!ml_anch) {
379           if ( !(prog->reganch & ROPT_ANCH_GPOS) /* Checked by the caller */
380                /* SvCUR is not set on references: SvRV and SvPVX overlap */
381                && sv && !SvROK(sv)
382                && (strpos != strbeg)) {
383               DEBUG_r(PerlIO_printf(Perl_debug_log, "Not at start...\n"));
384               goto fail;
385           }
386           if (prog->check_offset_min == prog->check_offset_max) {
387             /* Substring at constant offset from beg-of-str... */
388             I32 slen;
389
390             PL_regeol = strend;                 /* Used in HOP() */
391             s = HOPc(strpos, prog->check_offset_min);
392             if (SvTAIL(check)) {
393                 slen = SvCUR(check);    /* >= 1 */
394
395                 if ( strend - s > slen || strend - s < slen - 1 
396                      || (strend - s == slen && strend[-1] != '\n')) {
397                     DEBUG_r(PerlIO_printf(Perl_debug_log, "String too long...\n"));
398                     goto fail_finish;
399                 }
400                 /* Now should match s[0..slen-2] */
401                 slen--;
402                 if (slen && (*SvPVX(check) != *s
403                              || (slen > 1
404                                  && memNE(SvPVX(check), s, slen)))) {
405                   report_neq:
406                     DEBUG_r(PerlIO_printf(Perl_debug_log, "String not equal...\n"));
407                     goto fail_finish;
408                 }
409             }
410             else if (*SvPVX(check) != *s
411                      || ((slen = SvCUR(check)) > 1
412                          && memNE(SvPVX(check), s, slen)))
413                 goto report_neq;
414             goto success_at_start;
415           }
416         }
417         /* Match is anchored, but substr is not anchored wrt beg-of-str. */
418         s = strpos;
419         start_shift = prog->check_offset_min; /* okay to underestimate on CC */
420         end_shift = prog->minlen - start_shift -
421             CHR_SVLEN(check) + (SvTAIL(check) != 0);
422         if (!ml_anch) {
423             I32 end = prog->check_offset_max + CHR_SVLEN(check)
424                                          - (SvTAIL(check) != 0);
425             I32 eshift = strend - s - end;
426
427             if (end_shift < eshift)
428                 end_shift = eshift;
429         }
430     }
431     else {                              /* Can match at random position */
432         ml_anch = 0;
433         s = strpos;
434         start_shift = prog->check_offset_min; /* okay to underestimate on CC */
435         /* Should be nonnegative! */
436         end_shift = prog->minlen - start_shift -
437             CHR_SVLEN(check) + (SvTAIL(check) != 0);
438     }
439
440 #ifdef DEBUGGING        /* 7/99: reports of failure (with the older version) */
441     if (end_shift < 0)
442         Perl_croak(aTHX_ "panic: end_shift");
443 #endif
444
445   restart:
446     /* Find a possible match in the region s..strend by looking for
447        the "check" substring in the region corrected by start/end_shift. */
448     if (flags & REXEC_SCREAM) {
449         I32 p = -1;                     /* Internal iterator of scream. */
450         I32 *pp = data ? data->scream_pos : &p;
451
452         if (PL_screamfirst[BmRARE(check)] >= 0
453             || ( BmRARE(check) == '\n'
454                  && (BmPREVIOUS(check) == SvCUR(check) - 1)
455                  && SvTAIL(check) ))
456             s = screaminstr(sv, check, 
457                             start_shift + (s - strbeg), end_shift, pp, 0);
458         else
459             goto fail_finish;
460         if (data)
461             *data->scream_olds = s;
462     }
463     else
464         s = fbm_instr((unsigned char*)s + start_shift,
465                       (unsigned char*)strend - end_shift,
466                       check, PL_multiline ? FBMrf_MULTILINE : 0);
467
468     /* Update the count-of-usability, remove useless subpatterns,
469         unshift s.  */
470
471     DEBUG_r(PerlIO_printf(Perl_debug_log, "%s %s substr `%s%.*s%s'%s%s",
472                           (s ? "Found" : "Did not find"),
473                           ((check == prog->anchored_substr) ? "anchored" : "floating"),
474                           PL_colors[0],
475                           (int)(SvCUR(check) - (SvTAIL(check)!=0)),
476                           SvPVX(check),
477                           PL_colors[1], (SvTAIL(check) ? "$" : ""),
478                           (s ? " at offset " : "...\n") ) );
479
480     if (!s)
481         goto fail_finish;
482
483     check_at = s;
484
485     /* Finish the diagnostic message */
486     DEBUG_r(PerlIO_printf(Perl_debug_log, "%ld...\n", (long)(s - i_strpos)) );
487
488     /* Got a candidate.  Check MBOL anchoring, and the *other* substr.
489        Start with the other substr.
490        XXXX no SCREAM optimization yet - and a very coarse implementation
491        XXXX /ttx+/ results in anchored=`ttx', floating=`x'.  floating will
492                 *always* match.  Probably should be marked during compile...
493        Probably it is right to do no SCREAM here...
494      */
495
496     if (prog->float_substr && prog->anchored_substr) {
497         /* Take into account the "other" substring. */
498         /* XXXX May be hopelessly wrong for UTF... */
499         if (!other_last)
500             other_last = strpos;
501         if (check == prog->float_substr) {
502           do_other_anchored:
503             {
504                 char *last = s - start_shift, *last1, *last2;
505                 char *s1 = s;
506
507                 tmp = PL_bostr;
508                 t = s - prog->check_offset_max;
509                 if (s - strpos > prog->check_offset_max  /* signed-corrected t > strpos */
510                     && (!(prog->reganch & ROPT_UTF8)
511                         || (PL_bostr = strpos, /* Used in regcopmaybe() */
512                             (t = reghopmaybe_c(s, -(prog->check_offset_max)))
513                             && t > strpos)))
514                     /* EMPTY */;
515                 else
516                     t = strpos;
517                 t += prog->anchored_offset;
518                 if (t < other_last)     /* These positions already checked */
519                     t = other_last;
520                 PL_bostr = tmp;
521                 last2 = last1 = strend - prog->minlen;
522                 if (last < last1)
523                     last1 = last;
524  /* XXXX It is not documented what units *_offsets are in.  Assume bytes.  */
525                 /* On end-of-str: see comment below. */
526                 s = fbm_instr((unsigned char*)t,
527                               (unsigned char*)last1 + prog->anchored_offset
528                                  + SvCUR(prog->anchored_substr)
529                                  - (SvTAIL(prog->anchored_substr)!=0),
530                               prog->anchored_substr, PL_multiline ? FBMrf_MULTILINE : 0);
531                 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s anchored substr `%s%.*s%s'%s",
532                         (s ? "Found" : "Contradicts"),
533                         PL_colors[0],
534                           (int)(SvCUR(prog->anchored_substr)
535                           - (SvTAIL(prog->anchored_substr)!=0)),
536                           SvPVX(prog->anchored_substr),
537                           PL_colors[1], (SvTAIL(prog->anchored_substr) ? "$" : "")));
538                 if (!s) {
539                     if (last1 >= last2) {
540                         DEBUG_r(PerlIO_printf(Perl_debug_log,
541                                                 ", giving up...\n"));
542                         goto fail_finish;
543                     }
544                     DEBUG_r(PerlIO_printf(Perl_debug_log,
545                         ", trying floating at offset %ld...\n",
546                         (long)(s1 + 1 - i_strpos)));
547                     PL_regeol = strend;                 /* Used in HOP() */
548                     other_last = last1 + prog->anchored_offset + 1;
549                     s = HOPc(last, 1);
550                     goto restart;
551                 }
552                 else {
553                     DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
554                           (long)(s - i_strpos)));
555                     t = s - prog->anchored_offset;
556                     other_last = s + 1;
557                     s = s1;
558                     if (t == strpos)
559                         goto try_at_start;
560                     goto try_at_offset;
561                 }
562             }
563         }
564         else {          /* Take into account the floating substring. */
565                 char *last, *last1;
566                 char *s1 = s;
567
568                 t = s - start_shift;
569                 last1 = last = strend - prog->minlen + prog->float_min_offset;
570                 if (last - t > prog->float_max_offset)
571                     last = t + prog->float_max_offset;
572                 s = t + prog->float_min_offset;
573                 if (s < other_last)
574                     s = other_last;
575  /* XXXX It is not documented what units *_offsets are in.  Assume bytes.  */
576                 /* fbm_instr() takes into account exact value of end-of-str
577                    if the check is SvTAIL(ed).  Since false positives are OK,
578                    and end-of-str is not later than strend we are OK. */
579                 s = fbm_instr((unsigned char*)s,
580                               (unsigned char*)last + SvCUR(prog->float_substr)
581                                   - (SvTAIL(prog->float_substr)!=0),
582                               prog->float_substr, PL_multiline ? FBMrf_MULTILINE : 0);
583                 DEBUG_r(PerlIO_printf(Perl_debug_log, "%s floating substr `%s%.*s%s'%s",
584                         (s ? "Found" : "Contradicts"),
585                         PL_colors[0],
586                           (int)(SvCUR(prog->float_substr)
587                           - (SvTAIL(prog->float_substr)!=0)),
588                           SvPVX(prog->float_substr),
589                           PL_colors[1], (SvTAIL(prog->float_substr) ? "$" : "")));
590                 if (!s) {
591                     if (last1 == last) {
592                         DEBUG_r(PerlIO_printf(Perl_debug_log,
593                                                 ", giving up...\n"));
594                         goto fail_finish;
595                     }
596                     DEBUG_r(PerlIO_printf(Perl_debug_log,
597                         ", trying anchored starting at offset %ld...\n",
598                         (long)(s1 + 1 - i_strpos)));
599                     other_last = last + 1;
600                     PL_regeol = strend;                 /* Used in HOP() */
601                     s = HOPc(t, 1);
602                     goto restart;
603                 }
604                 else {
605                     DEBUG_r(PerlIO_printf(Perl_debug_log, " at offset %ld...\n",
606                           (long)(s - i_strpos)));
607                     other_last = s + 1;
608                     s = s1;
609                     if (t == strpos)
610                         goto try_at_start;
611                     goto try_at_offset;
612                 }
613         }
614     }
615
616     t = s - prog->check_offset_max;
617     tmp = PL_bostr;
618     if (s - strpos > prog->check_offset_max  /* signed-corrected t > strpos */
619         && (!(prog->reganch & ROPT_UTF8)
620             || (PL_bostr = strpos, /* Used in regcopmaybe() */
621                 ((t = reghopmaybe_c(s, -(prog->check_offset_max)))
622                  && t > strpos)))) {
623         PL_bostr = tmp;
624         /* Fixed substring is found far enough so that the match
625            cannot start at strpos. */
626       try_at_offset:
627         if (ml_anch && t[-1] != '\n') {
628             /* Eventually fbm_*() should handle this, but often
629                anchored_offset is not 0, so this check will not be wasted. */
630             /* XXXX In the code below we prefer to look for "^" even in
631                presence of anchored substrings.  And we search even
632                beyond the found float position.  These pessimizations
633                are historical artefacts only.  */
634           find_anchor:
635             while (t < strend - prog->minlen) {
636                 if (*t == '\n') {
637                     if (t < check_at - prog->check_offset_min) {
638                         if (prog->anchored_substr) {
639                             /* Since we moved from the found position,
640                                we definitely contradict the found anchored
641                                substr.  Due to the above check we do not
642                                contradict "check" substr.
643                                Thus we can arrive here only if check substr
644                                is float.  Redo checking for "other"=="fixed".
645                              */
646                             strpos = t + 1;                         
647                             DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld, rescanning for anchored from offset %ld...\n",
648                                 PL_colors[0],PL_colors[1], (long)(strpos - i_strpos), (long)(strpos - i_strpos + prog->anchored_offset)));
649                             goto do_other_anchored;
650                         }
651                         /* We don't contradict the found floating substring. */
652                         /* XXXX Why not check for STCLASS? */
653                         s = t + 1;
654                         DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m at offset %ld...\n",
655                             PL_colors[0],PL_colors[1], (long)(s - i_strpos)));
656                         goto set_useful;
657                     }
658                     /* Position contradicts check-string */
659                     /* XXXX probably better to look for check-string
660                        than for "\n", so one should lower the limit for t? */
661                     DEBUG_r(PerlIO_printf(Perl_debug_log, "Found /%s^%s/m, restarting lookup for check-string at offset %ld...\n",
662                         PL_colors[0],PL_colors[1], (long)(t + 1 - i_strpos)));
663                     other_last = strpos = s = t + 1;
664                     goto restart;
665                 }
666                 t++;
667             }
668             DEBUG_r(PerlIO_printf(Perl_debug_log, "Did not find /%s^%s/m...\n",
669                         PL_colors[0],PL_colors[1]));
670             goto fail_finish;
671         }
672         else {
673             DEBUG_r(PerlIO_printf(Perl_debug_log, "Starting position does not contradict /%s^%s/m...\n",
674                         PL_colors[0],PL_colors[1]));
675         }
676         s = t;
677       set_useful:
678         ++BmUSEFUL(prog->check_substr); /* hooray/5 */
679     }
680     else {
681         PL_bostr = tmp;
682         /* The found string does not prohibit matching at strpos,
683            - no optimization of calling REx engine can be performed,
684            unless it was an MBOL and we are not after MBOL,
685            or a future STCLASS check will fail this. */
686       try_at_start:
687         /* Even in this situation we may use MBOL flag if strpos is offset
688            wrt the start of the string. */
689         if (ml_anch && sv && !SvROK(sv) /* See prev comment on SvROK */
690             && (strpos != strbeg) && strpos[-1] != '\n'
691             /* May be due to an implicit anchor of m{.*foo}  */
692             && !(prog->reganch & ROPT_IMPLICIT))
693         {
694             t = strpos;
695             goto find_anchor;
696         }
697         DEBUG_r( if (ml_anch)
698             PerlIO_printf(Perl_debug_log, "Position at offset %ld does not contradict /%s^%s/m...\n",
699                         (long)(strpos - i_strpos), PL_colors[0],PL_colors[1]);
700         );
701       success_at_start:
702         if (!(prog->reganch & ROPT_NAUGHTY)     /* XXXX If strpos moved? */
703             && prog->check_substr               /* Could be deleted already */
704             && --BmUSEFUL(prog->check_substr) < 0
705             && prog->check_substr == prog->float_substr)
706         {
707             /* If flags & SOMETHING - do not do it many times on the same match */
708             DEBUG_r(PerlIO_printf(Perl_debug_log, "... Disabling check substring...\n"));
709             SvREFCNT_dec(prog->check_substr);
710             prog->check_substr = Nullsv;        /* disable */
711             prog->float_substr = Nullsv;        /* clear */
712             check = Nullsv;                     /* abort */
713             s = strpos;
714             /* XXXX This is a remnant of the old implementation.  It
715                     looks wasteful, since now INTUIT can use many
716                     other heuristics. */
717             prog->reganch &= ~RE_USE_INTUIT;
718         }
719         else
720             s = strpos;
721     }
722
723     /* Last resort... */
724     /* XXXX BmUSEFUL already changed, maybe multiple change is meaningful... */
725     if (prog->regstclass) {
726         /* minlen == 0 is possible if regstclass is \b or \B,
727            and the fixed substr is ''$.
728            Since minlen is already taken into account, s+1 is before strend;
729            accidentally, minlen >= 1 guaranties no false positives at s + 1
730            even for \b or \B.  But (minlen? 1 : 0) below assumes that
731            regstclass does not come from lookahead...  */
732         /* If regstclass takes bytelength more than 1: If charlength==1, OK.
733            This leaves EXACTF only, which is dealt with in find_byclass().  */
734         int cl_l = (PL_regkind[(U8)OP(prog->regstclass)] == EXACT
735                     ? STR_LEN(prog->regstclass)
736                     : 1);
737         char *endpos = (prog->anchored_substr || ml_anch)
738                 ? s + (prog->minlen? cl_l : 0)
739                 : (prog->float_substr ? check_at - start_shift + cl_l
740                                       : strend) ;
741         char *startpos = strbeg;
742
743         t = s;
744         if (prog->reganch & ROPT_UTF8) {        
745             PL_regdata = prog->data;    /* Used by REGINCLASS UTF logic */
746             PL_bostr = startpos;
747         }
748         s = find_byclass(prog, prog->regstclass, s, endpos, startpos, 1);
749         if (!s) {
750 #ifdef DEBUGGING
751             char *what;
752 #endif
753             if (endpos == strend) {
754                 DEBUG_r( PerlIO_printf(Perl_debug_log,
755                                 "Could not match STCLASS...\n") );
756                 goto fail;
757             }
758             DEBUG_r( PerlIO_printf(Perl_debug_log,
759                                    "This position contradicts STCLASS...\n") );
760             if ((prog->reganch & ROPT_ANCH) && !ml_anch)
761                 goto fail;
762             /* Contradict one of substrings */
763             if (prog->anchored_substr) {
764                 if (prog->anchored_substr == check) {
765                     DEBUG_r( what = "anchored" );
766                   hop_and_restart:
767                     PL_regeol = strend; /* Used in HOP() */
768                     s = HOPc(t, 1);
769                     if (s + start_shift + end_shift > strend) {
770                         /* XXXX Should be taken into account earlier? */
771                         DEBUG_r( PerlIO_printf(Perl_debug_log,
772                                                "Could not match STCLASS...\n") );
773                         goto fail;
774                     }
775                     if (!check)
776                         goto giveup;
777                     DEBUG_r( PerlIO_printf(Perl_debug_log,
778                                 "Looking for %s substr starting at offset %ld...\n",
779                                  what, (long)(s + start_shift - i_strpos)) );
780                     goto restart;
781                 }
782                 /* Have both, check_string is floating */
783                 if (t + start_shift >= check_at) /* Contradicts floating=check */
784                     goto retry_floating_check;
785                 /* Recheck anchored substring, but not floating... */
786                 s = check_at; 
787                 if (!check)
788                     goto giveup;
789                 DEBUG_r( PerlIO_printf(Perl_debug_log,
790                           "Looking for anchored substr starting at offset %ld...\n",
791                           (long)(other_last - i_strpos)) );
792                 goto do_other_anchored;
793             }
794             /* Another way we could have checked stclass at the
795                current position only: */
796             if (ml_anch) {
797                 s = t = t + 1;
798                 if (!check)
799                     goto giveup;
800                 DEBUG_r( PerlIO_printf(Perl_debug_log,
801                           "Looking for /%s^%s/m starting at offset %ld...\n",
802                           PL_colors[0],PL_colors[1], (long)(t - i_strpos)) );
803                 goto try_at_offset;
804             }
805             if (!prog->float_substr)    /* Could have been deleted */
806                 goto fail;
807             /* Check is floating subtring. */
808           retry_floating_check:
809             t = check_at - start_shift;
810             DEBUG_r( what = "floating" );
811             goto hop_and_restart;
812         }
813         DEBUG_r( if (t != s)
814                      PerlIO_printf(Perl_debug_log, 
815                         "By STCLASS: moving %ld --> %ld\n",
816                         (long)(t - i_strpos), (long)(s - i_strpos));
817                  else
818                      PerlIO_printf(Perl_debug_log, 
819                         "Does not contradict STCLASS...\n") );
820     }
821   giveup:
822     DEBUG_r(PerlIO_printf(Perl_debug_log, "%s%s:%s match at offset %ld\n",
823                           PL_colors[4], (check ? "Guessed" : "Giving up"),
824                           PL_colors[5], (long)(s - i_strpos)) );
825     return s;
826
827   fail_finish:                          /* Substring not found */
828     if (prog->check_substr)             /* could be removed already */
829         BmUSEFUL(prog->check_substr) += 5; /* hooray */
830   fail:
831     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch rejected by optimizer%s\n",
832                           PL_colors[4],PL_colors[5]));
833     return Nullch;
834 }
835
836 /* We know what class REx starts with.  Try to find this position... */
837 STATIC char *
838 S_find_byclass(pTHX_ regexp * prog, regnode *c, char *s, char *strend, char *startpos, I32 norun)
839 {
840         I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
841         char *m;
842         STRLEN ln;
843         unsigned int c1;
844         unsigned int c2;
845         char *e;
846         register I32 tmp = 1;   /* Scratch variable? */
847
848         /* We know what class it must start with. */
849         switch (OP(c)) {
850         case ANYOFUTF8:
851             while (s < strend) {
852                 if (REGINCLASSUTF8(c, (U8*)s)) {
853                     if (tmp && (norun || regtry(prog, s)))
854                         goto got_it;
855                     else
856                         tmp = doevery;
857                 }
858                 else
859                     tmp = 1;
860                 s += UTF8SKIP(s);
861             }
862             break;
863         case ANYOF:
864             while (s < strend) {
865                 if (REGINCLASS(c, *(U8*)s)) {
866                     if (tmp && (norun || regtry(prog, s)))
867                         goto got_it;
868                     else
869                         tmp = doevery;
870                 }
871                 else
872                     tmp = 1;
873                 s++;
874             }
875             break;
876         case EXACTF:
877             m = STRING(c);
878             ln = STR_LEN(c);
879             c1 = *(U8*)m;
880             c2 = PL_fold[c1];
881             goto do_exactf;
882         case EXACTFL:
883             m = STRING(c);
884             ln = STR_LEN(c);
885             c1 = *(U8*)m;
886             c2 = PL_fold_locale[c1];
887           do_exactf:
888             e = strend - ln;
889
890             if (norun && e < s)
891                 e = s;                  /* Due to minlen logic of intuit() */
892             /* Here it is NOT UTF!  */
893             if (c1 == c2) {
894                 while (s <= e) {
895                     if ( *(U8*)s == c1
896                          && (ln == 1 || !(OP(c) == EXACTF
897                                           ? ibcmp(s, m, ln)
898                                           : ibcmp_locale(s, m, ln)))
899                          && (norun || regtry(prog, s)) )
900                         goto got_it;
901                     s++;
902                 }
903             } else {
904                 while (s <= e) {
905                     if ( (*(U8*)s == c1 || *(U8*)s == c2)
906                          && (ln == 1 || !(OP(c) == EXACTF
907                                           ? ibcmp(s, m, ln)
908                                           : ibcmp_locale(s, m, ln)))
909                          && (norun || regtry(prog, s)) )
910                         goto got_it;
911                     s++;
912                 }
913             }
914             break;
915         case BOUNDL:
916             PL_reg_flags |= RF_tainted;
917             /* FALL THROUGH */
918         case BOUND:
919             tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
920             tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
921             while (s < strend) {
922                 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
923                     tmp = !tmp;
924                     if ((norun || regtry(prog, s)))
925                         goto got_it;
926                 }
927                 s++;
928             }
929             if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
930                 goto got_it;
931             break;
932         case BOUNDLUTF8:
933             PL_reg_flags |= RF_tainted;
934             /* FALL THROUGH */
935         case BOUNDUTF8:
936             if (s == startpos)
937                 tmp = '\n';
938             else {
939                 U8 *r = reghop((U8*)s, -1);
940
941                 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
942             }
943             tmp = ((OP(c) == BOUNDUTF8 ?
944                     isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
945             while (s < strend) {
946                 if (tmp == !(OP(c) == BOUNDUTF8 ?
947                              swash_fetch(PL_utf8_alnum, (U8*)s) :
948                              isALNUM_LC_utf8((U8*)s)))
949                 {
950                     tmp = !tmp;
951                     if ((norun || regtry(prog, s)))
952                         goto got_it;
953                 }
954                 s += UTF8SKIP(s);
955             }
956             if ((!prog->minlen && tmp) && (norun || regtry(prog, s)))
957                 goto got_it;
958             break;
959         case NBOUNDL:
960             PL_reg_flags |= RF_tainted;
961             /* FALL THROUGH */
962         case NBOUND:
963             tmp = (s != startpos) ? UCHARAT(s - 1) : '\n';
964             tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
965             while (s < strend) {
966                 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
967                     tmp = !tmp;
968                 else if ((norun || regtry(prog, s)))
969                     goto got_it;
970                 s++;
971             }
972             if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
973                 goto got_it;
974             break;
975         case NBOUNDLUTF8:
976             PL_reg_flags |= RF_tainted;
977             /* FALL THROUGH */
978         case NBOUNDUTF8:
979             if (s == startpos)
980                 tmp = '\n';
981             else {
982                 U8 *r = reghop((U8*)s, -1);
983
984                 tmp = (I32)utf8_to_uv(r, s - (char*)r, 0, 0);
985             }
986             tmp = ((OP(c) == NBOUNDUTF8 ?
987                     isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
988             while (s < strend) {
989                 if (tmp == !(OP(c) == NBOUNDUTF8 ?
990                              swash_fetch(PL_utf8_alnum, (U8*)s) :
991                              isALNUM_LC_utf8((U8*)s)))
992                     tmp = !tmp;
993                 else if ((norun || regtry(prog, s)))
994                     goto got_it;
995                 s += UTF8SKIP(s);
996             }
997             if ((!prog->minlen && !tmp) && (norun || regtry(prog, s)))
998                 goto got_it;
999             break;
1000         case ALNUM:
1001             while (s < strend) {
1002                 if (isALNUM(*s)) {
1003                     if (tmp && (norun || regtry(prog, s)))
1004                         goto got_it;
1005                     else
1006                         tmp = doevery;
1007                 }
1008                 else
1009                     tmp = 1;
1010                 s++;
1011             }
1012             break;
1013         case ALNUMUTF8:
1014             while (s < strend) {
1015                 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
1016                     if (tmp && (norun || regtry(prog, s)))
1017                         goto got_it;
1018                     else
1019                         tmp = doevery;
1020                 }
1021                 else
1022                     tmp = 1;
1023                 s += UTF8SKIP(s);
1024             }
1025             break;
1026         case ALNUML:
1027             PL_reg_flags |= RF_tainted;
1028             while (s < strend) {
1029                 if (isALNUM_LC(*s)) {
1030                     if (tmp && (norun || regtry(prog, s)))
1031                         goto got_it;
1032                     else
1033                         tmp = doevery;
1034                 }
1035                 else
1036                     tmp = 1;
1037                 s++;
1038             }
1039             break;
1040         case ALNUMLUTF8:
1041             PL_reg_flags |= RF_tainted;
1042             while (s < strend) {
1043                 if (isALNUM_LC_utf8((U8*)s)) {
1044                     if (tmp && (norun || regtry(prog, s)))
1045                         goto got_it;
1046                     else
1047                         tmp = doevery;
1048                 }
1049                 else
1050                     tmp = 1;
1051                 s += UTF8SKIP(s);
1052             }
1053             break;
1054         case NALNUM:
1055             while (s < strend) {
1056                 if (!isALNUM(*s)) {
1057                     if (tmp && (norun || regtry(prog, s)))
1058                         goto got_it;
1059                     else
1060                         tmp = doevery;
1061                 }
1062                 else
1063                     tmp = 1;
1064                 s++;
1065             }
1066             break;
1067         case NALNUMUTF8:
1068             while (s < strend) {
1069                 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
1070                     if (tmp && (norun || regtry(prog, s)))
1071                         goto got_it;
1072                     else
1073                         tmp = doevery;
1074                 }
1075                 else
1076                     tmp = 1;
1077                 s += UTF8SKIP(s);
1078             }
1079             break;
1080         case NALNUML:
1081             PL_reg_flags |= RF_tainted;
1082             while (s < strend) {
1083                 if (!isALNUM_LC(*s)) {
1084                     if (tmp && (norun || regtry(prog, s)))
1085                         goto got_it;
1086                     else
1087                         tmp = doevery;
1088                 }
1089                 else
1090                     tmp = 1;
1091                 s++;
1092             }
1093             break;
1094         case NALNUMLUTF8:
1095             PL_reg_flags |= RF_tainted;
1096             while (s < strend) {
1097                 if (!isALNUM_LC_utf8((U8*)s)) {
1098                     if (tmp && (norun || regtry(prog, s)))
1099                         goto got_it;
1100                     else
1101                         tmp = doevery;
1102                 }
1103                 else
1104                     tmp = 1;
1105                 s += UTF8SKIP(s);
1106             }
1107             break;
1108         case SPACE:
1109             while (s < strend) {
1110                 if (isSPACE(*s)) {
1111                     if (tmp && (norun || regtry(prog, s)))
1112                         goto got_it;
1113                     else
1114                         tmp = doevery;
1115                 }
1116                 else
1117                     tmp = 1;
1118                 s++;
1119             }
1120             break;
1121         case SPACEUTF8:
1122             while (s < strend) {
1123                 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
1124                     if (tmp && (norun || regtry(prog, s)))
1125                         goto got_it;
1126                     else
1127                         tmp = doevery;
1128                 }
1129                 else
1130                     tmp = 1;
1131                 s += UTF8SKIP(s);
1132             }
1133             break;
1134         case SPACEL:
1135             PL_reg_flags |= RF_tainted;
1136             while (s < strend) {
1137                 if (isSPACE_LC(*s)) {
1138                     if (tmp && (norun || regtry(prog, s)))
1139                         goto got_it;
1140                     else
1141                         tmp = doevery;
1142                 }
1143                 else
1144                     tmp = 1;
1145                 s++;
1146             }
1147             break;
1148         case SPACELUTF8:
1149             PL_reg_flags |= RF_tainted;
1150             while (s < strend) {
1151                 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
1152                     if (tmp && (norun || regtry(prog, s)))
1153                         goto got_it;
1154                     else
1155                         tmp = doevery;
1156                 }
1157                 else
1158                     tmp = 1;
1159                 s += UTF8SKIP(s);
1160             }
1161             break;
1162         case NSPACE:
1163             while (s < strend) {
1164                 if (!isSPACE(*s)) {
1165                     if (tmp && (norun || regtry(prog, s)))
1166                         goto got_it;
1167                     else
1168                         tmp = doevery;
1169                 }
1170                 else
1171                     tmp = 1;
1172                 s++;
1173             }
1174             break;
1175         case NSPACEUTF8:
1176             while (s < strend) {
1177                 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
1178                     if (tmp && (norun || regtry(prog, s)))
1179                         goto got_it;
1180                     else
1181                         tmp = doevery;
1182                 }
1183                 else
1184                     tmp = 1;
1185                 s += UTF8SKIP(s);
1186             }
1187             break;
1188         case NSPACEL:
1189             PL_reg_flags |= RF_tainted;
1190             while (s < strend) {
1191                 if (!isSPACE_LC(*s)) {
1192                     if (tmp && (norun || regtry(prog, s)))
1193                         goto got_it;
1194                     else
1195                         tmp = doevery;
1196                 }
1197                 else
1198                     tmp = 1;
1199                 s++;
1200             }
1201             break;
1202         case NSPACELUTF8:
1203             PL_reg_flags |= RF_tainted;
1204             while (s < strend) {
1205                 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
1206                     if (tmp && (norun || regtry(prog, s)))
1207                         goto got_it;
1208                     else
1209                         tmp = doevery;
1210                 }
1211                 else
1212                     tmp = 1;
1213                 s += UTF8SKIP(s);
1214             }
1215             break;
1216         case DIGIT:
1217             while (s < strend) {
1218                 if (isDIGIT(*s)) {
1219                     if (tmp && (norun || regtry(prog, s)))
1220                         goto got_it;
1221                     else
1222                         tmp = doevery;
1223                 }
1224                 else
1225                     tmp = 1;
1226                 s++;
1227             }
1228             break;
1229         case DIGITUTF8:
1230             while (s < strend) {
1231                 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
1232                     if (tmp && (norun || regtry(prog, s)))
1233                         goto got_it;
1234                     else
1235                         tmp = doevery;
1236                 }
1237                 else
1238                     tmp = 1;
1239                 s += UTF8SKIP(s);
1240             }
1241             break;
1242         case DIGITL:
1243             PL_reg_flags |= RF_tainted;
1244             while (s < strend) {
1245                 if (isDIGIT_LC(*s)) {
1246                     if (tmp && (norun || regtry(prog, s)))
1247                         goto got_it;
1248                     else
1249                         tmp = doevery;
1250                 }
1251                 else
1252                     tmp = 1;
1253                 s++;
1254             }
1255             break;
1256         case DIGITLUTF8:
1257             PL_reg_flags |= RF_tainted;
1258             while (s < strend) {
1259                 if (isDIGIT_LC_utf8((U8*)s)) {
1260                     if (tmp && (norun || regtry(prog, s)))
1261                         goto got_it;
1262                     else
1263                         tmp = doevery;
1264                 }
1265                 else
1266                     tmp = 1;
1267                 s += UTF8SKIP(s);
1268             }
1269             break;
1270         case NDIGIT:
1271             while (s < strend) {
1272                 if (!isDIGIT(*s)) {
1273                     if (tmp && (norun || regtry(prog, s)))
1274                         goto got_it;
1275                     else
1276                         tmp = doevery;
1277                 }
1278                 else
1279                     tmp = 1;
1280                 s++;
1281             }
1282             break;
1283         case NDIGITUTF8:
1284             while (s < strend) {
1285                 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
1286                     if (tmp && (norun || regtry(prog, s)))
1287                         goto got_it;
1288                     else
1289                         tmp = doevery;
1290                 }
1291                 else
1292                     tmp = 1;
1293                 s += UTF8SKIP(s);
1294             }
1295             break;
1296         case NDIGITL:
1297             PL_reg_flags |= RF_tainted;
1298             while (s < strend) {
1299                 if (!isDIGIT_LC(*s)) {
1300                     if (tmp && (norun || regtry(prog, s)))
1301                         goto got_it;
1302                     else
1303                         tmp = doevery;
1304                 }
1305                 else
1306                     tmp = 1;
1307                 s++;
1308             }
1309             break;
1310         case NDIGITLUTF8:
1311             PL_reg_flags |= RF_tainted;
1312             while (s < strend) {
1313                 if (!isDIGIT_LC_utf8((U8*)s)) {
1314                     if (tmp && (norun || regtry(prog, s)))
1315                         goto got_it;
1316                     else
1317                         tmp = doevery;
1318                 }
1319                 else
1320                     tmp = 1;
1321                 s += UTF8SKIP(s);
1322             }
1323             break;
1324         default:
1325             Perl_croak(aTHX_ "panic: unknown regstclass %d", (int)OP(c));
1326             break;
1327         }
1328         return 0;
1329       got_it:
1330         return s;
1331 }
1332
1333 /*
1334  - regexec_flags - match a regexp against a string
1335  */
1336 I32
1337 Perl_regexec_flags(pTHX_ register regexp *prog, char *stringarg, register char *strend,
1338               char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
1339 /* strend: pointer to null at end of string */
1340 /* strbeg: real beginning of string */
1341 /* minend: end of match must be >=minend after stringarg. */
1342 /* data: May be used for some additional optimizations. */
1343 /* nosave: For optimizations. */
1344 {
1345     dTHR;
1346     register char *s;
1347     register regnode *c;
1348     register char *startpos = stringarg;
1349     I32 minlen;         /* must match at least this many chars */
1350     I32 dontbother = 0; /* how many characters not to try at end */
1351     /* I32 start_shift = 0; */          /* Offset of the start to find
1352                                          constant substr. */            /* CC */
1353     I32 end_shift = 0;                  /* Same for the end. */         /* CC */
1354     I32 scream_pos = -1;                /* Internal iterator of scream. */
1355     char *scream_olds;
1356     SV* oreplsv = GvSV(PL_replgv);
1357
1358     PL_regcc = 0;
1359
1360     cache_re(prog);
1361 #ifdef DEBUGGING
1362     PL_regnarrate = PL_debug & 512;
1363 #endif
1364
1365     /* Be paranoid... */
1366     if (prog == NULL || startpos == NULL) {
1367         Perl_croak(aTHX_ "NULL regexp parameter");
1368         return 0;
1369     }
1370
1371     minlen = prog->minlen;
1372     if (strend - startpos < minlen) goto phooey;
1373
1374     if (startpos == strbeg)     /* is ^ valid at stringarg? */
1375         PL_regprev = '\n';
1376     else {
1377         PL_regprev = (U32)stringarg[-1];
1378         if (!PL_multiline && PL_regprev == '\n')
1379             PL_regprev = '\0';          /* force ^ to NOT match */
1380     }
1381
1382     /* Check validity of program. */
1383     if (UCHARAT(prog->program) != REG_MAGIC) {
1384         Perl_croak(aTHX_ "corrupted regexp program");
1385     }
1386
1387     PL_reg_flags = 0;
1388     PL_reg_eval_set = 0;
1389     PL_reg_maxiter = 0;
1390
1391     if (prog->reganch & ROPT_UTF8)
1392         PL_reg_flags |= RF_utf8;
1393
1394     /* Mark beginning of line for ^ and lookbehind. */
1395     PL_regbol = startpos;
1396     PL_bostr  = strbeg;
1397     PL_reg_sv = sv;
1398
1399     /* Mark end of line for $ (and such) */
1400     PL_regeol = strend;
1401
1402     /* see how far we have to get to not match where we matched before */
1403     PL_regtill = startpos+minend;
1404
1405     /* We start without call_cc context.  */
1406     PL_reg_call_cc = 0;
1407
1408     /* If there is a "must appear" string, look for it. */
1409     s = startpos;
1410
1411     if (prog->reganch & ROPT_GPOS_SEEN) { /* Need to have PL_reg_ganch */
1412         MAGIC *mg;
1413
1414         if (flags & REXEC_IGNOREPOS)    /* Means: check only at start */
1415             PL_reg_ganch = startpos;
1416         else if (sv && SvTYPE(sv) >= SVt_PVMG
1417                   && SvMAGIC(sv)
1418                   && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0) {
1419             PL_reg_ganch = strbeg + mg->mg_len; /* Defined pos() */
1420             if (prog->reganch & ROPT_ANCH_GPOS) {
1421                 if (s > PL_reg_ganch)
1422                     goto phooey;
1423                 s = PL_reg_ganch;
1424             }
1425         }
1426         else                            /* pos() not defined */
1427             PL_reg_ganch = strbeg;
1428     }
1429
1430     if (!(flags & REXEC_CHECKED) && prog->check_substr != Nullsv) {
1431         re_scream_pos_data d;
1432
1433         d.scream_olds = &scream_olds;
1434         d.scream_pos = &scream_pos;
1435         s = re_intuit_start(prog, sv, s, strend, flags, &d);
1436         if (!s)
1437             goto phooey;        /* not present */
1438     }
1439
1440     DEBUG_r( if (!PL_colorset) reginitcolors() );
1441     DEBUG_r(PerlIO_printf(Perl_debug_log,
1442                       "%sMatching REx%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
1443                       PL_colors[4],PL_colors[5],PL_colors[0],
1444                       prog->precomp,
1445                       PL_colors[1],
1446                       (strlen(prog->precomp) > 60 ? "..." : ""),
1447                       PL_colors[0],
1448                       (int)(strend - startpos > 60 ? 60 : strend - startpos),
1449                       startpos, PL_colors[1],
1450                       (strend - startpos > 60 ? "..." : ""))
1451         );
1452
1453     /* Simplest case:  anchored match need be tried only once. */
1454     /*  [unless only anchor is BOL and multiline is set] */
1455     if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
1456         if (s == startpos && regtry(prog, startpos))
1457             goto got_it;
1458         else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
1459                  || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
1460         {
1461             char *end;
1462
1463             if (minlen)
1464                 dontbother = minlen - 1;
1465             end = HOPc(strend, -dontbother) - 1;
1466             /* for multiline we only have to try after newlines */
1467             if (prog->check_substr) {
1468                 if (s == startpos)
1469                     goto after_try;
1470                 while (1) {
1471                     if (regtry(prog, s))
1472                         goto got_it;
1473                   after_try:
1474                     if (s >= end)
1475                         goto phooey;
1476                     if (prog->reganch & RE_USE_INTUIT) {
1477                         s = re_intuit_start(prog, sv, s + 1, strend, flags, NULL);
1478                         if (!s)
1479                             goto phooey;
1480                     }
1481                     else
1482                         s++;
1483                 }               
1484             } else {
1485                 if (s > startpos)
1486                     s--;
1487                 while (s < end) {
1488                     if (*s++ == '\n') { /* don't need PL_utf8skip here */
1489                         if (regtry(prog, s))
1490                             goto got_it;
1491                     }
1492                 }               
1493             }
1494         }
1495         goto phooey;
1496     } else if (prog->reganch & ROPT_ANCH_GPOS) {
1497         if (regtry(prog, PL_reg_ganch))
1498             goto got_it;
1499         goto phooey;
1500     }
1501
1502     /* Messy cases:  unanchored match. */
1503     if (prog->anchored_substr && prog->reganch & ROPT_SKIP) { 
1504         /* we have /x+whatever/ */
1505         /* it must be a one character string (XXXX Except UTF?) */
1506         char ch = SvPVX(prog->anchored_substr)[0];
1507 #ifdef DEBUGGING
1508         int did_match = 0;
1509 #endif
1510
1511         if (UTF) {
1512             while (s < strend) {
1513                 if (*s == ch) {
1514                     DEBUG_r( did_match = 1 );
1515                     if (regtry(prog, s)) goto got_it;
1516                     s += UTF8SKIP(s);
1517                     while (s < strend && *s == ch)
1518                         s += UTF8SKIP(s);
1519                 }
1520                 s += UTF8SKIP(s);
1521             }
1522         }
1523         else {
1524             while (s < strend) {
1525                 if (*s == ch) {
1526                     DEBUG_r( did_match = 1 );
1527                     if (regtry(prog, s)) goto got_it;
1528                     s++;
1529                     while (s < strend && *s == ch)
1530                         s++;
1531                 }
1532                 s++;
1533             }
1534         }
1535         DEBUG_r(did_match ||
1536                 PerlIO_printf(Perl_debug_log,
1537                               "Did not find anchored character...\n"));
1538     }
1539     /*SUPPRESS 560*/
1540     else if (prog->anchored_substr != Nullsv
1541              || (prog->float_substr != Nullsv 
1542                  && prog->float_max_offset < strend - s)) {
1543         SV *must = prog->anchored_substr 
1544             ? prog->anchored_substr : prog->float_substr;
1545         I32 back_max = 
1546             prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
1547         I32 back_min = 
1548             prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
1549         char *last = HOPc(strend,       /* Cannot start after this */
1550                           -(I32)(CHR_SVLEN(must)
1551                                  - (SvTAIL(must) != 0) + back_min));
1552         char *last1;            /* Last position checked before */
1553 #ifdef DEBUGGING
1554         int did_match = 0;
1555 #endif
1556
1557         if (s > PL_bostr)
1558             last1 = HOPc(s, -1);
1559         else
1560             last1 = s - 1;      /* bogus */
1561
1562         /* XXXX check_substr already used to find `s', can optimize if
1563            check_substr==must. */
1564         scream_pos = -1;
1565         dontbother = end_shift;
1566         strend = HOPc(strend, -dontbother);
1567         while ( (s <= last) &&
1568                 ((flags & REXEC_SCREAM) 
1569                  ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
1570                                     end_shift, &scream_pos, 0))
1571                  : (s = fbm_instr((unsigned char*)HOP(s, back_min),
1572                                   (unsigned char*)strend, must, 
1573                                   PL_multiline ? FBMrf_MULTILINE : 0))) ) {
1574             DEBUG_r( did_match = 1 );
1575             if (HOPc(s, -back_max) > last1) {
1576                 last1 = HOPc(s, -back_min);
1577                 s = HOPc(s, -back_max);
1578             }
1579             else {
1580                 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
1581
1582                 last1 = HOPc(s, -back_min);
1583                 s = t;          
1584             }
1585             if (UTF) {
1586                 while (s <= last1) {
1587                     if (regtry(prog, s))
1588                         goto got_it;
1589                     s += UTF8SKIP(s);
1590                 }
1591             }
1592             else {
1593                 while (s <= last1) {
1594                     if (regtry(prog, s))
1595                         goto got_it;
1596                     s++;
1597                 }
1598             }
1599         }
1600         DEBUG_r(did_match ||
1601                 PerlIO_printf(Perl_debug_log, "Did not find %s substr `%s%.*s%s'%s...\n",
1602                               ((must == prog->anchored_substr)
1603                                ? "anchored" : "floating"),
1604                               PL_colors[0],
1605                               (int)(SvCUR(must) - (SvTAIL(must)!=0)),
1606                               SvPVX(must),
1607                               PL_colors[1], (SvTAIL(must) ? "$" : "")));
1608         goto phooey;
1609     }
1610     else if ((c = prog->regstclass)) {
1611         if (minlen && PL_regkind[(U8)OP(prog->regstclass)] != EXACT)
1612             /* don't bother with what can't match */
1613             strend = HOPc(strend, -(minlen - 1));
1614         if (find_byclass(prog, c, s, strend, startpos, 0))
1615             goto got_it;
1616         DEBUG_r(PerlIO_printf(Perl_debug_log, "Contradicts stclass...\n"));
1617     }
1618     else {
1619         dontbother = 0;
1620         if (prog->float_substr != Nullsv) {     /* Trim the end. */
1621             char *last;
1622
1623             if (flags & REXEC_SCREAM) {
1624                 last = screaminstr(sv, prog->float_substr, s - strbeg,
1625                                    end_shift, &scream_pos, 1); /* last one */
1626                 if (!last)
1627                     last = scream_olds; /* Only one occurence. */
1628             }
1629             else {
1630                 STRLEN len;
1631                 char *little = SvPV(prog->float_substr, len);
1632
1633                 if (SvTAIL(prog->float_substr)) {
1634                     if (memEQ(strend - len + 1, little, len - 1))
1635                         last = strend - len + 1;
1636                     else if (!PL_multiline)
1637                         last = memEQ(strend - len, little, len) 
1638                             ? strend - len : Nullch;
1639                     else
1640                         goto find_last;
1641                 } else {
1642                   find_last:
1643                     if (len) 
1644                         last = rninstr(s, strend, little, little + len);
1645                     else
1646                         last = strend;  /* matching `$' */
1647                 }
1648             }
1649             if (last == NULL) {
1650                 DEBUG_r(PerlIO_printf(Perl_debug_log,
1651                                       "%sCan't trim the tail, match fails (should not happen)%s\n",
1652                                       PL_colors[4],PL_colors[5]));
1653                 goto phooey; /* Should not happen! */
1654             }
1655             dontbother = strend - last + prog->float_min_offset;
1656         }
1657         if (minlen && (dontbother < minlen))
1658             dontbother = minlen - 1;
1659         strend -= dontbother;              /* this one's always in bytes! */
1660         /* We don't know much -- general case. */
1661         if (UTF) {
1662             for (;;) {
1663                 if (regtry(prog, s))
1664                     goto got_it;
1665                 if (s >= strend)
1666                     break;
1667                 s += UTF8SKIP(s);
1668             };
1669         }
1670         else {
1671             do {
1672                 if (regtry(prog, s))
1673                     goto got_it;
1674             } while (s++ < strend);
1675         }
1676     }
1677
1678     /* Failure. */
1679     goto phooey;
1680
1681 got_it:
1682     RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
1683
1684     if (PL_reg_eval_set) {
1685         /* Preserve the current value of $^R */
1686         if (oreplsv != GvSV(PL_replgv))
1687             sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
1688                                                   restored, the value remains
1689                                                   the same. */
1690         restore_pos(aTHXo_ 0);
1691     }
1692
1693     /* make sure $`, $&, $', and $digit will work later */
1694     if ( !(flags & REXEC_NOT_FIRST) ) {
1695         if (RX_MATCH_COPIED(prog)) {
1696             Safefree(prog->subbeg);
1697             RX_MATCH_COPIED_off(prog);
1698         }
1699         if (flags & REXEC_COPY_STR) {
1700             I32 i = PL_regeol - startpos + (stringarg - strbeg);
1701
1702             s = savepvn(strbeg, i);
1703             prog->subbeg = s;
1704             prog->sublen = i;
1705             RX_MATCH_COPIED_on(prog);
1706         }
1707         else {
1708             prog->subbeg = strbeg;
1709             prog->sublen = PL_regeol - strbeg;  /* strend may have been modified */
1710         }
1711     }
1712     
1713     return 1;
1714
1715 phooey:
1716     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch failed%s\n",
1717                           PL_colors[4],PL_colors[5]));
1718     if (PL_reg_eval_set)
1719         restore_pos(aTHXo_ 0);
1720     return 0;
1721 }
1722
1723 /*
1724  - regtry - try match at specific point
1725  */
1726 STATIC I32                      /* 0 failure, 1 success */
1727 S_regtry(pTHX_ regexp *prog, char *startpos)
1728 {
1729     dTHR;
1730     register I32 i;
1731     register I32 *sp;
1732     register I32 *ep;
1733     CHECKPOINT lastcp;
1734
1735 #ifdef DEBUGGING
1736     PL_regindent = 0;   /* XXXX Not good when matches are reenterable... */
1737 #endif
1738     if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
1739         MAGIC *mg;
1740
1741         PL_reg_eval_set = RS_init;
1742         DEBUG_r(DEBUG_s(
1743             PerlIO_printf(Perl_debug_log, "  setting stack tmpbase at %"IVdf"\n",
1744                           (IV)(PL_stack_sp - PL_stack_base));
1745             ));
1746         SAVEI32(cxstack[cxstack_ix].blk_oldsp);
1747         cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
1748         /* Otherwise OP_NEXTSTATE will free whatever on stack now.  */
1749         SAVETMPS;
1750         /* Apparently this is not needed, judging by wantarray. */
1751         /* SAVEI8(cxstack[cxstack_ix].blk_gimme);
1752            cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
1753
1754         if (PL_reg_sv) {
1755             /* Make $_ available to executed code. */
1756             if (PL_reg_sv != DEFSV) {
1757                 /* SAVE_DEFSV does *not* suffice here for USE_THREADS */
1758                 SAVESPTR(DEFSV);
1759                 DEFSV = PL_reg_sv;
1760             }
1761         
1762             if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv) 
1763                   && (mg = mg_find(PL_reg_sv, 'g')))) {
1764                 /* prepare for quick setting of pos */
1765                 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1766                 mg = mg_find(PL_reg_sv, 'g');
1767                 mg->mg_len = -1;
1768             }
1769             PL_reg_magic    = mg;
1770             PL_reg_oldpos   = mg->mg_len;
1771             SAVEDESTRUCTOR_X(restore_pos, 0);
1772         }
1773         if (!PL_reg_curpm)
1774             Newz(22,PL_reg_curpm, 1, PMOP);
1775         PL_reg_curpm->op_pmregexp = prog;
1776         PL_reg_oldcurpm = PL_curpm;
1777         PL_curpm = PL_reg_curpm;
1778         if (RX_MATCH_COPIED(prog)) {
1779             /*  Here is a serious problem: we cannot rewrite subbeg,
1780                 since it may be needed if this match fails.  Thus
1781                 $` inside (?{}) could fail... */
1782             PL_reg_oldsaved = prog->subbeg;
1783             PL_reg_oldsavedlen = prog->sublen;
1784             RX_MATCH_COPIED_off(prog);
1785         }
1786         else
1787             PL_reg_oldsaved = Nullch;
1788         prog->subbeg = PL_bostr;
1789         prog->sublen = PL_regeol - PL_bostr; /* strend may have been modified */
1790     }
1791     prog->startp[0] = startpos - PL_bostr;
1792     PL_reginput = startpos;
1793     PL_regstartp = prog->startp;
1794     PL_regendp = prog->endp;
1795     PL_reglastparen = &prog->lastparen;
1796     prog->lastparen = 0;
1797     PL_regsize = 0;
1798     DEBUG_r(PL_reg_starttry = startpos);
1799     if (PL_reg_start_tmpl <= prog->nparens) {
1800         PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1801         if(PL_reg_start_tmp)
1802             Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1803         else
1804             New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
1805     }
1806
1807     /* XXXX What this code is doing here?!!!  There should be no need
1808        to do this again and again, PL_reglastparen should take care of
1809        this!  --ilya*/
1810
1811     /* Tests pat.t#187 and split.t#{13,14} seem to depend on this code.
1812      * Actually, the code in regcppop() (which Ilya may be meaning by
1813      * PL_reglastparen), is not needed at all by the test suite
1814      * (op/regexp, op/pat, op/split), but that code is needed, oddly
1815      * enough, for building DynaLoader, or otherwise this
1816      * "Error: '*' not in typemap in DynaLoader.xs, line 164"
1817      * will happen.  Meanwhile, this code *is* needed for the
1818      * above-mentioned test suite tests to succeed.  The common theme
1819      * on those tests seems to be returning null fields from matches.
1820      * --jhi */
1821 #if 1
1822     sp = prog->startp;
1823     ep = prog->endp;
1824     if (prog->nparens) {
1825         for (i = prog->nparens; i > *PL_reglastparen; i--) {
1826             *++sp = -1;
1827             *++ep = -1;
1828         }
1829     }
1830 #endif
1831     REGCP_SET(lastcp);
1832     if (regmatch(prog->program + 1)) {
1833         prog->endp[0] = PL_reginput - PL_bostr;
1834         return 1;
1835     }
1836     REGCP_UNWIND(lastcp);
1837     return 0;
1838 }
1839
1840 #define RE_UNWIND_BRANCH        1
1841 #define RE_UNWIND_BRANCHJ       2
1842
1843 union re_unwind_t;
1844
1845 typedef struct {                /* XX: makes sense to enlarge it... */
1846     I32 type;
1847     I32 prev;
1848     CHECKPOINT lastcp;
1849 } re_unwind_generic_t;
1850
1851 typedef struct {
1852     I32 type;
1853     I32 prev;
1854     CHECKPOINT lastcp;
1855     I32 lastparen;
1856     regnode *next;
1857     char *locinput;
1858     I32 nextchr;
1859 #ifdef DEBUGGING
1860     int regindent;
1861 #endif
1862 } re_unwind_branch_t;
1863
1864 typedef union re_unwind_t {
1865     I32 type;
1866     re_unwind_generic_t generic;
1867     re_unwind_branch_t branch;
1868 } re_unwind_t;
1869
1870 /*
1871  - regmatch - main matching routine
1872  *
1873  * Conceptually the strategy is simple:  check to see whether the current
1874  * node matches, call self recursively to see whether the rest matches,
1875  * and then act accordingly.  In practice we make some effort to avoid
1876  * recursion, in particular by going through "ordinary" nodes (that don't
1877  * need to know whether the rest of the match failed) by a loop instead of
1878  * by recursion.
1879  */
1880 /* [lwall] I've hoisted the register declarations to the outer block in order to
1881  * maybe save a little bit of pushing and popping on the stack.  It also takes
1882  * advantage of machines that use a register save mask on subroutine entry.
1883  */
1884 STATIC I32                      /* 0 failure, 1 success */
1885 S_regmatch(pTHX_ regnode *prog)
1886 {
1887     dTHR;
1888     register regnode *scan;     /* Current node. */
1889     regnode *next;              /* Next node. */
1890     regnode *inner;             /* Next node in internal branch. */
1891     register I32 nextchr;       /* renamed nextchr - nextchar colides with
1892                                    function of same name */
1893     register I32 n;             /* no or next */
1894     register I32 ln;            /* len or last */
1895     register char *s;           /* operand or save */
1896     register char *locinput = PL_reginput;
1897     register I32 c1, c2, paren; /* case fold search, parenth */
1898     int minmod = 0, sw = 0, logical = 0;
1899     I32 unwind = 0;
1900     I32 firstcp = PL_savestack_ix;
1901
1902 #ifdef DEBUGGING
1903     PL_regindent++;
1904 #endif
1905
1906     /* Note that nextchr is a byte even in UTF */
1907     nextchr = UCHARAT(locinput);
1908     scan = prog;
1909     while (scan != NULL) {
1910 #define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
1911 #if 1
1912 #  define sayYES goto yes
1913 #  define sayNO goto no
1914 #  define sayYES_FINAL goto yes_final
1915 #  define sayYES_LOUD  goto yes_loud
1916 #  define sayNO_FINAL  goto no_final
1917 #  define sayNO_SILENT goto do_no
1918 #  define saySAME(x) if (x) goto yes; else goto no
1919 #  define REPORT_CODE_OFF 24
1920 #else
1921 #  define sayYES return 1
1922 #  define sayNO return 0
1923 #  define sayYES_FINAL return 1
1924 #  define sayYES_LOUD  return 1
1925 #  define sayNO_FINAL  return 0
1926 #  define sayNO_SILENT return 0
1927 #  define saySAME(x) return x
1928 #endif
1929         DEBUG_r( {
1930             SV *prop = sv_newmortal();
1931             int docolor = *PL_colors[0];
1932             int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
1933             int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1934             /* The part of the string before starttry has one color
1935                (pref0_len chars), between starttry and current
1936                position another one (pref_len - pref0_len chars),
1937                after the current position the third one.
1938                We assume that pref0_len <= pref_len, otherwise we
1939                decrease pref0_len.  */
1940             int pref_len = (locinput - PL_bostr > (5 + taill) - l 
1941                             ? (5 + taill) - l : locinput - PL_bostr);
1942             int pref0_len = pref_len  - (locinput - PL_reg_starttry);
1943
1944             if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1945                 l = ( PL_regeol - locinput > (5 + taill) - pref_len 
1946                       ? (5 + taill) - pref_len : PL_regeol - locinput);
1947             if (pref0_len < 0)
1948                 pref0_len = 0;
1949             if (pref0_len > pref_len)
1950                 pref0_len = pref_len;
1951             regprop(prop, scan);
1952             PerlIO_printf(Perl_debug_log, 
1953                           "%4"IVdf" <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3"IVdf":%*s%s\n",
1954                           (IV)(locinput - PL_bostr), 
1955                           PL_colors[4], pref0_len, 
1956                           locinput - pref_len, PL_colors[5],
1957                           PL_colors[2], pref_len - pref0_len, 
1958                           locinput - pref_len + pref0_len, PL_colors[3],
1959                           (docolor ? "" : "> <"),
1960                           PL_colors[0], l, locinput, PL_colors[1],
1961                           15 - l - pref_len + 1,
1962                           "",
1963                           (IV)(scan - PL_regprogram), PL_regindent*2, "",
1964                           SvPVX(prop));
1965         } );
1966
1967         next = scan + NEXT_OFF(scan);
1968         if (next == scan)
1969             next = NULL;
1970
1971         switch (OP(scan)) {
1972         case BOL:
1973             if (locinput == PL_bostr
1974                 ? PL_regprev == '\n'
1975                 : (PL_multiline && 
1976                    (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1977             {
1978                 /* regtill = regbol; */
1979                 break;
1980             }
1981             sayNO;
1982         case MBOL:
1983             if (locinput == PL_bostr
1984                 ? PL_regprev == '\n'
1985                 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
1986             {
1987                 break;
1988             }
1989             sayNO;
1990         case SBOL:
1991             if (locinput == PL_bostr)
1992                 break;
1993             sayNO;
1994         case GPOS:
1995             if (locinput == PL_reg_ganch)
1996                 break;
1997             sayNO;
1998         case EOL:
1999             if (PL_multiline)
2000                 goto meol;
2001             else
2002                 goto seol;
2003         case MEOL:
2004           meol:
2005             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2006                 sayNO;
2007             break;
2008         case SEOL:
2009           seol:
2010             if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
2011                 sayNO;
2012             if (PL_regeol - locinput > 1)
2013                 sayNO;
2014             break;
2015         case EOS:
2016             if (PL_regeol != locinput)
2017                 sayNO;
2018             break;
2019         case SANYUTF8:
2020             if (nextchr & 0x80) {
2021                 locinput += PL_utf8skip[nextchr];
2022                 if (locinput > PL_regeol)
2023                     sayNO;
2024                 nextchr = UCHARAT(locinput);
2025                 break;
2026             }
2027             if (!nextchr && locinput >= PL_regeol)
2028                 sayNO;
2029             nextchr = UCHARAT(++locinput);
2030             break;
2031         case SANY:
2032             if (!nextchr && locinput >= PL_regeol)
2033                 sayNO;
2034             nextchr = UCHARAT(++locinput);
2035             break;
2036         case ANYUTF8:
2037             if (nextchr & 0x80) {
2038                 locinput += PL_utf8skip[nextchr];
2039                 if (locinput > PL_regeol)
2040                     sayNO;
2041                 nextchr = UCHARAT(locinput);
2042                 break;
2043             }
2044             if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2045                 sayNO;
2046             nextchr = UCHARAT(++locinput);
2047             break;
2048         case REG_ANY:
2049             if ((!nextchr && locinput >= PL_regeol) || nextchr == '\n')
2050                 sayNO;
2051             nextchr = UCHARAT(++locinput);
2052             break;
2053         case EXACT:
2054             s = STRING(scan);
2055             ln = STR_LEN(scan);
2056             /* Inline the first character, for speed. */
2057             if (UCHARAT(s) != nextchr)
2058                 sayNO;
2059             if (PL_regeol - locinput < ln)
2060                 sayNO;
2061             if (ln > 1 && memNE(s, locinput, ln))
2062                 sayNO;
2063             locinput += ln;
2064             nextchr = UCHARAT(locinput);
2065             break;
2066         case EXACTFL:
2067             PL_reg_flags |= RF_tainted;
2068             /* FALL THROUGH */
2069         case EXACTF:
2070             s = STRING(scan);
2071             ln = STR_LEN(scan);
2072
2073             if (UTF) {
2074                 char *l = locinput;
2075                 char *e = s + ln;
2076                 c1 = OP(scan) == EXACTF;
2077                 while (s < e) {
2078                     if (l >= PL_regeol)
2079                         sayNO;
2080                     if (utf8_to_uv((U8*)s, e - s, 0, 0) !=
2081                         (c1 ?
2082                          toLOWER_utf8((U8*)l) :
2083                          toLOWER_LC_utf8((U8*)l)))
2084                     {
2085                         sayNO;
2086                     }
2087                     s += UTF8SKIP(s);
2088                     l += UTF8SKIP(l);
2089                 }
2090                 locinput = l;
2091                 nextchr = UCHARAT(locinput);
2092                 break;
2093             }
2094
2095             /* Inline the first character, for speed. */
2096             if (UCHARAT(s) != nextchr &&
2097                 UCHARAT(s) != ((OP(scan) == EXACTF)
2098                                ? PL_fold : PL_fold_locale)[nextchr])
2099                 sayNO;
2100             if (PL_regeol - locinput < ln)
2101                 sayNO;
2102             if (ln > 1 && (OP(scan) == EXACTF
2103                            ? ibcmp(s, locinput, ln)
2104                            : ibcmp_locale(s, locinput, ln)))
2105                 sayNO;
2106             locinput += ln;
2107             nextchr = UCHARAT(locinput);
2108             break;
2109         case ANYOFUTF8:
2110             if (!REGINCLASSUTF8(scan, (U8*)locinput))
2111                 sayNO;
2112             if (locinput >= PL_regeol)
2113                 sayNO;
2114             locinput += PL_utf8skip[nextchr];
2115             nextchr = UCHARAT(locinput);
2116             break;
2117         case ANYOF:
2118             if (nextchr < 0)
2119                 nextchr = UCHARAT(locinput);
2120             if (!REGINCLASS(scan, nextchr))
2121                 sayNO;
2122             if (!nextchr && locinput >= PL_regeol)
2123                 sayNO;
2124             nextchr = UCHARAT(++locinput);
2125             break;
2126         case ALNUML:
2127             PL_reg_flags |= RF_tainted;
2128             /* FALL THROUGH */
2129         case ALNUM:
2130             if (!nextchr)
2131                 sayNO;
2132             if (!(OP(scan) == ALNUM
2133                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2134                 sayNO;
2135             nextchr = UCHARAT(++locinput);
2136             break;
2137         case ALNUMLUTF8:
2138             PL_reg_flags |= RF_tainted;
2139             /* FALL THROUGH */
2140         case ALNUMUTF8:
2141             if (!nextchr)
2142                 sayNO;
2143             if (nextchr & 0x80) {
2144                 if (!(OP(scan) == ALNUMUTF8
2145                       ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2146                       : isALNUM_LC_utf8((U8*)locinput)))
2147                 {
2148                     sayNO;
2149                 }
2150                 locinput += PL_utf8skip[nextchr];
2151                 nextchr = UCHARAT(locinput);
2152                 break;
2153             }
2154             if (!(OP(scan) == ALNUMUTF8
2155                   ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
2156                 sayNO;
2157             nextchr = UCHARAT(++locinput);
2158             break;
2159         case NALNUML:
2160             PL_reg_flags |= RF_tainted;
2161             /* FALL THROUGH */
2162         case NALNUM:
2163             if (!nextchr && locinput >= PL_regeol)
2164                 sayNO;
2165             if (OP(scan) == NALNUM
2166                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2167                 sayNO;
2168             nextchr = UCHARAT(++locinput);
2169             break;
2170         case NALNUMLUTF8:
2171             PL_reg_flags |= RF_tainted;
2172             /* FALL THROUGH */
2173         case NALNUMUTF8:
2174             if (!nextchr && locinput >= PL_regeol)
2175                 sayNO;
2176             if (nextchr & 0x80) {
2177                 if (OP(scan) == NALNUMUTF8
2178                     ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
2179                     : isALNUM_LC_utf8((U8*)locinput))
2180                 {
2181                     sayNO;
2182                 }
2183                 locinput += PL_utf8skip[nextchr];
2184                 nextchr = UCHARAT(locinput);
2185                 break;
2186             }
2187             if (OP(scan) == NALNUMUTF8
2188                 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
2189                 sayNO;
2190             nextchr = UCHARAT(++locinput);
2191             break;
2192         case BOUNDL:
2193         case NBOUNDL:
2194             PL_reg_flags |= RF_tainted;
2195             /* FALL THROUGH */
2196         case BOUND:
2197         case NBOUND:
2198             /* was last char in word? */
2199             ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
2200             if (OP(scan) == BOUND || OP(scan) == NBOUND) {
2201                 ln = isALNUM(ln);
2202                 n = isALNUM(nextchr);
2203             }
2204             else {
2205                 ln = isALNUM_LC(ln);
2206                 n = isALNUM_LC(nextchr);
2207             }
2208             if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
2209                 sayNO;
2210             break;
2211         case BOUNDLUTF8:
2212         case NBOUNDLUTF8:
2213             PL_reg_flags |= RF_tainted;
2214             /* FALL THROUGH */
2215         case BOUNDUTF8:
2216         case NBOUNDUTF8:
2217             /* was last char in word? */
2218             if (locinput == PL_regbol)
2219                 ln = PL_regprev;
2220             else {
2221                 U8 *r = reghop((U8*)locinput, -1);
2222
2223                 ln = utf8_to_uv(r, s - (char*)r, 0, 0);
2224             }
2225             if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
2226                 ln = isALNUM_uni(ln);
2227                 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
2228             }
2229             else {
2230                 ln = isALNUM_LC_uni(ln);
2231                 n = isALNUM_LC_utf8((U8*)locinput);
2232             }
2233             if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
2234                 sayNO;
2235             break;
2236         case SPACEL:
2237             PL_reg_flags |= RF_tainted;
2238             /* FALL THROUGH */
2239         case SPACE:
2240             if (!nextchr)
2241                 sayNO;
2242             if (!(OP(scan) == SPACE
2243                   ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2244                 sayNO;
2245             nextchr = UCHARAT(++locinput);
2246             break;
2247         case SPACELUTF8:
2248             PL_reg_flags |= RF_tainted;
2249             /* FALL THROUGH */
2250         case SPACEUTF8:
2251             if (!nextchr)
2252                 sayNO;
2253             if (nextchr & 0x80) {
2254                 if (!(OP(scan) == SPACEUTF8
2255                       ? swash_fetch(PL_utf8_space, (U8*)locinput)
2256                       : isSPACE_LC_utf8((U8*)locinput)))
2257                 {
2258                     sayNO;
2259                 }
2260                 locinput += PL_utf8skip[nextchr];
2261                 nextchr = UCHARAT(locinput);
2262                 break;
2263             }
2264             if (!(OP(scan) == SPACEUTF8
2265                   ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
2266                 sayNO;
2267             nextchr = UCHARAT(++locinput);
2268             break;
2269         case NSPACEL:
2270             PL_reg_flags |= RF_tainted;
2271             /* FALL THROUGH */
2272         case NSPACE:
2273             if (!nextchr && locinput >= PL_regeol)
2274                 sayNO;
2275             if (OP(scan) == NSPACE
2276                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2277                 sayNO;
2278             nextchr = UCHARAT(++locinput);
2279             break;
2280         case NSPACELUTF8:
2281             PL_reg_flags |= RF_tainted;
2282             /* FALL THROUGH */
2283         case NSPACEUTF8:
2284             if (!nextchr && locinput >= PL_regeol)
2285                 sayNO;
2286             if (nextchr & 0x80) {
2287                 if (OP(scan) == NSPACEUTF8
2288                     ? swash_fetch(PL_utf8_space, (U8*)locinput)
2289                     : isSPACE_LC_utf8((U8*)locinput))
2290                 {
2291                     sayNO;
2292                 }
2293                 locinput += PL_utf8skip[nextchr];
2294                 nextchr = UCHARAT(locinput);
2295                 break;
2296             }
2297             if (OP(scan) == NSPACEUTF8
2298                 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
2299                 sayNO;
2300             nextchr = UCHARAT(++locinput);
2301             break;
2302         case DIGITL:
2303             PL_reg_flags |= RF_tainted;
2304             /* FALL THROUGH */
2305         case DIGIT:
2306             if (!nextchr)
2307                 sayNO;
2308             if (!(OP(scan) == DIGIT
2309                   ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2310                 sayNO;
2311             nextchr = UCHARAT(++locinput);
2312             break;
2313         case DIGITLUTF8:
2314             PL_reg_flags |= RF_tainted;
2315             /* FALL THROUGH */
2316         case DIGITUTF8:
2317             if (!nextchr)
2318                 sayNO;
2319             if (nextchr & 0x80) {
2320                 if (!(OP(scan) == DIGITUTF8
2321                       ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2322                       : isDIGIT_LC_utf8((U8*)locinput)))
2323                 {
2324                     sayNO;
2325                 }
2326                 locinput += PL_utf8skip[nextchr];
2327                 nextchr = UCHARAT(locinput);
2328                 break;
2329             }
2330             if (!(OP(scan) == DIGITUTF8
2331                   ? isDIGIT(nextchr) : isDIGIT_LC(nextchr)))
2332                 sayNO;
2333             nextchr = UCHARAT(++locinput);
2334             break;
2335         case NDIGITL:
2336             PL_reg_flags |= RF_tainted;
2337             /* FALL THROUGH */
2338         case NDIGIT:
2339             if (!nextchr && locinput >= PL_regeol)
2340                 sayNO;
2341             if (OP(scan) == NDIGIT
2342                 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2343                 sayNO;
2344             nextchr = UCHARAT(++locinput);
2345             break;
2346         case NDIGITLUTF8:
2347             PL_reg_flags |= RF_tainted;
2348             /* FALL THROUGH */
2349         case NDIGITUTF8:
2350             if (!nextchr && locinput >= PL_regeol)
2351                 sayNO;
2352             if (nextchr & 0x80) {
2353                 if (OP(scan) == NDIGITUTF8
2354                     ? swash_fetch(PL_utf8_digit, (U8*)locinput)
2355                     : isDIGIT_LC_utf8((U8*)locinput))
2356                 {
2357                     sayNO;
2358                 }
2359                 locinput += PL_utf8skip[nextchr];
2360                 nextchr = UCHARAT(locinput);
2361                 break;
2362             }
2363             if (OP(scan) == NDIGITUTF8
2364                 ? isDIGIT(nextchr) : isDIGIT_LC(nextchr))
2365                 sayNO;
2366             nextchr = UCHARAT(++locinput);
2367             break;
2368         case CLUMP:
2369             if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
2370                 sayNO;
2371             locinput += PL_utf8skip[nextchr];
2372             while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
2373                 locinput += UTF8SKIP(locinput);
2374             if (locinput > PL_regeol)
2375                 sayNO;
2376             nextchr = UCHARAT(locinput);
2377             break;
2378         case REFFL:
2379             PL_reg_flags |= RF_tainted;
2380             /* FALL THROUGH */
2381         case REF:
2382         case REFF:
2383             n = ARG(scan);  /* which paren pair */
2384             ln = PL_regstartp[n];
2385             PL_reg_leftiter = PL_reg_maxiter;           /* Void cache */
2386             if (*PL_reglastparen < n || ln == -1)
2387                 sayNO;                  /* Do not match unless seen CLOSEn. */
2388             if (ln == PL_regendp[n])
2389                 break;
2390
2391             s = PL_bostr + ln;
2392             if (UTF && OP(scan) != REF) {       /* REF can do byte comparison */
2393                 char *l = locinput;
2394                 char *e = PL_bostr + PL_regendp[n];
2395                 /*
2396                  * Note that we can't do the "other character" lookup trick as
2397                  * in the 8-bit case (no pun intended) because in Unicode we
2398                  * have to map both upper and title case to lower case.
2399                  */
2400                 if (OP(scan) == REFF) {
2401                     while (s < e) {
2402                         if (l >= PL_regeol)
2403                             sayNO;
2404                         if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
2405                             sayNO;
2406                         s += UTF8SKIP(s);
2407                         l += UTF8SKIP(l);
2408                     }
2409                 }
2410                 else {
2411                     while (s < e) {
2412                         if (l >= PL_regeol)
2413                             sayNO;
2414                         if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
2415                             sayNO;
2416                         s += UTF8SKIP(s);
2417                         l += UTF8SKIP(l);
2418                     }
2419                 }
2420                 locinput = l;
2421                 nextchr = UCHARAT(locinput);
2422                 break;
2423             }
2424
2425             /* Inline the first character, for speed. */
2426             if (UCHARAT(s) != nextchr &&
2427                 (OP(scan) == REF ||
2428                  (UCHARAT(s) != ((OP(scan) == REFF
2429                                   ? PL_fold : PL_fold_locale)[nextchr]))))
2430                 sayNO;
2431             ln = PL_regendp[n] - ln;
2432             if (locinput + ln > PL_regeol)
2433                 sayNO;
2434             if (ln > 1 && (OP(scan) == REF
2435                            ? memNE(s, locinput, ln)
2436                            : (OP(scan) == REFF
2437                               ? ibcmp(s, locinput, ln)
2438                               : ibcmp_locale(s, locinput, ln))))
2439                 sayNO;
2440             locinput += ln;
2441             nextchr = UCHARAT(locinput);
2442             break;
2443
2444         case NOTHING:
2445         case TAIL:
2446             break;
2447         case BACK:
2448             break;
2449         case EVAL:
2450         {
2451             dSP;
2452             OP_4tree *oop = PL_op;
2453             COP *ocurcop = PL_curcop;
2454             SV **ocurpad = PL_curpad;
2455             SV *ret;
2456             
2457             n = ARG(scan);
2458             PL_op = (OP_4tree*)PL_regdata->data[n];
2459             DEBUG_r( PerlIO_printf(Perl_debug_log, "  re_eval 0x%"UVxf"\n", PTR2UV(PL_op)) );
2460             PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 2]);
2461             PL_regendp[0] = PL_reg_magic->mg_len = locinput - PL_bostr;
2462
2463             CALLRUNOPS(aTHX);                   /* Scalar context. */
2464             SPAGAIN;
2465             ret = POPs;
2466             PUTBACK;
2467             
2468             PL_op = oop;
2469             PL_curpad = ocurpad;
2470             PL_curcop = ocurcop;
2471             if (logical) {
2472                 if (logical == 2) {     /* Postponed subexpression. */
2473                     regexp *re;
2474                     MAGIC *mg = Null(MAGIC*);
2475                     re_cc_state state;
2476                     CHECKPOINT cp, lastcp;
2477
2478                     if(SvROK(ret) || SvRMAGICAL(ret)) {
2479                         SV *sv = SvROK(ret) ? SvRV(ret) : ret;
2480
2481                         if(SvMAGICAL(sv))
2482                             mg = mg_find(sv, 'r');
2483                     }
2484                     if (mg) {
2485                         re = (regexp *)mg->mg_obj;
2486                         (void)ReREFCNT_inc(re);
2487                     }
2488                     else {
2489                         STRLEN len;
2490                         char *t = SvPV(ret, len);
2491                         PMOP pm;
2492                         char *oprecomp = PL_regprecomp;
2493                         I32 osize = PL_regsize;
2494                         I32 onpar = PL_regnpar;
2495
2496                         pm.op_pmflags = 0;
2497                         pm.op_pmdynflags = (UTF||DO_UTF8(ret) ? PMdf_UTF8 : 0);
2498                         re = CALLREGCOMP(aTHX_ t, t + len, &pm);
2499                         if (!(SvFLAGS(ret) 
2500                               & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
2501                             sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
2502                         PL_regprecomp = oprecomp;
2503                         PL_regsize = osize;
2504                         PL_regnpar = onpar;
2505                     }
2506                     DEBUG_r(
2507                         PerlIO_printf(Perl_debug_log, 
2508                                       "Entering embedded `%s%.60s%s%s'\n",
2509                                       PL_colors[0],
2510                                       re->precomp,
2511                                       PL_colors[1],
2512                                       (strlen(re->precomp) > 60 ? "..." : ""))
2513                         );
2514                     state.node = next;
2515                     state.prev = PL_reg_call_cc;
2516                     state.cc = PL_regcc;
2517                     state.re = PL_reg_re;
2518
2519                     PL_regcc = 0;
2520                     
2521                     cp = regcppush(0);  /* Save *all* the positions. */
2522                     REGCP_SET(lastcp);
2523                     cache_re(re);
2524                     state.ss = PL_savestack_ix;
2525                     *PL_reglastparen = 0;
2526                     PL_reg_call_cc = &state;
2527                     PL_reginput = locinput;
2528
2529                     /* XXXX This is too dramatic a measure... */
2530                     PL_reg_maxiter = 0;
2531
2532                     if (regmatch(re->program + 1)) {
2533                         /* Even though we succeeded, we need to restore
2534                            global variables, since we may be wrapped inside
2535                            SUSPEND, thus the match may be not finished yet. */
2536
2537                         /* XXXX Do this only if SUSPENDed? */
2538                         PL_reg_call_cc = state.prev;
2539                         PL_regcc = state.cc;
2540                         PL_reg_re = state.re;
2541                         cache_re(PL_reg_re);
2542
2543                         /* XXXX This is too dramatic a measure... */
2544                         PL_reg_maxiter = 0;
2545
2546                         /* These are needed even if not SUSPEND. */
2547                         ReREFCNT_dec(re);
2548                         regcpblow(cp);
2549                         sayYES;
2550                     }
2551                     ReREFCNT_dec(re);
2552                     REGCP_UNWIND(lastcp);
2553                     regcppop();
2554                     PL_reg_call_cc = state.prev;
2555                     PL_regcc = state.cc;
2556                     PL_reg_re = state.re;
2557                     cache_re(PL_reg_re);
2558
2559                     /* XXXX This is too dramatic a measure... */
2560                     PL_reg_maxiter = 0;
2561
2562                     sayNO;
2563                 }
2564                 sw = SvTRUE(ret);
2565                 logical = 0;
2566             }
2567             else
2568                 sv_setsv(save_scalar(PL_replgv), ret);
2569             break;
2570         }
2571         case OPEN:
2572             n = ARG(scan);  /* which paren pair */
2573             PL_reg_start_tmp[n] = locinput;
2574             if (n > PL_regsize)
2575                 PL_regsize = n;
2576             break;
2577         case CLOSE:
2578             n = ARG(scan);  /* which paren pair */
2579             PL_regstartp[n] = PL_reg_start_tmp[n] - PL_bostr;
2580             PL_regendp[n] = locinput - PL_bostr;
2581             if (n > *PL_reglastparen)
2582                 *PL_reglastparen = n;
2583             break;
2584         case GROUPP:
2585             n = ARG(scan);  /* which paren pair */
2586             sw = (*PL_reglastparen >= n && PL_regendp[n] != -1);
2587             break;
2588         case IFTHEN:
2589             PL_reg_leftiter = PL_reg_maxiter;           /* Void cache */
2590             if (sw)
2591                 next = NEXTOPER(NEXTOPER(scan));
2592             else {
2593                 next = scan + ARG(scan);
2594                 if (OP(next) == IFTHEN) /* Fake one. */
2595                     next = NEXTOPER(NEXTOPER(next));
2596             }
2597             break;
2598         case LOGICAL:
2599             logical = scan->flags;
2600             break;
2601 /*******************************************************************
2602  PL_regcc contains infoblock about the innermost (...)* loop, and
2603  a pointer to the next outer infoblock.
2604
2605  Here is how Y(A)*Z is processed (if it is compiled into CURLYX/WHILEM):
2606
2607    1) After matching X, regnode for CURLYX is processed;
2608
2609    2) This regnode creates infoblock on the stack, and calls 
2610       regmatch() recursively with the starting point at WHILEM node;
2611
2612    3) Each hit of WHILEM node tries to match A and Z (in the order
2613       depending on the current iteration, min/max of {min,max} and
2614       greediness).  The information about where are nodes for "A"
2615       and "Z" is read from the infoblock, as is info on how many times "A"
2616       was already matched, and greediness.
2617
2618    4) After A matches, the same WHILEM node is hit again.
2619
2620    5) Each time WHILEM is hit, PL_regcc is the infoblock created by CURLYX
2621       of the same pair.  Thus when WHILEM tries to match Z, it temporarily
2622       resets PL_regcc, since this Y(A)*Z can be a part of some other loop:
2623       as in (Y(A)*Z)*.  If Z matches, the automaton will hit the WHILEM node
2624       of the external loop.
2625
2626  Currently present infoblocks form a tree with a stem formed by PL_curcc
2627  and whatever it mentions via ->next, and additional attached trees
2628  corresponding to temporarily unset infoblocks as in "5" above.
2629
2630  In the following picture infoblocks for outer loop of 
2631  (Y(A)*?Z)*?T are denoted O, for inner I.  NULL starting block
2632  is denoted by x.  The matched string is YAAZYAZT.  Temporarily postponed
2633  infoblocks are drawn below the "reset" infoblock.
2634
2635  In fact in the picture below we do not show failed matches for Z and T
2636  by WHILEM blocks.  [We illustrate minimal matches, since for them it is
2637  more obvious *why* one needs to *temporary* unset infoblocks.]
2638
2639   Matched       REx position    InfoBlocks      Comment
2640                 (Y(A)*?Z)*?T    x
2641                 Y(A)*?Z)*?T     x <- O
2642   Y             (A)*?Z)*?T      x <- O
2643   Y             A)*?Z)*?T       x <- O <- I
2644   YA            )*?Z)*?T        x <- O <- I
2645   YA            A)*?Z)*?T       x <- O <- I
2646   YAA           )*?Z)*?T        x <- O <- I
2647   YAA           Z)*?T           x <- O          # Temporary unset I
2648                                      I
2649
2650   YAAZ          Y(A)*?Z)*?T     x <- O
2651                                      I
2652
2653   YAAZY         (A)*?Z)*?T      x <- O
2654                                      I
2655
2656   YAAZY         A)*?Z)*?T       x <- O <- I
2657                                      I
2658
2659   YAAZYA        )*?Z)*?T        x <- O <- I     
2660                                      I
2661
2662   YAAZYA        Z)*?T           x <- O          # Temporary unset I
2663                                      I,I
2664
2665   YAAZYAZ       )*?T            x <- O
2666                                      I,I
2667
2668   YAAZYAZ       T               x               # Temporary unset O
2669                                 O
2670                                 I,I
2671
2672   YAAZYAZT                      x
2673                                 O
2674                                 I,I
2675  *******************************************************************/
2676         case CURLYX: {
2677                 CURCUR cc;
2678                 CHECKPOINT cp = PL_savestack_ix;
2679                 /* No need to save/restore up to this paren */
2680                 I32 parenfloor = scan->flags;
2681
2682                 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
2683                     next += ARG(next);
2684                 cc.oldcc = PL_regcc;
2685                 PL_regcc = &cc;
2686                 /* XXXX Probably it is better to teach regpush to support
2687                    parenfloor > PL_regsize... */
2688                 if (parenfloor > *PL_reglastparen)
2689                     parenfloor = *PL_reglastparen; /* Pessimization... */
2690                 cc.parenfloor = parenfloor;
2691                 cc.cur = -1;
2692                 cc.min = ARG1(scan);
2693                 cc.max  = ARG2(scan);
2694                 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
2695                 cc.next = next;
2696                 cc.minmod = minmod;
2697                 cc.lastloc = 0;
2698                 PL_reginput = locinput;
2699                 n = regmatch(PREVOPER(next));   /* start on the WHILEM */
2700                 regcpblow(cp);
2701                 PL_regcc = cc.oldcc;
2702                 saySAME(n);
2703             }
2704             /* NOT REACHED */
2705         case WHILEM: {
2706                 /*
2707                  * This is really hard to understand, because after we match
2708                  * what we're trying to match, we must make sure the rest of
2709                  * the REx is going to match for sure, and to do that we have
2710                  * to go back UP the parse tree by recursing ever deeper.  And
2711                  * if it fails, we have to reset our parent's current state
2712                  * that we can try again after backing off.
2713                  */
2714
2715                 CHECKPOINT cp, lastcp;
2716                 CURCUR* cc = PL_regcc;
2717                 char *lastloc = cc->lastloc; /* Detection of 0-len. */
2718                 
2719                 n = cc->cur + 1;        /* how many we know we matched */
2720                 PL_reginput = locinput;
2721
2722                 DEBUG_r(
2723                     PerlIO_printf(Perl_debug_log, 
2724                                   "%*s  %ld out of %ld..%ld  cc=%lx\n", 
2725                                   REPORT_CODE_OFF+PL_regindent*2, "",
2726                                   (long)n, (long)cc->min, 
2727                                   (long)cc->max, (long)cc)
2728                     );
2729
2730                 /* If degenerate scan matches "", assume scan done. */
2731
2732                 if (locinput == cc->lastloc && n >= cc->min) {
2733                     PL_regcc = cc->oldcc;
2734                     if (PL_regcc)
2735                         ln = PL_regcc->cur;
2736                     DEBUG_r(
2737                         PerlIO_printf(Perl_debug_log,
2738                            "%*s  empty match detected, try continuation...\n",
2739                            REPORT_CODE_OFF+PL_regindent*2, "")
2740                         );
2741                     if (regmatch(cc->next))
2742                         sayYES;
2743                     if (PL_regcc)
2744                         PL_regcc->cur = ln;
2745                     PL_regcc = cc;
2746                     sayNO;
2747                 }
2748
2749                 /* First just match a string of min scans. */
2750
2751                 if (n < cc->min) {
2752                     cc->cur = n;
2753                     cc->lastloc = locinput;
2754                     if (regmatch(cc->scan))
2755                         sayYES;
2756                     cc->cur = n - 1;
2757                     cc->lastloc = lastloc;
2758                     sayNO;
2759                 }
2760
2761                 if (scan->flags) {
2762                     /* Check whether we already were at this position.
2763                         Postpone detection until we know the match is not
2764                         *that* much linear. */
2765                 if (!PL_reg_maxiter) {
2766                     PL_reg_maxiter = (PL_regeol - PL_bostr + 1) * (scan->flags>>4);
2767                     PL_reg_leftiter = PL_reg_maxiter;
2768                 }
2769                 if (PL_reg_leftiter-- == 0) {
2770                     I32 size = (PL_reg_maxiter + 7)/8;
2771                     if (PL_reg_poscache) {
2772                         if (PL_reg_poscache_size < size) {
2773                             Renew(PL_reg_poscache, size, char);
2774                             PL_reg_poscache_size = size;
2775                         }
2776                         Zero(PL_reg_poscache, size, char);
2777                     }
2778                     else {
2779                         PL_reg_poscache_size = size;
2780                         Newz(29, PL_reg_poscache, size, char);
2781                     }
2782                     DEBUG_r(
2783                         PerlIO_printf(Perl_debug_log,
2784               "%sDetected a super-linear match, switching on caching%s...\n",
2785                                       PL_colors[4], PL_colors[5])
2786                         );
2787                 }
2788                 if (PL_reg_leftiter < 0) {
2789                     I32 o = locinput - PL_bostr, b;
2790
2791                     o = (scan->flags & 0xf) - 1 + o * (scan->flags>>4);
2792                     b = o % 8;
2793                     o /= 8;
2794                     if (PL_reg_poscache[o] & (1<<b)) {
2795                     DEBUG_r(
2796                         PerlIO_printf(Perl_debug_log,
2797                                       "%*s  already tried at this position...\n",
2798                                       REPORT_CODE_OFF+PL_regindent*2, "")
2799                         );
2800                         sayNO_SILENT;
2801                     }
2802                     PL_reg_poscache[o] |= (1<<b);
2803                 }
2804                 }
2805
2806                 /* Prefer next over scan for minimal matching. */
2807
2808                 if (cc->minmod) {
2809                     PL_regcc = cc->oldcc;
2810                     if (PL_regcc)
2811                         ln = PL_regcc->cur;
2812                     cp = regcppush(cc->parenfloor);
2813                     REGCP_SET(lastcp);
2814                     if (regmatch(cc->next)) {
2815                         regcpblow(cp);
2816                         sayYES; /* All done. */
2817                     }
2818                     REGCP_UNWIND(lastcp);
2819                     regcppop();
2820                     if (PL_regcc)
2821                         PL_regcc->cur = ln;
2822                     PL_regcc = cc;
2823
2824                     if (n >= cc->max) { /* Maximum greed exceeded? */
2825                         if (ckWARN(WARN_REGEXP) && n >= REG_INFTY 
2826                             && !(PL_reg_flags & RF_warned)) {
2827                             PL_reg_flags |= RF_warned;
2828                             Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2829                                  "Complex regular subexpression recursion",
2830                                  REG_INFTY - 1);
2831                         }
2832                         sayNO;
2833                     }
2834
2835                     DEBUG_r(
2836                         PerlIO_printf(Perl_debug_log,
2837                                       "%*s  trying longer...\n",
2838                                       REPORT_CODE_OFF+PL_regindent*2, "")
2839                         );
2840                     /* Try scanning more and see if it helps. */
2841                     PL_reginput = locinput;
2842                     cc->cur = n;
2843                     cc->lastloc = locinput;
2844                     cp = regcppush(cc->parenfloor);
2845                     REGCP_SET(lastcp);
2846                     if (regmatch(cc->scan)) {
2847                         regcpblow(cp);
2848                         sayYES;
2849                     }
2850                     REGCP_UNWIND(lastcp);
2851                     regcppop();
2852                     cc->cur = n - 1;
2853                     cc->lastloc = lastloc;
2854                     sayNO;
2855                 }
2856
2857                 /* Prefer scan over next for maximal matching. */
2858
2859                 if (n < cc->max) {      /* More greed allowed? */
2860                     cp = regcppush(cc->parenfloor);
2861                     cc->cur = n;
2862                     cc->lastloc = locinput;
2863                     REGCP_SET(lastcp);
2864                     if (regmatch(cc->scan)) {
2865                         regcpblow(cp);
2866                         sayYES;
2867                     }
2868                     REGCP_UNWIND(lastcp);
2869                     regcppop();         /* Restore some previous $<digit>s? */
2870                     PL_reginput = locinput;
2871                     DEBUG_r(
2872                         PerlIO_printf(Perl_debug_log,
2873                                       "%*s  failed, try continuation...\n",
2874                                       REPORT_CODE_OFF+PL_regindent*2, "")
2875                         );
2876                 }
2877                 if (ckWARN(WARN_REGEXP) && n >= REG_INFTY 
2878                         && !(PL_reg_flags & RF_warned)) {
2879                     PL_reg_flags |= RF_warned;
2880                     Perl_warner(aTHX_ WARN_REGEXP, "%s limit (%d) exceeded",
2881                          "Complex regular subexpression recursion",
2882                          REG_INFTY - 1);
2883                 }
2884
2885                 /* Failed deeper matches of scan, so see if this one works. */
2886                 PL_regcc = cc->oldcc;
2887                 if (PL_regcc)
2888                     ln = PL_regcc->cur;
2889                 if (regmatch(cc->next))
2890                     sayYES;
2891                 if (PL_regcc)
2892                     PL_regcc->cur = ln;
2893                 PL_regcc = cc;
2894                 cc->cur = n - 1;
2895                 cc->lastloc = lastloc;
2896                 sayNO;
2897             }
2898             /* NOT REACHED */
2899         case BRANCHJ: 
2900             next = scan + ARG(scan);
2901             if (next == scan)
2902                 next = NULL;
2903             inner = NEXTOPER(NEXTOPER(scan));
2904             goto do_branch;
2905         case BRANCH: 
2906             inner = NEXTOPER(scan);
2907           do_branch:
2908             {
2909                 CHECKPOINT lastcp;
2910                 c1 = OP(scan);
2911                 if (OP(next) != c1)     /* No choice. */
2912                     next = inner;       /* Avoid recursion. */
2913                 else {
2914                     I32 lastparen = *PL_reglastparen;
2915                     I32 unwind1;
2916                     re_unwind_branch_t *uw;
2917
2918                     /* Put unwinding data on stack */
2919                     unwind1 = SSNEWt(1,re_unwind_branch_t);
2920                     uw = SSPTRt(unwind1,re_unwind_branch_t);
2921                     uw->prev = unwind;
2922                     unwind = unwind1;
2923                     uw->type = ((c1 == BRANCH)
2924                                 ? RE_UNWIND_BRANCH
2925                                 : RE_UNWIND_BRANCHJ);
2926                     uw->lastparen = lastparen;
2927                     uw->next = next;
2928                     uw->locinput = locinput;
2929                     uw->nextchr = nextchr;
2930 #ifdef DEBUGGING
2931                     uw->regindent = ++PL_regindent;
2932 #endif
2933
2934                     REGCP_SET(uw->lastcp);
2935
2936                     /* Now go into the first branch */
2937                     next = inner;
2938                 }
2939             }
2940             break;
2941         case MINMOD:
2942             minmod = 1;
2943             break;
2944         case CURLYM:
2945         {
2946             I32 l = 0;
2947             CHECKPOINT lastcp;
2948             
2949             /* We suppose that the next guy does not need
2950                backtracking: in particular, it is of constant length,
2951                and has no parenths to influence future backrefs. */
2952             ln = ARG1(scan);  /* min to match */
2953             n  = ARG2(scan);  /* max to match */
2954             paren = scan->flags;
2955             if (paren) {
2956                 if (paren > PL_regsize)
2957                     PL_regsize = paren;
2958                 if (paren > *PL_reglastparen)
2959                     *PL_reglastparen = paren;
2960             }
2961             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
2962             if (paren)
2963                 scan += NEXT_OFF(scan); /* Skip former OPEN. */
2964             PL_reginput = locinput;
2965             if (minmod) {
2966                 minmod = 0;
2967                 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2968                     sayNO;
2969                 if (ln && l == 0 && n >= ln
2970                     /* In fact, this is tricky.  If paren, then the
2971                        fact that we did/didnot match may influence
2972                        future execution. */
2973                     && !(paren && ln == 0))
2974                     ln = n;
2975                 locinput = PL_reginput;
2976                 if (PL_regkind[(U8)OP(next)] == EXACT) {
2977                     c1 = (U8)*STRING(next);
2978                     if (OP(next) == EXACTF)
2979                         c2 = PL_fold[c1];
2980                     else if (OP(next) == EXACTFL)
2981                         c2 = PL_fold_locale[c1];
2982                     else
2983                         c2 = c1;
2984                 }
2985                 else
2986                     c1 = c2 = -1000;
2987                 REGCP_SET(lastcp);
2988                 /* This may be improved if l == 0.  */
2989                 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2990                     /* If it could work, try it. */
2991                     if (c1 == -1000 ||
2992                         UCHARAT(PL_reginput) == c1 ||
2993                         UCHARAT(PL_reginput) == c2)
2994                     {
2995                         if (paren) {
2996                             if (n) {
2997                                 PL_regstartp[paren] =
2998                                     HOPc(PL_reginput, -l) - PL_bostr;
2999                                 PL_regendp[paren] = PL_reginput - PL_bostr;
3000                             }
3001                             else
3002                                 PL_regendp[paren] = -1;
3003                         }
3004                         if (regmatch(next))
3005                             sayYES;
3006                         REGCP_UNWIND(lastcp);
3007                     }
3008                     /* Couldn't or didn't -- move forward. */
3009                     PL_reginput = locinput;
3010                     if (regrepeat_hard(scan, 1, &l)) {
3011                         ln++;
3012                         locinput = PL_reginput;
3013                     }
3014                     else
3015                         sayNO;
3016                 }
3017             }
3018             else {
3019                 n = regrepeat_hard(scan, n, &l);
3020                 if (n != 0 && l == 0
3021                     /* In fact, this is tricky.  If paren, then the
3022                        fact that we did/didnot match may influence
3023                        future execution. */
3024                     && !(paren && ln == 0))
3025                     ln = n;
3026                 locinput = PL_reginput;
3027                 DEBUG_r(
3028                     PerlIO_printf(Perl_debug_log,
3029                                   "%*s  matched %"IVdf" times, len=%"IVdf"...\n",
3030                                   (int)(REPORT_CODE_OFF+PL_regindent*2), "",
3031                                   (IV) n, (IV)l)
3032                     );
3033                 if (n >= ln) {
3034                     if (PL_regkind[(U8)OP(next)] == EXACT) {
3035                         c1 = (U8)*STRING(next);
3036                         if (OP(next) == EXACTF)
3037                             c2 = PL_fold[c1];
3038                         else if (OP(next) == EXACTFL)
3039                             c2 = PL_fold_locale[c1];
3040                         else
3041                             c2 = c1;
3042                     }
3043                     else
3044                         c1 = c2 = -1000;
3045                 }
3046                 REGCP_SET(lastcp);
3047                 while (n >= ln) {
3048                     /* If it could work, try it. */
3049                     if (c1 == -1000 ||
3050                         UCHARAT(PL_reginput) == c1 ||
3051                         UCHARAT(PL_reginput) == c2)
3052                     {
3053                         DEBUG_r(
3054                                 PerlIO_printf(Perl_debug_log,
3055                                               "%*s  trying tail with n=%"IVdf"...\n",
3056                                               (int)(REPORT_CODE_OFF+PL_regindent*2), "", (IV)n)
3057                             );
3058                         if (paren) {
3059                             if (n) {
3060                                 PL_regstartp[paren] = HOPc(PL_reginput, -l) - PL_bostr;
3061                                 PL_regendp[paren] = PL_reginput - PL_bostr;
3062                             }
3063                             else
3064                                 PL_regendp[paren] = -1;
3065                         }
3066                         if (regmatch(next))
3067                             sayYES;
3068                         REGCP_UNWIND(lastcp);
3069                     }
3070                     /* Couldn't or didn't -- back up. */
3071                     n--;
3072                     locinput = HOPc(locinput, -l);
3073                     PL_reginput = locinput;
3074                 }
3075             }
3076             sayNO;
3077             break;
3078         }
3079         case CURLYN:
3080             paren = scan->flags;        /* Which paren to set */
3081             if (paren > PL_regsize)
3082                 PL_regsize = paren;
3083             if (paren > *PL_reglastparen)
3084                 *PL_reglastparen = paren;
3085             ln = ARG1(scan);  /* min to match */
3086             n  = ARG2(scan);  /* max to match */
3087             scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
3088             goto repeat;
3089         case CURLY:
3090             paren = 0;
3091             ln = ARG1(scan);  /* min to match */
3092             n  = ARG2(scan);  /* max to match */
3093             scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
3094             goto repeat;
3095         case STAR:
3096             ln = 0;
3097             n = REG_INFTY;
3098             scan = NEXTOPER(scan);
3099             paren = 0;
3100             goto repeat;
3101         case PLUS:
3102             ln = 1;
3103             n = REG_INFTY;
3104             scan = NEXTOPER(scan);
3105             paren = 0;
3106           repeat:
3107             /*
3108             * Lookahead to avoid useless match attempts
3109             * when we know what character comes next.
3110             */
3111             if (PL_regkind[(U8)OP(next)] == EXACT) {
3112                 c1 = (U8)*STRING(next);
3113                 if (OP(next) == EXACTF)
3114                     c2 = PL_fold[c1];
3115                 else if (OP(next) == EXACTFL)
3116                     c2 = PL_fold_locale[c1];
3117                 else
3118                     c2 = c1;
3119             }
3120             else
3121                 c1 = c2 = -1000;
3122             PL_reginput = locinput;
3123             if (minmod) {
3124                 CHECKPOINT lastcp;
3125                 minmod = 0;
3126                 if (ln && regrepeat(scan, ln) < ln)
3127                     sayNO;
3128                 locinput = PL_reginput;
3129                 REGCP_SET(lastcp);
3130                 if (c1 != -1000) {
3131                     char *e = locinput + n - ln; /* Should not check after this */
3132                     char *old = locinput;
3133
3134                     if (e >= PL_regeol || (n == REG_INFTY))
3135                         e = PL_regeol - 1;
3136                     while (1) {
3137                         /* Find place 'next' could work */
3138                         if (c1 == c2) {
3139                             while (locinput <= e && *locinput != c1)
3140                                 locinput++;
3141                         } else {
3142                             while (locinput <= e 
3143                                    && *locinput != c1
3144                                    && *locinput != c2)
3145                                 locinput++;                         
3146                         }
3147                         if (locinput > e) 
3148                             sayNO;
3149                         /* PL_reginput == old now */
3150                         if (locinput != old) {
3151                             ln = 1;     /* Did some */
3152                             if (regrepeat(scan, locinput - old) <
3153                                  locinput - old)
3154                                 sayNO;
3155                         }
3156                         /* PL_reginput == locinput now */
3157                         TRYPAREN(paren, ln, locinput);
3158                         PL_reginput = locinput; /* Could be reset... */
3159                         REGCP_UNWIND(lastcp);
3160                         /* Couldn't or didn't -- move forward. */
3161                         old = locinput++;
3162                     }
3163                 }
3164                 else
3165                 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
3166                     /* If it could work, try it. */
3167                     if (c1 == -1000 ||
3168                         UCHARAT(PL_reginput) == c1 ||
3169                         UCHARAT(PL_reginput) == c2)
3170                     {
3171                         TRYPAREN(paren, n, PL_reginput);
3172                         REGCP_UNWIND(lastcp);
3173                     }
3174                     /* Couldn't or didn't -- move forward. */
3175                     PL_reginput = locinput;
3176                     if (regrepeat(scan, 1)) {
3177                         ln++;
3178                         locinput = PL_reginput;
3179                     }
3180                     else
3181                         sayNO;
3182                 }
3183             }
3184             else {
3185                 CHECKPOINT lastcp;
3186                 n = regrepeat(scan, n);
3187                 locinput = PL_reginput;
3188                 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3189                     (!PL_multiline  || OP(next) == SEOL || OP(next) == EOS)) {
3190                     ln = n;                     /* why back off? */
3191                     /* ...because $ and \Z can match before *and* after
3192                        newline at the end.  Consider "\n\n" =~ /\n+\Z\n/.
3193                        We should back off by one in this case. */
3194                     if (UCHARAT(PL_reginput - 1) == '\n' && OP(next) != EOS)
3195                         ln--;
3196                 }
3197                 REGCP_SET(lastcp);
3198                 if (paren) {
3199                     while (n >= ln) {
3200                         /* If it could work, try it. */
3201                         if (c1 == -1000 ||
3202                             UCHARAT(PL_reginput) == c1 ||
3203                             UCHARAT(PL_reginput) == c2)
3204                             {
3205                                 TRYPAREN(paren, n, PL_reginput);
3206                                 REGCP_UNWIND(lastcp);
3207                             }
3208                         /* Couldn't or didn't -- back up. */
3209                         n--;
3210                         PL_reginput = locinput = HOPc(locinput, -1);
3211                     }
3212                 }
3213                 else {
3214                     while (n >= ln) {
3215                         /* If it could work, try it. */
3216                         if (c1 == -1000 ||
3217                             UCHARAT(PL_reginput) == c1 ||
3218                             UCHARAT(PL_reginput) == c2)
3219                             {
3220                                 TRYPAREN(paren, n, PL_reginput);
3221                                 REGCP_UNWIND(lastcp);
3222                             }
3223                         /* Couldn't or didn't -- back up. */
3224                         n--;
3225                         PL_reginput = locinput = HOPc(locinput, -1);
3226                     }
3227                 }
3228             }
3229             sayNO;
3230             break;
3231         case END:
3232             if (PL_reg_call_cc) {
3233                 re_cc_state *cur_call_cc = PL_reg_call_cc;
3234                 CURCUR *cctmp = PL_regcc;
3235                 regexp *re = PL_reg_re;
3236                 CHECKPOINT cp, lastcp;
3237                 
3238                 cp = regcppush(0);      /* Save *all* the positions. */
3239                 REGCP_SET(lastcp);
3240                 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
3241                                                     the caller. */
3242                 PL_reginput = locinput; /* Make position available to
3243                                            the callcc. */
3244                 cache_re(PL_reg_call_cc->re);
3245                 PL_regcc = PL_reg_call_cc->cc;
3246                 PL_reg_call_cc = PL_reg_call_cc->prev;
3247                 if (regmatch(cur_call_cc->node)) {
3248                     PL_reg_call_cc = cur_call_cc;
3249                     regcpblow(cp);
3250                     sayYES;
3251                 }
3252                 REGCP_UNWIND(lastcp);
3253                 regcppop();
3254                 PL_reg_call_cc = cur_call_cc;
3255                 PL_regcc = cctmp;
3256                 PL_reg_re = re;
3257                 cache_re(re);
3258
3259                 DEBUG_r(
3260                     PerlIO_printf(Perl_debug_log,
3261                                   "%*s  continuation failed...\n",
3262                                   REPORT_CODE_OFF+PL_regindent*2, "")
3263                     );
3264                 sayNO_SILENT;
3265             }
3266             if (locinput < PL_regtill) {
3267                 DEBUG_r(PerlIO_printf(Perl_debug_log,
3268                                       "%sMatch possible, but length=%ld is smaller than requested=%ld, failing!%s\n",
3269                                       PL_colors[4],
3270                                       (long)(locinput - PL_reg_starttry),
3271                                       (long)(PL_regtill - PL_reg_starttry),
3272                                       PL_colors[5]));
3273                 sayNO_FINAL;            /* Cannot match: too short. */
3274             }
3275             PL_reginput = locinput;     /* put where regtry can find it */
3276             sayYES_FINAL;               /* Success! */
3277         case SUCCEED:
3278             PL_reginput = locinput;     /* put where regtry can find it */
3279             sayYES_LOUD;                /* Success! */
3280         case SUSPEND:
3281             n = 1;
3282             PL_reginput = locinput;
3283             goto do_ifmatch;        
3284         case UNLESSM:
3285             n = 0;
3286             if (scan->flags) {
3287                 if (UTF) {              /* XXXX This is absolutely
3288                                            broken, we read before
3289                                            start of string. */
3290                     s = HOPMAYBEc(locinput, -scan->flags);
3291                     if (!s)
3292                         goto say_yes;
3293                     PL_reginput = s;
3294                 }
3295                 else {
3296                     if (locinput < PL_bostr + scan->flags) 
3297                         goto say_yes;
3298                     PL_reginput = locinput - scan->flags;
3299                     goto do_ifmatch;
3300                 }
3301             }
3302             else
3303                 PL_reginput = locinput;
3304             goto do_ifmatch;
3305         case IFMATCH:
3306             n = 1;
3307             if (scan->flags) {
3308                 if (UTF) {              /* XXXX This is absolutely
3309                                            broken, we read before
3310                                            start of string. */
3311                     s = HOPMAYBEc(locinput, -scan->flags);
3312                     if (!s || s < PL_bostr)
3313                         goto say_no;
3314                     PL_reginput = s;
3315                 }
3316                 else {
3317                     if (locinput < PL_bostr + scan->flags) 
3318                         goto say_no;
3319                     PL_reginput = locinput - scan->flags;
3320                     goto do_ifmatch;
3321                 }
3322             }
3323             else
3324                 PL_reginput = locinput;
3325
3326           do_ifmatch:
3327             inner = NEXTOPER(NEXTOPER(scan));
3328             if (regmatch(inner) != n) {
3329               say_no:
3330                 if (logical) {
3331                     logical = 0;
3332                     sw = 0;
3333                     goto do_longjump;
3334                 }
3335                 else
3336                     sayNO;
3337             }
3338           say_yes:
3339             if (logical) {
3340                 logical = 0;
3341                 sw = 1;
3342             }
3343             if (OP(scan) == SUSPEND) {
3344                 locinput = PL_reginput;
3345                 nextchr = UCHARAT(locinput);
3346             }
3347             /* FALL THROUGH. */
3348         case LONGJMP:
3349           do_longjump:
3350             next = scan + ARG(scan);
3351             if (next == scan)
3352                 next = NULL;
3353             break;
3354         default:
3355             PerlIO_printf(Perl_error_log, "%"UVxf" %d\n",
3356                           PTR2UV(scan), OP(scan));
3357             Perl_croak(aTHX_ "regexp memory corruption");
3358         }
3359       reenter:
3360         scan = next;
3361     }
3362
3363     /*
3364     * We get here only if there's trouble -- normally "case END" is
3365     * the terminating point.
3366     */
3367     Perl_croak(aTHX_ "corrupted regexp pointers");
3368     /*NOTREACHED*/
3369     sayNO;
3370
3371 yes_loud:
3372     DEBUG_r(
3373         PerlIO_printf(Perl_debug_log,
3374                       "%*s  %scould match...%s\n",
3375                       REPORT_CODE_OFF+PL_regindent*2, "", PL_colors[4],PL_colors[5])
3376         );
3377     goto yes;
3378 yes_final:
3379     DEBUG_r(PerlIO_printf(Perl_debug_log, "%sMatch successful!%s\n",
3380                           PL_colors[4],PL_colors[5]));
3381 yes:
3382 #ifdef DEBUGGING
3383     PL_regindent--;
3384 #endif
3385
3386 #if 0                                   /* Breaks $^R */
3387     if (unwind)
3388         regcpblow(firstcp);
3389 #endif
3390     return 1;
3391
3392 no:
3393     DEBUG_r(
3394         PerlIO_printf(Perl_debug_log,
3395                       "%*s  %sfailed...%s\n",
3396                       REPORT_CODE_OFF+PL_regindent*2, "",PL_colors[4],PL_colors[5])
3397         );
3398     goto do_no;
3399 no_final:
3400 do_no:
3401     if (unwind) {
3402         re_unwind_t *uw = SSPTRt(unwind,re_unwind_t);
3403
3404         switch (uw->type) {
3405         case RE_UNWIND_BRANCH:
3406         case RE_UNWIND_BRANCHJ:
3407         {
3408             re_unwind_branch_t *uwb = &(uw->branch);
3409             I32 lastparen = uwb->lastparen;
3410             
3411             REGCP_UNWIND(uwb->lastcp);
3412             for (n = *PL_reglastparen; n > lastparen; n--)
3413                 PL_regendp[n] = -1;
3414             *PL_reglastparen = n;
3415             scan = next = uwb->next;
3416             if ( !scan || 
3417                  OP(scan) != (uwb->type == RE_UNWIND_BRANCH 
3418                               ? BRANCH : BRANCHJ) ) {           /* Failure */
3419                 unwind = uwb->prev;
3420 #ifdef DEBUGGING
3421                 PL_regindent--;
3422 #endif
3423                 goto do_no;
3424             }
3425             /* Have more choice yet.  Reuse the same uwb.  */
3426             /*SUPPRESS 560*/
3427             if ((n = (uwb->type == RE_UNWIND_BRANCH
3428                       ? NEXT_OFF(next) : ARG(next))))
3429                 next += n;
3430             else
3431                 next = NULL;    /* XXXX Needn't unwinding in this case... */
3432             uwb->next = next;
3433             next = NEXTOPER(scan);
3434             if (uwb->type == RE_UNWIND_BRANCHJ)
3435                 next = NEXTOPER(next);
3436             locinput = uwb->locinput;
3437             nextchr = uwb->nextchr;
3438 #ifdef DEBUGGING
3439             PL_regindent = uwb->regindent;
3440 #endif
3441
3442             goto reenter;
3443         }
3444         /* NOT REACHED */
3445         default:
3446             Perl_croak(aTHX_ "regexp unwind memory corruption");
3447         }
3448         /* NOT REACHED */
3449     }
3450 #ifdef DEBUGGING
3451     PL_regindent--;
3452 #endif
3453     return 0;
3454 }
3455
3456 /*
3457  - regrepeat - repeatedly match something simple, report how many
3458  */
3459 /*
3460  * [This routine now assumes that it will only match on things of length 1.
3461  * That was true before, but now we assume scan - reginput is the count,
3462  * rather than incrementing count on every character.  [Er, except utf8.]]
3463  */
3464 STATIC I32
3465 S_regrepeat(pTHX_ regnode *p, I32 max)
3466 {
3467     dTHR;
3468     register char *scan;
3469     register I32 c;
3470     register char *loceol = PL_regeol;
3471     register I32 hardcount = 0;
3472
3473     scan = PL_reginput;
3474     if (max != REG_INFTY && max < loceol - scan)
3475       loceol = scan + max;
3476     switch (OP(p)) {
3477     case REG_ANY:
3478         while (scan < loceol && *scan != '\n')
3479             scan++;
3480         break;
3481     case SANY:
3482         scan = loceol;
3483         break;
3484     case ANYUTF8:
3485         loceol = PL_regeol;
3486         while (scan < loceol && *scan != '\n') {
3487             scan += UTF8SKIP(scan);
3488             hardcount++;
3489         }
3490         break;
3491     case SANYUTF8:
3492         loceol = PL_regeol;
3493         while (scan < loceol) {
3494             scan += UTF8SKIP(scan);
3495             hardcount++;
3496         }
3497         break;
3498     case EXACT:         /* length of string is 1 */
3499         c = (U8)*STRING(p);
3500         while (scan < loceol && UCHARAT(scan) == c)
3501             scan++;
3502         break;
3503     case EXACTF:        /* length of string is 1 */
3504         c = (U8)*STRING(p);
3505         while (scan < loceol &&
3506                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
3507             scan++;
3508         break;
3509     case EXACTFL:       /* length of string is 1 */
3510         PL_reg_flags |= RF_tainted;
3511         c = (U8)*STRING(p);
3512         while (scan < loceol &&
3513                (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
3514             scan++;
3515         break;
3516     case ANYOFUTF8:
3517         loceol = PL_regeol;
3518         while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
3519             scan += UTF8SKIP(scan);
3520             hardcount++;
3521         }
3522         break;
3523     case ANYOF:
3524         while (scan < loceol && REGINCLASS(p, *scan))
3525             scan++;
3526         break;
3527     case ALNUM:
3528         while (scan < loceol && isALNUM(*scan))
3529             scan++;
3530         break;
3531     case ALNUMUTF8:
3532         loceol = PL_regeol;
3533         while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3534             scan += UTF8SKIP(scan);
3535             hardcount++;
3536         }
3537         break;
3538     case ALNUML:
3539         PL_reg_flags |= RF_tainted;
3540         while (scan < loceol && isALNUM_LC(*scan))
3541             scan++;
3542         break;
3543     case ALNUMLUTF8:
3544         PL_reg_flags |= RF_tainted;
3545         loceol = PL_regeol;
3546         while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
3547             scan += UTF8SKIP(scan);
3548             hardcount++;
3549         }
3550         break;
3551         break;
3552     case NALNUM:
3553         while (scan < loceol && !isALNUM(*scan))
3554             scan++;
3555         break;
3556     case NALNUMUTF8:
3557         loceol = PL_regeol;
3558         while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
3559             scan += UTF8SKIP(scan);
3560             hardcount++;
3561         }
3562         break;
3563     case NALNUML:
3564         PL_reg_flags |= RF_tainted;
3565         while (scan < loceol && !isALNUM_LC(*scan))
3566             scan++;
3567         break;
3568     case NALNUMLUTF8:
3569         PL_reg_flags |= RF_tainted;
3570         loceol = PL_regeol;
3571         while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
3572             scan += UTF8SKIP(scan);
3573             hardcount++;
3574         }
3575         break;
3576     case SPACE:
3577         while (scan < loceol && isSPACE(*scan))
3578             scan++;
3579         break;
3580     case SPACEUTF8:
3581         loceol = PL_regeol;
3582         while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3583             scan += UTF8SKIP(scan);
3584             hardcount++;
3585         }
3586         break;
3587     case SPACEL:
3588         PL_reg_flags |= RF_tainted;
3589         while (scan < loceol && isSPACE_LC(*scan))
3590             scan++;
3591         break;
3592     case SPACELUTF8:
3593         PL_reg_flags |= RF_tainted;
3594         loceol = PL_regeol;
3595         while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3596             scan += UTF8SKIP(scan);
3597             hardcount++;
3598         }
3599         break;
3600     case NSPACE:
3601         while (scan < loceol && !isSPACE(*scan))
3602             scan++;
3603         break;
3604     case NSPACEUTF8:
3605         loceol = PL_regeol;
3606         while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
3607             scan += UTF8SKIP(scan);
3608             hardcount++;
3609         }
3610         break;
3611     case NSPACEL:
3612         PL_reg_flags |= RF_tainted;
3613         while (scan < loceol && !isSPACE_LC(*scan))
3614             scan++;
3615         break;
3616     case NSPACELUTF8:
3617         PL_reg_flags |= RF_tainted;
3618         loceol = PL_regeol;
3619         while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
3620             scan += UTF8SKIP(scan);
3621             hardcount++;
3622         }
3623         break;
3624     case DIGIT:
3625         while (scan < loceol && isDIGIT(*scan))
3626             scan++;
3627         break;
3628     case DIGITUTF8:
3629         loceol = PL_regeol;
3630         while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
3631             scan += UTF8SKIP(scan);
3632             hardcount++;
3633         }
3634         break;
3635         break;
3636     case NDIGIT:
3637         while (scan < loceol && !isDIGIT(*scan))
3638             scan++;
3639         break;
3640     case NDIGITUTF8:
3641         loceol = PL_regeol;
3642         while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
3643             scan += UTF8SKIP(scan);
3644             hardcount++;
3645         }
3646         break;
3647     default:            /* Called on something of 0 width. */
3648         break;          /* So match right here or not at all. */
3649     }
3650
3651     if (hardcount)
3652         c = hardcount;
3653     else
3654         c = scan - PL_reginput;
3655     PL_reginput = scan;
3656
3657     DEBUG_r( 
3658         {
3659                 SV *prop = sv_newmortal();
3660
3661                 regprop(prop, p);
3662                 PerlIO_printf(Perl_debug_log, 
3663                               "%*s  %s can match %"IVdf" times out of %"IVdf"...\n", 
3664                               REPORT_CODE_OFF+1, "", SvPVX(prop),(IV)c,(IV)max);
3665         });
3666     
3667     return(c);
3668 }
3669
3670 /*
3671  - regrepeat_hard - repeatedly match something, report total lenth and length
3672  * 
3673  * The repeater is supposed to have constant length.
3674  */
3675
3676 STATIC I32
3677 S_regrepeat_hard(pTHX_ regnode *p, I32 max, I32 *lp)
3678 {
3679     dTHR;
3680     register char *scan;
3681     register char *start;
3682     register char *loceol = PL_regeol;
3683     I32 l = 0;
3684     I32 count = 0, res = 1;
3685
3686     if (!max)
3687         return 0;
3688
3689     start = PL_reginput;
3690     if (UTF) {
3691         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3692             if (!count++) {
3693                 l = 0;
3694                 while (start < PL_reginput) {
3695                     l++;
3696                     start += UTF8SKIP(start);
3697                 }
3698                 *lp = l;
3699                 if (l == 0)
3700                     return max;
3701             }
3702             if (count == max)
3703                 return count;
3704         }
3705     }
3706     else {
3707         while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
3708             if (!count++) {
3709                 *lp = l = PL_reginput - start;
3710                 if (max != REG_INFTY && l*max < loceol - scan)
3711                     loceol = scan + l*max;
3712                 if (l == 0)
3713                     return max;
3714             }
3715         }
3716     }
3717     if (!res)
3718         PL_reginput = scan;
3719     
3720     return count;
3721 }
3722
3723 /*
3724  - reginclass - determine if a character falls into a character class
3725  */
3726
3727 STATIC bool
3728 S_reginclass(pTHX_ register regnode *p, register I32 c)
3729 {
3730     dTHR;
3731     char flags = ANYOF_FLAGS(p);
3732     bool match = FALSE;
3733
3734     c &= 0xFF;
3735     if (ANYOF_BITMAP_TEST(p, c))
3736         match = TRUE;
3737     else if (flags & ANYOF_FOLD) {
3738         I32 cf;
3739         if (flags & ANYOF_LOCALE) {
3740             PL_reg_flags |= RF_tainted;
3741             cf = PL_fold_locale[c];
3742         }
3743         else
3744             cf = PL_fold[c];
3745         if (ANYOF_BITMAP_TEST(p, cf))
3746             match = TRUE;
3747     }
3748
3749     if (!match && (flags & ANYOF_CLASS)) {
3750         PL_reg_flags |= RF_tainted;
3751         if (
3752             (ANYOF_CLASS_TEST(p, ANYOF_ALNUM)   &&  isALNUM_LC(c))  ||
3753             (ANYOF_CLASS_TEST(p, ANYOF_NALNUM)  && !isALNUM_LC(c))  ||
3754             (ANYOF_CLASS_TEST(p, ANYOF_SPACE)   &&  isSPACE_LC(c))  ||
3755             (ANYOF_CLASS_TEST(p, ANYOF_NSPACE)  && !isSPACE_LC(c))  ||
3756             (ANYOF_CLASS_TEST(p, ANYOF_DIGIT)   &&  isDIGIT_LC(c))  ||
3757             (ANYOF_CLASS_TEST(p, ANYOF_NDIGIT)  && !isDIGIT_LC(c))  ||
3758             (ANYOF_CLASS_TEST(p, ANYOF_ALNUMC)  &&  isALNUMC_LC(c)) ||
3759             (ANYOF_CLASS_TEST(p, ANYOF_NALNUMC) && !isALNUMC_LC(c)) ||
3760             (ANYOF_CLASS_TEST(p, ANYOF_ALPHA)   &&  isALPHA_LC(c))  ||
3761             (ANYOF_CLASS_TEST(p, ANYOF_NALPHA)  && !isALPHA_LC(c))  ||
3762             (ANYOF_CLASS_TEST(p, ANYOF_ASCII)   &&  isASCII(c))     ||
3763             (ANYOF_CLASS_TEST(p, ANYOF_NASCII)  && !isASCII(c))     ||
3764             (ANYOF_CLASS_TEST(p, ANYOF_CNTRL)   &&  isCNTRL_LC(c))  ||
3765             (ANYOF_CLASS_TEST(p, ANYOF_NCNTRL)  && !isCNTRL_LC(c))  ||
3766             (ANYOF_CLASS_TEST(p, ANYOF_GRAPH)   &&  isGRAPH_LC(c))  ||
3767             (ANYOF_CLASS_TEST(p, ANYOF_NGRAPH)  && !isGRAPH_LC(c))  ||
3768             (ANYOF_CLASS_TEST(p, ANYOF_LOWER)   &&  isLOWER_LC(c))  ||
3769             (ANYOF_CLASS_TEST(p, ANYOF_NLOWER)  && !isLOWER_LC(c))  ||
3770             (ANYOF_CLASS_TEST(p, ANYOF_PRINT)   &&  isPRINT_LC(c))  ||
3771             (ANYOF_CLASS_TEST(p, ANYOF_NPRINT)  && !isPRINT_LC(c))  ||
3772             (ANYOF_CLASS_TEST(p, ANYOF_PUNCT)   &&  isPUNCT_LC(c))  ||
3773             (ANYOF_CLASS_TEST(p, ANYOF_NPUNCT)  && !isPUNCT_LC(c))  ||
3774             (ANYOF_CLASS_TEST(p, ANYOF_UPPER)   &&  isUPPER_LC(c))  ||
3775             (ANYOF_CLASS_TEST(p, ANYOF_NUPPER)  && !isUPPER_LC(c))  ||
3776             (ANYOF_CLASS_TEST(p, ANYOF_XDIGIT)  &&  isXDIGIT(c))    ||
3777             (ANYOF_CLASS_TEST(p, ANYOF_NXDIGIT) && !isXDIGIT(c))    ||
3778             (ANYOF_CLASS_TEST(p, ANYOF_PSXSPC)  &&  isPSXSPC(c))    ||
3779             (ANYOF_CLASS_TEST(p, ANYOF_NPSXSPC) && !isPSXSPC(c))    ||
3780             (ANYOF_CLASS_TEST(p, ANYOF_BLANK)   &&  isBLANK(c))     ||
3781             (ANYOF_CLASS_TEST(p, ANYOF_NBLANK)  && !isBLANK(c))
3782             ) /* How's that for a conditional? */
3783         {
3784             match = TRUE;
3785         }
3786     }
3787
3788     return (flags & ANYOF_INVERT) ? !match : match;
3789 }
3790
3791 STATIC bool
3792 S_reginclassutf8(pTHX_ regnode *f, U8 *p)
3793 {                                           
3794     dTHR;
3795     char flags = ARG1(f);
3796     bool match = FALSE;
3797 #ifdef DEBUGGING
3798     SV *rv = (SV*)PL_regdata->data[ARG2(f)];
3799     AV *av = (AV*)SvRV((SV*)rv);
3800     SV *sw = *av_fetch(av, 0, FALSE);
3801     SV *lv = *av_fetch(av, 1, FALSE);
3802 #else
3803     SV *sw = (SV*)PL_regdata->data[ARG2(f)];
3804 #endif
3805
3806     if (swash_fetch(sw, p))
3807         match = TRUE;
3808     else if (flags & ANYOF_FOLD) {
3809         U8 tmpbuf[UTF8_MAXLEN+1];
3810         if (flags & ANYOF_LOCALE) {
3811             PL_reg_flags |= RF_tainted;
3812             uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
3813         }
3814         else
3815             uv_to_utf8(tmpbuf, toLOWER_utf8(p));
3816         if (swash_fetch(sw, tmpbuf))
3817             match = TRUE;
3818     }
3819
3820     /* UTF8 combined with ANYOF_CLASS is ill-defined. */
3821
3822     return (flags & ANYOF_INVERT) ? !match : match;
3823 }
3824
3825 STATIC U8 *
3826 S_reghop(pTHX_ U8 *s, I32 off)
3827 {                               
3828     dTHR;
3829     if (off >= 0) {
3830         while (off-- && s < (U8*)PL_regeol)
3831             s += UTF8SKIP(s);
3832     }
3833     else {
3834         while (off++) {
3835             if (s > (U8*)PL_bostr) {
3836                 s--;
3837                 if (*s & 0x80) {
3838                     while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3839                         s--;
3840                 }               /* XXX could check well-formedness here */
3841             }
3842         }
3843     }
3844     return s;
3845 }
3846
3847 STATIC U8 *
3848 S_reghopmaybe(pTHX_ U8* s, I32 off)
3849 {
3850     dTHR;
3851     if (off >= 0) {
3852         while (off-- && s < (U8*)PL_regeol)
3853             s += UTF8SKIP(s);
3854         if (off >= 0)
3855             return 0;
3856     }
3857     else {
3858         while (off++) {
3859             if (s > (U8*)PL_bostr) {
3860                 s--;
3861                 if (*s & 0x80) {
3862                     while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
3863                         s--;
3864                 }               /* XXX could check well-formedness here */
3865             }
3866             else
3867                 break;
3868         }
3869         if (off <= 0)
3870             return 0;
3871     }
3872     return s;
3873 }
3874
3875 #ifdef PERL_OBJECT
3876 #include "XSUB.h"
3877 #endif
3878
3879 static void
3880 restore_pos(pTHXo_ void *arg)
3881 {
3882     dTHR;
3883     if (PL_reg_eval_set) {
3884         if (PL_reg_oldsaved) {
3885             PL_reg_re->subbeg = PL_reg_oldsaved;
3886             PL_reg_re->sublen = PL_reg_oldsavedlen;
3887             RX_MATCH_COPIED_on(PL_reg_re);
3888         }
3889         PL_reg_magic->mg_len = PL_reg_oldpos;
3890         PL_reg_eval_set = 0;
3891         PL_curpm = PL_reg_oldcurpm;
3892     }   
3893 }