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