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