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