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