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