allow final period in a file (not followed by a newline) to
[p5sagit/p5-mst-13.2.git] / regexec.c
CommitLineData
a0d0e21e 1/* regexec.c
2 */
3
4/*
5 * "One Ring to rule them all, One Ring to find them..."
6 */
7
a687059c 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 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 22#ifdef PERL_EXT_RE_BUILD
23/* need to replace pregcomp et al, so enable that */
24# ifndef PERL_IN_XSUB_RE
25# define PERL_IN_XSUB_RE
26# endif
27/* need access to debugger hooks */
28# ifndef DEBUGGING
29# define DEBUGGING
30# endif
31#endif
32
33#ifdef PERL_IN_XSUB_RE
d06ea78c 34/* We *really* need to overwrite these symbols: */
56953603 35# define Perl_regexec_flags my_regexec
36# define Perl_regdump my_regdump
37# define Perl_regprop my_regprop
d06ea78c 38/* *These* symbols are masked to allow static link. */
39# define Perl_pregexec my_pregexec
56953603 40#endif
41
f0fcb552 42/*SUPPRESS 112*/
a687059c 43/*
e50aee73 44 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c 45 *
46 * Copyright (c) 1986 by University of Toronto.
47 * Written by Henry Spencer. Not derived from licensed software.
48 *
49 * Permission is granted to anyone to use this software for any
50 * purpose on any computer system, and to redistribute it freely,
51 * subject to the following restrictions:
52 *
53 * 1. The author is not responsible for the consequences of use of
54 * this software, no matter how awful, even if they arise
55 * from defects in it.
56 *
57 * 2. The origin of this software must not be misrepresented, either
58 * by explicit claim or by omission.
59 *
60 * 3. Altered versions must be plainly marked as such, and must not
61 * be misrepresented as being the original software.
62 *
63 **** Alterations to Henry's code are...
64 ****
a0ed51b3 65 **** Copyright (c) 1991-1998, Larry Wall
a687059c 66 ****
9ef589d8 67 **** You may distribute under the terms of either the GNU General Public
68 **** License or the Artistic License, as specified in the README file.
a687059c 69 *
70 * Beware that some of this code is subtly aware of the way operator
71 * precedence is structured in regular expressions. Serious changes in
72 * regular-expression syntax might require a total rethink.
73 */
74#include "EXTERN.h"
75#include "perl.h"
0f5d15d6 76
a687059c 77#include "regcomp.h"
78
c277df42 79#define RF_tainted 1 /* tainted information used? */
80#define RF_warned 2 /* warned about big count? */
ce862d02 81#define RF_evaled 4 /* Did an EVAL with setting? */
a0ed51b3 82#define RF_utf8 8 /* String contains multibyte chars? */
83
84#define UTF (PL_reg_flags & RF_utf8)
ce862d02 85
86#define RS_init 1 /* eval environment created */
87#define RS_set 2 /* replsv value is set */
c277df42 88
a687059c 89#ifndef STATIC
90#define STATIC static
91#endif
92
76e3520e 93#ifndef PERL_OBJECT
a0d0e21e 94typedef I32 CHECKPOINT;
95
c277df42 96/*
97 * Forwards.
98 */
99
100static I32 regmatch _((regnode *prog));
101static I32 regrepeat _((regnode *p, I32 max));
102static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
103static I32 regtry _((regexp *prog, char *startpos));
ae5c130c 104
c277df42 105static bool reginclass _((char *p, I32 c));
a0ed51b3 106static bool reginclassutf8 _((regnode *f, U8* p));
55497cff 107static CHECKPOINT regcppush _((I32 parenfloor));
108static char * regcppop _((void));
942e002e 109static char * regcp_set_to _((I32 ss));
110static void cache_re _((regexp *prog));
9661b544 111static void restore_pos _((void *arg));
76e3520e 112#endif
a0ed51b3 113
ae5c130c 114#define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
a0ed51b3 115#define REGINCLASSUTF8(f,p) (ARG1(f) ? reginclassutf8(f,p) : swash_fetch((SV*)PL_regdata->data[ARG2(f)],p))
116
117#define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
118#define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
119
dfe13c55 120#ifndef PERL_OBJECT
121static U8 * reghop _((U8 *pos, I32 off));
122static U8 * reghopmaybe _((U8 *pos, I32 off));
123#endif
124#define reghop_c(pos,off) ((char*)reghop((U8*)pos, off))
125#define reghopmaybe_c(pos,off) ((char*)reghopmaybe((U8*)pos, off))
126#define HOP(pos,off) (UTF ? reghop((U8*)pos, off) : (U8*)(pos + off))
127#define HOPMAYBE(pos,off) (UTF ? reghopmaybe((U8*)pos, off) : (U8*)(pos + off))
128#define HOPc(pos,off) ((char*)HOP(pos,off))
129#define HOPMAYBEc(pos,off) ((char*)HOPMAYBE(pos,off))
a0d0e21e 130
76e3520e 131STATIC CHECKPOINT
8ac85365 132regcppush(I32 parenfloor)
a0d0e21e 133{
11343788 134 dTHR;
3280af22 135 int retval = PL_savestack_ix;
136 int i = (PL_regsize - parenfloor) * 4;
a0d0e21e 137 int p;
138
139 SSCHECK(i + 5);
3280af22 140 for (p = PL_regsize; p > parenfloor; p--) {
141 SSPUSHPTR(PL_regendp[p]);
142 SSPUSHPTR(PL_regstartp[p]);
143 SSPUSHPTR(PL_reg_start_tmp[p]);
a0d0e21e 144 SSPUSHINT(p);
145 }
3280af22 146 SSPUSHINT(PL_regsize);
147 SSPUSHINT(*PL_reglastparen);
148 SSPUSHPTR(PL_reginput);
a0d0e21e 149 SSPUSHINT(i + 3);
150 SSPUSHINT(SAVEt_REGCONTEXT);
151 return retval;
152}
153
c277df42 154/* These are needed since we do not localize EVAL nodes: */
c3464db5 155# define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, \
156 " Setting an EVAL scope, savestack=%i\n", \
3280af22 157 PL_savestack_ix)); lastcp = PL_savestack_ix
c3464db5 158
3280af22 159# define REGCP_UNWIND DEBUG_r(lastcp != PL_savestack_ix ? \
c3464db5 160 PerlIO_printf(Perl_debug_log, \
161 " Clearing an EVAL scope, savestack=%i..%i\n", \
3280af22 162 lastcp, PL_savestack_ix) : 0); regcpblow(lastcp)
c277df42 163
76e3520e 164STATIC char *
8ac85365 165regcppop(void)
a0d0e21e 166{
11343788 167 dTHR;
a0d0e21e 168 I32 i = SSPOPINT;
169 U32 paren = 0;
170 char *input;
171 char *tmps;
172 assert(i == SAVEt_REGCONTEXT);
173 i = SSPOPINT;
174 input = (char *) SSPOPPTR;
3280af22 175 *PL_reglastparen = SSPOPINT;
176 PL_regsize = SSPOPINT;
c277df42 177 for (i -= 3; i > 0; i -= 4) {
a0d0e21e 178 paren = (U32)SSPOPINT;
3280af22 179 PL_reg_start_tmp[paren] = (char *) SSPOPPTR;
180 PL_regstartp[paren] = (char *) SSPOPPTR;
a0d0e21e 181 tmps = (char*)SSPOPPTR;
3280af22 182 if (paren <= *PL_reglastparen)
183 PL_regendp[paren] = tmps;
c277df42 184 DEBUG_r(
c3464db5 185 PerlIO_printf(Perl_debug_log,
186 " restoring \\%d to %d(%d)..%d%s\n",
3280af22 187 paren, PL_regstartp[paren] - PL_regbol,
188 PL_reg_start_tmp[paren] - PL_regbol,
189 PL_regendp[paren] - PL_regbol,
190 (paren > *PL_reglastparen ? "(no)" : ""));
c277df42 191 );
a0d0e21e 192 }
c277df42 193 DEBUG_r(
3280af22 194 if (*PL_reglastparen + 1 <= PL_regnpar) {
c3464db5 195 PerlIO_printf(Perl_debug_log,
196 " restoring \\%d..\\%d to undef\n",
3280af22 197 *PL_reglastparen + 1, PL_regnpar);
c277df42 198 }
199 );
3280af22 200 for (paren = *PL_reglastparen + 1; paren <= PL_regnpar; paren++) {
201 if (paren > PL_regsize)
202 PL_regstartp[paren] = Nullch;
203 PL_regendp[paren] = Nullch;
a0d0e21e 204 }
205 return input;
206}
207
0f5d15d6 208STATIC char *
209regcp_set_to(I32 ss)
210{
46124e9e 211 dTHR;
0f5d15d6 212 I32 tmp = PL_savestack_ix;
213
214 PL_savestack_ix = ss;
215 regcppop();
216 PL_savestack_ix = tmp;
942e002e 217 return Nullch;
0f5d15d6 218}
219
220typedef struct re_cc_state
221{
222 I32 ss;
223 regnode *node;
224 struct re_cc_state *prev;
225 CURCUR *cc;
226 regexp *re;
227} re_cc_state;
228
c277df42 229#define regcpblow(cp) LEAVE_SCOPE(cp)
a0d0e21e 230
a687059c 231/*
e50aee73 232 * pregexec and friends
a687059c 233 */
234
235/*
c277df42 236 - pregexec - match a regexp against a string
a687059c 237 */
c277df42 238I32
c3464db5 239pregexec(register regexp *prog, char *stringarg, register char *strend,
240 char *strbeg, I32 minend, SV *screamer, U32 nosave)
c277df42 241/* strend: pointer to null at end of string */
242/* strbeg: real beginning of string */
243/* minend: end of match must be >=minend after stringarg. */
244/* nosave: For optimizations. */
245{
246 return
247 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
248 nosave ? 0 : REXEC_COPY_STR);
249}
0f5d15d6 250
251STATIC void
252cache_re(regexp *prog)
253{
46124e9e 254 dTHR;
0f5d15d6 255 PL_regprecomp = prog->precomp; /* Needed for FAIL. */
256#ifdef DEBUGGING
257 PL_regprogram = prog->program;
258#endif
259 PL_regnpar = prog->nparens;
260 PL_regdata = prog->data;
261 PL_reg_re = prog;
262}
22e551b9 263
9661b544 264STATIC void
265restore_pos(void *arg)
266{
267 if (PL_reg_eval_set) {
268 PL_reg_magic->mg_len = PL_reg_oldpos;
269 PL_reg_eval_set = 0;
270 }
271}
272
273
a687059c 274/*
c277df42 275 - regexec_flags - match a regexp against a string
a687059c 276 */
79072805 277I32
c3464db5 278regexec_flags(register regexp *prog, char *stringarg, register char *strend,
22e551b9 279 char *strbeg, I32 minend, SV *sv, void *data, U32 flags)
c277df42 280/* strend: pointer to null at end of string */
281/* strbeg: real beginning of string */
282/* minend: end of match must be >=minend after stringarg. */
283/* data: May be used for some additional optimizations. */
284/* nosave: For optimizations. */
a687059c 285{
5c0ca799 286 dTHR;
a0d0e21e 287 register char *s;
c277df42 288 register regnode *c;
a0d0e21e 289 register char *startpos = stringarg;
290 register I32 tmp;
c277df42 291 I32 minlen; /* must match at least this many chars */
a0d0e21e 292 I32 dontbother = 0; /* how many characters not to try at end */
293 CURCUR cc;
c277df42 294 I32 start_shift = 0; /* Offset of the start to find
a0ed51b3 295 constant substr. */ /* CC */
296 I32 end_shift = 0; /* Same for the end. */ /* CC */
c277df42 297 I32 scream_pos = -1; /* Internal iterator of scream. */
298 char *scream_olds;
3280af22 299 SV* oreplsv = GvSV(PL_replgv);
a687059c 300
a0d0e21e 301 cc.cur = 0;
4633a7c4 302 cc.oldcc = 0;
3280af22 303 PL_regcc = &cc;
a0d0e21e 304
0f5d15d6 305 cache_re(prog);
a0d0e21e 306#ifdef DEBUGGING
3280af22 307 PL_regnarrate = PL_debug & 512;
a0d0e21e 308#endif
309
310 /* Be paranoid... */
311 if (prog == NULL || startpos == NULL) {
312 croak("NULL regexp parameter");
313 return 0;
314 }
315
c277df42 316 minlen = prog->minlen;
317 if (strend - startpos < minlen) goto phooey;
318
a0d0e21e 319 if (startpos == strbeg) /* is ^ valid at stringarg? */
3280af22 320 PL_regprev = '\n';
a0d0e21e 321 else {
a0ed51b3 322 PL_regprev = (U32)stringarg[-1];
3280af22 323 if (!PL_multiline && PL_regprev == '\n')
324 PL_regprev = '\0'; /* force ^ to NOT match */
a0d0e21e 325 }
bbce6d69 326
a0d0e21e 327 /* Check validity of program. */
22c35a8c 328 if (UCHARAT(prog->program) != REG_MAGIC) {
a0d0e21e 329 FAIL("corrupted regexp program");
330 }
331
3280af22 332 PL_reg_flags = 0;
333 PL_reg_eval_set = 0;
a0d0e21e 334
a0ed51b3 335 if (prog->reganch & ROPT_UTF8)
336 PL_reg_flags |= RF_utf8;
337
338 /* Mark beginning of line for ^ and lookbehind. */
339 PL_regbol = startpos;
340 PL_bostr = strbeg;
9661b544 341 PL_reg_sv = sv;
a0ed51b3 342
343 /* Mark end of line for $ (and such) */
344 PL_regeol = strend;
345
346 /* see how far we have to get to not match where we matched before */
347 PL_regtill = startpos+minend;
348
0f5d15d6 349 /* We start without call_cc context. */
350 PL_reg_call_cc = 0;
351
a0d0e21e 352 /* If there is a "must appear" string, look for it. */
353 s = startpos;
c277df42 354 if (!(flags & REXEC_CHECKED)
355 && prog->check_substr != Nullsv &&
774d564b 356 !(prog->reganch & ROPT_ANCH_GPOS) &&
c277df42 357 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
3280af22 358 || (PL_multiline && prog->check_substr == prog->anchored_substr)) )
a0d0e21e 359 {
a0ed51b3 360 char *t;
361 start_shift = prog->check_offset_min; /* okay to underestimate on CC */
c277df42 362 /* Should be nonnegative! */
a0ed51b3 363 end_shift = minlen - start_shift - CHR_SVLEN(prog->check_substr);
22e551b9 364 if (flags & REXEC_SCREAM) {
3280af22 365 if (PL_screamfirst[BmRARE(prog->check_substr)] >= 0)
22e551b9 366 s = screaminstr(sv, prog->check_substr,
c277df42 367 start_shift + (stringarg - strbeg),
368 end_shift, &scream_pos, 0);
a0d0e21e 369 else
370 s = Nullch;
c277df42 371 scream_olds = s;
0a12ae7d 372 }
a0d0e21e 373 else
c277df42 374 s = fbm_instr((unsigned char*)s + start_shift,
375 (unsigned char*)strend - end_shift,
411d5715 376 prog->check_substr, 0);
a0d0e21e 377 if (!s) {
c277df42 378 ++BmUSEFUL(prog->check_substr); /* hooray */
a0d0e21e 379 goto phooey; /* not present */
a0ed51b3 380 }
381 else if (s - stringarg > prog->check_offset_max &&
382 (UTF
dfe13c55 383 ? ((t = reghopmaybe_c(s, -(prog->check_offset_max))) && t >= stringarg)
a0ed51b3 384 : (t = s - prog->check_offset_max) != 0
385 )
386 )
387 {
c277df42 388 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
a0ed51b3 389 s = t;
390 }
391 else if (!(prog->reganch & ROPT_NAUGHTY)
c277df42 392 && --BmUSEFUL(prog->check_substr) < 0
393 && prog->check_substr == prog->float_substr) { /* boo */
394 SvREFCNT_dec(prog->check_substr);
395 prog->check_substr = Nullsv; /* disable */
396 prog->float_substr = Nullsv; /* clear */
a0d0e21e 397 s = startpos;
a0ed51b3 398 }
399 else
400 s = startpos;
a0d0e21e 401 }
a687059c 402
c277df42 403 DEBUG_r(
404 PerlIO_printf(Perl_debug_log,
8d300b32 405 "%sMatching%s `%s%.60s%s%s' against `%s%.*s%s%s'\n",
406 PL_colors[4],PL_colors[5],PL_colors[0],
407 prog->precomp,
408 PL_colors[1],
c277df42 409 (strlen(prog->precomp) > 60 ? "..." : ""),
8d300b32 410 PL_colors[0],
c277df42 411 (strend - startpos > 60 ? 60 : strend - startpos),
8d300b32 412 startpos, PL_colors[1],
c277df42 413 (strend - startpos > 60 ? "..." : ""))
414 );
415
22e551b9 416 if (prog->reganch & ROPT_GPOS_SEEN) {
417 MAGIC *mg;
418 int pos = 0;
419
420 if (SvTYPE(sv) >= SVt_PVMG && SvMAGIC(sv)
421 && (mg = mg_find(sv, 'g')) && mg->mg_len >= 0)
422 pos = mg->mg_len;
423 PL_reg_ganch = startpos + pos;
424 }
425
a0d0e21e 426 /* Simplest case: anchored match need be tried only once. */
774d564b 427 /* [unless only anchor is BOL and multiline is set] */
22e551b9 428 if (prog->reganch & (ROPT_ANCH & ~ROPT_ANCH_GPOS)) {
a0d0e21e 429 if (regtry(prog, startpos))
430 goto got_it;
22e551b9 431 else if (PL_multiline || (prog->reganch & ROPT_IMPLICIT)
432 || (prog->reganch & ROPT_ANCH_MBOL)) /* XXXX SBOL? */
774d564b 433 {
a0d0e21e 434 if (minlen)
435 dontbother = minlen - 1;
dfe13c55 436 strend = HOPc(strend, -dontbother);
a0d0e21e 437 /* for multiline we only have to try after newlines */
438 if (s > startpos)
439 s--;
440 while (s < strend) {
6f06b55f 441 if (*s++ == '\n') { /* don't need PL_utf8skip here */
a0d0e21e 442 if (s < strend && regtry(prog, s))
443 goto got_it;
444 }
35c8bce7 445 }
35c8bce7 446 }
a0d0e21e 447 goto phooey;
22e551b9 448 } else if (prog->reganch & ROPT_ANCH_GPOS) {
449 if (regtry(prog, PL_reg_ganch))
450 goto got_it;
451 goto phooey;
a0d0e21e 452 }
35c8bce7 453
a0d0e21e 454 /* Messy cases: unanchored match. */
c277df42 455 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
456 /* we have /x+whatever/ */
457 /* it must be a one character string */
458 char ch = SvPVX(prog->anchored_substr)[0];
a0ed51b3 459 if (UTF) {
460 while (s < strend) {
461 if (*s == ch) {
462 if (regtry(prog, s)) goto got_it;
463 s += UTF8SKIP(s);
464 while (s < strend && *s == ch)
465 s += UTF8SKIP(s);
466 }
467 s += UTF8SKIP(s);
468 }
469 }
470 else {
471 while (s < strend) {
472 if (*s == ch) {
473 if (regtry(prog, s)) goto got_it;
c277df42 474 s++;
a0ed51b3 475 while (s < strend && *s == ch)
476 s++;
477 }
478 s++;
a0d0e21e 479 }
a687059c 480 }
c277df42 481 }
482 /*SUPPRESS 560*/
483 else if (prog->anchored_substr != Nullsv
484 || (prog->float_substr != Nullsv
485 && prog->float_max_offset < strend - s)) {
486 SV *must = prog->anchored_substr
487 ? prog->anchored_substr : prog->float_substr;
488 I32 back_max =
489 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
490 I32 back_min =
491 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
492 I32 delta = back_max - back_min;
dfe13c55 493 char *last = HOPc(strend, 0-(CHR_SVLEN(must) + back_min)); /* Cannot start after this */
a0ed51b3 494 char *last1; /* Last position checked before */
495
496 if (s > PL_bostr)
dfe13c55 497 last1 = HOPc(s, -1);
a0ed51b3 498 else
499 last1 = s - 1; /* bogus */
c277df42 500
501 /* XXXX check_substr already used to find `s', can optimize if
502 check_substr==must. */
503 scream_pos = -1;
504 dontbother = end_shift;
dfe13c55 505 strend = HOPc(strend, -dontbother);
c277df42 506 while ( (s <= last) &&
22e551b9 507 ((flags & REXEC_SCREAM)
508 ? (s = screaminstr(sv, must, HOPc(s, back_min) - strbeg,
c277df42 509 end_shift, &scream_pos, 0))
a0ed51b3 510 : (s = fbm_instr((unsigned char*)HOP(s, back_min),
411d5715 511 (unsigned char*)strend, must, 0))) ) {
dfe13c55 512 if (HOPc(s, -back_max) > last1) {
513 last1 = HOPc(s, -back_min);
514 s = HOPc(s, -back_max);
a0ed51b3 515 }
516 else {
dfe13c55 517 char *t = (last1 >= PL_bostr) ? HOPc(last1, 1) : last1 + 1;
c277df42 518
dfe13c55 519 last1 = HOPc(s, -back_min);
c277df42 520 s = t;
a0d0e21e 521 }
a0ed51b3 522 if (UTF) {
523 while (s <= last1) {
524 if (regtry(prog, s))
525 goto got_it;
526 s += UTF8SKIP(s);
527 }
528 }
529 else {
530 while (s <= last1) {
531 if (regtry(prog, s))
532 goto got_it;
533 s++;
534 }
a0d0e21e 535 }
536 }
537 goto phooey;
a0ed51b3 538 }
539 else if (c = prog->regstclass) {
a0d0e21e 540 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
a0ed51b3 541 char *cc;
a687059c 542
a0d0e21e 543 if (minlen)
544 dontbother = minlen - 1;
dfe13c55 545 strend = HOPc(strend, -dontbother); /* don't bother with what can't match */
a0d0e21e 546 tmp = 1;
547 /* We know what class it must start with. */
548 switch (OP(c)) {
a0ed51b3 549 case ANYOFUTF8:
550 cc = (char *) OPERAND(c);
551 while (s < strend) {
552 if (REGINCLASSUTF8(c, (U8*)s)) {
553 if (tmp && regtry(prog, s))
554 goto got_it;
555 else
556 tmp = doevery;
557 }
558 else
559 tmp = 1;
560 s += UTF8SKIP(s);
561 }
562 break;
a0d0e21e 563 case ANYOF:
a0ed51b3 564 cc = (char *) OPERAND(c);
a0d0e21e 565 while (s < strend) {
a0ed51b3 566 if (REGINCLASS(cc, *s)) {
a0d0e21e 567 if (tmp && regtry(prog, s))
568 goto got_it;
569 else
570 tmp = doevery;
a687059c 571 }
a0d0e21e 572 else
573 tmp = 1;
574 s++;
575 }
576 break;
bbce6d69 577 case BOUNDL:
3280af22 578 PL_reg_flags |= RF_tainted;
bbce6d69 579 /* FALL THROUGH */
a0d0e21e 580 case BOUND:
a0ed51b3 581 if (minlen) {
582 dontbother++;
583 strend -= 1;
584 }
3280af22 585 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
95bac841 586 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 587 while (s < strend) {
95bac841 588 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
a0d0e21e 589 tmp = !tmp;
590 if (regtry(prog, s))
591 goto got_it;
a687059c 592 }
a0d0e21e 593 s++;
594 }
595 if ((minlen || tmp) && regtry(prog,s))
596 goto got_it;
597 break;
a0ed51b3 598 case BOUNDLUTF8:
599 PL_reg_flags |= RF_tainted;
600 /* FALL THROUGH */
601 case BOUNDUTF8:
602 if (minlen) {
603 dontbother++;
dfe13c55 604 strend = reghop_c(strend, -1);
a0ed51b3 605 }
dfe13c55 606 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
a0ed51b3 607 tmp = ((OP(c) == BOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
608 while (s < strend) {
dfe13c55 609 if (tmp == !(OP(c) == BOUND ?
610 swash_fetch(PL_utf8_alnum, (U8*)s) :
611 isALNUM_LC_utf8((U8*)s)))
612 {
a0ed51b3 613 tmp = !tmp;
614 if (regtry(prog, s))
615 goto got_it;
616 }
617 s += UTF8SKIP(s);
618 }
619 if ((minlen || tmp) && regtry(prog,s))
620 goto got_it;
621 break;
bbce6d69 622 case NBOUNDL:
3280af22 623 PL_reg_flags |= RF_tainted;
bbce6d69 624 /* FALL THROUGH */
a0d0e21e 625 case NBOUND:
a0ed51b3 626 if (minlen) {
627 dontbother++;
628 strend -= 1;
629 }
3280af22 630 tmp = (s != startpos) ? UCHARAT(s - 1) : PL_regprev;
95bac841 631 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 632 while (s < strend) {
95bac841 633 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
a0d0e21e 634 tmp = !tmp;
635 else if (regtry(prog, s))
636 goto got_it;
637 s++;
638 }
639 if ((minlen || !tmp) && regtry(prog,s))
640 goto got_it;
641 break;
a0ed51b3 642 case NBOUNDLUTF8:
643 PL_reg_flags |= RF_tainted;
644 /* FALL THROUGH */
645 case NBOUNDUTF8:
646 if (minlen) {
647 dontbother++;
dfe13c55 648 strend = reghop_c(strend, -1);
a0ed51b3 649 }
dfe13c55 650 tmp = (I32)(s != startpos) ? utf8_to_uv(reghop((U8*)s, -1), 0) : PL_regprev;
a0ed51b3 651 tmp = ((OP(c) == NBOUND ? isALNUM_uni(tmp) : isALNUM_LC_uni(tmp)) != 0);
652 while (s < strend) {
dfe13c55 653 if (tmp == !(OP(c) == NBOUND ?
654 swash_fetch(PL_utf8_alnum, (U8*)s) :
655 isALNUM_LC_utf8((U8*)s)))
a0ed51b3 656 tmp = !tmp;
657 else if (regtry(prog, s))
658 goto got_it;
659 s += UTF8SKIP(s);
660 }
661 if ((minlen || !tmp) && regtry(prog,s))
662 goto got_it;
663 break;
a0d0e21e 664 case ALNUM:
665 while (s < strend) {
bbce6d69 666 if (isALNUM(*s)) {
667 if (tmp && regtry(prog, s))
668 goto got_it;
669 else
670 tmp = doevery;
671 }
672 else
673 tmp = 1;
674 s++;
675 }
676 break;
a0ed51b3 677 case ALNUMUTF8:
678 while (s < strend) {
dfe13c55 679 if (swash_fetch(PL_utf8_alnum, (U8*)s)) {
a0ed51b3 680 if (tmp && regtry(prog, s))
681 goto got_it;
682 else
683 tmp = doevery;
684 }
685 else
686 tmp = 1;
687 s += UTF8SKIP(s);
688 }
689 break;
bbce6d69 690 case ALNUML:
3280af22 691 PL_reg_flags |= RF_tainted;
bbce6d69 692 while (s < strend) {
693 if (isALNUM_LC(*s)) {
a0d0e21e 694 if (tmp && regtry(prog, s))
695 goto got_it;
a687059c 696 else
a0d0e21e 697 tmp = doevery;
698 }
699 else
700 tmp = 1;
701 s++;
702 }
703 break;
a0ed51b3 704 case ALNUMLUTF8:
705 PL_reg_flags |= RF_tainted;
706 while (s < strend) {
dfe13c55 707 if (isALNUM_LC_utf8((U8*)s)) {
a0ed51b3 708 if (tmp && regtry(prog, s))
709 goto got_it;
710 else
711 tmp = doevery;
712 }
713 else
714 tmp = 1;
715 s += UTF8SKIP(s);
716 }
717 break;
a0d0e21e 718 case NALNUM:
719 while (s < strend) {
bbce6d69 720 if (!isALNUM(*s)) {
721 if (tmp && regtry(prog, s))
722 goto got_it;
723 else
724 tmp = doevery;
725 }
726 else
727 tmp = 1;
728 s++;
729 }
730 break;
a0ed51b3 731 case NALNUMUTF8:
732 while (s < strend) {
dfe13c55 733 if (!swash_fetch(PL_utf8_alnum, (U8*)s)) {
a0ed51b3 734 if (tmp && regtry(prog, s))
735 goto got_it;
736 else
737 tmp = doevery;
738 }
739 else
740 tmp = 1;
741 s += UTF8SKIP(s);
742 }
743 break;
bbce6d69 744 case NALNUML:
3280af22 745 PL_reg_flags |= RF_tainted;
bbce6d69 746 while (s < strend) {
747 if (!isALNUM_LC(*s)) {
a0d0e21e 748 if (tmp && regtry(prog, s))
749 goto got_it;
a687059c 750 else
a0d0e21e 751 tmp = doevery;
a687059c 752 }
a0d0e21e 753 else
754 tmp = 1;
755 s++;
756 }
757 break;
a0ed51b3 758 case NALNUMLUTF8:
759 PL_reg_flags |= RF_tainted;
760 while (s < strend) {
dfe13c55 761 if (!isALNUM_LC_utf8((U8*)s)) {
a0ed51b3 762 if (tmp && regtry(prog, s))
763 goto got_it;
764 else
765 tmp = doevery;
766 }
767 else
768 tmp = 1;
769 s += UTF8SKIP(s);
770 }
771 break;
a0d0e21e 772 case SPACE:
773 while (s < strend) {
774 if (isSPACE(*s)) {
775 if (tmp && regtry(prog, s))
776 goto got_it;
777 else
778 tmp = doevery;
2304df62 779 }
a0d0e21e 780 else
781 tmp = 1;
782 s++;
783 }
784 break;
a0ed51b3 785 case SPACEUTF8:
786 while (s < strend) {
dfe13c55 787 if (*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s)) {
a0ed51b3 788 if (tmp && regtry(prog, s))
789 goto got_it;
790 else
791 tmp = doevery;
792 }
793 else
794 tmp = 1;
795 s += UTF8SKIP(s);
796 }
797 break;
bbce6d69 798 case SPACEL:
3280af22 799 PL_reg_flags |= RF_tainted;
bbce6d69 800 while (s < strend) {
801 if (isSPACE_LC(*s)) {
802 if (tmp && regtry(prog, s))
803 goto got_it;
804 else
805 tmp = doevery;
806 }
807 else
808 tmp = 1;
809 s++;
810 }
811 break;
a0ed51b3 812 case SPACELUTF8:
813 PL_reg_flags |= RF_tainted;
814 while (s < strend) {
dfe13c55 815 if (*s == ' ' || isSPACE_LC_utf8((U8*)s)) {
a0ed51b3 816 if (tmp && regtry(prog, s))
817 goto got_it;
818 else
819 tmp = doevery;
820 }
821 else
822 tmp = 1;
823 s += UTF8SKIP(s);
824 }
825 break;
a0d0e21e 826 case NSPACE:
827 while (s < strend) {
828 if (!isSPACE(*s)) {
829 if (tmp && regtry(prog, s))
830 goto got_it;
831 else
832 tmp = doevery;
a687059c 833 }
a0d0e21e 834 else
835 tmp = 1;
836 s++;
837 }
838 break;
a0ed51b3 839 case NSPACEUTF8:
840 while (s < strend) {
dfe13c55 841 if (!(*s == ' ' || swash_fetch(PL_utf8_space,(U8*)s))) {
a0ed51b3 842 if (tmp && regtry(prog, s))
843 goto got_it;
844 else
845 tmp = doevery;
846 }
847 else
848 tmp = 1;
849 s += UTF8SKIP(s);
850 }
851 break;
bbce6d69 852 case NSPACEL:
3280af22 853 PL_reg_flags |= RF_tainted;
bbce6d69 854 while (s < strend) {
855 if (!isSPACE_LC(*s)) {
856 if (tmp && regtry(prog, s))
857 goto got_it;
858 else
859 tmp = doevery;
860 }
861 else
862 tmp = 1;
863 s++;
864 }
865 break;
a0ed51b3 866 case NSPACELUTF8:
867 PL_reg_flags |= RF_tainted;
868 while (s < strend) {
dfe13c55 869 if (!(*s == ' ' || isSPACE_LC_utf8((U8*)s))) {
a0ed51b3 870 if (tmp && regtry(prog, s))
871 goto got_it;
872 else
873 tmp = doevery;
874 }
875 else
876 tmp = 1;
877 s += UTF8SKIP(s);
878 }
879 break;
a0d0e21e 880 case DIGIT:
881 while (s < strend) {
882 if (isDIGIT(*s)) {
883 if (tmp && regtry(prog, s))
884 goto got_it;
885 else
886 tmp = doevery;
2b69d0c2 887 }
a0d0e21e 888 else
889 tmp = 1;
890 s++;
891 }
892 break;
a0ed51b3 893 case DIGITUTF8:
894 while (s < strend) {
dfe13c55 895 if (swash_fetch(PL_utf8_digit,(U8*)s)) {
a0ed51b3 896 if (tmp && regtry(prog, s))
897 goto got_it;
898 else
899 tmp = doevery;
900 }
901 else
902 tmp = 1;
903 s += UTF8SKIP(s);
904 }
905 break;
a0d0e21e 906 case NDIGIT:
907 while (s < strend) {
908 if (!isDIGIT(*s)) {
909 if (tmp && regtry(prog, s))
910 goto got_it;
911 else
912 tmp = doevery;
a687059c 913 }
a0d0e21e 914 else
915 tmp = 1;
916 s++;
917 }
918 break;
a0ed51b3 919 case NDIGITUTF8:
920 while (s < strend) {
dfe13c55 921 if (!swash_fetch(PL_utf8_digit,(U8*)s)) {
a0ed51b3 922 if (tmp && regtry(prog, s))
923 goto got_it;
924 else
925 tmp = doevery;
926 }
927 else
928 tmp = 1;
929 s += UTF8SKIP(s);
930 }
931 break;
a687059c 932 }
a0d0e21e 933 }
934 else {
c277df42 935 dontbother = 0;
936 if (prog->float_substr != Nullsv) { /* Trim the end. */
937 char *last;
938 I32 oldpos = scream_pos;
939
22e551b9 940 if (flags & REXEC_SCREAM) {
941 last = screaminstr(sv, prog->float_substr, s - strbeg,
c277df42 942 end_shift, &scream_pos, 1); /* last one */
943 if (!last) {
944 last = scream_olds; /* Only one occurence. */
945 }
a0ed51b3 946 }
947 else {
c277df42 948 STRLEN len;
949 char *little = SvPV(prog->float_substr, len);
19b4f81a 950 if (len)
951 last = rninstr(s, strend, little, little + len);
952 else
953 last = strend; /* matching `$' */
c277df42 954 }
955 if (last == NULL) goto phooey; /* Should not happen! */
19b4f81a 956 dontbother = strend - last + prog->float_min_offset;
c277df42 957 }
958 if (minlen && (dontbother < minlen))
a0d0e21e 959 dontbother = minlen - 1;
a0ed51b3 960 strend -= dontbother; /* this one's always in bytes! */
a0d0e21e 961 /* We don't know much -- general case. */
a0ed51b3 962 if (UTF) {
963 for (;;) {
84df6dba 964 if (regtry(prog, s))
a0ed51b3 965 goto got_it;
a0ed51b3 966 if (s >= strend)
967 break;
968 s += UTF8SKIP(s);
969 };
970 }
971 else {
972 do {
973 if (regtry(prog, s))
974 goto got_it;
975 } while (s++ < strend);
976 }
a0d0e21e 977 }
978
979 /* Failure. */
980 goto phooey;
a687059c 981
a0d0e21e 982got_it:
983 prog->subbeg = strbeg;
19b4f81a 984 prog->subend = PL_regeol; /* strend may have been modified */
3280af22 985 RX_MATCH_TAINTED_set(prog, PL_reg_flags & RF_tainted);
5f05dabc 986
987 /* make sure $`, $&, $', and $digit will work later */
c277df42 988 if (strbeg != prog->subbase) { /* second+ //g match. */
989 if (!(flags & REXEC_COPY_STR)) {
137443ea 990 if (prog->subbase) {
991 Safefree(prog->subbase);
992 prog->subbase = Nullch;
993 }
994 }
995 else {
19b4f81a 996 I32 i = PL_regeol - startpos + (stringarg - strbeg);
137443ea 997 s = savepvn(strbeg, i);
998 Safefree(prog->subbase);
999 prog->subbase = s;
1000 prog->subbeg = prog->subbase;
1001 prog->subend = prog->subbase + i;
1002 s = prog->subbase + (stringarg - strbeg);
1003 for (i = 0; i <= prog->nparens; i++) {
1004 if (prog->endp[i]) {
1005 prog->startp[i] = s + (prog->startp[i] - startpos);
1006 prog->endp[i] = s + (prog->endp[i] - startpos);
1007 }
a0d0e21e 1008 }
1009 }
a0d0e21e 1010 }
ce862d02 1011 /* Preserve the current value of $^R */
3280af22 1012 if (oreplsv != GvSV(PL_replgv)) {
1013 sv_setsv(oreplsv, GvSV(PL_replgv));/* So that when GvSV(replgv) is
ce862d02 1014 restored, the value remains
1015 the same. */
1016 }
9661b544 1017 if (PL_reg_eval_set)
1018 restore_pos(0);
a0d0e21e 1019 return 1;
1020
1021phooey:
9661b544 1022 if (PL_reg_eval_set)
1023 restore_pos(0);
a0d0e21e 1024 return 0;
a687059c 1025}
1026
1027/*
1028 - regtry - try match at specific point
1029 */
76e3520e 1030STATIC I32 /* 0 failure, 1 success */
8ac85365 1031regtry(regexp *prog, char *startpos)
a687059c 1032{
c277df42 1033 dTHR;
a0d0e21e 1034 register I32 i;
1035 register char **sp;
1036 register char **ep;
c277df42 1037 CHECKPOINT lastcp;
a0d0e21e 1038
3280af22 1039 if ((prog->reganch & ROPT_EVAL_SEEN) && !PL_reg_eval_set) {
9661b544 1040 MAGIC *mg;
1041
3280af22 1042 PL_reg_eval_set = RS_init;
ce862d02 1043 DEBUG_r(DEBUG_s(
c3464db5 1044 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n",
3280af22 1045 PL_stack_sp - PL_stack_base);
ce862d02 1046 ));
1047 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
3280af22 1048 cxstack[cxstack_ix].blk_oldsp = PL_stack_sp - PL_stack_base;
ce862d02 1049 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
1050 SAVETMPS;
1051 /* Apparently this is not needed, judging by wantarray. */
1052 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
1053 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
9661b544 1054
1055 if (PL_reg_sv) {
1056 /* Make $_ available to executed code. */
1057 if (PL_reg_sv != GvSV(PL_defgv)) {
1058 SAVESPTR(GvSV(PL_defgv));
1059 GvSV(PL_defgv) = PL_reg_sv;
1060 }
1061
1062 if (!(SvTYPE(PL_reg_sv) >= SVt_PVMG && SvMAGIC(PL_reg_sv)
1063 && (mg = mg_find(PL_reg_sv, 'g')))) {
1064 /* prepare for quick setting of pos */
1065 sv_magic(PL_reg_sv, (SV*)0, 'g', Nullch, 0);
1066 mg = mg_find(PL_reg_sv, 'g');
1067 mg->mg_len = -1;
1068 }
1069 PL_reg_magic = mg;
1070 PL_reg_oldpos = mg->mg_len;
1071 SAVEDESTRUCTOR(restore_pos, 0);
1072 }
ce862d02 1073 }
3280af22 1074 PL_reginput = startpos;
1075 PL_regstartp = prog->startp;
1076 PL_regendp = prog->endp;
1077 PL_reglastparen = &prog->lastparen;
a0d0e21e 1078 prog->lastparen = 0;
3280af22 1079 PL_regsize = 0;
1080 if (PL_reg_start_tmpl <= prog->nparens) {
1081 PL_reg_start_tmpl = prog->nparens*3/2 + 3;
1082 if(PL_reg_start_tmp)
1083 Renew(PL_reg_start_tmp, PL_reg_start_tmpl, char*);
c277df42 1084 else
3280af22 1085 New(22,PL_reg_start_tmp, PL_reg_start_tmpl, char*);
c277df42 1086 }
a0d0e21e 1087
1088 sp = prog->startp;
1089 ep = prog->endp;
1090 if (prog->nparens) {
1091 for (i = prog->nparens; i >= 0; i--) {
1092 *sp++ = NULL;
1093 *ep++ = NULL;
a687059c 1094 }
a0d0e21e 1095 }
c277df42 1096 REGCP_SET;
7e5428c5 1097 if (regmatch(prog->program + 1)) {
a0d0e21e 1098 prog->startp[0] = startpos;
3280af22 1099 prog->endp[0] = PL_reginput;
a0d0e21e 1100 return 1;
1101 }
c277df42 1102 REGCP_UNWIND;
1103 return 0;
a687059c 1104}
1105
1106/*
1107 - regmatch - main matching routine
1108 *
1109 * Conceptually the strategy is simple: check to see whether the current
1110 * node matches, call self recursively to see whether the rest matches,
1111 * and then act accordingly. In practice we make some effort to avoid
1112 * recursion, in particular by going through "ordinary" nodes (that don't
1113 * need to know whether the rest of the match failed) by a loop instead of
1114 * by recursion.
1115 */
1116/* [lwall] I've hoisted the register declarations to the outer block in order to
1117 * maybe save a little bit of pushing and popping on the stack. It also takes
1118 * advantage of machines that use a register save mask on subroutine entry.
1119 */
76e3520e 1120STATIC I32 /* 0 failure, 1 success */
c277df42 1121regmatch(regnode *prog)
a687059c 1122{
c277df42 1123 dTHR;
1124 register regnode *scan; /* Current node. */
1125 regnode *next; /* Next node. */
1126 regnode *inner; /* Next node in internal branch. */
c3464db5 1127 register I32 nextchr; /* renamed nextchr - nextchar colides with
1128 function of same name */
a0d0e21e 1129 register I32 n; /* no or next */
1130 register I32 ln; /* len or last */
1131 register char *s; /* operand or save */
3280af22 1132 register char *locinput = PL_reginput;
c277df42 1133 register I32 c1, c2, paren; /* case fold search, parenth */
1134 int minmod = 0, sw = 0, logical = 0;
4633a7c4 1135#ifdef DEBUGGING
3280af22 1136 PL_regindent++;
4633a7c4 1137#endif
a0d0e21e 1138
a0ed51b3 1139 /* Note that nextchr is a byte even in UTF */
76e3520e 1140 nextchr = UCHARAT(locinput);
a0d0e21e 1141 scan = prog;
1142 while (scan != NULL) {
c277df42 1143#define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
a687059c 1144#ifdef DEBUGGING
c277df42 1145# define sayYES goto yes
1146# define sayNO goto no
1147# define saySAME(x) if (x) goto yes; else goto no
1148# define REPORT_CODE_OFF 24
4633a7c4 1149#else
c277df42 1150# define sayYES return 1
1151# define sayNO return 0
1152# define saySAME(x) return x
a687059c 1153#endif
c277df42 1154 DEBUG_r( {
1155 SV *prop = sv_newmortal();
3280af22 1156 int docolor = *PL_colors[0];
c277df42 1157 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
3280af22 1158 int l = (PL_regeol - locinput > taill ? taill : PL_regeol - locinput);
1159 int pref_len = (locinput - PL_bostr > (5 + taill) - l
1160 ? (5 + taill) - l : locinput - PL_bostr);
8d300b32 1161 int pref0_len = pref_len - (locinput - PL_reginput);
c277df42 1162
3280af22 1163 if (l + pref_len < (5 + taill) && l < PL_regeol - locinput)
1164 l = ( PL_regeol - locinput > (5 + taill) - pref_len
1165 ? (5 + taill) - pref_len : PL_regeol - locinput);
8d300b32 1166 if (pref0_len < 0)
1167 pref0_len = 0;
c277df42 1168 regprop(prop, scan);
1169 PerlIO_printf(Perl_debug_log,
8d300b32 1170 "%4i <%s%.*s%s%s%.*s%s%s%s%.*s%s>%*s|%3d:%*s%s\n",
3280af22 1171 locinput - PL_bostr,
8d300b32 1172 PL_colors[4], pref0_len,
1173 locinput - pref_len, PL_colors[5],
1174 PL_colors[2], pref_len - pref0_len,
1175 locinput - pref_len + pref0_len, PL_colors[3],
c277df42 1176 (docolor ? "" : "> <"),
3280af22 1177 PL_colors[0], l, locinput, PL_colors[1],
c277df42 1178 15 - l - pref_len + 1,
1179 "",
3280af22 1180 scan - PL_regprogram, PL_regindent*2, "",
c277df42 1181 SvPVX(prop));
1182 } );
a687059c 1183
c277df42 1184 next = scan + NEXT_OFF(scan);
a0d0e21e 1185 if (next == scan)
1186 next = NULL;
a687059c 1187
a0d0e21e 1188 switch (OP(scan)) {
1189 case BOL:
3280af22 1190 if (locinput == PL_bostr
1191 ? PL_regprev == '\n'
1192 : (PL_multiline &&
1193 (nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
a0d0e21e 1194 {
a0ed51b3 1195 /* regtill = regbol; */
a0d0e21e 1196 break;
1197 }
4633a7c4 1198 sayNO;
a0d0e21e 1199 case MBOL:
3280af22 1200 if (locinput == PL_bostr
1201 ? PL_regprev == '\n'
1202 : ((nextchr || locinput < PL_regeol) && locinput[-1] == '\n') )
a0d0e21e 1203 {
1204 break;
1205 }
4633a7c4 1206 sayNO;
a0d0e21e 1207 case SBOL:
3280af22 1208 if (locinput == PL_regbol && PL_regprev == '\n')
a0d0e21e 1209 break;
4633a7c4 1210 sayNO;
774d564b 1211 case GPOS:
22e551b9 1212 if (locinput == PL_reg_ganch)
a0d0e21e 1213 break;
4633a7c4 1214 sayNO;
a0d0e21e 1215 case EOL:
3280af22 1216 if (PL_multiline)
a0d0e21e 1217 goto meol;
1218 else
1219 goto seol;
1220 case MEOL:
1221 meol:
3280af22 1222 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
4633a7c4 1223 sayNO;
a0d0e21e 1224 break;
1225 case SEOL:
1226 seol:
3280af22 1227 if ((nextchr || locinput < PL_regeol) && nextchr != '\n')
4633a7c4 1228 sayNO;
3280af22 1229 if (PL_regeol - locinput > 1)
4633a7c4 1230 sayNO;
a0d0e21e 1231 break;
b85d18e9 1232 case EOS:
3280af22 1233 if (PL_regeol != locinput)
b85d18e9 1234 sayNO;
1235 break;
a0ed51b3 1236 case SANYUTF8:
1237 if (nextchr & 0x80) {
6f06b55f 1238 locinput += PL_utf8skip[nextchr];
a0ed51b3 1239 if (locinput > PL_regeol)
1240 sayNO;
1241 nextchr = UCHARAT(locinput);
1242 break;
1243 }
1244 if (!nextchr && locinput >= PL_regeol)
1245 sayNO;
1246 nextchr = UCHARAT(++locinput);
1247 break;
a0d0e21e 1248 case SANY:
3280af22 1249 if (!nextchr && locinput >= PL_regeol)
4633a7c4 1250 sayNO;
76e3520e 1251 nextchr = UCHARAT(++locinput);
a0d0e21e 1252 break;
a0ed51b3 1253 case ANYUTF8:
1254 if (nextchr & 0x80) {
6f06b55f 1255 locinput += PL_utf8skip[nextchr];
a0ed51b3 1256 if (locinput > PL_regeol)
1257 sayNO;
1258 nextchr = UCHARAT(locinput);
1259 break;
1260 }
1261 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
1262 sayNO;
1263 nextchr = UCHARAT(++locinput);
1264 break;
22c35a8c 1265 case REG_ANY:
3280af22 1266 if (!nextchr && locinput >= PL_regeol || nextchr == '\n')
4633a7c4 1267 sayNO;
76e3520e 1268 nextchr = UCHARAT(++locinput);
a0d0e21e 1269 break;
bbce6d69 1270 case EXACT:
161b471a 1271 s = (char *) OPERAND(scan);
c277df42 1272 ln = UCHARAT(s++);
a0d0e21e 1273 /* Inline the first character, for speed. */
76e3520e 1274 if (UCHARAT(s) != nextchr)
4633a7c4 1275 sayNO;
3280af22 1276 if (PL_regeol - locinput < ln)
4633a7c4 1277 sayNO;
36477c24 1278 if (ln > 1 && memNE(s, locinput, ln))
4633a7c4 1279 sayNO;
a0d0e21e 1280 locinput += ln;
76e3520e 1281 nextchr = UCHARAT(locinput);
bbce6d69 1282 break;
1283 case EXACTFL:
3280af22 1284 PL_reg_flags |= RF_tainted;
bbce6d69 1285 /* FALL THROUGH */
1286 case EXACTF:
161b471a 1287 s = (char *) OPERAND(scan);
c277df42 1288 ln = UCHARAT(s++);
a0ed51b3 1289
1290 if (UTF) {
1291 char *l = locinput;
1292 char *e = s + ln;
1293 c1 = OP(scan) == EXACTF;
1294 while (s < e) {
1295 if (l >= PL_regeol)
1296 sayNO;
dfe13c55 1297 if (utf8_to_uv((U8*)s, 0) != (c1 ?
1298 toLOWER_utf8((U8*)l) :
1299 toLOWER_LC_utf8((U8*)l)))
1300 {
a0ed51b3 1301 sayNO;
dfe13c55 1302 }
a0ed51b3 1303 s += UTF8SKIP(s);
1304 l += UTF8SKIP(l);
1305 }
1306 locinput = l;
1307 nextchr = UCHARAT(locinput);
1308 break;
1309 }
1310
bbce6d69 1311 /* Inline the first character, for speed. */
76e3520e 1312 if (UCHARAT(s) != nextchr &&
bbce6d69 1313 UCHARAT(s) != ((OP(scan) == EXACTF)
22c35a8c 1314 ? PL_fold : PL_fold_locale)[nextchr])
bbce6d69 1315 sayNO;
3280af22 1316 if (PL_regeol - locinput < ln)
bbce6d69 1317 sayNO;
5f05dabc 1318 if (ln > 1 && (OP(scan) == EXACTF
1319 ? ibcmp(s, locinput, ln)
1320 : ibcmp_locale(s, locinput, ln)))
bbce6d69 1321 sayNO;
1322 locinput += ln;
76e3520e 1323 nextchr = UCHARAT(locinput);
a0d0e21e 1324 break;
a0ed51b3 1325 case ANYOFUTF8:
1326 s = (char *) OPERAND(scan);
1327 if (!REGINCLASSUTF8(scan, (U8*)locinput))
1328 sayNO;
1329 if (locinput >= PL_regeol)
1330 sayNO;
6f06b55f 1331 locinput += PL_utf8skip[nextchr];
a0ed51b3 1332 nextchr = UCHARAT(locinput);
1333 break;
a0d0e21e 1334 case ANYOF:
161b471a 1335 s = (char *) OPERAND(scan);
76e3520e 1336 if (nextchr < 0)
1337 nextchr = UCHARAT(locinput);
873ef191 1338 if (!REGINCLASS(s, nextchr))
4633a7c4 1339 sayNO;
3280af22 1340 if (!nextchr && locinput >= PL_regeol)
4633a7c4 1341 sayNO;
76e3520e 1342 nextchr = UCHARAT(++locinput);
a0d0e21e 1343 break;
bbce6d69 1344 case ALNUML:
3280af22 1345 PL_reg_flags |= RF_tainted;
bbce6d69 1346 /* FALL THROUGH */
a0d0e21e 1347 case ALNUM:
76e3520e 1348 if (!nextchr)
4633a7c4 1349 sayNO;
bbce6d69 1350 if (!(OP(scan) == ALNUM
76e3520e 1351 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
4633a7c4 1352 sayNO;
76e3520e 1353 nextchr = UCHARAT(++locinput);
a0d0e21e 1354 break;
a0ed51b3 1355 case ALNUMLUTF8:
1356 PL_reg_flags |= RF_tainted;
1357 /* FALL THROUGH */
1358 case ALNUMUTF8:
1359 if (!nextchr)
1360 sayNO;
1361 if (nextchr & 0x80) {
1362 if (!(OP(scan) == ALNUMUTF8
dfe13c55 1363 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1364 : isALNUM_LC_utf8((U8*)locinput)))
1365 {
a0ed51b3 1366 sayNO;
dfe13c55 1367 }
6f06b55f 1368 locinput += PL_utf8skip[nextchr];
a0ed51b3 1369 nextchr = UCHARAT(locinput);
1370 break;
1371 }
1372 if (!(OP(scan) == ALNUMUTF8
1373 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
1374 sayNO;
1375 nextchr = UCHARAT(++locinput);
1376 break;
bbce6d69 1377 case NALNUML:
3280af22 1378 PL_reg_flags |= RF_tainted;
bbce6d69 1379 /* FALL THROUGH */
a0d0e21e 1380 case NALNUM:
3280af22 1381 if (!nextchr && locinput >= PL_regeol)
4633a7c4 1382 sayNO;
bbce6d69 1383 if (OP(scan) == NALNUM
76e3520e 1384 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
4633a7c4 1385 sayNO;
76e3520e 1386 nextchr = UCHARAT(++locinput);
a0d0e21e 1387 break;
a0ed51b3 1388 case NALNUMLUTF8:
1389 PL_reg_flags |= RF_tainted;
1390 /* FALL THROUGH */
1391 case NALNUMUTF8:
1392 if (!nextchr && locinput >= PL_regeol)
1393 sayNO;
1394 if (nextchr & 0x80) {
1395 if (OP(scan) == NALNUMUTF8
dfe13c55 1396 ? swash_fetch(PL_utf8_alnum, (U8*)locinput)
1397 : isALNUM_LC_utf8((U8*)locinput))
1398 {
a0ed51b3 1399 sayNO;
dfe13c55 1400 }
6f06b55f 1401 locinput += PL_utf8skip[nextchr];
a0ed51b3 1402 nextchr = UCHARAT(locinput);
1403 break;
1404 }
1405 if (OP(scan) == NALNUMUTF8
1406 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
1407 sayNO;
1408 nextchr = UCHARAT(++locinput);
1409 break;
bbce6d69 1410 case BOUNDL:
1411 case NBOUNDL:
3280af22 1412 PL_reg_flags |= RF_tainted;
bbce6d69 1413 /* FALL THROUGH */
a0d0e21e 1414 case BOUND:
bbce6d69 1415 case NBOUND:
1416 /* was last char in word? */
3280af22 1417 ln = (locinput != PL_regbol) ? UCHARAT(locinput - 1) : PL_regprev;
bbce6d69 1418 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
1419 ln = isALNUM(ln);
76e3520e 1420 n = isALNUM(nextchr);
bbce6d69 1421 }
1422 else {
1423 ln = isALNUM_LC(ln);
76e3520e 1424 n = isALNUM_LC(nextchr);
bbce6d69 1425 }
95bac841 1426 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
4633a7c4 1427 sayNO;
a0d0e21e 1428 break;
a0ed51b3 1429 case BOUNDLUTF8:
1430 case NBOUNDLUTF8:
1431 PL_reg_flags |= RF_tainted;
1432 /* FALL THROUGH */
1433 case BOUNDUTF8:
1434 case NBOUNDUTF8:
1435 /* was last char in word? */
dfe13c55 1436 ln = (locinput != PL_regbol)
1437 ? utf8_to_uv(reghop((U8*)locinput, -1), 0) : PL_regprev;
a0ed51b3 1438 if (OP(scan) == BOUNDUTF8 || OP(scan) == NBOUNDUTF8) {
1439 ln = isALNUM_uni(ln);
dfe13c55 1440 n = swash_fetch(PL_utf8_alnum, (U8*)locinput);
a0ed51b3 1441 }
1442 else {
1443 ln = isALNUM_LC_uni(ln);
dfe13c55 1444 n = isALNUM_LC_utf8((U8*)locinput);
a0ed51b3 1445 }
1446 if (((!ln) == (!n)) == (OP(scan) == BOUNDUTF8 || OP(scan) == BOUNDLUTF8))
1447 sayNO;
1448 break;
bbce6d69 1449 case SPACEL:
3280af22 1450 PL_reg_flags |= RF_tainted;
bbce6d69 1451 /* FALL THROUGH */
a0d0e21e 1452 case SPACE:
3280af22 1453 if (!nextchr && locinput >= PL_regeol)
4633a7c4 1454 sayNO;
bbce6d69 1455 if (!(OP(scan) == SPACE
76e3520e 1456 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
4633a7c4 1457 sayNO;
76e3520e 1458 nextchr = UCHARAT(++locinput);
a0d0e21e 1459 break;
a0ed51b3 1460 case SPACELUTF8:
1461 PL_reg_flags |= RF_tainted;
1462 /* FALL THROUGH */
1463 case SPACEUTF8:
1464 if (!nextchr && locinput >= PL_regeol)
1465 sayNO;
1466 if (nextchr & 0x80) {
1467 if (!(OP(scan) == SPACEUTF8
dfe13c55 1468 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1469 : isSPACE_LC_utf8((U8*)locinput)))
1470 {
a0ed51b3 1471 sayNO;
dfe13c55 1472 }
6f06b55f 1473 locinput += PL_utf8skip[nextchr];
a0ed51b3 1474 nextchr = UCHARAT(locinput);
1475 break;
1476 }
1477 if (!(OP(scan) == SPACEUTF8
1478 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
1479 sayNO;
1480 nextchr = UCHARAT(++locinput);
1481 break;
bbce6d69 1482 case NSPACEL:
3280af22 1483 PL_reg_flags |= RF_tainted;
bbce6d69 1484 /* FALL THROUGH */
a0d0e21e 1485 case NSPACE:
76e3520e 1486 if (!nextchr)
4633a7c4 1487 sayNO;
bbce6d69 1488 if (OP(scan) == SPACE
76e3520e 1489 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
4633a7c4 1490 sayNO;
76e3520e 1491 nextchr = UCHARAT(++locinput);
a0d0e21e 1492 break;
a0ed51b3 1493 case NSPACELUTF8:
1494 PL_reg_flags |= RF_tainted;
1495 /* FALL THROUGH */
1496 case NSPACEUTF8:
1497 if (!nextchr)
1498 sayNO;
1499 if (nextchr & 0x80) {
1500 if (OP(scan) == NSPACEUTF8
dfe13c55 1501 ? swash_fetch(PL_utf8_space,(U8*)locinput)
1502 : isSPACE_LC_utf8((U8*)locinput))
1503 {
a0ed51b3 1504 sayNO;
dfe13c55 1505 }
6f06b55f 1506 locinput += PL_utf8skip[nextchr];
a0ed51b3 1507 nextchr = UCHARAT(locinput);
1508 break;
1509 }
1510 if (OP(scan) == NSPACEUTF8
1511 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
1512 sayNO;
1513 nextchr = UCHARAT(++locinput);
1514 break;
a0d0e21e 1515 case DIGIT:
76e3520e 1516 if (!isDIGIT(nextchr))
4633a7c4 1517 sayNO;
76e3520e 1518 nextchr = UCHARAT(++locinput);
a0d0e21e 1519 break;
a0ed51b3 1520 case DIGITUTF8:
1521 if (nextchr & 0x80) {
dfe13c55 1522 if (!(swash_fetch(PL_utf8_digit,(U8*)locinput)))
a0ed51b3 1523 sayNO;
6f06b55f 1524 locinput += PL_utf8skip[nextchr];
a0ed51b3 1525 nextchr = UCHARAT(locinput);
1526 break;
1527 }
1528 if (!isDIGIT(nextchr))
1529 sayNO;
1530 nextchr = UCHARAT(++locinput);
1531 break;
a0d0e21e 1532 case NDIGIT:
3280af22 1533 if (!nextchr && locinput >= PL_regeol)
4633a7c4 1534 sayNO;
76e3520e 1535 if (isDIGIT(nextchr))
4633a7c4 1536 sayNO;
76e3520e 1537 nextchr = UCHARAT(++locinput);
a0d0e21e 1538 break;
a0ed51b3 1539 case NDIGITUTF8:
1540 if (!nextchr && locinput >= PL_regeol)
1541 sayNO;
1542 if (nextchr & 0x80) {
dfe13c55 1543 if (swash_fetch(PL_utf8_digit,(U8*)locinput))
a0ed51b3 1544 sayNO;
6f06b55f 1545 locinput += PL_utf8skip[nextchr];
a0ed51b3 1546 nextchr = UCHARAT(locinput);
1547 break;
1548 }
1549 if (isDIGIT(nextchr))
1550 sayNO;
1551 nextchr = UCHARAT(++locinput);
1552 break;
1553 case CLUMP:
dfe13c55 1554 if (locinput >= PL_regeol || swash_fetch(PL_utf8_mark,(U8*)locinput))
a0ed51b3 1555 sayNO;
6f06b55f 1556 locinput += PL_utf8skip[nextchr];
dfe13c55 1557 while (locinput < PL_regeol && swash_fetch(PL_utf8_mark,(U8*)locinput))
a0ed51b3 1558 locinput += UTF8SKIP(locinput);
1559 if (locinput > PL_regeol)
1560 sayNO;
1561 nextchr = UCHARAT(locinput);
1562 break;
c8756f30 1563 case REFFL:
3280af22 1564 PL_reg_flags |= RF_tainted;
c8756f30 1565 /* FALL THROUGH */
c277df42 1566 case REF:
c8756f30 1567 case REFF:
c277df42 1568 n = ARG(scan); /* which paren pair */
3280af22 1569 s = PL_regstartp[n];
1570 if (*PL_reglastparen < n || !s)
af3f8c16 1571 sayNO; /* Do not match unless seen CLOSEn. */
3280af22 1572 if (s == PL_regendp[n])
a0d0e21e 1573 break;
a0ed51b3 1574
1575 if (UTF && OP(scan) != REF) { /* REF can do byte comparison */
1576 char *l = locinput;
1577 char *e = PL_regendp[n];
1578 /*
1579 * Note that we can't do the "other character" lookup trick as
1580 * in the 8-bit case (no pun intended) because in Unicode we
1581 * have to map both upper and title case to lower case.
1582 */
1583 if (OP(scan) == REFF) {
1584 while (s < e) {
1585 if (l >= PL_regeol)
1586 sayNO;
dfe13c55 1587 if (toLOWER_utf8((U8*)s) != toLOWER_utf8((U8*)l))
a0ed51b3 1588 sayNO;
1589 s += UTF8SKIP(s);
1590 l += UTF8SKIP(l);
1591 }
1592 }
1593 else {
1594 while (s < e) {
1595 if (l >= PL_regeol)
1596 sayNO;
dfe13c55 1597 if (toLOWER_LC_utf8((U8*)s) != toLOWER_LC_utf8((U8*)l))
a0ed51b3 1598 sayNO;
1599 s += UTF8SKIP(s);
1600 l += UTF8SKIP(l);
1601 }
1602 }
1603 locinput = l;
1604 nextchr = UCHARAT(locinput);
1605 break;
1606 }
1607
a0d0e21e 1608 /* Inline the first character, for speed. */
76e3520e 1609 if (UCHARAT(s) != nextchr &&
c8756f30 1610 (OP(scan) == REF ||
1611 (UCHARAT(s) != ((OP(scan) == REFF
22c35a8c 1612 ? PL_fold : PL_fold_locale)[nextchr]))))
4633a7c4 1613 sayNO;
3280af22 1614 ln = PL_regendp[n] - s;
1615 if (locinput + ln > PL_regeol)
4633a7c4 1616 sayNO;
c8756f30 1617 if (ln > 1 && (OP(scan) == REF
1618 ? memNE(s, locinput, ln)
1619 : (OP(scan) == REFF
1620 ? ibcmp(s, locinput, ln)
1621 : ibcmp_locale(s, locinput, ln))))
4633a7c4 1622 sayNO;
a0d0e21e 1623 locinput += ln;
76e3520e 1624 nextchr = UCHARAT(locinput);
a0d0e21e 1625 break;
1626
1627 case NOTHING:
c277df42 1628 case TAIL:
a0d0e21e 1629 break;
1630 case BACK:
1631 break;
c277df42 1632 case EVAL:
1633 {
1634 dSP;
533c011a 1635 OP_4tree *oop = PL_op;
3280af22 1636 COP *ocurcop = PL_curcop;
1637 SV **ocurpad = PL_curpad;
c277df42 1638 SV *ret;
1639
1640 n = ARG(scan);
533c011a 1641 PL_op = (OP_4tree*)PL_regdata->data[n];
1642 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", PL_op) );
3280af22 1643 PL_curpad = AvARRAY((AV*)PL_regdata->data[n + 1]);
9661b544 1644 PL_reg_magic->mg_len = locinput - PL_bostr;
c277df42 1645
76e3520e 1646 CALLRUNOPS(); /* Scalar context. */
c277df42 1647 SPAGAIN;
1648 ret = POPs;
1649 PUTBACK;
1650
0f5d15d6 1651 PL_op = oop;
1652 PL_curpad = ocurpad;
1653 PL_curcop = ocurcop;
c277df42 1654 if (logical) {
0f5d15d6 1655 if (logical == 2) { /* Postponed subexpression. */
1656 regexp *re;
22c35a8c 1657 MAGIC *mg = Null(MAGIC*);
0f5d15d6 1658 re_cc_state state;
1659 CURCUR cctmp;
1660 CHECKPOINT cp, lastcp;
1661
1662 if(SvROK(ret) || SvRMAGICAL(ret)) {
1663 SV *sv = SvROK(ret) ? SvRV(ret) : ret;
1664
1665 if(SvMAGICAL(sv))
1666 mg = mg_find(sv, 'r');
1667 }
1668 if (mg) {
1669 re = (regexp *)mg->mg_obj;
df0003d4 1670 (void)ReREFCNT_inc(re);
0f5d15d6 1671 }
1672 else {
1673 STRLEN len;
1674 char *t = SvPV(ret, len);
1675 PMOP pm;
1676 char *oprecomp = PL_regprecomp;
1677 I32 osize = PL_regsize;
1678 I32 onpar = PL_regnpar;
1679
1680 pm.op_pmflags = 0;
1681 re = CALLREGCOMP(t, t + len, &pm);
1682 if (!(SvFLAGS(ret)
1683 & (SVs_TEMP | SVs_PADTMP | SVf_READONLY)))
1684 sv_magic(ret,(SV*)ReREFCNT_inc(re),'r',0,0);
1685 PL_regprecomp = oprecomp;
1686 PL_regsize = osize;
1687 PL_regnpar = onpar;
1688 }
1689 DEBUG_r(
1690 PerlIO_printf(Perl_debug_log,
1691 "Entering embedded `%s%.60s%s%s'\n",
1692 PL_colors[0],
1693 re->precomp,
1694 PL_colors[1],
1695 (strlen(re->precomp) > 60 ? "..." : ""))
1696 );
1697 state.node = next;
1698 state.prev = PL_reg_call_cc;
1699 state.cc = PL_regcc;
1700 state.re = PL_reg_re;
1701
1702 cctmp.cur = 0;
1703 cctmp.oldcc = 0;
1704 PL_regcc = &cctmp;
1705
1706 cp = regcppush(0); /* Save *all* the positions. */
1707 REGCP_SET;
1708 cache_re(re);
1709 state.ss = PL_savestack_ix;
1710 *PL_reglastparen = 0;
1711 PL_reg_call_cc = &state;
1712 PL_reginput = locinput;
1713 if (regmatch(re->program + 1)) {
1714 ReREFCNT_dec(re);
1715 regcpblow(cp);
1716 sayYES;
1717 }
1718 DEBUG_r(
1719 PerlIO_printf(Perl_debug_log,
1720 "%*s failed...\n",
1721 REPORT_CODE_OFF+PL_regindent*2, "")
1722 );
1723 ReREFCNT_dec(re);
1724 REGCP_UNWIND;
1725 regcppop();
1726 PL_reg_call_cc = state.prev;
1727 PL_regcc = state.cc;
1728 PL_reg_re = state.re;
d3790889 1729 cache_re(PL_reg_re);
0f5d15d6 1730 sayNO;
1731 }
c277df42 1732 sw = SvTRUE(ret);
0f5d15d6 1733 logical = 0;
a0ed51b3 1734 }
1735 else
3280af22 1736 sv_setsv(save_scalar(PL_replgv), ret);
c277df42 1737 break;
1738 }
a0d0e21e 1739 case OPEN:
c277df42 1740 n = ARG(scan); /* which paren pair */
3280af22 1741 PL_reg_start_tmp[n] = locinput;
1742 if (n > PL_regsize)
1743 PL_regsize = n;
a0d0e21e 1744 break;
1745 case CLOSE:
c277df42 1746 n = ARG(scan); /* which paren pair */
3280af22 1747 PL_regstartp[n] = PL_reg_start_tmp[n];
1748 PL_regendp[n] = locinput;
1749 if (n > *PL_reglastparen)
1750 *PL_reglastparen = n;
a0d0e21e 1751 break;
c277df42 1752 case GROUPP:
1753 n = ARG(scan); /* which paren pair */
3280af22 1754 sw = (*PL_reglastparen >= n && PL_regendp[n] != NULL);
c277df42 1755 break;
1756 case IFTHEN:
1757 if (sw)
1758 next = NEXTOPER(NEXTOPER(scan));
1759 else {
1760 next = scan + ARG(scan);
1761 if (OP(next) == IFTHEN) /* Fake one. */
1762 next = NEXTOPER(NEXTOPER(next));
1763 }
1764 break;
1765 case LOGICAL:
0f5d15d6 1766 logical = scan->flags;
c277df42 1767 break;
a0d0e21e 1768 case CURLYX: {
1769 CURCUR cc;
3280af22 1770 CHECKPOINT cp = PL_savestack_ix;
c277df42 1771
1772 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1773 next += ARG(next);
3280af22 1774 cc.oldcc = PL_regcc;
1775 PL_regcc = &cc;
1776 cc.parenfloor = *PL_reglastparen;
a0d0e21e 1777 cc.cur = -1;
1778 cc.min = ARG1(scan);
1779 cc.max = ARG2(scan);
c277df42 1780 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
a0d0e21e 1781 cc.next = next;
1782 cc.minmod = minmod;
1783 cc.lastloc = 0;
3280af22 1784 PL_reginput = locinput;
a0d0e21e 1785 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1786 regcpblow(cp);
3280af22 1787 PL_regcc = cc.oldcc;
4633a7c4 1788 saySAME(n);
a0d0e21e 1789 }
1790 /* NOT REACHED */
1791 case WHILEM: {
1792 /*
1793 * This is really hard to understand, because after we match
1794 * what we're trying to match, we must make sure the rest of
1795 * the RE is going to match for sure, and to do that we have
1796 * to go back UP the parse tree by recursing ever deeper. And
1797 * if it fails, we have to reset our parent's current state
1798 * that we can try again after backing off.
1799 */
1800
c277df42 1801 CHECKPOINT cp, lastcp;
3280af22 1802 CURCUR* cc = PL_regcc;
c277df42 1803 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1804
4633a7c4 1805 n = cc->cur + 1; /* how many we know we matched */
3280af22 1806 PL_reginput = locinput;
a0d0e21e 1807
c277df42 1808 DEBUG_r(
1809 PerlIO_printf(Perl_debug_log,
1810 "%*s %ld out of %ld..%ld cc=%lx\n",
3280af22 1811 REPORT_CODE_OFF+PL_regindent*2, "",
c277df42 1812 (long)n, (long)cc->min,
1813 (long)cc->max, (long)cc)
1814 );
4633a7c4 1815
a0d0e21e 1816 /* If degenerate scan matches "", assume scan done. */
1817
579cf2c3 1818 if (locinput == cc->lastloc && n >= cc->min) {
3280af22 1819 PL_regcc = cc->oldcc;
1820 ln = PL_regcc->cur;
c277df42 1821 DEBUG_r(
c3464db5 1822 PerlIO_printf(Perl_debug_log,
1823 "%*s empty match detected, try continuation...\n",
3280af22 1824 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1825 );
a0d0e21e 1826 if (regmatch(cc->next))
4633a7c4 1827 sayYES;
c277df42 1828 DEBUG_r(
c3464db5 1829 PerlIO_printf(Perl_debug_log,
1830 "%*s failed...\n",
3280af22 1831 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1832 );
3280af22 1833 PL_regcc->cur = ln;
1834 PL_regcc = cc;
4633a7c4 1835 sayNO;
a0d0e21e 1836 }
1837
1838 /* First just match a string of min scans. */
1839
1840 if (n < cc->min) {
1841 cc->cur = n;
1842 cc->lastloc = locinput;
4633a7c4 1843 if (regmatch(cc->scan))
1844 sayYES;
1845 cc->cur = n - 1;
c277df42 1846 cc->lastloc = lastloc;
1847 DEBUG_r(
c3464db5 1848 PerlIO_printf(Perl_debug_log,
1849 "%*s failed...\n",
3280af22 1850 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1851 );
4633a7c4 1852 sayNO;
a0d0e21e 1853 }
1854
1855 /* Prefer next over scan for minimal matching. */
1856
1857 if (cc->minmod) {
3280af22 1858 PL_regcc = cc->oldcc;
1859 ln = PL_regcc->cur;
5f05dabc 1860 cp = regcppush(cc->parenfloor);
c277df42 1861 REGCP_SET;
5f05dabc 1862 if (regmatch(cc->next)) {
c277df42 1863 regcpblow(cp);
4633a7c4 1864 sayYES; /* All done. */
5f05dabc 1865 }
c277df42 1866 REGCP_UNWIND;
5f05dabc 1867 regcppop();
3280af22 1868 PL_regcc->cur = ln;
1869 PL_regcc = cc;
a0d0e21e 1870
c277df42 1871 if (n >= cc->max) { /* Maximum greed exceeded? */
599cee73 1872 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
3280af22 1873 && !(PL_reg_flags & RF_warned)) {
1874 PL_reg_flags |= RF_warned;
599cee73 1875 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
2f3ca594 1876 "Complex regular subexpression recursion",
1877 REG_INFTY - 1);
c277df42 1878 }
4633a7c4 1879 sayNO;
c277df42 1880 }
a687059c 1881
c277df42 1882 DEBUG_r(
c3464db5 1883 PerlIO_printf(Perl_debug_log,
1884 "%*s trying longer...\n",
3280af22 1885 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1886 );
a0d0e21e 1887 /* Try scanning more and see if it helps. */
3280af22 1888 PL_reginput = locinput;
a0d0e21e 1889 cc->cur = n;
1890 cc->lastloc = locinput;
5f05dabc 1891 cp = regcppush(cc->parenfloor);
c277df42 1892 REGCP_SET;
5f05dabc 1893 if (regmatch(cc->scan)) {
c277df42 1894 regcpblow(cp);
4633a7c4 1895 sayYES;
5f05dabc 1896 }
c277df42 1897 DEBUG_r(
c3464db5 1898 PerlIO_printf(Perl_debug_log,
1899 "%*s failed...\n",
3280af22 1900 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1901 );
1902 REGCP_UNWIND;
5f05dabc 1903 regcppop();
4633a7c4 1904 cc->cur = n - 1;
c277df42 1905 cc->lastloc = lastloc;
4633a7c4 1906 sayNO;
a0d0e21e 1907 }
1908
1909 /* Prefer scan over next for maximal matching. */
1910
1911 if (n < cc->max) { /* More greed allowed? */
5f05dabc 1912 cp = regcppush(cc->parenfloor);
a0d0e21e 1913 cc->cur = n;
1914 cc->lastloc = locinput;
c277df42 1915 REGCP_SET;
5f05dabc 1916 if (regmatch(cc->scan)) {
c277df42 1917 regcpblow(cp);
4633a7c4 1918 sayYES;
5f05dabc 1919 }
c277df42 1920 REGCP_UNWIND;
a0d0e21e 1921 regcppop(); /* Restore some previous $<digit>s? */
3280af22 1922 PL_reginput = locinput;
c277df42 1923 DEBUG_r(
c3464db5 1924 PerlIO_printf(Perl_debug_log,
1925 "%*s failed, try continuation...\n",
3280af22 1926 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1927 );
1928 }
599cee73 1929 if (ckWARN(WARN_UNSAFE) && n >= REG_INFTY
1930 && !(PL_reg_flags & RF_warned)) {
3280af22 1931 PL_reg_flags |= RF_warned;
599cee73 1932 warner(WARN_UNSAFE, "%s limit (%d) exceeded",
cb5d145d 1933 "Complex regular subexpression recursion",
1934 REG_INFTY - 1);
a0d0e21e 1935 }
1936
1937 /* Failed deeper matches of scan, so see if this one works. */
3280af22 1938 PL_regcc = cc->oldcc;
1939 ln = PL_regcc->cur;
a0d0e21e 1940 if (regmatch(cc->next))
4633a7c4 1941 sayYES;
c277df42 1942 DEBUG_r(
c3464db5 1943 PerlIO_printf(Perl_debug_log, "%*s failed...\n",
3280af22 1944 REPORT_CODE_OFF+PL_regindent*2, "")
c277df42 1945 );
3280af22 1946 PL_regcc->cur = ln;
1947 PL_regcc = cc;
4633a7c4 1948 cc->cur = n - 1;
c277df42 1949 cc->lastloc = lastloc;
4633a7c4 1950 sayNO;
a0d0e21e 1951 }
1952 /* NOT REACHED */
c277df42 1953 case BRANCHJ:
1954 next = scan + ARG(scan);
1955 if (next == scan)
1956 next = NULL;
1957 inner = NEXTOPER(NEXTOPER(scan));
1958 goto do_branch;
1959 case BRANCH:
1960 inner = NEXTOPER(scan);
1961 do_branch:
1962 {
1963 CHECKPOINT lastcp;
1964 c1 = OP(scan);
1965 if (OP(next) != c1) /* No choice. */
1966 next = inner; /* Avoid recursion. */
a0d0e21e 1967 else {
3280af22 1968 int lastparen = *PL_reglastparen;
c277df42 1969
1970 REGCP_SET;
a0d0e21e 1971 do {
3280af22 1972 PL_reginput = locinput;
c277df42 1973 if (regmatch(inner))
4633a7c4 1974 sayYES;
c277df42 1975 REGCP_UNWIND;
3280af22 1976 for (n = *PL_reglastparen; n > lastparen; n--)
1977 PL_regendp[n] = 0;
1978 *PL_reglastparen = n;
c277df42 1979 scan = next;
a0d0e21e 1980 /*SUPPRESS 560*/
c277df42 1981 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1982 next += n;
a0d0e21e 1983 else
c277df42 1984 next = NULL;
c277df42 1985 inner = NEXTOPER(scan);
1986 if (c1 == BRANCHJ) {
1987 inner = NEXTOPER(inner);
1988 }
1989 } while (scan != NULL && OP(scan) == c1);
4633a7c4 1990 sayNO;
a0d0e21e 1991 /* NOTREACHED */
a687059c 1992 }
a0d0e21e 1993 }
1994 break;
1995 case MINMOD:
1996 minmod = 1;
1997 break;
c277df42 1998 case CURLYM:
1999 {
00db4c45 2000 I32 l = 0;
c277df42 2001 CHECKPOINT lastcp;
2002
2003 /* We suppose that the next guy does not need
2004 backtracking: in particular, it is of constant length,
2005 and has no parenths to influence future backrefs. */
2006 ln = ARG1(scan); /* min to match */
2007 n = ARG2(scan); /* max to match */
c277df42 2008 paren = scan->flags;
2009 if (paren) {
3280af22 2010 if (paren > PL_regsize)
2011 PL_regsize = paren;
2012 if (paren > *PL_reglastparen)
2013 *PL_reglastparen = paren;
c277df42 2014 }
dc45a647 2015 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
c277df42 2016 if (paren)
2017 scan += NEXT_OFF(scan); /* Skip former OPEN. */
3280af22 2018 PL_reginput = locinput;
c277df42 2019 if (minmod) {
2020 minmod = 0;
2021 if (ln && regrepeat_hard(scan, ln, &l) < ln)
2022 sayNO;
5f4b28b2 2023 if (ln && l == 0 && n >= ln
c277df42 2024 /* In fact, this is tricky. If paren, then the
2025 fact that we did/didnot match may influence
2026 future execution. */
2027 && !(paren && ln == 0))
2028 ln = n;
3280af22 2029 locinput = PL_reginput;
22c35a8c 2030 if (PL_regkind[(U8)OP(next)] == EXACT) {
c277df42 2031 c1 = UCHARAT(OPERAND(next) + 1);
2032 if (OP(next) == EXACTF)
22c35a8c 2033 c2 = PL_fold[c1];
c277df42 2034 else if (OP(next) == EXACTFL)
22c35a8c 2035 c2 = PL_fold_locale[c1];
c277df42 2036 else
2037 c2 = c1;
a0ed51b3 2038 }
2039 else
c277df42 2040 c1 = c2 = -1000;
2041 REGCP_SET;
5f4b28b2 2042 /* This may be improved if l == 0. */
c277df42 2043 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
2044 /* If it could work, try it. */
2045 if (c1 == -1000 ||
3280af22 2046 UCHARAT(PL_reginput) == c1 ||
2047 UCHARAT(PL_reginput) == c2)
c277df42 2048 {
2049 if (paren) {
2050 if (n) {
dfe13c55 2051 PL_regstartp[paren] = HOPc(PL_reginput, -l);
3280af22 2052 PL_regendp[paren] = PL_reginput;
a0ed51b3 2053 }
2054 else
3280af22 2055 PL_regendp[paren] = NULL;
c277df42 2056 }
2057 if (regmatch(next))
2058 sayYES;
2059 REGCP_UNWIND;
2060 }
2061 /* Couldn't or didn't -- move forward. */
3280af22 2062 PL_reginput = locinput;
c277df42 2063 if (regrepeat_hard(scan, 1, &l)) {
2064 ln++;
3280af22 2065 locinput = PL_reginput;
c277df42 2066 }
2067 else
2068 sayNO;
2069 }
a0ed51b3 2070 }
2071 else {
c277df42 2072 n = regrepeat_hard(scan, n, &l);
2073 if (n != 0 && l == 0
2074 /* In fact, this is tricky. If paren, then the
2075 fact that we did/didnot match may influence
2076 future execution. */
2077 && !(paren && ln == 0))
2078 ln = n;
3280af22 2079 locinput = PL_reginput;
c277df42 2080 DEBUG_r(
5c0ca799 2081 PerlIO_printf(Perl_debug_log,
2082 "%*s matched %ld times, len=%ld...\n",
3280af22 2083 REPORT_CODE_OFF+PL_regindent*2, "", n, l)
c277df42 2084 );
2085 if (n >= ln) {
22c35a8c 2086 if (PL_regkind[(U8)OP(next)] == EXACT) {
c277df42 2087 c1 = UCHARAT(OPERAND(next) + 1);
2088 if (OP(next) == EXACTF)
22c35a8c 2089 c2 = PL_fold[c1];
c277df42 2090 else if (OP(next) == EXACTFL)
22c35a8c 2091 c2 = PL_fold_locale[c1];
c277df42 2092 else
2093 c2 = c1;
a0ed51b3 2094 }
2095 else
c277df42 2096 c1 = c2 = -1000;
2097 }
2098 REGCP_SET;
2099 while (n >= ln) {
2100 /* If it could work, try it. */
2101 if (c1 == -1000 ||
3280af22 2102 UCHARAT(PL_reginput) == c1 ||
2103 UCHARAT(PL_reginput) == c2)
a0ed51b3 2104 {
2105 DEBUG_r(
c3464db5 2106 PerlIO_printf(Perl_debug_log,
2107 "%*s trying tail with n=%ld...\n",
3280af22 2108 REPORT_CODE_OFF+PL_regindent*2, "", n)
a0ed51b3 2109 );
2110 if (paren) {
2111 if (n) {
dfe13c55 2112 PL_regstartp[paren] = HOPc(PL_reginput, -l);
a0ed51b3 2113 PL_regendp[paren] = PL_reginput;
c277df42 2114 }
a0ed51b3 2115 else
2116 PL_regendp[paren] = NULL;
c277df42 2117 }
a0ed51b3 2118 if (regmatch(next))
2119 sayYES;
2120 REGCP_UNWIND;
2121 }
c277df42 2122 /* Couldn't or didn't -- back up. */
2123 n--;
dfe13c55 2124 locinput = HOPc(locinput, -l);
3280af22 2125 PL_reginput = locinput;
c277df42 2126 }
2127 }
2128 sayNO;
2129 break;
2130 }
2131 case CURLYN:
2132 paren = scan->flags; /* Which paren to set */
3280af22 2133 if (paren > PL_regsize)
2134 PL_regsize = paren;
2135 if (paren > *PL_reglastparen)
2136 *PL_reglastparen = paren;
c277df42 2137 ln = ARG1(scan); /* min to match */
2138 n = ARG2(scan); /* max to match */
dc45a647 2139 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
c277df42 2140 goto repeat;
a0d0e21e 2141 case CURLY:
c277df42 2142 paren = 0;
a0d0e21e 2143 ln = ARG1(scan); /* min to match */
2144 n = ARG2(scan); /* max to match */
dc45a647 2145 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
a0d0e21e 2146 goto repeat;
2147 case STAR:
2148 ln = 0;
c277df42 2149 n = REG_INFTY;
a0d0e21e 2150 scan = NEXTOPER(scan);
c277df42 2151 paren = 0;
a0d0e21e 2152 goto repeat;
2153 case PLUS:
c277df42 2154 ln = 1;
2155 n = REG_INFTY;
2156 scan = NEXTOPER(scan);
2157 paren = 0;
2158 repeat:
a0d0e21e 2159 /*
2160 * Lookahead to avoid useless match attempts
2161 * when we know what character comes next.
2162 */
22c35a8c 2163 if (PL_regkind[(U8)OP(next)] == EXACT) {
bbce6d69 2164 c1 = UCHARAT(OPERAND(next) + 1);
2165 if (OP(next) == EXACTF)
22c35a8c 2166 c2 = PL_fold[c1];
bbce6d69 2167 else if (OP(next) == EXACTFL)
22c35a8c 2168 c2 = PL_fold_locale[c1];
bbce6d69 2169 else
2170 c2 = c1;
2171 }
a0d0e21e 2172 else
bbce6d69 2173 c1 = c2 = -1000;
3280af22 2174 PL_reginput = locinput;
a0d0e21e 2175 if (minmod) {
c277df42 2176 CHECKPOINT lastcp;
a0d0e21e 2177 minmod = 0;
2178 if (ln && regrepeat(scan, ln) < ln)
4633a7c4 2179 sayNO;
a0ed51b3 2180 locinput = PL_reginput;
c277df42 2181 REGCP_SET;
2182 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
a0d0e21e 2183 /* If it could work, try it. */
bbce6d69 2184 if (c1 == -1000 ||
3280af22 2185 UCHARAT(PL_reginput) == c1 ||
2186 UCHARAT(PL_reginput) == c2)
bbce6d69 2187 {
c277df42 2188 if (paren) {
2189 if (n) {
dfe13c55 2190 PL_regstartp[paren] = HOPc(PL_reginput, -1);
3280af22 2191 PL_regendp[paren] = PL_reginput;
a0ed51b3 2192 }
2193 else
3280af22 2194 PL_regendp[paren] = NULL;
c277df42 2195 }
a0d0e21e 2196 if (regmatch(next))
4633a7c4 2197 sayYES;
c277df42 2198 REGCP_UNWIND;
bbce6d69 2199 }
c277df42 2200 /* Couldn't or didn't -- move forward. */
a0ed51b3 2201 PL_reginput = locinput;
a0d0e21e 2202 if (regrepeat(scan, 1)) {
2203 ln++;
a0ed51b3 2204 locinput = PL_reginput;
2205 }
2206 else
4633a7c4 2207 sayNO;
a0d0e21e 2208 }
2209 }
2210 else {
c277df42 2211 CHECKPOINT lastcp;
a0d0e21e 2212 n = regrepeat(scan, n);
a0ed51b3 2213 locinput = PL_reginput;
22c35a8c 2214 if (ln < n && PL_regkind[(U8)OP(next)] == EOL &&
3280af22 2215 (!PL_multiline || OP(next) == SEOL))
a0d0e21e 2216 ln = n; /* why back off? */
c277df42 2217 REGCP_SET;
2218 if (paren) {
2219 while (n >= ln) {
2220 /* If it could work, try it. */
2221 if (c1 == -1000 ||
3280af22 2222 UCHARAT(PL_reginput) == c1 ||
2223 UCHARAT(PL_reginput) == c2)
c277df42 2224 {
2225 if (paren && n) {
2226 if (n) {
dfe13c55 2227 PL_regstartp[paren] = HOPc(PL_reginput, -1);
3280af22 2228 PL_regendp[paren] = PL_reginput;
a0ed51b3 2229 }
2230 else
3280af22 2231 PL_regendp[paren] = NULL;
c277df42 2232 }
2233 if (regmatch(next))
2234 sayYES;
2235 REGCP_UNWIND;
2236 }
2237 /* Couldn't or didn't -- back up. */
2238 n--;
dfe13c55 2239 PL_reginput = locinput = HOPc(locinput, -1);
c277df42 2240 }
a0ed51b3 2241 }
2242 else {
c277df42 2243 while (n >= ln) {
2244 /* If it could work, try it. */
2245 if (c1 == -1000 ||
3280af22 2246 UCHARAT(PL_reginput) == c1 ||
2247 UCHARAT(PL_reginput) == c2)
c277df42 2248 {
2249 if (regmatch(next))
2250 sayYES;
2251 REGCP_UNWIND;
2252 }
2253 /* Couldn't or didn't -- back up. */
2254 n--;
dfe13c55 2255 PL_reginput = locinput = HOPc(locinput, -1);
bbce6d69 2256 }
a0d0e21e 2257 }
2258 }
4633a7c4 2259 sayNO;
c277df42 2260 break;
a0d0e21e 2261 case END:
0f5d15d6 2262 if (PL_reg_call_cc) {
2263 re_cc_state *cur_call_cc = PL_reg_call_cc;
2264 CURCUR *cctmp = PL_regcc;
2265 regexp *re = PL_reg_re;
2266 CHECKPOINT cp, lastcp;
2267
2268 cp = regcppush(0); /* Save *all* the positions. */
2269 REGCP_SET;
2270 regcp_set_to(PL_reg_call_cc->ss); /* Restore parens of
2271 the caller. */
2272 PL_reginput = locinput; /* Make position available to
2273 the callcc. */
2274 cache_re(PL_reg_call_cc->re);
2275 PL_regcc = PL_reg_call_cc->cc;
2276 PL_reg_call_cc = PL_reg_call_cc->prev;
2277 if (regmatch(cur_call_cc->node)) {
2278 PL_reg_call_cc = cur_call_cc;
2279 regcpblow(cp);
2280 sayYES;
2281 }
2282 REGCP_UNWIND;
2283 regcppop();
2284 PL_reg_call_cc = cur_call_cc;
2285 PL_regcc = cctmp;
2286 PL_reg_re = re;
2287 cache_re(re);
2288
2289 DEBUG_r(
2290 PerlIO_printf(Perl_debug_log,
2291 "%*s continuation failed...\n",
2292 REPORT_CODE_OFF+PL_regindent*2, "")
2293 );
2294 sayNO;
2295 }
3280af22 2296 if (locinput < PL_regtill)
7e5428c5 2297 sayNO; /* Cannot match: too short. */
2298 /* Fall through */
2299 case SUCCEED:
3280af22 2300 PL_reginput = locinput; /* put where regtry can find it */
4633a7c4 2301 sayYES; /* Success! */
c277df42 2302 case SUSPEND:
2303 n = 1;
9fe1d20c 2304 PL_reginput = locinput;
c277df42 2305 goto do_ifmatch;
a0d0e21e 2306 case UNLESSM:
c277df42 2307 n = 0;
a0ed51b3 2308 if (scan->flags) {
dfe13c55 2309 s = HOPMAYBEc(locinput, -scan->flags);
a0ed51b3 2310 if (!s)
2311 goto say_yes;
2312 PL_reginput = s;
2313 }
2314 else
2315 PL_reginput = locinput;
c277df42 2316 goto do_ifmatch;
2317 case IFMATCH:
2318 n = 1;
a0ed51b3 2319 if (scan->flags) {
dfe13c55 2320 s = HOPMAYBEc(locinput, -scan->flags);
642653c7 2321 if (!s || s < PL_bostr)
a0ed51b3 2322 goto say_no;
2323 PL_reginput = s;
2324 }
2325 else
2326 PL_reginput = locinput;
2327
c277df42 2328 do_ifmatch:
c277df42 2329 inner = NEXTOPER(NEXTOPER(scan));
2330 if (regmatch(inner) != n) {
2331 say_no:
2332 if (logical) {
2333 logical = 0;
2334 sw = 0;
2335 goto do_longjump;
a0ed51b3 2336 }
2337 else
c277df42 2338 sayNO;
2339 }
2340 say_yes:
2341 if (logical) {
2342 logical = 0;
2343 sw = 1;
2344 }
fe44a5e8 2345 if (OP(scan) == SUSPEND) {
3280af22 2346 locinput = PL_reginput;
565764a8 2347 nextchr = UCHARAT(locinput);
fe44a5e8 2348 }
c277df42 2349 /* FALL THROUGH. */
2350 case LONGJMP:
2351 do_longjump:
2352 next = scan + ARG(scan);
2353 if (next == scan)
2354 next = NULL;
a0d0e21e 2355 break;
2356 default:
c030ccd9 2357 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
c277df42 2358 (unsigned long)scan, OP(scan));
a0d0e21e 2359 FAIL("regexp memory corruption");
a687059c 2360 }
a0d0e21e 2361 scan = next;
2362 }
a687059c 2363
a0d0e21e 2364 /*
2365 * We get here only if there's trouble -- normally "case END" is
2366 * the terminating point.
2367 */
2368 FAIL("corrupted regexp pointers");
2369 /*NOTREACHED*/
4633a7c4 2370 sayNO;
2371
2372yes:
2373#ifdef DEBUGGING
3280af22 2374 PL_regindent--;
4633a7c4 2375#endif
2376 return 1;
2377
2378no:
2379#ifdef DEBUGGING
3280af22 2380 PL_regindent--;
4633a7c4 2381#endif
a0d0e21e 2382 return 0;
a687059c 2383}
2384
2385/*
2386 - regrepeat - repeatedly match something simple, report how many
2387 */
2388/*
2389 * [This routine now assumes that it will only match on things of length 1.
2390 * That was true before, but now we assume scan - reginput is the count,
a0ed51b3 2391 * rather than incrementing count on every character. [Er, except utf8.]]
a687059c 2392 */
76e3520e 2393STATIC I32
c277df42 2394regrepeat(regnode *p, I32 max)
a687059c 2395{
5c0ca799 2396 dTHR;
a0d0e21e 2397 register char *scan;
2398 register char *opnd;
2399 register I32 c;
3280af22 2400 register char *loceol = PL_regeol;
a0ed51b3 2401 register I32 hardcount = 0;
a0d0e21e 2402
3280af22 2403 scan = PL_reginput;
c277df42 2404 if (max != REG_INFTY && max < loceol - scan)
a0d0e21e 2405 loceol = scan + max;
161b471a 2406 opnd = (char *) OPERAND(p);
a0d0e21e 2407 switch (OP(p)) {
22c35a8c 2408 case REG_ANY:
a0d0e21e 2409 while (scan < loceol && *scan != '\n')
2410 scan++;
2411 break;
2412 case SANY:
2413 scan = loceol;
2414 break;
a0ed51b3 2415 case ANYUTF8:
2416 loceol = PL_regeol;
2417 while (scan < loceol && *scan != '\n') {
2418 scan += UTF8SKIP(scan);
2419 hardcount++;
2420 }
2421 break;
2422 case SANYUTF8:
2423 loceol = PL_regeol;
2424 while (scan < loceol) {
2425 scan += UTF8SKIP(scan);
2426 hardcount++;
2427 }
2428 break;
bbce6d69 2429 case EXACT: /* length of string is 1 */
2430 c = UCHARAT(++opnd);
2431 while (scan < loceol && UCHARAT(scan) == c)
2432 scan++;
2433 break;
2434 case EXACTF: /* length of string is 1 */
2435 c = UCHARAT(++opnd);
2436 while (scan < loceol &&
22c35a8c 2437 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold[c]))
bbce6d69 2438 scan++;
2439 break;
2440 case EXACTFL: /* length of string is 1 */
3280af22 2441 PL_reg_flags |= RF_tainted;
bbce6d69 2442 c = UCHARAT(++opnd);
2443 while (scan < loceol &&
22c35a8c 2444 (UCHARAT(scan) == c || UCHARAT(scan) == PL_fold_locale[c]))
a0d0e21e 2445 scan++;
2446 break;
a0ed51b3 2447 case ANYOFUTF8:
2448 loceol = PL_regeol;
2449 while (scan < loceol && REGINCLASSUTF8(p, (U8*)scan)) {
2450 scan += UTF8SKIP(scan);
2451 hardcount++;
2452 }
2453 break;
a0d0e21e 2454 case ANYOF:
ae5c130c 2455 while (scan < loceol && REGINCLASS(opnd, *scan))
a0d0e21e 2456 scan++;
a0d0e21e 2457 break;
2458 case ALNUM:
2459 while (scan < loceol && isALNUM(*scan))
2460 scan++;
2461 break;
a0ed51b3 2462 case ALNUMUTF8:
2463 loceol = PL_regeol;
dfe13c55 2464 while (scan < loceol && swash_fetch(PL_utf8_alnum, (U8*)scan)) {
a0ed51b3 2465 scan += UTF8SKIP(scan);
2466 hardcount++;
2467 }
2468 break;
bbce6d69 2469 case ALNUML:
3280af22 2470 PL_reg_flags |= RF_tainted;
bbce6d69 2471 while (scan < loceol && isALNUM_LC(*scan))
2472 scan++;
2473 break;
a0ed51b3 2474 case ALNUMLUTF8:
2475 PL_reg_flags |= RF_tainted;
2476 loceol = PL_regeol;
dfe13c55 2477 while (scan < loceol && isALNUM_LC_utf8((U8*)scan)) {
a0ed51b3 2478 scan += UTF8SKIP(scan);
2479 hardcount++;
2480 }
2481 break;
2482 break;
a0d0e21e 2483 case NALNUM:
2484 while (scan < loceol && !isALNUM(*scan))
2485 scan++;
2486 break;
a0ed51b3 2487 case NALNUMUTF8:
2488 loceol = PL_regeol;
dfe13c55 2489 while (scan < loceol && !swash_fetch(PL_utf8_alnum, (U8*)scan)) {
a0ed51b3 2490 scan += UTF8SKIP(scan);
2491 hardcount++;
2492 }
2493 break;
bbce6d69 2494 case NALNUML:
3280af22 2495 PL_reg_flags |= RF_tainted;
bbce6d69 2496 while (scan < loceol && !isALNUM_LC(*scan))
2497 scan++;
2498 break;
a0ed51b3 2499 case NALNUMLUTF8:
2500 PL_reg_flags |= RF_tainted;
2501 loceol = PL_regeol;
dfe13c55 2502 while (scan < loceol && !isALNUM_LC_utf8((U8*)scan)) {
a0ed51b3 2503 scan += UTF8SKIP(scan);
2504 hardcount++;
2505 }
2506 break;
a0d0e21e 2507 case SPACE:
2508 while (scan < loceol && isSPACE(*scan))
2509 scan++;
2510 break;
a0ed51b3 2511 case SPACEUTF8:
2512 loceol = PL_regeol;
dfe13c55 2513 while (scan < loceol && (*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
a0ed51b3 2514 scan += UTF8SKIP(scan);
2515 hardcount++;
2516 }
2517 break;
bbce6d69 2518 case SPACEL:
3280af22 2519 PL_reg_flags |= RF_tainted;
bbce6d69 2520 while (scan < loceol && isSPACE_LC(*scan))
2521 scan++;
2522 break;
a0ed51b3 2523 case SPACELUTF8:
2524 PL_reg_flags |= RF_tainted;
2525 loceol = PL_regeol;
dfe13c55 2526 while (scan < loceol && (*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
a0ed51b3 2527 scan += UTF8SKIP(scan);
2528 hardcount++;
2529 }
2530 break;
a0d0e21e 2531 case NSPACE:
2532 while (scan < loceol && !isSPACE(*scan))
2533 scan++;
2534 break;
a0ed51b3 2535 case NSPACEUTF8:
2536 loceol = PL_regeol;
dfe13c55 2537 while (scan < loceol && !(*scan == ' ' || swash_fetch(PL_utf8_space,(U8*)scan))) {
a0ed51b3 2538 scan += UTF8SKIP(scan);
2539 hardcount++;
2540 }
2541 break;
bbce6d69 2542 case NSPACEL:
3280af22 2543 PL_reg_flags |= RF_tainted;
bbce6d69 2544 while (scan < loceol && !isSPACE_LC(*scan))
2545 scan++;
2546 break;
a0ed51b3 2547 case NSPACELUTF8:
2548 PL_reg_flags |= RF_tainted;
2549 loceol = PL_regeol;
dfe13c55 2550 while (scan < loceol && !(*scan == ' ' || isSPACE_LC_utf8((U8*)scan))) {
a0ed51b3 2551 scan += UTF8SKIP(scan);
2552 hardcount++;
2553 }
2554 break;
a0d0e21e 2555 case DIGIT:
2556 while (scan < loceol && isDIGIT(*scan))
2557 scan++;
2558 break;
a0ed51b3 2559 case DIGITUTF8:
2560 loceol = PL_regeol;
dfe13c55 2561 while (scan < loceol && swash_fetch(PL_utf8_digit,(U8*)scan)) {
a0ed51b3 2562 scan += UTF8SKIP(scan);
2563 hardcount++;
2564 }
2565 break;
2566 break;
a0d0e21e 2567 case NDIGIT:
2568 while (scan < loceol && !isDIGIT(*scan))
2569 scan++;
2570 break;
a0ed51b3 2571 case NDIGITUTF8:
2572 loceol = PL_regeol;
dfe13c55 2573 while (scan < loceol && !swash_fetch(PL_utf8_digit,(U8*)scan)) {
a0ed51b3 2574 scan += UTF8SKIP(scan);
2575 hardcount++;
2576 }
2577 break;
a0d0e21e 2578 default: /* Called on something of 0 width. */
2579 break; /* So match right here or not at all. */
2580 }
a687059c 2581
a0ed51b3 2582 if (hardcount)
2583 c = hardcount;
2584 else
2585 c = scan - PL_reginput;
3280af22 2586 PL_reginput = scan;
a687059c 2587
c277df42 2588 DEBUG_r(
2589 {
2590 SV *prop = sv_newmortal();
2591
2592 regprop(prop, p);
2593 PerlIO_printf(Perl_debug_log,
2594 "%*s %s can match %ld times out of %ld...\n",
2595 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
2596 });
2597
a0d0e21e 2598 return(c);
a687059c 2599}
2600
2601/*
c277df42 2602 - regrepeat_hard - repeatedly match something, report total lenth and length
2603 *
2604 * The repeater is supposed to have constant length.
2605 */
2606
76e3520e 2607STATIC I32
c277df42 2608regrepeat_hard(regnode *p, I32 max, I32 *lp)
2609{
5c0ca799 2610 dTHR;
c277df42 2611 register char *scan;
2612 register char *start;
3280af22 2613 register char *loceol = PL_regeol;
a0ed51b3 2614 I32 l = 0;
708e3b05 2615 I32 count = 0, res = 1;
a0ed51b3 2616
2617 if (!max)
2618 return 0;
c277df42 2619
3280af22 2620 start = PL_reginput;
a0ed51b3 2621 if (UTF) {
708e3b05 2622 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
a0ed51b3 2623 if (!count++) {
2624 l = 0;
2625 while (start < PL_reginput) {
2626 l++;
2627 start += UTF8SKIP(start);
2628 }
2629 *lp = l;
2630 if (l == 0)
2631 return max;
2632 }
2633 if (count == max)
2634 return count;
2635 }
2636 }
2637 else {
708e3b05 2638 while (PL_reginput < loceol && (scan = PL_reginput, res = regmatch(p))) {
a0ed51b3 2639 if (!count++) {
2640 *lp = l = PL_reginput - start;
2641 if (max != REG_INFTY && l*max < loceol - scan)
2642 loceol = scan + l*max;
2643 if (l == 0)
2644 return max;
c277df42 2645 }
2646 }
2647 }
708e3b05 2648 if (!res)
3280af22 2649 PL_reginput = scan;
c277df42 2650
a0ed51b3 2651 return count;
c277df42 2652}
2653
2654/*
bbce6d69 2655 - regclass - determine if a character falls into a character class
2656 */
2657
76e3520e 2658STATIC bool
8ac85365 2659reginclass(register char *p, register I32 c)
bbce6d69 2660{
5c0ca799 2661 dTHR;
bbce6d69 2662 char flags = *p;
2663 bool match = FALSE;
2664
2665 c &= 0xFF;
ae5c130c 2666 if (ANYOF_TEST(p, c))
bbce6d69 2667 match = TRUE;
2668 else if (flags & ANYOF_FOLD) {
2669 I32 cf;
2670 if (flags & ANYOF_LOCALE) {
3280af22 2671 PL_reg_flags |= RF_tainted;
22c35a8c 2672 cf = PL_fold_locale[c];
bbce6d69 2673 }
2674 else
22c35a8c 2675 cf = PL_fold[c];
ae5c130c 2676 if (ANYOF_TEST(p, cf))
bbce6d69 2677 match = TRUE;
2678 }
2679
2680 if (!match && (flags & ANYOF_ISA)) {
3280af22 2681 PL_reg_flags |= RF_tainted;
bbce6d69 2682
2683 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
2684 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
2685 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
2686 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
2687 {
2688 match = TRUE;
2689 }
2690 }
2691
ae5c130c 2692 return (flags & ANYOF_INVERT) ? !match : match;
bbce6d69 2693}
2694
a0ed51b3 2695STATIC bool
2696reginclassutf8(regnode *f, U8 *p)
c485e607 2697{
2698 dTHR;
a0ed51b3 2699 char flags = ARG1(f);
2700 bool match = FALSE;
2701 SV *sv = (SV*)PL_regdata->data[ARG2(f)];
2702
2703 if (swash_fetch(sv, p))
2704 match = TRUE;
2705 else if (flags & ANYOF_FOLD) {
2706 I32 cf;
dfe13c55 2707 U8 tmpbuf[10];
a0ed51b3 2708 if (flags & ANYOF_LOCALE) {
2709 PL_reg_flags |= RF_tainted;
2710 uv_to_utf8(tmpbuf, toLOWER_LC_utf8(p));
2711 }
2712 else
2713 uv_to_utf8(tmpbuf, toLOWER_utf8(p));
2714 if (swash_fetch(sv, tmpbuf))
2715 match = TRUE;
2716 }
2717
2718 if (!match && (flags & ANYOF_ISA)) {
2719 PL_reg_flags |= RF_tainted;
2720
2721 if (((flags & ANYOF_ALNUML) && isALNUM_LC_utf8(p)) ||
2722 ((flags & ANYOF_NALNUML) && !isALNUM_LC_utf8(p)) ||
2723 ((flags & ANYOF_SPACEL) && isSPACE_LC_utf8(p)) ||
2724 ((flags & ANYOF_NSPACEL) && !isSPACE_LC_utf8(p)))
2725 {
2726 match = TRUE;
2727 }
2728 }
2729
2730 return (flags & ANYOF_INVERT) ? !match : match;
2731}
161b471a 2732
dfe13c55 2733STATIC U8 *
2734reghop(U8 *s, I32 off)
c485e607 2735{
2736 dTHR;
a0ed51b3 2737 if (off >= 0) {
2738 while (off-- && s < (U8*)PL_regeol)
2739 s += UTF8SKIP(s);
2740 }
2741 else {
2742 while (off++) {
2743 if (s > (U8*)PL_bostr) {
2744 s--;
2745 if (*s & 0x80) {
2746 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2747 s--;
2748 } /* XXX could check well-formedness here */
2749 }
2750 }
2751 }
2752 return s;
2753}
161b471a 2754
dfe13c55 2755STATIC U8 *
2756reghopmaybe(U8* s, I32 off)
a0ed51b3 2757{
c485e607 2758 dTHR;
a0ed51b3 2759 if (off >= 0) {
2760 while (off-- && s < (U8*)PL_regeol)
2761 s += UTF8SKIP(s);
2762 if (off >= 0)
2763 return 0;
2764 }
2765 else {
2766 while (off++) {
2767 if (s > (U8*)PL_bostr) {
2768 s--;
2769 if (*s & 0x80) {
2770 while (s > (U8*)PL_bostr && (*s & 0xc0) == 0x80)
2771 s--;
2772 } /* XXX could check well-formedness here */
2773 }
2774 else
2775 break;
2776 }
2777 if (off <= 0)
2778 return 0;
2779 }
2780 return s;
2781}