This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
5_59 configured with -Dd_bincompat5005 can't be made
[perl5.git] / regcomp.c
CommitLineData
a0d0e21e
LW
1/* regcomp.c
2 */
3
4/*
5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
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 */
cad2e5aa 28# if defined(PERL_EXT_RE_DEBUG) && !defined(DEBUGGING)
b9d5759e
AD
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_pregcomp my_regcomp
36# define Perl_regdump my_regdump
37# define Perl_regprop my_regprop
d06ea78c 38# define Perl_pregfree my_regfree
cad2e5aa
JH
39# define Perl_re_intuit_string my_re_intuit_string
40/* *These* symbols are masked to allow static link. */
d06ea78c 41# define Perl_regnext my_regnext
f0b8d043 42# define Perl_save_re_context my_save_re_context
d88dccdf 43# define Perl_reginitcolors my_reginitcolors
c5be433b
GS
44
45# define PERL_NO_GET_CONTEXT
56953603
IZ
46#endif
47
f0fcb552 48/*SUPPRESS 112*/
a687059c 49/*
e50aee73 50 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
51 *
52 * Copyright (c) 1986 by University of Toronto.
53 * Written by Henry Spencer. Not derived from licensed software.
54 *
55 * Permission is granted to anyone to use this software for any
56 * purpose on any computer system, and to redistribute it freely,
57 * subject to the following restrictions:
58 *
59 * 1. The author is not responsible for the consequences of use of
60 * this software, no matter how awful, even if they arise
61 * from defects in it.
62 *
63 * 2. The origin of this software must not be misrepresented, either
64 * by explicit claim or by omission.
65 *
66 * 3. Altered versions must be plainly marked as such, and must not
67 * be misrepresented as being the original software.
68 *
69 *
70 **** Alterations to Henry's code are...
71 ****
4eb8286e 72 **** Copyright (c) 1991-1999, Larry Wall
a687059c 73 ****
9ef589d8
LW
74 **** You may distribute under the terms of either the GNU General Public
75 **** License or the Artistic License, as specified in the README file.
76
a687059c
LW
77 *
78 * Beware that some of this code is subtly aware of the way operator
79 * precedence is structured in regular expressions. Serious changes in
80 * regular-expression syntax might require a total rethink.
81 */
82#include "EXTERN.h"
864dbfa3 83#define PERL_IN_REGCOMP_C
a687059c 84#include "perl.h"
d06ea78c 85
c5be433b
GS
86#ifdef PERL_IN_XSUB_RE
87# if defined(PERL_CAPI) || defined(PERL_OBJECT)
88# include "XSUB.h"
89# endif
90#else
d06ea78c
GS
91# include "INTERN.h"
92#endif
c277df42
IZ
93
94#define REG_COMP_C
a687059c
LW
95#include "regcomp.h"
96
d4cce5f1 97#ifdef op
11343788 98#undef op
d4cce5f1 99#endif /* op */
11343788 100
fe14fcc3
LW
101#ifdef MSDOS
102# if defined(BUGGY_MSC6)
103 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
104 # pragma optimize("a",off)
105 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
106 # pragma optimize("w",on )
107# endif /* BUGGY_MSC6 */
108#endif /* MSDOS */
109
a687059c
LW
110#ifndef STATIC
111#define STATIC static
112#endif
113
114#define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
115#define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
116 ((*s) == '{' && regcurly(s)))
2b69d0c2
LW
117#ifdef atarist
118#define PERL_META "^$.[()|?+*\\"
119#else
a687059c 120#define META "^$.[()|?+*\\"
2b69d0c2 121#endif
a687059c 122
35c8bce7
LW
123#ifdef SPSTART
124#undef SPSTART /* dratted cpp namespace... */
125#endif
a687059c
LW
126/*
127 * Flags to be passed up and down.
128 */
a687059c 129#define WORST 0 /* Worst case. */
821b33a5 130#define HASWIDTH 0x1 /* Known to match non-null strings. */
a0d0e21e
LW
131#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
132#define SPSTART 0x4 /* Starts with * or +. */
133#define TRYAGAIN 0x8 /* Weeded out a declaration. */
a687059c 134
2c2d71f5
JH
135/* Length of a variant. */
136
137typedef struct scan_data_t {
138 I32 len_min;
139 I32 len_delta;
140 I32 pos_min;
141 I32 pos_delta;
142 SV *last_found;
143 I32 last_end; /* min value, <0 unless valid. */
144 I32 last_start_min;
145 I32 last_start_max;
146 SV **longest; /* Either &l_fixed, or &l_float. */
147 SV *longest_fixed;
148 I32 offset_fixed;
149 SV *longest_float;
150 I32 offset_float_min;
151 I32 offset_float_max;
152 I32 flags;
153 I32 whilem_c;
154} scan_data_t;
155
a687059c 156/*
e50aee73 157 * Forward declarations for pregcomp()'s friends.
a687059c 158 */
a0d0e21e 159
cd488c12 160static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
2c2d71f5 161 0, 0, 0, 0 };
c277df42
IZ
162
163#define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
164#define SF_BEFORE_SEOL 0x1
165#define SF_BEFORE_MEOL 0x2
166#define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
167#define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
168
09b7f37c
CB
169#ifdef NO_UNARY_PLUS
170# define SF_FIX_SHIFT_EOL (0+2)
171# define SF_FL_SHIFT_EOL (0+4)
172#else
173# define SF_FIX_SHIFT_EOL (+2)
174# define SF_FL_SHIFT_EOL (+4)
175#endif
c277df42
IZ
176
177#define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
178#define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
179
180#define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
181#define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
182#define SF_IS_INF 0x40
183#define SF_HAS_PAR 0x80
184#define SF_IN_PAR 0x100
185#define SF_HAS_EVAL 0x200
4bfe0158 186#define SCF_DO_SUBSTR 0x400
c277df42 187
a0ed51b3
LW
188#define RF_utf8 8
189#define UTF (PL_reg_flags & RF_utf8)
190#define LOC (PL_regflags & PMf_LOCALE)
191#define FOLD (PL_regflags & PMf_FOLD)
192
b8c5462f
JH
193#define OOB_CHAR8 1234
194#define OOB_UTF8 123456
195
a0ed51b3
LW
196#define CHR_SVLEN(sv) (UTF ? sv_len_utf8(sv) : SvCUR(sv))
197#define CHR_DIST(a,b) (UTF ? utf8_distance(a,b) : a - b)
198
51371543 199static void clear_re(pTHXo_ void *r);
4327152a
IZ
200
201STATIC void
cea2e8a9 202S_scan_commit(pTHX_ scan_data_t *data)
c277df42 203{
c485e607 204 dTHR;
a0ed51b3
LW
205 STRLEN l = CHR_SVLEN(data->last_found);
206 STRLEN old_l = CHR_SVLEN(*data->longest);
c277df42
IZ
207
208 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
209 sv_setsv(*data->longest, data->last_found);
210 if (*data->longest == data->longest_fixed) {
211 data->offset_fixed = l ? data->last_start_min : data->pos_min;
212 if (data->flags & SF_BEFORE_EOL)
213 data->flags
214 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
215 else
216 data->flags &= ~SF_FIX_BEFORE_EOL;
a0ed51b3
LW
217 }
218 else {
c277df42
IZ
219 data->offset_float_min = l ? data->last_start_min : data->pos_min;
220 data->offset_float_max = (l
221 ? data->last_start_max
222 : data->pos_min + data->pos_delta);
223 if (data->flags & SF_BEFORE_EOL)
224 data->flags
225 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
226 else
227 data->flags &= ~SF_FL_BEFORE_EOL;
228 }
229 }
230 SvCUR_set(data->last_found, 0);
231 data->last_end = -1;
232 data->flags &= ~SF_BEFORE_EOL;
233}
234
c277df42
IZ
235/* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
236 to the position after last scanned or to NULL. */
237
76e3520e 238STATIC I32
cea2e8a9 239S_study_chunk(pTHX_ regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
c277df42
IZ
240 /* scanp: Start here (read-write). */
241 /* deltap: Write maxlen-minlen here. */
242 /* last: Stop before this one. */
243{
5c0ca799 244 dTHR;
c277df42
IZ
245 I32 min = 0, pars = 0, code;
246 regnode *scan = *scanp, *next;
247 I32 delta = 0;
248 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
aca2d497 249 int is_inf_internal = 0; /* The studied chunk is infinite */
c277df42
IZ
250 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
251 scan_data_t data_fake;
252
253 while (scan && OP(scan) != END && scan < last) {
254 /* Peephole optimizer: */
255
22c35a8c 256 if (PL_regkind[(U8)OP(scan)] == EXACT) {
c277df42
IZ
257 regnode *n = regnext(scan);
258 U32 stringok = 1;
259#ifdef DEBUGGING
260 regnode *stop = scan;
261#endif
262
263 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
264 /* Skip NOTHING, merge EXACT*. */
265 while (n &&
22c35a8c 266 ( PL_regkind[(U8)OP(n)] == NOTHING ||
c277df42
IZ
267 (stringok && (OP(n) == OP(scan))))
268 && NEXT_OFF(n)
269 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
270 if (OP(n) == TAIL || n > next)
271 stringok = 0;
22c35a8c 272 if (PL_regkind[(U8)OP(n)] == NOTHING) {
c277df42
IZ
273 NEXT_OFF(scan) += NEXT_OFF(n);
274 next = n + NODE_STEP_REGNODE;
275#ifdef DEBUGGING
276 if (stringok)
277 stop = n;
278#endif
279 n = regnext(n);
a0ed51b3
LW
280 }
281 else {
c277df42
IZ
282 int oldl = *OPERAND(scan);
283 regnode *nnext = regnext(n);
284
285 if (oldl + *OPERAND(n) > U8_MAX)
286 break;
287 NEXT_OFF(scan) += NEXT_OFF(n);
288 *OPERAND(scan) += *OPERAND(n);
289 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
290 /* Now we can overwrite *n : */
291 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
292 *OPERAND(n) + 1, char);
293#ifdef DEBUGGING
294 if (stringok)
295 stop = next - 1;
296#endif
297 n = nnext;
298 }
299 }
300#ifdef DEBUGGING
301 /* Allow dumping */
302 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
303 while (n <= stop) {
ca04da08
GS
304 /* Purify reports a benign UMR here sometimes, because we
305 * don't initialize the OP() slot of a node when that node
306 * is occupied by just the trailing null of the string in
307 * an EXACT node */
22c35a8c 308 if (PL_regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
c277df42
IZ
309 OP(n) = OPTIMIZED;
310 NEXT_OFF(n) = 0;
311 }
312 n++;
313 }
314#endif
315
316 }
317 if (OP(scan) != CURLYX) {
048cfca1
GS
318 int max = (reg_off_by_arg[OP(scan)]
319 ? I32_MAX
320 /* I32 may be smaller than U16 on CRAYs! */
321 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
c277df42
IZ
322 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
323 int noff;
324 regnode *n = scan;
325
326 /* Skip NOTHING and LONGJMP. */
327 while ((n = regnext(n))
22c35a8c 328 && ((PL_regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
c277df42
IZ
329 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
330 && off + noff < max)
331 off += noff;
332 if (reg_off_by_arg[OP(scan)])
333 ARG(scan) = off;
334 else
335 NEXT_OFF(scan) = off;
336 }
337 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
338 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
339 next = regnext(scan);
340 code = OP(scan);
341
342 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
343 I32 max1 = 0, min1 = I32_MAX, num = 0;
344
345 if (flags & SCF_DO_SUBSTR)
346 scan_commit(data);
347 while (OP(scan) == code) {
348 I32 deltanext, minnext;
349
350 num++;
351 data_fake.flags = 0;
2c2d71f5
JH
352 if (data)
353 data_fake.whilem_c = data->whilem_c;
c277df42
IZ
354 next = regnext(scan);
355 scan = NEXTOPER(scan);
356 if (code != BRANCH)
357 scan = NEXTOPER(scan);
358 /* We suppose the run is continuous, last=next...*/
359 minnext = study_chunk(&scan, &deltanext, next,
360 &data_fake, 0);
361 if (min1 > minnext)
362 min1 = minnext;
363 if (max1 < minnext + deltanext)
364 max1 = minnext + deltanext;
365 if (deltanext == I32_MAX)
aca2d497 366 is_inf = is_inf_internal = 1;
c277df42
IZ
367 scan = next;
368 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
369 pars++;
405ff068 370 if (data && (data_fake.flags & SF_HAS_EVAL))
c277df42 371 data->flags |= SF_HAS_EVAL;
2c2d71f5
JH
372 if (data)
373 data->whilem_c = data_fake.whilem_c;
c277df42
IZ
374 if (code == SUSPEND)
375 break;
376 }
377 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
378 min1 = 0;
379 if (flags & SCF_DO_SUBSTR) {
380 data->pos_min += min1;
381 data->pos_delta += max1 - min1;
382 if (max1 != min1 || is_inf)
383 data->longest = &(data->longest_float);
384 }
385 min += min1;
386 delta += max1 - min1;
a0ed51b3
LW
387 }
388 else if (code == BRANCHJ) /* single branch is optimized. */
c277df42
IZ
389 scan = NEXTOPER(NEXTOPER(scan));
390 else /* single branch is optimized. */
391 scan = NEXTOPER(scan);
392 continue;
a0ed51b3
LW
393 }
394 else if (OP(scan) == EXACT) {
395 I32 l = *OPERAND(scan);
396 if (UTF) {
397 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
398 unsigned char *e = s + l;
399 I32 newl = 0;
400 while (s < e) {
401 newl++;
402 s += UTF8SKIP(s);
403 }
404 l = newl;
405 }
406 min += l;
c277df42 407 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
c277df42
IZ
408 /* The code below prefers earlier match for fixed
409 offset, later match for variable offset. */
410 if (data->last_end == -1) { /* Update the start info. */
411 data->last_start_min = data->pos_min;
412 data->last_start_max = is_inf
413 ? I32_MAX : data->pos_min + data->pos_delta;
414 }
a0ed51b3 415 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), *OPERAND(scan));
c277df42
IZ
416 data->last_end = data->pos_min + l;
417 data->pos_min += l; /* As in the first entry. */
418 data->flags &= ~SF_BEFORE_EOL;
419 }
a0ed51b3 420 }
22c35a8c 421 else if (PL_regkind[(U8)OP(scan)] == EXACT) {
a0ed51b3 422 I32 l = *OPERAND(scan);
c277df42
IZ
423 if (flags & SCF_DO_SUBSTR)
424 scan_commit(data);
a0ed51b3
LW
425 if (UTF) {
426 unsigned char *s = (unsigned char *)(OPERAND(scan)+1);
427 unsigned char *e = s + l;
428 I32 newl = 0;
429 while (s < e) {
430 newl++;
431 s += UTF8SKIP(s);
432 }
433 l = newl;
434 }
435 min += l;
c277df42 436 if (data && (flags & SCF_DO_SUBSTR))
a0ed51b3
LW
437 data->pos_min += l;
438 }
4d61ec05 439 else if (strchr((char*)PL_varies,OP(scan))) {
c277df42
IZ
440 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
441 regnode *oscan = scan;
442
22c35a8c 443 switch (PL_regkind[(U8)OP(scan)]) {
c277df42
IZ
444 case WHILEM:
445 scan = NEXTOPER(scan);
446 goto finish;
447 case PLUS:
448 if (flags & SCF_DO_SUBSTR) {
449 next = NEXTOPER(scan);
450 if (OP(next) == EXACT) {
451 mincount = 1;
452 maxcount = REG_INFTY;
453 next = regnext(scan);
454 scan = NEXTOPER(scan);
455 goto do_curly;
456 }
457 }
458 if (flags & SCF_DO_SUBSTR)
459 data->pos_min++;
460 min++;
461 /* Fall through. */
462 case STAR:
aca2d497 463 is_inf = is_inf_internal = 1;
c277df42
IZ
464 scan = regnext(scan);
465 if (flags & SCF_DO_SUBSTR) {
466 scan_commit(data);
467 data->longest = &(data->longest_float);
468 }
469 goto optimize_curly_tail;
470 case CURLY:
471 mincount = ARG1(scan);
472 maxcount = ARG2(scan);
473 next = regnext(scan);
474 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
475 do_curly:
476 if (flags & SCF_DO_SUBSTR) {
477 if (mincount == 0) scan_commit(data);
478 pos_before = data->pos_min;
479 }
480 if (data) {
481 fl = data->flags;
482 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
483 if (is_inf)
484 data->flags |= SF_IS_INF;
485 }
486 /* This will finish on WHILEM, setting scan, or on NULL: */
487 minnext = study_chunk(&scan, &deltanext, last, data,
488 mincount == 0
489 ? (flags & ~SCF_DO_SUBSTR) : flags);
490 if (!scan) /* It was not CURLYX, but CURLY. */
491 scan = next;
599cee73 492 if (ckWARN(WARN_UNSAFE) && (minnext + deltanext == 0)
821b33a5 493 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
17feb5d5 494 && maxcount <= REG_INFTY/3) /* Complement check for big count */
c5be433b
GS
495 Perl_warner(aTHX_ WARN_UNSAFE,
496 "Strange *+?{} on zero-length expression");
c277df42 497 min += minnext * mincount;
aca2d497
IZ
498 is_inf_internal |= (maxcount == REG_INFTY
499 && (minnext + deltanext) > 0
500 || deltanext == I32_MAX);
501 is_inf |= is_inf_internal;
c277df42
IZ
502 delta += (minnext + deltanext) * maxcount - minnext * mincount;
503
504 /* Try powerful optimization CURLYX => CURLYN. */
c277df42
IZ
505 if ( OP(oscan) == CURLYX && data
506 && data->flags & SF_IN_PAR
507 && !(data->flags & SF_HAS_EVAL)
508 && !deltanext && minnext == 1 ) {
509 /* Try to optimize to CURLYN. */
510 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
511 regnode *nxt1 = nxt, *nxt2;
512
513 /* Skip open. */
514 nxt = regnext(nxt);
4d61ec05 515 if (!strchr((char*)PL_simple,OP(nxt))
22c35a8c 516 && !(PL_regkind[(U8)OP(nxt)] == EXACT
c277df42
IZ
517 && *OPERAND(nxt) == 1))
518 goto nogo;
519 nxt2 = nxt;
520 nxt = regnext(nxt);
521 if (OP(nxt) != CLOSE)
522 goto nogo;
523 /* Now we know that nxt2 is the only contents: */
524 oscan->flags = ARG(nxt);
525 OP(oscan) = CURLYN;
526 OP(nxt1) = NOTHING; /* was OPEN. */
527#ifdef DEBUGGING
528 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
529 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
530 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
531 OP(nxt) = OPTIMIZED; /* was CLOSE. */
532 OP(nxt + 1) = OPTIMIZED; /* was count. */
533 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
534#endif
535 }
c277df42
IZ
536 nogo:
537
538 /* Try optimization CURLYX => CURLYM. */
539 if ( OP(oscan) == CURLYX && data
c277df42 540 && !(data->flags & SF_HAS_PAR)
c277df42
IZ
541 && !(data->flags & SF_HAS_EVAL)
542 && !deltanext ) {
543 /* XXXX How to optimize if data == 0? */
544 /* Optimize to a simpler form. */
545 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
546 regnode *nxt2;
547
548 OP(oscan) = CURLYM;
549 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
550 && (OP(nxt2) != WHILEM))
551 nxt = nxt2;
552 OP(nxt2) = SUCCEED; /* Whas WHILEM */
c277df42
IZ
553 /* Need to optimize away parenths. */
554 if (data->flags & SF_IN_PAR) {
555 /* Set the parenth number. */
556 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
557
558 if (OP(nxt) != CLOSE)
559 FAIL("panic opt close");
560 oscan->flags = ARG(nxt);
561 OP(nxt1) = OPTIMIZED; /* was OPEN. */
562 OP(nxt) = OPTIMIZED; /* was CLOSE. */
563#ifdef DEBUGGING
564 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
565 OP(nxt + 1) = OPTIMIZED; /* was count. */
566 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
567 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
568#endif
569#if 0
570 while ( nxt1 && (OP(nxt1) != WHILEM)) {
571 regnode *nnxt = regnext(nxt1);
572
573 if (nnxt == nxt) {
574 if (reg_off_by_arg[OP(nxt1)])
575 ARG_SET(nxt1, nxt2 - nxt1);
576 else if (nxt2 - nxt1 < U16_MAX)
577 NEXT_OFF(nxt1) = nxt2 - nxt1;
578 else
579 OP(nxt) = NOTHING; /* Cannot beautify */
580 }
581 nxt1 = nnxt;
582 }
583#endif
584 /* Optimize again: */
585 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
a0ed51b3
LW
586 }
587 else
c277df42 588 oscan->flags = 0;
c277df42 589 }
2c2d71f5
JH
590 else if (OP(oscan) == CURLYX && data && ++data->whilem_c < 16) {
591 /* This stays as CURLYX, and can put the count/of pair. */
592 /* Find WHILEM (as in regexec.c) */
593 regnode *nxt = oscan + NEXT_OFF(oscan);
594
595 if (OP(PREVOPER(nxt)) == NOTHING) /* LONGJMP */
596 nxt += ARG(nxt);
597 PREVOPER(nxt)->flags = data->whilem_c
598 | (PL_reg_whilem_seen << 4); /* On WHILEM */
599 }
c277df42
IZ
600 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
601 pars++;
602 if (flags & SCF_DO_SUBSTR) {
603 SV *last_str = Nullsv;
604 int counted = mincount != 0;
605
606 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
607 I32 b = pos_before >= data->last_start_min
608 ? pos_before : data->last_start_min;
609 STRLEN l;
610 char *s = SvPV(data->last_found, l);
a0ed51b3
LW
611 I32 old = b - data->last_start_min;
612
613 if (UTF)
614 old = utf8_hop((U8*)s, old) - (U8*)s;
c277df42 615
a0ed51b3 616 l -= old;
c277df42 617 /* Get the added string: */
79cb57f6 618 last_str = newSVpvn(s + old, l);
c277df42
IZ
619 if (deltanext == 0 && pos_before == b) {
620 /* What was added is a constant string */
621 if (mincount > 1) {
622 SvGROW(last_str, (mincount * l) + 1);
623 repeatcpy(SvPVX(last_str) + l,
624 SvPVX(last_str), l, mincount - 1);
625 SvCUR(last_str) *= mincount;
626 /* Add additional parts. */
627 SvCUR_set(data->last_found,
628 SvCUR(data->last_found) - l);
629 sv_catsv(data->last_found, last_str);
630 data->last_end += l * (mincount - 1);
631 }
632 }
633 }
634 /* It is counted once already... */
635 data->pos_min += minnext * (mincount - counted);
636 data->pos_delta += - counted * deltanext +
637 (minnext + deltanext) * maxcount - minnext * mincount;
638 if (mincount != maxcount) {
639 scan_commit(data);
640 if (mincount && last_str) {
641 sv_setsv(data->last_found, last_str);
642 data->last_end = data->pos_min;
643 data->last_start_min =
a0ed51b3 644 data->pos_min - CHR_SVLEN(last_str);
c277df42
IZ
645 data->last_start_max = is_inf
646 ? I32_MAX
647 : data->pos_min + data->pos_delta
a0ed51b3 648 - CHR_SVLEN(last_str);
c277df42
IZ
649 }
650 data->longest = &(data->longest_float);
651 }
aca2d497 652 SvREFCNT_dec(last_str);
c277df42 653 }
405ff068 654 if (data && (fl & SF_HAS_EVAL))
c277df42
IZ
655 data->flags |= SF_HAS_EVAL;
656 optimize_curly_tail:
c277df42 657 if (OP(oscan) != CURLYX) {
22c35a8c 658 while (PL_regkind[(U8)OP(next = regnext(oscan))] == NOTHING
c277df42
IZ
659 && NEXT_OFF(next))
660 NEXT_OFF(oscan) += NEXT_OFF(next);
661 }
c277df42
IZ
662 continue;
663 default: /* REF only? */
664 if (flags & SCF_DO_SUBSTR) {
665 scan_commit(data);
666 data->longest = &(data->longest_float);
667 }
aca2d497 668 is_inf = is_inf_internal = 1;
c277df42
IZ
669 break;
670 }
a0ed51b3 671 }
4d61ec05 672 else if (strchr((char*)PL_simple,OP(scan)) || PL_regkind[(U8)OP(scan)] == ANYUTF8) {
c277df42
IZ
673 if (flags & SCF_DO_SUBSTR) {
674 scan_commit(data);
675 data->pos_min++;
676 }
677 min++;
a0ed51b3 678 }
22c35a8c 679 else if (PL_regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
c277df42
IZ
680 data->flags |= (OP(scan) == MEOL
681 ? SF_BEFORE_MEOL
682 : SF_BEFORE_SEOL);
a0ed51b3 683 }
22c35a8c 684 else if (PL_regkind[(U8)OP(scan)] == BRANCHJ
c277df42
IZ
685 && (scan->flags || data)
686 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
687 I32 deltanext, minnext;
688 regnode *nscan;
689
690 data_fake.flags = 0;
2c2d71f5
JH
691 if (data)
692 data_fake.whilem_c = data->whilem_c;
c277df42
IZ
693 next = regnext(scan);
694 nscan = NEXTOPER(NEXTOPER(scan));
695 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
696 if (scan->flags) {
697 if (deltanext) {
698 FAIL("variable length lookbehind not implemented");
a0ed51b3
LW
699 }
700 else if (minnext > U8_MAX) {
0f4b6630
JH
701#ifdef UV_IS_QUAD
702 FAIL2("lookbehind longer than %" PERL_PRIu64 " not implemented", (UV)U8_MAX);
703#else
c277df42 704 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
0f4b6630 705#endif
c277df42
IZ
706 }
707 scan->flags = minnext;
708 }
709 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
710 pars++;
405ff068 711 if (data && (data_fake.flags & SF_HAS_EVAL))
c277df42 712 data->flags |= SF_HAS_EVAL;
2c2d71f5
JH
713 if (data)
714 data->whilem_c = data_fake.whilem_c;
a0ed51b3
LW
715 }
716 else if (OP(scan) == OPEN) {
c277df42 717 pars++;
a0ed51b3
LW
718 }
719 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
c277df42
IZ
720 next = regnext(scan);
721
722 if ( next && (OP(next) != WHILEM) && next < last)
c277df42 723 is_par = 0; /* Disable optimization */
a0ed51b3
LW
724 }
725 else if (OP(scan) == EVAL) {
c277df42
IZ
726 if (data)
727 data->flags |= SF_HAS_EVAL;
728 }
0f5d15d6
IZ
729 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
730 if (flags & SCF_DO_SUBSTR) {
731 scan_commit(data);
732 data->longest = &(data->longest_float);
733 }
734 is_inf = is_inf_internal = 1;
735 }
c277df42
IZ
736 /* Else: zero-length, ignore. */
737 scan = regnext(scan);
738 }
739
740 finish:
741 *scanp = scan;
aca2d497 742 *deltap = is_inf_internal ? I32_MAX : delta;
c277df42
IZ
743 if (flags & SCF_DO_SUBSTR && is_inf)
744 data->pos_delta = I32_MAX - data->pos_min;
745 if (is_par > U8_MAX)
746 is_par = 0;
747 if (is_par && pars==1 && data) {
748 data->flags |= SF_IN_PAR;
749 data->flags &= ~SF_HAS_PAR;
a0ed51b3
LW
750 }
751 else if (pars && data) {
c277df42
IZ
752 data->flags |= SF_HAS_PAR;
753 data->flags &= ~SF_IN_PAR;
754 }
755 return min;
756}
757
76e3520e 758STATIC I32
cea2e8a9 759S_add_data(pTHX_ I32 n, char *s)
c277df42 760{
5c0ca799 761 dTHR;
3280af22
NIS
762 if (PL_regcomp_rx->data) {
763 Renewc(PL_regcomp_rx->data,
764 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
c277df42 765 char, struct reg_data);
3280af22
NIS
766 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
767 PL_regcomp_rx->data->count += n;
a0ed51b3
LW
768 }
769 else {
3280af22 770 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
c277df42 771 char, struct reg_data);
3280af22
NIS
772 New(1208, PL_regcomp_rx->data->what, n, U8);
773 PL_regcomp_rx->data->count = n;
c277df42 774 }
3280af22
NIS
775 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
776 return PL_regcomp_rx->data->count - n;
c277df42
IZ
777}
778
d88dccdf 779void
864dbfa3 780Perl_reginitcolors(pTHX)
d88dccdf
IZ
781{
782 dTHR;
783 int i = 0;
784 char *s = PerlEnv_getenv("PERL_RE_COLORS");
785
786 if (s) {
787 PL_colors[0] = s = savepv(s);
788 while (++i < 6) {
789 s = strchr(s, '\t');
790 if (s) {
791 *s = '\0';
792 PL_colors[i] = ++s;
793 }
794 else
c712d376 795 PL_colors[i] = s = "";
d88dccdf
IZ
796 }
797 } else {
798 while (i < 6)
799 PL_colors[i++] = "";
800 }
801 PL_colorset = 1;
802}
803
a687059c 804/*
e50aee73 805 - pregcomp - compile a regular expression into internal code
a687059c
LW
806 *
807 * We can't allocate space until we know how big the compiled form will be,
808 * but we can't compile it (and thus know how big it is) until we've got a
809 * place to put the code. So we cheat: we compile it twice, once with code
810 * generation turned off and size counting turned on, and once "for real".
811 * This also means that we don't allocate space until we are sure that the
812 * thing really will compile successfully, and we never have to move the
813 * code and thus invalidate pointers into it. (Note that it has to be in
814 * one piece because free() must be able to free it all.) [NB: not true in perl]
815 *
816 * Beware that the optimization-preparation code in here knows about some
817 * of the structure of the compiled regexp. [I'll say.]
818 */
819regexp *
864dbfa3 820Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
a687059c 821{
5c0ca799 822 dTHR;
a0d0e21e 823 register regexp *r;
c277df42
IZ
824 regnode *scan;
825 SV **longest;
826 SV *longest_fixed;
827 SV *longest_float;
828 regnode *first;
a0d0e21e 829 I32 flags;
a0d0e21e
LW
830 I32 minlen = 0;
831 I32 sawplus = 0;
832 I32 sawopen = 0;
2c2d71f5 833 scan_data_t data;
a0d0e21e
LW
834
835 if (exp == NULL)
c277df42 836 FAIL("NULL regexp argument");
a0d0e21e 837
e24b16f9 838 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
a0ed51b3
LW
839 PL_reg_flags |= RF_utf8;
840 else
841 PL_reg_flags = 0;
842
3280af22 843 PL_regprecomp = savepvn(exp, xend - exp);
35ef4773 844 DEBUG_r(if (!PL_colorset) reginitcolors());
2c2d71f5 845 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
d88dccdf
IZ
846 PL_colors[4],PL_colors[5],PL_colors[0],
847 xend - exp, PL_regprecomp, PL_colors[1]));
3280af22
NIS
848 PL_regflags = pm->op_pmflags;
849 PL_regsawback = 0;
bbce6d69 850
3280af22
NIS
851 PL_regseen = 0;
852 PL_seen_zerolen = *exp == '^' ? -1 : 0;
853 PL_seen_evals = 0;
854 PL_extralen = 0;
c277df42 855
bbce6d69 856 /* First pass: determine size, legality. */
3280af22
NIS
857 PL_regcomp_parse = exp;
858 PL_regxend = xend;
859 PL_regnaughty = 0;
860 PL_regnpar = 1;
861 PL_regsize = 0L;
862 PL_regcode = &PL_regdummy;
2c2d71f5 863 PL_reg_whilem_seen = 0;
22c35a8c 864 regc((U8)REG_MAGIC, (char*)PL_regcode);
a0d0e21e 865 if (reg(0, &flags) == NULL) {
3280af22
NIS
866 Safefree(PL_regprecomp);
867 PL_regprecomp = Nullch;
a0d0e21e
LW
868 return(NULL);
869 }
3280af22 870 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
c277df42 871
c277df42
IZ
872 /* Small enough for pointer-storage convention?
873 If extralen==0, this means that we will not need long jumps. */
3280af22
NIS
874 if (PL_regsize >= 0x10000L && PL_extralen)
875 PL_regsize += PL_extralen;
c277df42 876 else
3280af22 877 PL_extralen = 0;
2c2d71f5
JH
878 if (PL_reg_whilem_seen > 15)
879 PL_reg_whilem_seen = 15;
a0d0e21e 880
bbce6d69 881 /* Allocate space and initialize. */
3280af22 882 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
c277df42 883 char, regexp);
a0d0e21e
LW
884 if (r == NULL)
885 FAIL("regexp out of space");
c277df42 886 r->refcnt = 1;
bbce6d69 887 r->prelen = xend - exp;
3280af22 888 r->precomp = PL_regprecomp;
cf93c79d
IZ
889 r->subbeg = NULL;
890 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
4327152a
IZ
891 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
892
893 r->substrs = 0; /* Useful during FAIL. */
894 r->startp = 0; /* Useful during FAIL. */
895 r->endp = 0; /* Useful during FAIL. */
896
3280af22 897 PL_regcomp_rx = r;
bbce6d69
PP
898
899 /* Second pass: emit code. */
3280af22
NIS
900 PL_regcomp_parse = exp;
901 PL_regxend = xend;
902 PL_regnaughty = 0;
903 PL_regnpar = 1;
904 PL_regcode = r->program;
2cd61cdb 905 /* Store the count of eval-groups for security checks: */
3280af22 906 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
22c35a8c 907 regc((U8)REG_MAGIC, (char*) PL_regcode++);
c277df42 908 r->data = 0;
a0d0e21e
LW
909 if (reg(0, &flags) == NULL)
910 return(NULL);
911
912 /* Dig out information for optimizations. */
cf93c79d 913 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
3280af22 914 pm->op_pmflags = PL_regflags;
a0ed51b3
LW
915 if (UTF)
916 r->reganch |= ROPT_UTF8;
c277df42 917 r->regstclass = NULL;
a0ed51b3
LW
918 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
919 r->reganch |= ROPT_NAUGHTY;
c277df42 920 scan = r->program + 1; /* First BRANCH. */
2779dcf1
IZ
921
922 /* XXXX To minimize changes to RE engine we always allocate
923 3-units-long substrs field. */
924 Newz(1004, r->substrs, 1, struct reg_substr_data);
925
2c2d71f5 926 StructCopy(&zero_scan_data, &data, scan_data_t);
c277df42 927 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
c277df42 928 I32 fake;
c5254dd6 929 STRLEN longest_float_length, longest_fixed_length;
a0d0e21e
LW
930
931 first = scan;
c277df42 932 /* Skip introductions and multiplicators >= 1. */
a0d0e21e
LW
933 while ((OP(first) == OPEN && (sawopen = 1)) ||
934 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
935 (OP(first) == PLUS) ||
936 (OP(first) == MINMOD) ||
22c35a8c 937 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
a0d0e21e
LW
938 if (OP(first) == PLUS)
939 sawplus = 1;
940 else
941 first += regarglen[(U8)OP(first)];
942 first = NEXTOPER(first);
a687059c
LW
943 }
944
a0d0e21e
LW
945 /* Starting-point info. */
946 again:
c277df42 947 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
4d61ec05 948 else if (strchr((char*)PL_simple+4,OP(first)))
a0d0e21e 949 r->regstclass = first;
22c35a8c
GS
950 else if (PL_regkind[(U8)OP(first)] == BOUND ||
951 PL_regkind[(U8)OP(first)] == NBOUND)
a0d0e21e 952 r->regstclass = first;
22c35a8c 953 else if (PL_regkind[(U8)OP(first)] == BOL) {
cad2e5aa
JH
954 r->reganch |= (OP(first) == MBOL
955 ? ROPT_ANCH_MBOL
956 : (OP(first) == SBOL
957 ? ROPT_ANCH_SBOL
958 : ROPT_ANCH_BOL));
a0d0e21e 959 first = NEXTOPER(first);
774d564b
PP
960 goto again;
961 }
962 else if (OP(first) == GPOS) {
963 r->reganch |= ROPT_ANCH_GPOS;
964 first = NEXTOPER(first);
965 goto again;
a0d0e21e
LW
966 }
967 else if ((OP(first) == STAR &&
22c35a8c 968 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
a0d0e21e
LW
969 !(r->reganch & ROPT_ANCH) )
970 {
971 /* turn .* into ^.* with an implied $*=1 */
cad2e5aa
JH
972 int type = OP(NEXTOPER(first));
973
974 if (type == REG_ANY || type == ANYUTF8)
975 type = ROPT_ANCH_MBOL;
976 else
977 type = ROPT_ANCH_SBOL;
978
979 r->reganch |= type | ROPT_IMPLICIT;
a0d0e21e 980 first = NEXTOPER(first);
774d564b 981 goto again;
a0d0e21e 982 }
cad2e5aa
JH
983 if (sawplus && (!sawopen || !PL_regsawback)
984 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
985 /* x+ must match at the 1st pos of run of x's */
986 r->reganch |= ROPT_SKIP;
a0d0e21e 987
c277df42
IZ
988 /* Scan is after the zeroth branch, first is atomic matcher. */
989 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
990 first - scan + 1));
a0d0e21e
LW
991 /*
992 * If there's something expensive in the r.e., find the
993 * longest literal string that must appear and make it the
994 * regmust. Resolve ties in favor of later strings, since
995 * the regstart check works with the beginning of the r.e.
996 * and avoiding duplication strengthens checking. Not a
997 * strong reason, but sufficient in the absence of others.
998 * [Now we resolve ties in favor of the earlier string if
c277df42 999 * it happens that c_offset_min has been invalidated, since the
a0d0e21e
LW
1000 * earlier string may buy us something the later one won't.]
1001 */
a0d0e21e 1002 minlen = 0;
a687059c 1003
79cb57f6
GS
1004 data.longest_fixed = newSVpvn("",0);
1005 data.longest_float = newSVpvn("",0);
1006 data.last_found = newSVpvn("",0);
c277df42
IZ
1007 data.longest = &(data.longest_fixed);
1008 first = scan;
1009
3280af22 1010 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
c277df42 1011 &data, SCF_DO_SUBSTR);
3280af22 1012 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
c277df42 1013 && data.last_start_min == 0 && data.last_end > 0
3280af22
NIS
1014 && !PL_seen_zerolen
1015 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
c277df42
IZ
1016 r->reganch |= ROPT_CHECK_ALL;
1017 scan_commit(&data);
1018 SvREFCNT_dec(data.last_found);
1019
a0ed51b3 1020 longest_float_length = CHR_SVLEN(data.longest_float);
c5254dd6 1021 if (longest_float_length
c277df42
IZ
1022 || (data.flags & SF_FL_BEFORE_EOL
1023 && (!(data.flags & SF_FL_BEFORE_MEOL)
3280af22 1024 || (PL_regflags & PMf_MULTILINE)))) {
cf93c79d
IZ
1025 int t;
1026
a0ed51b3 1027 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
aca2d497
IZ
1028 && data.offset_fixed == data.offset_float_min
1029 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1030 goto remove_float; /* As in (a)+. */
1031
c277df42
IZ
1032 r->float_substr = data.longest_float;
1033 r->float_min_offset = data.offset_float_min;
1034 r->float_max_offset = data.offset_float_max;
cf93c79d
IZ
1035 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1036 && (!(data.flags & SF_FL_BEFORE_MEOL)
1037 || (PL_regflags & PMf_MULTILINE)));
1038 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
a0ed51b3
LW
1039 }
1040 else {
aca2d497 1041 remove_float:
c277df42
IZ
1042 r->float_substr = Nullsv;
1043 SvREFCNT_dec(data.longest_float);
c5254dd6 1044 longest_float_length = 0;
a0d0e21e 1045 }
c277df42 1046
a0ed51b3 1047 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
c5254dd6 1048 if (longest_fixed_length
c277df42
IZ
1049 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1050 && (!(data.flags & SF_FIX_BEFORE_MEOL)
3280af22 1051 || (PL_regflags & PMf_MULTILINE)))) {
cf93c79d
IZ
1052 int t;
1053
c277df42
IZ
1054 r->anchored_substr = data.longest_fixed;
1055 r->anchored_offset = data.offset_fixed;
cf93c79d
IZ
1056 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1057 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1058 || (PL_regflags & PMf_MULTILINE)));
1059 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
a0ed51b3
LW
1060 }
1061 else {
c277df42
IZ
1062 r->anchored_substr = Nullsv;
1063 SvREFCNT_dec(data.longest_fixed);
c5254dd6 1064 longest_fixed_length = 0;
a0d0e21e 1065 }
c277df42
IZ
1066
1067 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
c5254dd6 1068 if (longest_fixed_length > longest_float_length) {
c277df42
IZ
1069 r->check_substr = r->anchored_substr;
1070 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1071 if (r->reganch & ROPT_ANCH_SINGLE)
1072 r->reganch |= ROPT_NOSCAN;
a0ed51b3
LW
1073 }
1074 else {
c277df42
IZ
1075 r->check_substr = r->float_substr;
1076 r->check_offset_min = data.offset_float_min;
1077 r->check_offset_max = data.offset_float_max;
a0d0e21e 1078 }
cad2e5aa
JH
1079 if (r->check_substr) {
1080 r->reganch |= RE_USE_INTUIT;
1081 if (SvTAIL(r->check_substr))
1082 r->reganch |= RE_INTUIT_TAIL;
1083 }
a0ed51b3
LW
1084 }
1085 else {
c277df42
IZ
1086 /* Several toplevels. Best we can is to set minlen. */
1087 I32 fake;
1088
1089 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1090 scan = r->program + 1;
2c2d71f5 1091 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, 0);
c277df42 1092 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
a0d0e21e
LW
1093 }
1094
a0d0e21e 1095 r->minlen = minlen;
3280af22 1096 if (PL_regseen & REG_SEEN_GPOS)
c277df42 1097 r->reganch |= ROPT_GPOS_SEEN;
3280af22 1098 if (PL_regseen & REG_SEEN_LOOKBEHIND)
c277df42 1099 r->reganch |= ROPT_LOOKBEHIND_SEEN;
3280af22 1100 if (PL_regseen & REG_SEEN_EVAL)
ce862d02 1101 r->reganch |= ROPT_EVAL_SEEN;
cf93c79d
IZ
1102 Newz(1002, r->startp, PL_regnpar, I32);
1103 Newz(1002, r->endp, PL_regnpar, I32);
a0d0e21e
LW
1104 DEBUG_r(regdump(r));
1105 return(r);
a687059c
LW
1106}
1107
1108/*
1109 - reg - regular expression, i.e. main body or parenthesized thing
1110 *
1111 * Caller must absorb opening parenthesis.
1112 *
1113 * Combining parenthesis handling with the base level of regular expression
1114 * is a trifle forced, but the need to tie the tails of the branches to what
1115 * follows makes it hard to avoid.
1116 */
76e3520e 1117STATIC regnode *
cea2e8a9 1118S_reg(pTHX_ I32 paren, I32 *flagp)
c277df42 1119 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
a687059c 1120{
5c0ca799 1121 dTHR;
c277df42
IZ
1122 register regnode *ret; /* Will be the head of the group. */
1123 register regnode *br;
1124 register regnode *lastbr;
1125 register regnode *ender = 0;
a0d0e21e 1126 register I32 parno = 0;
3280af22 1127 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
c277df42 1128 char c;
a0d0e21e 1129
821b33a5 1130 *flagp = 0; /* Tentatively. */
a0d0e21e
LW
1131
1132 /* Make an OPEN node, if parenthesized. */
1133 if (paren) {
3280af22 1134 if (*PL_regcomp_parse == '?') {
ca9dfc88
IZ
1135 U16 posflags = 0, negflags = 0;
1136 U16 *flagsp = &posflags;
0f5d15d6 1137 int logical = 0;
ca9dfc88 1138
3280af22
NIS
1139 PL_regcomp_parse++;
1140 paren = *PL_regcomp_parse++;
c277df42 1141 ret = NULL; /* For look-ahead/behind. */
a0d0e21e 1142 switch (paren) {
c277df42 1143 case '<':
3280af22
NIS
1144 PL_regseen |= REG_SEEN_LOOKBEHIND;
1145 if (*PL_regcomp_parse == '!')
c277df42 1146 paren = ',';
3280af22 1147 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
c277df42 1148 goto unknown;
3280af22 1149 PL_regcomp_parse++;
a0d0e21e
LW
1150 case '=':
1151 case '!':
3280af22 1152 PL_seen_zerolen++;
c277df42
IZ
1153 case ':':
1154 case '>':
a0d0e21e
LW
1155 break;
1156 case '$':
1157 case '@':
c277df42 1158 FAIL2("Sequence (?%c...) not implemented", (int)paren);
a0d0e21e
LW
1159 break;
1160 case '#':
3280af22
NIS
1161 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1162 PL_regcomp_parse++;
1163 if (*PL_regcomp_parse != ')')
c277df42 1164 FAIL("Sequence (?#... not terminated");
a0d0e21e
LW
1165 nextchar();
1166 *flagp = TRYAGAIN;
1167 return NULL;
0f5d15d6
IZ
1168 case 'p':
1169 logical = 1;
1170 paren = *PL_regcomp_parse++;
1171 /* FALL THROUGH */
c277df42
IZ
1172 case '{':
1173 {
1174 dTHR;
1175 I32 count = 1, n = 0;
1176 char c;
3280af22 1177 char *s = PL_regcomp_parse;
c277df42
IZ
1178 SV *sv;
1179 OP_4tree *sop, *rop;
1180
3280af22
NIS
1181 PL_seen_zerolen++;
1182 PL_regseen |= REG_SEEN_EVAL;
1183 while (count && (c = *PL_regcomp_parse)) {
1184 if (c == '\\' && PL_regcomp_parse[1])
1185 PL_regcomp_parse++;
c277df42
IZ
1186 else if (c == '{')
1187 count++;
1188 else if (c == '}')
1189 count--;
3280af22 1190 PL_regcomp_parse++;
c277df42 1191 }
3280af22 1192 if (*PL_regcomp_parse != ')')
c277df42
IZ
1193 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1194 if (!SIZE_ONLY) {
1195 AV *av;
1196
3280af22 1197 if (PL_regcomp_parse - 1 - s)
79cb57f6 1198 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
c277df42 1199 else
79cb57f6 1200 sv = newSVpvn("", 0);
c277df42
IZ
1201
1202 rop = sv_compile_2op(sv, &sop, "re", &av);
1203
dfad63ad 1204 n = add_data(3, "nop");
3280af22 1205 PL_regcomp_rx->data->data[n] = (void*)rop;
dfad63ad
HS
1206 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1207 PL_regcomp_rx->data->data[n+2] = (void*)av;
c277df42 1208 SvREFCNT_dec(sv);
a0ed51b3 1209 }
e24b16f9
GS
1210 else { /* First pass */
1211 if (PL_reginterp_cnt < ++PL_seen_evals
1212 && PL_curcop != &PL_compiling)
2cd61cdb
IZ
1213 /* No compiled RE interpolated, has runtime
1214 components ===> unsafe. */
1215 FAIL("Eval-group not allowed at runtime, use re 'eval'");
3280af22 1216 if (PL_tainted)
cc6b7395 1217 FAIL("Eval-group in insecure regular expression");
c277df42
IZ
1218 }
1219
1220 nextchar();
0f5d15d6
IZ
1221 if (logical) {
1222 ret = reg_node(LOGICAL);
1223 if (!SIZE_ONLY)
1224 ret->flags = 2;
1225 regtail(ret, reganode(EVAL, n));
1226 return ret;
1227 }
c277df42
IZ
1228 return reganode(EVAL, n);
1229 }
1230 case '(':
1231 {
3280af22
NIS
1232 if (PL_regcomp_parse[0] == '?') {
1233 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1234 || PL_regcomp_parse[1] == '<'
1235 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
c277df42
IZ
1236 I32 flag;
1237
1238 ret = reg_node(LOGICAL);
0f5d15d6
IZ
1239 if (!SIZE_ONLY)
1240 ret->flags = 1;
c277df42
IZ
1241 regtail(ret, reg(1, &flag));
1242 goto insert_if;
1243 }
a0ed51b3
LW
1244 }
1245 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
3280af22 1246 parno = atoi(PL_regcomp_parse++);
c277df42 1247
3280af22
NIS
1248 while (isDIGIT(*PL_regcomp_parse))
1249 PL_regcomp_parse++;
c277df42
IZ
1250 ret = reganode(GROUPP, parno);
1251 if ((c = *nextchar()) != ')')
1252 FAIL2("Switch (?(number%c not recognized", c);
1253 insert_if:
1254 regtail(ret, reganode(IFTHEN, 0));
1255 br = regbranch(&flags, 1);
1256 if (br == NULL)
1257 br = reganode(LONGJMP, 0);
1258 else
1259 regtail(br, reganode(LONGJMP, 0));
1260 c = *nextchar();
d1b80229
IZ
1261 if (flags&HASWIDTH)
1262 *flagp |= HASWIDTH;
c277df42
IZ
1263 if (c == '|') {
1264 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1265 regbranch(&flags, 1);
1266 regtail(ret, lastbr);
d1b80229
IZ
1267 if (flags&HASWIDTH)
1268 *flagp |= HASWIDTH;
c277df42 1269 c = *nextchar();
a0ed51b3
LW
1270 }
1271 else
c277df42
IZ
1272 lastbr = NULL;
1273 if (c != ')')
1274 FAIL("Switch (?(condition)... contains too many branches");
1275 ender = reg_node(TAIL);
1276 regtail(br, ender);
1277 if (lastbr) {
1278 regtail(lastbr, ender);
1279 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
a0ed51b3
LW
1280 }
1281 else
c277df42
IZ
1282 regtail(ret, ender);
1283 return ret;
a0ed51b3
LW
1284 }
1285 else {
3280af22 1286 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
c277df42
IZ
1287 }
1288 }
1b1626e4 1289 case 0:
c277df42 1290 FAIL("Sequence (? incomplete");
1b1626e4 1291 break;
a0d0e21e 1292 default:
3280af22 1293 --PL_regcomp_parse;
ca9dfc88 1294 parse_flags:
3280af22
NIS
1295 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1296 if (*PL_regcomp_parse != 'o')
1297 pmflag(flagsp, *PL_regcomp_parse);
1298 ++PL_regcomp_parse;
ca9dfc88 1299 }
3280af22 1300 if (*PL_regcomp_parse == '-') {
ca9dfc88 1301 flagsp = &negflags;
3280af22 1302 ++PL_regcomp_parse;
ca9dfc88 1303 goto parse_flags;
48c036b1 1304 }
3280af22
NIS
1305 PL_regflags |= posflags;
1306 PL_regflags &= ~negflags;
1307 if (*PL_regcomp_parse == ':') {
1308 PL_regcomp_parse++;
ca9dfc88
IZ
1309 paren = ':';
1310 break;
1311 }
c277df42 1312 unknown:
3280af22
NIS
1313 if (*PL_regcomp_parse != ')')
1314 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
a0d0e21e
LW
1315 nextchar();
1316 *flagp = TRYAGAIN;
1317 return NULL;
1318 }
1319 }
1320 else {
3280af22
NIS
1321 parno = PL_regnpar;
1322 PL_regnpar++;
a0d0e21e 1323 ret = reganode(OPEN, parno);
c277df42 1324 open = 1;
a0d0e21e 1325 }
a0ed51b3
LW
1326 }
1327 else
a0d0e21e
LW
1328 ret = NULL;
1329
1330 /* Pick up the branches, linking them together. */
c277df42 1331 br = regbranch(&flags, 1);
a0d0e21e
LW
1332 if (br == NULL)
1333 return(NULL);
3280af22
NIS
1334 if (*PL_regcomp_parse == '|') {
1335 if (!SIZE_ONLY && PL_extralen) {
c277df42 1336 reginsert(BRANCHJ, br);
a0ed51b3
LW
1337 }
1338 else
c277df42
IZ
1339 reginsert(BRANCH, br);
1340 have_branch = 1;
1341 if (SIZE_ONLY)
3280af22 1342 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
a0ed51b3
LW
1343 }
1344 else if (paren == ':') {
c277df42
IZ
1345 *flagp |= flags&SIMPLE;
1346 }
1347 if (open) { /* Starts with OPEN. */
1348 regtail(ret, br); /* OPEN -> first. */
a0ed51b3
LW
1349 }
1350 else if (paren != '?') /* Not Conditional */
a0d0e21e 1351 ret = br;
821b33a5
IZ
1352 if (flags&HASWIDTH)
1353 *flagp |= HASWIDTH;
a0d0e21e 1354 *flagp |= flags&SPSTART;
c277df42 1355 lastbr = br;
3280af22
NIS
1356 while (*PL_regcomp_parse == '|') {
1357 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1358 ender = reganode(LONGJMP,0);
1359 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1360 }
1361 if (SIZE_ONLY)
3280af22 1362 PL_extralen += 2; /* Account for LONGJMP. */
a0d0e21e 1363 nextchar();
c277df42 1364 br = regbranch(&flags, 0);
a687059c 1365 if (br == NULL)
a0d0e21e 1366 return(NULL);
c277df42
IZ
1367 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1368 lastbr = br;
821b33a5
IZ
1369 if (flags&HASWIDTH)
1370 *flagp |= HASWIDTH;
a687059c 1371 *flagp |= flags&SPSTART;
a0d0e21e
LW
1372 }
1373
c277df42
IZ
1374 if (have_branch || paren != ':') {
1375 /* Make a closing node, and hook it on the end. */
1376 switch (paren) {
1377 case ':':
1378 ender = reg_node(TAIL);
1379 break;
1380 case 1:
1381 ender = reganode(CLOSE, parno);
1382 break;
1383 case '<':
c277df42
IZ
1384 case ',':
1385 case '=':
1386 case '!':
c277df42 1387 *flagp &= ~HASWIDTH;
821b33a5
IZ
1388 /* FALL THROUGH */
1389 case '>':
1390 ender = reg_node(SUCCEED);
c277df42
IZ
1391 break;
1392 case 0:
1393 ender = reg_node(END);
1394 break;
1395 }
1396 regtail(lastbr, ender);
a0d0e21e 1397
c277df42
IZ
1398 if (have_branch) {
1399 /* Hook the tails of the branches to the closing node. */
1400 for (br = ret; br != NULL; br = regnext(br)) {
1401 regoptail(br, ender);
1402 }
1403 }
a0d0e21e 1404 }
c277df42
IZ
1405
1406 {
1407 char *p;
1408 static char parens[] = "=!<,>";
1409
1410 if (paren && (p = strchr(parens, paren))) {
1411 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1412 int flag = (p - parens) > 1;
1413
1414 if (paren == '>')
1415 node = SUSPEND, flag = 0;
1416 reginsert(node,ret);
c277df42 1417 ret->flags = flag;
c277df42
IZ
1418 regtail(ret, reg_node(TAIL));
1419 }
a0d0e21e
LW
1420 }
1421
1422 /* Check for proper termination. */
ce3e6498
HS
1423 if (paren) {
1424 PL_regflags = oregflags;
1425 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1426 FAIL("unmatched () in regexp");
1427 }
a0ed51b3
LW
1428 }
1429 else if (!paren && PL_regcomp_parse < PL_regxend) {
3280af22 1430 if (*PL_regcomp_parse == ')') {
a0d0e21e 1431 FAIL("unmatched () in regexp");
a0ed51b3
LW
1432 }
1433 else
a0d0e21e
LW
1434 FAIL("junk on end of regexp"); /* "Can't happen". */
1435 /* NOTREACHED */
1436 }
a687059c 1437
a0d0e21e 1438 return(ret);
a687059c
LW
1439}
1440
1441/*
1442 - regbranch - one alternative of an | operator
1443 *
1444 * Implements the concatenation operator.
1445 */
76e3520e 1446STATIC regnode *
cea2e8a9 1447S_regbranch(pTHX_ I32 *flagp, I32 first)
a687059c 1448{
5c0ca799 1449 dTHR;
c277df42
IZ
1450 register regnode *ret;
1451 register regnode *chain = NULL;
1452 register regnode *latest;
1453 I32 flags = 0, c = 0;
a0d0e21e 1454
c277df42
IZ
1455 if (first)
1456 ret = NULL;
1457 else {
3280af22 1458 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1459 ret = reganode(BRANCHJ,0);
1460 else
1461 ret = reg_node(BRANCH);
1462 }
1463
1464 if (!first && SIZE_ONLY)
3280af22 1465 PL_extralen += 1; /* BRANCHJ */
c277df42
IZ
1466
1467 *flagp = WORST; /* Tentatively. */
a0d0e21e 1468
3280af22 1469 PL_regcomp_parse--;
a0d0e21e 1470 nextchar();
3280af22 1471 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
a0d0e21e
LW
1472 flags &= ~TRYAGAIN;
1473 latest = regpiece(&flags);
1474 if (latest == NULL) {
1475 if (flags & TRYAGAIN)
1476 continue;
1477 return(NULL);
a0ed51b3
LW
1478 }
1479 else if (ret == NULL)
c277df42 1480 ret = latest;
a0d0e21e 1481 *flagp |= flags&HASWIDTH;
c277df42 1482 if (chain == NULL) /* First piece. */
a0d0e21e
LW
1483 *flagp |= flags&SPSTART;
1484 else {
3280af22 1485 PL_regnaughty++;
a0d0e21e 1486 regtail(chain, latest);
a687059c 1487 }
a0d0e21e 1488 chain = latest;
c277df42
IZ
1489 c++;
1490 }
1491 if (chain == NULL) { /* Loop ran zero times. */
1492 chain = reg_node(NOTHING);
1493 if (ret == NULL)
1494 ret = chain;
1495 }
1496 if (c == 1) {
1497 *flagp |= flags&SIMPLE;
a0d0e21e 1498 }
a687059c 1499
a0d0e21e 1500 return(ret);
a687059c
LW
1501}
1502
1503/*
1504 - regpiece - something followed by possible [*+?]
1505 *
1506 * Note that the branching code sequences used for ? and the general cases
1507 * of * and + are somewhat optimized: they use the same NOTHING node as
1508 * both the endmarker for their branch list and the body of the last branch.
1509 * It might seem that this node could be dispensed with entirely, but the
1510 * endmarker role is not redundant.
1511 */
76e3520e 1512STATIC regnode *
cea2e8a9 1513S_regpiece(pTHX_ I32 *flagp)
a687059c 1514{
5c0ca799 1515 dTHR;
c277df42 1516 register regnode *ret;
a0d0e21e
LW
1517 register char op;
1518 register char *next;
1519 I32 flags;
3280af22 1520 char *origparse = PL_regcomp_parse;
a0d0e21e
LW
1521 char *maxpos;
1522 I32 min;
c277df42 1523 I32 max = REG_INFTY;
a0d0e21e
LW
1524
1525 ret = regatom(&flags);
1526 if (ret == NULL) {
1527 if (flags & TRYAGAIN)
1528 *flagp |= TRYAGAIN;
1529 return(NULL);
1530 }
1531
3280af22 1532 op = *PL_regcomp_parse;
a0d0e21e 1533
3280af22
NIS
1534 if (op == '{' && regcurly(PL_regcomp_parse)) {
1535 next = PL_regcomp_parse + 1;
a0d0e21e
LW
1536 maxpos = Nullch;
1537 while (isDIGIT(*next) || *next == ',') {
1538 if (*next == ',') {
1539 if (maxpos)
1540 break;
1541 else
1542 maxpos = next;
a687059c 1543 }
a0d0e21e
LW
1544 next++;
1545 }
1546 if (*next == '}') { /* got one */
1547 if (!maxpos)
1548 maxpos = next;
3280af22
NIS
1549 PL_regcomp_parse++;
1550 min = atoi(PL_regcomp_parse);
a0d0e21e
LW
1551 if (*maxpos == ',')
1552 maxpos++;
1553 else
3280af22 1554 maxpos = PL_regcomp_parse;
a0d0e21e
LW
1555 max = atoi(maxpos);
1556 if (!max && *maxpos != '0')
c277df42
IZ
1557 max = REG_INFTY; /* meaning "infinity" */
1558 else if (max >= REG_INFTY)
1559 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
3280af22 1560 PL_regcomp_parse = next;
a0d0e21e
LW
1561 nextchar();
1562
1563 do_curly:
1564 if ((flags&SIMPLE)) {
3280af22 1565 PL_regnaughty += 2 + PL_regnaughty / 2;
a0d0e21e
LW
1566 reginsert(CURLY, ret);
1567 }
1568 else {
2c2d71f5
JH
1569 regnode *w = reg_node(WHILEM);
1570
1571 w->flags = 0;
1572 regtail(ret, w);
3280af22 1573 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1574 reginsert(LONGJMP,ret);
1575 reginsert(NOTHING,ret);
1576 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1577 }
a0d0e21e 1578 reginsert(CURLYX,ret);
3280af22 1579 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1580 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1581 regtail(ret, reg_node(NOTHING));
1582 if (SIZE_ONLY)
2c2d71f5
JH
1583 PL_reg_whilem_seen++, PL_extralen += 3;
1584 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
a0d0e21e 1585 }
c277df42 1586 ret->flags = 0;
a0d0e21e
LW
1587
1588 if (min > 0)
821b33a5
IZ
1589 *flagp = WORST;
1590 if (max > 0)
1591 *flagp |= HASWIDTH;
a0d0e21e 1592 if (max && max < min)
c277df42
IZ
1593 FAIL("Can't do {n,m} with n > m");
1594 if (!SIZE_ONLY) {
1595 ARG1_SET(ret, min);
1596 ARG2_SET(ret, max);
a687059c 1597 }
a687059c 1598
a0d0e21e 1599 goto nest_check;
a687059c 1600 }
a0d0e21e 1601 }
a687059c 1602
a0d0e21e
LW
1603 if (!ISMULT1(op)) {
1604 *flagp = flags;
a687059c 1605 return(ret);
a0d0e21e 1606 }
bb20fd44 1607
c277df42 1608#if 0 /* Now runtime fix should be reliable. */
bb20fd44 1609 if (!(flags&HASWIDTH) && op != '?')
c277df42
IZ
1610 FAIL("regexp *+ operand could be empty");
1611#endif
bb20fd44 1612
a0d0e21e
LW
1613 nextchar();
1614
821b33a5 1615 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
a0d0e21e
LW
1616
1617 if (op == '*' && (flags&SIMPLE)) {
1618 reginsert(STAR, ret);
c277df42 1619 ret->flags = 0;
3280af22 1620 PL_regnaughty += 4;
a0d0e21e
LW
1621 }
1622 else if (op == '*') {
1623 min = 0;
1624 goto do_curly;
a0ed51b3
LW
1625 }
1626 else if (op == '+' && (flags&SIMPLE)) {
a0d0e21e 1627 reginsert(PLUS, ret);
c277df42 1628 ret->flags = 0;
3280af22 1629 PL_regnaughty += 3;
a0d0e21e
LW
1630 }
1631 else if (op == '+') {
1632 min = 1;
1633 goto do_curly;
a0ed51b3
LW
1634 }
1635 else if (op == '?') {
a0d0e21e
LW
1636 min = 0; max = 1;
1637 goto do_curly;
1638 }
1639 nest_check:
17feb5d5 1640 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
cea2e8a9 1641 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
3280af22 1642 PL_regcomp_parse - origparse, origparse);
a0d0e21e
LW
1643 }
1644
3280af22 1645 if (*PL_regcomp_parse == '?') {
a0d0e21e
LW
1646 nextchar();
1647 reginsert(MINMOD, ret);
c277df42 1648 regtail(ret, ret + NODE_STEP_REGNODE);
a0d0e21e 1649 }
3280af22 1650 if (ISMULT2(PL_regcomp_parse))
a0d0e21e
LW
1651 FAIL("nested *?+ in regexp");
1652
1653 return(ret);
a687059c
LW
1654}
1655
1656/*
1657 - regatom - the lowest level
1658 *
1659 * Optimization: gobbles an entire sequence of ordinary characters so that
1660 * it can turn them into a single node, which is smaller to store and
1661 * faster to run. Backslashed characters are exceptions, each becoming a
1662 * separate node; the code is simpler that way and it's not worth fixing.
1663 *
1664 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1665 */
76e3520e 1666STATIC regnode *
cea2e8a9 1667S_regatom(pTHX_ I32 *flagp)
a687059c 1668{
5c0ca799 1669 dTHR;
c277df42 1670 register regnode *ret = 0;
a0d0e21e
LW
1671 I32 flags;
1672
1673 *flagp = WORST; /* Tentatively. */
1674
1675tryagain:
3280af22 1676 switch (*PL_regcomp_parse) {
a0d0e21e 1677 case '^':
3280af22 1678 PL_seen_zerolen++;
a0d0e21e 1679 nextchar();
3280af22 1680 if (PL_regflags & PMf_MULTILINE)
c277df42 1681 ret = reg_node(MBOL);
3280af22 1682 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1683 ret = reg_node(SBOL);
a0d0e21e 1684 else
c277df42 1685 ret = reg_node(BOL);
a0d0e21e
LW
1686 break;
1687 case '$':
3280af22
NIS
1688 if (PL_regcomp_parse[1])
1689 PL_seen_zerolen++;
a0d0e21e 1690 nextchar();
3280af22 1691 if (PL_regflags & PMf_MULTILINE)
c277df42 1692 ret = reg_node(MEOL);
3280af22 1693 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1694 ret = reg_node(SEOL);
a0d0e21e 1695 else
c277df42 1696 ret = reg_node(EOL);
a0d0e21e
LW
1697 break;
1698 case '.':
1699 nextchar();
a0ed51b3
LW
1700 if (UTF) {
1701 if (PL_regflags & PMf_SINGLELINE)
1702 ret = reg_node(SANYUTF8);
1703 else
1704 ret = reg_node(ANYUTF8);
1705 *flagp |= HASWIDTH;
1706 }
1707 else {
1708 if (PL_regflags & PMf_SINGLELINE)
1709 ret = reg_node(SANY);
1710 else
22c35a8c 1711 ret = reg_node(REG_ANY);
a0ed51b3
LW
1712 *flagp |= HASWIDTH|SIMPLE;
1713 }
3280af22 1714 PL_regnaughty++;
a0d0e21e
LW
1715 break;
1716 case '[':
3280af22 1717 PL_regcomp_parse++;
a0ed51b3 1718 ret = (UTF ? regclassutf8() : regclass());
a14b48bc
LW
1719 if (*PL_regcomp_parse != ']')
1720 FAIL("unmatched [] in regexp");
1721 nextchar();
a0d0e21e
LW
1722 *flagp |= HASWIDTH|SIMPLE;
1723 break;
1724 case '(':
1725 nextchar();
1726 ret = reg(1, &flags);
1727 if (ret == NULL) {
1728 if (flags & TRYAGAIN)
1729 goto tryagain;
1730 return(NULL);
1731 }
c277df42 1732 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
a0d0e21e
LW
1733 break;
1734 case '|':
1735 case ')':
1736 if (flags & TRYAGAIN) {
1737 *flagp |= TRYAGAIN;
1738 return NULL;
1739 }
3280af22 1740 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
a0d0e21e
LW
1741 /* Supposed to be caught earlier. */
1742 break;
85afd4ae 1743 case '{':
3280af22
NIS
1744 if (!regcurly(PL_regcomp_parse)) {
1745 PL_regcomp_parse++;
85afd4ae
CS
1746 goto defchar;
1747 }
1748 /* FALL THROUGH */
a0d0e21e
LW
1749 case '?':
1750 case '+':
1751 case '*':
3115e423 1752 FAIL("?+*{} follows nothing in regexp");
a0d0e21e
LW
1753 break;
1754 case '\\':
3280af22 1755 switch (*++PL_regcomp_parse) {
a0d0e21e 1756 case 'A':
3280af22 1757 PL_seen_zerolen++;
c277df42 1758 ret = reg_node(SBOL);
a0d0e21e
LW
1759 *flagp |= SIMPLE;
1760 nextchar();
1761 break;
1762 case 'G':
c277df42 1763 ret = reg_node(GPOS);
3280af22 1764 PL_regseen |= REG_SEEN_GPOS;
a0d0e21e
LW
1765 *flagp |= SIMPLE;
1766 nextchar();
1767 break;
1768 case 'Z':
c277df42 1769 ret = reg_node(SEOL);
a0d0e21e
LW
1770 *flagp |= SIMPLE;
1771 nextchar();
1772 break;
b85d18e9
IZ
1773 case 'z':
1774 ret = reg_node(EOS);
1775 *flagp |= SIMPLE;
3280af22 1776 PL_seen_zerolen++; /* Do not optimize RE away */
b85d18e9
IZ
1777 nextchar();
1778 break;
a0ed51b3
LW
1779 case 'C':
1780 ret = reg_node(SANY);
1781 *flagp |= HASWIDTH|SIMPLE;
1782 nextchar();
1783 break;
1784 case 'X':
1785 ret = reg_node(CLUMP);
1786 *flagp |= HASWIDTH;
1787 nextchar();
1788 if (UTF && !PL_utf8_mark)
dfe13c55 1789 is_utf8_mark((U8*)"~"); /* preload table */
a0ed51b3 1790 break;
a0d0e21e 1791 case 'w':
a0ed51b3
LW
1792 ret = reg_node(
1793 UTF
1794 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1795 : (LOC ? ALNUML : ALNUM));
a0d0e21e
LW
1796 *flagp |= HASWIDTH|SIMPLE;
1797 nextchar();
a0ed51b3 1798 if (UTF && !PL_utf8_alnum)
dfe13c55 1799 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1800 break;
1801 case 'W':
a0ed51b3
LW
1802 ret = reg_node(
1803 UTF
1804 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1805 : (LOC ? NALNUML : NALNUM));
a0d0e21e
LW
1806 *flagp |= HASWIDTH|SIMPLE;
1807 nextchar();
a0ed51b3 1808 if (UTF && !PL_utf8_alnum)
dfe13c55 1809 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1810 break;
1811 case 'b':
3280af22 1812 PL_seen_zerolen++;
f5c9036e 1813 PL_regseen |= REG_SEEN_LOOKBEHIND;
a0ed51b3
LW
1814 ret = reg_node(
1815 UTF
1816 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1817 : (LOC ? BOUNDL : BOUND));
a0d0e21e
LW
1818 *flagp |= SIMPLE;
1819 nextchar();
a0ed51b3 1820 if (UTF && !PL_utf8_alnum)
dfe13c55 1821 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1822 break;
1823 case 'B':
3280af22 1824 PL_seen_zerolen++;
f5c9036e 1825 PL_regseen |= REG_SEEN_LOOKBEHIND;
a0ed51b3
LW
1826 ret = reg_node(
1827 UTF
1828 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1829 : (LOC ? NBOUNDL : NBOUND));
a0d0e21e
LW
1830 *flagp |= SIMPLE;
1831 nextchar();
a0ed51b3 1832 if (UTF && !PL_utf8_alnum)
dfe13c55 1833 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1834 break;
1835 case 's':
a0ed51b3
LW
1836 ret = reg_node(
1837 UTF
1838 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1839 : (LOC ? SPACEL : SPACE));
a0d0e21e
LW
1840 *flagp |= HASWIDTH|SIMPLE;
1841 nextchar();
a0ed51b3 1842 if (UTF && !PL_utf8_space)
dfe13c55 1843 is_utf8_space((U8*)" "); /* preload table */
a0d0e21e
LW
1844 break;
1845 case 'S':
a0ed51b3
LW
1846 ret = reg_node(
1847 UTF
1848 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1849 : (LOC ? NSPACEL : NSPACE));
a0d0e21e
LW
1850 *flagp |= HASWIDTH|SIMPLE;
1851 nextchar();
a0ed51b3 1852 if (UTF && !PL_utf8_space)
dfe13c55 1853 is_utf8_space((U8*)" "); /* preload table */
a0d0e21e
LW
1854 break;
1855 case 'd':
a0ed51b3 1856 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
a0d0e21e
LW
1857 *flagp |= HASWIDTH|SIMPLE;
1858 nextchar();
a0ed51b3 1859 if (UTF && !PL_utf8_digit)
dfe13c55 1860 is_utf8_digit((U8*)"1"); /* preload table */
a0d0e21e
LW
1861 break;
1862 case 'D':
a0ed51b3 1863 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
a0d0e21e
LW
1864 *flagp |= HASWIDTH|SIMPLE;
1865 nextchar();
a0ed51b3 1866 if (UTF && !PL_utf8_digit)
dfe13c55 1867 is_utf8_digit((U8*)"1"); /* preload table */
a0d0e21e 1868 break;
a14b48bc
LW
1869 case 'p':
1870 case 'P':
1871 { /* a lovely hack--pretend we saw [\pX] instead */
1872 char* oldregxend = PL_regxend;
1873
1874 if (PL_regcomp_parse[1] == '{') {
1875 PL_regxend = strchr(PL_regcomp_parse, '}');
1876 if (!PL_regxend)
1877 FAIL("Missing right brace on \\p{}");
1878 PL_regxend++;
1879 }
1880 else
1881 PL_regxend = PL_regcomp_parse + 2;
1882 PL_regcomp_parse--;
1883
1884 ret = regclassutf8();
1885
1886 PL_regxend = oldregxend;
1887 PL_regcomp_parse--;
1888 nextchar();
1889 *flagp |= HASWIDTH|SIMPLE;
1890 }
1891 break;
a0d0e21e
LW
1892 case 'n':
1893 case 'r':
1894 case 't':
1895 case 'f':
1896 case 'e':
1897 case 'a':
1898 case 'x':
1899 case 'c':
1900 case '0':
1901 goto defchar;
1902 case '1': case '2': case '3': case '4':
1903 case '5': case '6': case '7': case '8': case '9':
1904 {
3280af22 1905 I32 num = atoi(PL_regcomp_parse);
a0d0e21e 1906
3280af22 1907 if (num > 9 && num >= PL_regnpar)
a0d0e21e
LW
1908 goto defchar;
1909 else {
3280af22 1910 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
ef64f398 1911 FAIL("reference to nonexistent group");
3280af22 1912 PL_regsawback = 1;
a0ed51b3
LW
1913 ret = reganode(FOLD
1914 ? (LOC ? REFFL : REFF)
c8756f30 1915 : REF, num);
a0d0e21e 1916 *flagp |= HASWIDTH;
3280af22
NIS
1917 while (isDIGIT(*PL_regcomp_parse))
1918 PL_regcomp_parse++;
1919 PL_regcomp_parse--;
a0d0e21e
LW
1920 nextchar();
1921 }
1922 }
1923 break;
1924 case '\0':
3280af22 1925 if (PL_regcomp_parse >= PL_regxend)
a0d0e21e
LW
1926 FAIL("trailing \\ in regexp");
1927 /* FALL THROUGH */
1928 default:
c9f97d15
IZ
1929 /* Do not generate `unrecognized' warnings here, we fall
1930 back into the quick-grab loop below */
a0d0e21e
LW
1931 goto defchar;
1932 }
1933 break;
4633a7c4
LW
1934
1935 case '#':
3280af22
NIS
1936 if (PL_regflags & PMf_EXTENDED) {
1937 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1938 if (PL_regcomp_parse < PL_regxend)
4633a7c4
LW
1939 goto tryagain;
1940 }
1941 /* FALL THROUGH */
1942
a0d0e21e
LW
1943 default: {
1944 register I32 len;
a0ed51b3 1945 register UV ender;
a0d0e21e 1946 register char *p;
c277df42 1947 char *oldp, *s;
a0d0e21e
LW
1948 I32 numlen;
1949
3280af22 1950 PL_regcomp_parse++;
a0d0e21e
LW
1951
1952 defchar:
a0ed51b3
LW
1953 ret = reg_node(FOLD
1954 ? (LOC ? EXACTFL : EXACTF)
bbce6d69 1955 : EXACT);
161b471a 1956 s = (char *) OPERAND(ret);
c277df42 1957 regc(0, s++); /* save spot for len */
3280af22
NIS
1958 for (len = 0, p = PL_regcomp_parse - 1;
1959 len < 127 && p < PL_regxend;
a0d0e21e
LW
1960 len++)
1961 {
1962 oldp = p;
5b5a24f7 1963
3280af22
NIS
1964 if (PL_regflags & PMf_EXTENDED)
1965 p = regwhite(p, PL_regxend);
a0d0e21e
LW
1966 switch (*p) {
1967 case '^':
1968 case '$':
1969 case '.':
1970 case '[':
1971 case '(':
1972 case ')':
1973 case '|':
1974 goto loopdone;
1975 case '\\':
1976 switch (*++p) {
1977 case 'A':
1978 case 'G':
1979 case 'Z':
b85d18e9 1980 case 'z':
a0d0e21e
LW
1981 case 'w':
1982 case 'W':
1983 case 'b':
1984 case 'B':
1985 case 's':
1986 case 'S':
1987 case 'd':
1988 case 'D':
a14b48bc
LW
1989 case 'p':
1990 case 'P':
a0d0e21e
LW
1991 --p;
1992 goto loopdone;
1993 case 'n':
1994 ender = '\n';
1995 p++;
a687059c 1996 break;
a0d0e21e
LW
1997 case 'r':
1998 ender = '\r';
1999 p++;
a687059c 2000 break;
a0d0e21e
LW
2001 case 't':
2002 ender = '\t';
2003 p++;
a687059c 2004 break;
a0d0e21e
LW
2005 case 'f':
2006 ender = '\f';
2007 p++;
a687059c 2008 break;
a0d0e21e
LW
2009 case 'e':
2010 ender = '\033';
2011 p++;
a687059c 2012 break;
a0d0e21e
LW
2013 case 'a':
2014 ender = '\007';
2015 p++;
a687059c 2016 break;
a0d0e21e 2017 case 'x':
a0ed51b3
LW
2018 if (*++p == '{') {
2019 char* e = strchr(p, '}');
2020
2021 if (!e)
2022 FAIL("Missing right brace on \\x{}");
2023 else if (UTF) {
2024 ender = scan_hex(p + 1, e - p, &numlen);
2025 if (numlen + len >= 127) { /* numlen is generous */
2026 p--;
2027 goto loopdone;
2028 }
2029 p = e + 1;
2030 }
2031 else
2032 FAIL("Can't use \\x{} without 'use utf8' declaration");
2033 }
2034 else {
2035 ender = scan_hex(p, 2, &numlen);
2036 p += numlen;
2037 }
a687059c 2038 break;
a0d0e21e
LW
2039 case 'c':
2040 p++;
bbce6d69
PP
2041 ender = UCHARAT(p++);
2042 ender = toCTRL(ender);
a687059c 2043 break;
a0d0e21e
LW
2044 case '0': case '1': case '2': case '3':case '4':
2045 case '5': case '6': case '7': case '8':case '9':
2046 if (*p == '0' ||
3280af22 2047 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
a0d0e21e
LW
2048 ender = scan_oct(p, 3, &numlen);
2049 p += numlen;
2050 }
2051 else {
2052 --p;
2053 goto loopdone;
a687059c
LW
2054 }
2055 break;
a0d0e21e 2056 case '\0':
3280af22 2057 if (p >= PL_regxend)
a687059c
LW
2058 FAIL("trailing \\ in regexp");
2059 /* FALL THROUGH */
a0d0e21e 2060 default:
c9f97d15 2061 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
cea2e8a9 2062 Perl_warner(aTHX_ WARN_UNSAFE,
c9f97d15
IZ
2063 "/%.127s/: Unrecognized escape \\%c passed through",
2064 PL_regprecomp,
2065 *p);
a0ed51b3 2066 goto normal_default;
a0d0e21e
LW
2067 }
2068 break;
a687059c 2069 default:
a0ed51b3
LW
2070 normal_default:
2071 if ((*p & 0xc0) == 0xc0 && UTF) {
dfe13c55 2072 ender = utf8_to_uv((U8*)p, &numlen);
a0ed51b3
LW
2073 p += numlen;
2074 }
2075 else
2076 ender = *p++;
a0d0e21e 2077 break;
a687059c 2078 }
3280af22
NIS
2079 if (PL_regflags & PMf_EXTENDED)
2080 p = regwhite(p, PL_regxend);
a0ed51b3
LW
2081 if (UTF && FOLD) {
2082 if (LOC)
2083 ender = toLOWER_LC_uni(ender);
2084 else
2085 ender = toLOWER_uni(ender);
2086 }
a0d0e21e
LW
2087 if (ISMULT2(p)) { /* Back off on ?+*. */
2088 if (len)
2089 p = oldp;
a0ed51b3
LW
2090 else if (ender >= 0x80 && UTF) {
2091 reguni(ender, s, &numlen);
2092 s += numlen;
2093 len += numlen;
2094 }
a0d0e21e
LW
2095 else {
2096 len++;
c277df42 2097 regc(ender, s++);
a0d0e21e
LW
2098 }
2099 break;
a687059c 2100 }
a0ed51b3
LW
2101 if (ender >= 0x80 && UTF) {
2102 reguni(ender, s, &numlen);
2103 s += numlen;
2104 len += numlen - 1;
2105 }
2106 else
2107 regc(ender, s++);
a0d0e21e
LW
2108 }
2109 loopdone:
3280af22 2110 PL_regcomp_parse = p - 1;
a0d0e21e
LW
2111 nextchar();
2112 if (len < 0)
2113 FAIL("internal disaster in regexp");
2114 if (len > 0)
2115 *flagp |= HASWIDTH;
2116 if (len == 1)
2117 *flagp |= SIMPLE;
c277df42 2118 if (!SIZE_ONLY)
a0d0e21e 2119 *OPERAND(ret) = len;
c277df42
IZ
2120 regc('\0', s++);
2121 if (SIZE_ONLY) {
3280af22 2122 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
a0ed51b3
LW
2123 }
2124 else {
3280af22 2125 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
c277df42 2126 }
a687059c 2127 }
a0d0e21e
LW
2128 break;
2129 }
a687059c 2130
a0d0e21e 2131 return(ret);
a687059c
LW
2132}
2133
873ef191 2134STATIC char *
cea2e8a9 2135S_regwhite(pTHX_ char *p, char *e)
5b5a24f7
CS
2136{
2137 while (p < e) {
2138 if (isSPACE(*p))
2139 ++p;
2140 else if (*p == '#') {
2141 do {
2142 p++;
2143 } while (p < e && *p != '\n');
2144 }
2145 else
2146 break;
2147 }
2148 return p;
2149}
2150
b8c5462f
JH
2151/* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2152 Character classes ([:foo:]) can also be negated ([:^foo:]).
2153 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2154 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2155 but trigger warnings because they are currently unimplemented. */
2156STATIC I32
cea2e8a9 2157S_regpposixcc(pTHX_ I32 value)
620e46c5 2158{
11b8faa4 2159 dTHR;
620e46c5 2160 char *posixcc = 0;
b8c5462f 2161 I32 namedclass = -1;
620e46c5
JH
2162
2163 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2164 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2165 (*PL_regcomp_parse == ':' ||
2166 *PL_regcomp_parse == '=' ||
2167 *PL_regcomp_parse == '.')) {
2168 char c = *PL_regcomp_parse;
2169 char* s = PL_regcomp_parse++;
2170
2171 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2172 PL_regcomp_parse++;
2173 if (PL_regcomp_parse == PL_regxend)
2174 /* Grandfather lone [:, [=, [. */
2175 PL_regcomp_parse = s;
2176 else {
b8c5462f
JH
2177 char* t = PL_regcomp_parse++; /* skip over the c */
2178
2179 if (*PL_regcomp_parse == ']') {
2180 PL_regcomp_parse++; /* skip over the ending ] */
2181 posixcc = s + 1;
2182 if (*s == ':') {
2183 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2184 I32 skip = 5; /* the most common skip */
2185
2186 switch (*posixcc) {
2187 case 'a':
2188 if (strnEQ(posixcc, "alnum", 5))
2189 namedclass =
2190 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2191 else if (strnEQ(posixcc, "alpha", 5))
2192 namedclass =
2193 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2194 else if (strnEQ(posixcc, "ascii", 5))
2195 namedclass =
2196 complement ? ANYOF_NASCII : ANYOF_ASCII;
2197 break;
2198 case 'c':
2199 if (strnEQ(posixcc, "cntrl", 5))
2200 namedclass =
2201 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2202 break;
2203 case 'd':
2204 if (strnEQ(posixcc, "digit", 5))
2205 namedclass =
2206 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2207 break;
2208 case 'g':
2209 if (strnEQ(posixcc, "graph", 5))
2210 namedclass =
2211 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2212 break;
2213 case 'l':
2214 if (strnEQ(posixcc, "lower", 5))
2215 namedclass =
2216 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2217 break;
2218 case 'p':
2219 if (strnEQ(posixcc, "print", 5))
2220 namedclass =
2221 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2222 else if (strnEQ(posixcc, "punct", 5))
2223 namedclass =
2224 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2225 break;
2226 case 's':
2227 if (strnEQ(posixcc, "space", 5))
2228 namedclass =
2229 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2230 case 'u':
2231 if (strnEQ(posixcc, "upper", 5))
2232 namedclass =
2233 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2234 break;
2235 case 'w': /* this is not POSIX, this is the Perl \w */
2236 if (strnEQ(posixcc, "word", 4)) {
2237 namedclass =
2238 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2239 skip = 4;
2240 }
2241 break;
2242 case 'x':
2243 if (strnEQ(posixcc, "xdigit", 6)) {
2244 namedclass =
2245 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2246 skip = 6;
2247 }
2248 break;
2249 }
2250 if ((namedclass == -1 ||
2251 !(posixcc + skip + 2 < PL_regxend &&
2252 (posixcc[skip] == ':' &&
2253 posixcc[skip + 1] == ']'))))
2254 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2255 t - s - 1, s + 1);
2256 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2257 /* [[=foo=]] and [[.foo.]] are still future. */
cea2e8a9 2258 Perl_warner(aTHX_ WARN_UNSAFE,
b8c5462f
JH
2259 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2260 } else {
2261 /* Maternal grandfather:
2262 * "[:" ending in ":" but not in ":]" */
767d463e
HS
2263 PL_regcomp_parse = s;
2264 }
620e46c5
JH
2265 }
2266 }
2267
b8c5462f
JH
2268 return namedclass;
2269}
2270
2271STATIC void
2272S_checkposixcc(pTHX)
2273{
2274 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2275 (*PL_regcomp_parse == ':' ||
2276 *PL_regcomp_parse == '=' ||
2277 *PL_regcomp_parse == '.')) {
2278 char *s = PL_regcomp_parse;
2279 char c = *s++;
2280
2281 while(*s && isALNUM(*s))
2282 s++;
2283 if (*s && c == *s && s[1] == ']') {
2284 Perl_warner(aTHX_ WARN_UNSAFE,
2285 "Character class syntax [%c %c] belongs inside character classes", c, c);
2286 if (c == '=' || c == '.')
2287 Perl_warner(aTHX_ WARN_UNSAFE,
2288 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2289 }
2290 }
620e46c5
JH
2291}
2292
76e3520e 2293STATIC regnode *
cea2e8a9 2294S_regclass(pTHX)
a687059c 2295{
5c0ca799 2296 dTHR;
c277df42 2297 register char *opnd, *s;
a0ed51b3 2298 register I32 value;
b8c5462f 2299 register I32 lastvalue = OOB_CHAR8;
a0d0e21e 2300 register I32 range = 0;
c277df42 2301 register regnode *ret;
a0d0e21e
LW
2302 register I32 def;
2303 I32 numlen;
b8c5462f 2304 I32 namedclass;
a0d0e21e 2305
3280af22 2306 s = opnd = (char *) OPERAND(PL_regcode);
c277df42 2307 ret = reg_node(ANYOF);
b8c5462f 2308 for (value = 0; value < ANYOF_SIZE; value++)
c277df42 2309 regc(0, s++);
3280af22
NIS
2310 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2311 PL_regnaughty++;
2312 PL_regcomp_parse++;
c277df42 2313 if (!SIZE_ONLY)
b8c5462f 2314 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
bbce6d69 2315 }
c277df42 2316 if (!SIZE_ONLY) {
3280af22 2317 PL_regcode += ANY_SKIP;
a0ed51b3 2318 if (FOLD)
b8c5462f 2319 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
a0ed51b3 2320 if (LOC)
b8c5462f 2321 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
a0ed51b3
LW
2322 }
2323 else {
3280af22 2324 PL_regsize += ANY_SKIP;
a0d0e21e 2325 }
b8c5462f
JH
2326
2327 checkposixcc();
2328
3280af22 2329 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
a0d0e21e 2330 goto skipcond; /* allow 1st char to be ] or - */
3280af22 2331 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
a0d0e21e 2332 skipcond:
b8c5462f 2333 namedclass = -1;
a0ed51b3 2334 value = UCHARAT(PL_regcomp_parse++);
620e46c5 2335 if (value == '[')
b8c5462f 2336 namedclass = regpposixcc(value);
620e46c5 2337 else if (value == '\\') {
a0ed51b3
LW
2338 value = UCHARAT(PL_regcomp_parse++);
2339 switch (value) {
b8c5462f
JH
2340 case 'w': namedclass = ANYOF_ALNUM; break;
2341 case 'W': namedclass = ANYOF_NALNUM; break;
2342 case 's': namedclass = ANYOF_SPACE; break;
2343 case 'S': namedclass = ANYOF_NSPACE; break;
2344 case 'd': namedclass = ANYOF_DIGIT; break;
2345 case 'D': namedclass = ANYOF_NDIGIT; break;
2346 case 'n': value = '\n'; break;
2347 case 'r': value = '\r'; break;
2348 case 't': value = '\t'; break;
2349 case 'f': value = '\f'; break;
2350 case 'b': value = '\b'; break;
2351 case 'e': value = '\033'; break;
2352 case 'a': value = '\007'; break;
2353 case 'x':
2354 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2355 PL_regcomp_parse += numlen;
2356 break;
2357 case 'c':
2358 value = UCHARAT(PL_regcomp_parse++);
2359 value = toCTRL(value);
2360 break;
2361 case '0': case '1': case '2': case '3': case '4':
2362 case '5': case '6': case '7': case '8': case '9':
2363 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2364 PL_regcomp_parse += numlen;
2365 break;
2366 }
2367 }
2368 if (!SIZE_ONLY && namedclass > -1) {
2369 switch (namedclass) {
2370 case ANYOF_ALNUM:
2371 if (LOC)
2372 ANYOF_CLASS_SET(opnd, ANYOF_ALNUM);
2373 else {
2374 for (value = 0; value < 256; value++)
2375 if (isALNUM(value))
2376 ANYOF_BITMAP_SET(opnd, value);
bbce6d69 2377 }
b8c5462f
JH
2378 break;
2379 case ANYOF_NALNUM:
2380 if (LOC)
2381 ANYOF_CLASS_SET(opnd, ANYOF_NALNUM);
2382 else {
2383 for (value = 0; value < 256; value++)
2384 if (!isALNUM(value))
2385 ANYOF_BITMAP_SET(opnd, value);
bbce6d69 2386 }
b8c5462f
JH
2387 break;
2388 case ANYOF_SPACE:
2389 if (LOC)
2390 ANYOF_CLASS_SET(opnd, ANYOF_SPACE);
2391 else {
2392 for (value = 0; value < 256; value++)
2393 if (isSPACE(value))
2394 ANYOF_BITMAP_SET(opnd, value);
bbce6d69 2395 }
b8c5462f
JH
2396 break;
2397 case ANYOF_NSPACE:
2398 if (LOC)
2399 ANYOF_CLASS_SET(opnd, ANYOF_NSPACE);
2400 else {
2401 for (value = 0; value < 256; value++)
2402 if (!isSPACE(value))
2403 ANYOF_BITMAP_SET(opnd, value);
bbce6d69 2404 }
b8c5462f
JH
2405 break;
2406 case ANYOF_DIGIT:
2407 if (LOC)
2408 ANYOF_CLASS_SET(opnd, ANYOF_DIGIT);
2409 else {
a0ed51b3 2410 for (value = '0'; value <= '9'; value++)
b8c5462f 2411 ANYOF_BITMAP_SET(opnd, value);
ae5c130c 2412 }
b8c5462f
JH
2413 break;
2414 case ANYOF_NDIGIT:
2415 if (LOC)
2416 ANYOF_CLASS_SET(opnd, ANYOF_NDIGIT);
2417 else {
a0ed51b3 2418 for (value = 0; value < '0'; value++)
b8c5462f 2419 ANYOF_BITMAP_SET(opnd, value);
a0ed51b3 2420 for (value = '9' + 1; value < 256; value++)
b8c5462f
JH
2421 ANYOF_BITMAP_SET(opnd, value);
2422 }
2423 break;
2424 case ANYOF_NALNUMC:
2425 if (LOC)
2426 ANYOF_CLASS_SET(opnd, ANYOF_NALNUMC);
2427 else {
2428 for (value = 0; value < 256; value++)
2429 if (!isALNUMC(value))
2430 ANYOF_BITMAP_SET(opnd, value);
2431 }
2432 break;
2433 case ANYOF_ALNUMC:
2434 if (LOC)
2435 ANYOF_CLASS_SET(opnd, ANYOF_ALNUMC);
2436 else {
2437 for (value = 0; value < 256; value++)
2438 if (isALNUMC(value))
2439 ANYOF_BITMAP_SET(opnd, value);
2440 }
2441 break;
2442 case ANYOF_ALPHA:
2443 if (LOC)
2444 ANYOF_CLASS_SET(opnd, ANYOF_ALPHA);
2445 else {
2446 for (value = 0; value < 256; value++)
2447 if (isALPHA(value))
2448 ANYOF_BITMAP_SET(opnd, value);
ae5c130c 2449 }
a0d0e21e 2450 break;
b8c5462f
JH
2451 case ANYOF_NALPHA:
2452 if (LOC)
2453 ANYOF_CLASS_SET(opnd, ANYOF_NALPHA);
2454 else {
2455 for (value = 0; value < 256; value++)
2456 if (!isALPHA(value))
2457 ANYOF_BITMAP_SET(opnd, value);
2458 }
a0d0e21e 2459 break;
b8c5462f
JH
2460 case ANYOF_ASCII:
2461 if (LOC)
2462 ANYOF_CLASS_SET(opnd, ANYOF_ASCII);
2463 else {
2464 for (value = 0; value < 128; value++)
2465 ANYOF_BITMAP_SET(opnd, value);
2466 }
a0d0e21e 2467 break;
b8c5462f
JH
2468 case ANYOF_NASCII:
2469 if (LOC)
2470 ANYOF_CLASS_SET(opnd, ANYOF_NASCII);
2471 else {
2472 for (value = 128; value < 256; value++)
2473 ANYOF_BITMAP_SET(opnd, value);
2474 }
a0d0e21e 2475 break;
b8c5462f
JH
2476 case ANYOF_CNTRL:
2477 if (LOC)
2478 ANYOF_CLASS_SET(opnd, ANYOF_CNTRL);
2479 else {
2480 for (value = 0; value < 256; value++)
2481 if (isCNTRL(value))
2482 ANYOF_BITMAP_SET(opnd, value);
2483 }
2484 lastvalue = OOB_CHAR8;
a0d0e21e 2485 break;
b8c5462f
JH
2486 case ANYOF_NCNTRL:
2487 if (LOC)
2488 ANYOF_CLASS_SET(opnd, ANYOF_NCNTRL);
2489 else {
2490 for (value = 0; value < 256; value++)
2491 if (!isCNTRL(value))
2492 ANYOF_BITMAP_SET(opnd, value);
2493 }
a0d0e21e 2494 break;
b8c5462f
JH
2495 case ANYOF_GRAPH:
2496 if (LOC)
2497 ANYOF_CLASS_SET(opnd, ANYOF_GRAPH);
2498 else {
2499 for (value = 0; value < 256; value++)
2500 if (isGRAPH(value))
2501 ANYOF_BITMAP_SET(opnd, value);
2502 }
a0d0e21e 2503 break;
b8c5462f
JH
2504 case ANYOF_NGRAPH:
2505 if (LOC)
2506 ANYOF_CLASS_SET(opnd, ANYOF_NGRAPH);
2507 else {
2508 for (value = 0; value < 256; value++)
2509 if (!isGRAPH(value))
2510 ANYOF_BITMAP_SET(opnd, value);
2511 }
a0d0e21e 2512 break;
b8c5462f
JH
2513 case ANYOF_LOWER:
2514 if (LOC)
2515 ANYOF_CLASS_SET(opnd, ANYOF_LOWER);
2516 else {
2517 for (value = 0; value < 256; value++)
2518 if (isLOWER(value))
2519 ANYOF_BITMAP_SET(opnd, value);
2520 }
a0d0e21e 2521 break;
b8c5462f
JH
2522 case ANYOF_NLOWER:
2523 if (LOC)
2524 ANYOF_CLASS_SET(opnd, ANYOF_NLOWER);
2525 else {
2526 for (value = 0; value < 256; value++)
2527 if (!isLOWER(value))
2528 ANYOF_BITMAP_SET(opnd, value);
2529 }
2530 break;
2531 case ANYOF_PRINT:
2532 if (LOC)
2533 ANYOF_CLASS_SET(opnd, ANYOF_PRINT);
2534 else {
2535 for (value = 0; value < 256; value++)
2536 if (isPRINT(value))
2537 ANYOF_BITMAP_SET(opnd, value);
2538 }
2539 break;
2540 case ANYOF_NPRINT:
2541 if (LOC)
2542 ANYOF_CLASS_SET(opnd, ANYOF_NPRINT);
2543 else {
2544 for (value = 0; value < 256; value++)
2545 if (!isPRINT(value))
2546 ANYOF_BITMAP_SET(opnd, value);
2547 }
2548 break;
2549 case ANYOF_PUNCT:
2550 if (LOC)
2551 ANYOF_CLASS_SET(opnd, ANYOF_PUNCT);
2552 else {
2553 for (value = 0; value < 256; value++)
2554 if (isPUNCT(value))
2555 ANYOF_BITMAP_SET(opnd, value);
2556 }
2557 break;
2558 case ANYOF_NPUNCT:
2559 if (LOC)
2560 ANYOF_CLASS_SET(opnd, ANYOF_NPUNCT);
2561 else {
2562 for (value = 0; value < 256; value++)
2563 if (!isPUNCT(value))
2564 ANYOF_BITMAP_SET(opnd, value);
2565 }
2566 break;
2567 case ANYOF_UPPER:
2568 if (LOC)
2569 ANYOF_CLASS_SET(opnd, ANYOF_UPPER);
2570 else {
2571 for (value = 0; value < 256; value++)
2572 if (isUPPER(value))
2573 ANYOF_BITMAP_SET(opnd, value);
2574 }
2575 break;
2576 case ANYOF_NUPPER:
2577 if (LOC)
2578 ANYOF_CLASS_SET(opnd, ANYOF_NUPPER);
2579 else {
2580 for (value = 0; value < 256; value++)
2581 if (!isUPPER(value))
2582 ANYOF_BITMAP_SET(opnd, value);
2583 }
2584 break;
2585 case ANYOF_XDIGIT:
2586 if (LOC)
2587 ANYOF_CLASS_SET(opnd, ANYOF_XDIGIT);
2588 else {
2589 for (value = 0; value < 256; value++)
2590 if (isXDIGIT(value))
2591 ANYOF_BITMAP_SET(opnd, value);
2592 }
2593 break;
2594 case ANYOF_NXDIGIT:
2595 if (LOC)
2596 ANYOF_CLASS_SET(opnd, ANYOF_NXDIGIT);
2597 else {
2598 for (value = 0; value < 256; value++)
2599 if (!isXDIGIT(value))
2600 ANYOF_BITMAP_SET(opnd, value);
2601 }
2602 break;
2603 default:
2604 FAIL("invalid [::] class in regexp");
a0d0e21e
LW
2605 break;
2606 }
b8c5462f
JH
2607 if (LOC)
2608 ANYOF_FLAGS(opnd) |= ANYOF_CLASS;
2609 lastvalue = OOB_CHAR8;
a0d0e21e 2610 }
b8c5462f 2611 else
a0d0e21e 2612 if (range) {
a0ed51b3 2613 if (lastvalue > value)
a0d0e21e
LW
2614 FAIL("invalid [] range in regexp");
2615 range = 0;
2616 }
2617 else {
a0ed51b3 2618 lastvalue = value;
3280af22
NIS
2619 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2620 PL_regcomp_parse[1] != ']') {
2621 PL_regcomp_parse++;
a0d0e21e
LW
2622 range = 1;
2623 continue; /* do it next time */
2624 }
a687059c 2625 }
ae5c130c 2626 if (!SIZE_ONLY) {
8ada0baa
JH
2627#ifndef ASCIIish
2628 if ((isLOWER(lastvalue) && isLOWER(value)) ||
c8dba6f3
GS
2629 (isUPPER(lastvalue) && isUPPER(value)))
2630 {
2631 I32 i;
8ada0baa
JH
2632 if (isLOWER(lastvalue)) {
2633 for (i = lastvalue; i <= value; i++)
2634 if (isLOWER(i))
b8c5462f 2635 ANYOF_BITMAP_SET(opnd, i);
8ada0baa
JH
2636 } else {
2637 for (i = lastvalue; i <= value; i++)
2638 if (isUPPER(i))
b8c5462f 2639 ANYOF_BITMAP_SET(opnd, i);
8ada0baa
JH
2640 }
2641 }
2642 else
2643#endif
2644 for ( ; lastvalue <= value; lastvalue++)
b8c5462f 2645 ANYOF_BITMAP_SET(opnd, lastvalue);
8ada0baa 2646 }
a0ed51b3 2647 lastvalue = value;
a0d0e21e 2648 }
ae5c130c 2649 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
b8c5462f
JH
2650 if (!SIZE_ONLY &&
2651 (ANYOF_FLAGS(opnd) & (ANYOF_FLAGS_ALL ^ ANYOF_INVERT)) == ANYOF_FOLD) {
a0ed51b3 2652 for (value = 0; value < 256; ++value) {
b8c5462f 2653 if (ANYOF_BITMAP_TEST(opnd, value)) {
22c35a8c 2654 I32 cf = PL_fold[value];
b8c5462f 2655 ANYOF_BITMAP_SET(opnd, cf);
ae5c130c
GS
2656 }
2657 }
b8c5462f 2658 ANYOF_FLAGS(opnd) &= ~ANYOF_FOLD;
ae5c130c
GS
2659 }
2660 /* optimize inverted simple patterns (e.g. [^a-z]) */
b8c5462f
JH
2661 if (!SIZE_ONLY && (ANYOF_FLAGS(opnd) & ANYOF_FLAGS_ALL) == ANYOF_INVERT) {
2662 for (value = 0; value < ANYOF_BITMAP_SIZE; ++value)
2663 opnd[ANYOF_BITMAP_OFFSET + value] ^= ANYOF_FLAGS_ALL;
2664 ANYOF_FLAGS(opnd) = 0;
ae5c130c 2665 }
a0d0e21e
LW
2666 return ret;
2667}
2668
a0ed51b3 2669STATIC regnode *
cea2e8a9 2670S_regclassutf8(pTHX)
a0ed51b3 2671{
b8c5462f 2672 dTHR;
a0ed51b3
LW
2673 register char *opnd, *e;
2674 register U32 value;
b8c5462f 2675 register U32 lastvalue = OOB_UTF8;
a0ed51b3
LW
2676 register I32 range = 0;
2677 register regnode *ret;
2678 I32 numlen;
2679 I32 n;
2680 SV *listsv;
2681 U8 flags = 0;
b8c5462f 2682 I32 namedclass;
a0ed51b3
LW
2683
2684 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2685 PL_regnaughty++;
2686 PL_regcomp_parse++;
2687 if (!SIZE_ONLY)
2688 flags |= ANYOF_INVERT;
2689 }
2690 if (!SIZE_ONLY) {
2691 if (FOLD)
2692 flags |= ANYOF_FOLD;
2693 if (LOC)
2694 flags |= ANYOF_LOCALE;
79cb57f6 2695 listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2696 }
2697
b8c5462f
JH
2698 checkposixcc();
2699
a0ed51b3
LW
2700 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2701 goto skipcond; /* allow 1st char to be ] or - */
2702
2703 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2704 skipcond:
b8c5462f 2705 namedclass = -1;
dfe13c55 2706 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
a0ed51b3
LW
2707 PL_regcomp_parse += numlen;
2708
620e46c5 2709 if (value == '[')
b8c5462f 2710 namedclass = regpposixcc(value);
620e46c5 2711 else if (value == '\\') {
dfe13c55 2712 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
a0ed51b3
LW
2713 PL_regcomp_parse += numlen;
2714 switch (value) {
b8c5462f
JH
2715 case 'w': namedclass = ANYOF_ALNUM; break;
2716 case 'W': namedclass = ANYOF_NALNUM; break;
2717 case 's': namedclass = ANYOF_SPACE; break;
2718 case 'S': namedclass = ANYOF_NSPACE; break;
2719 case 'd': namedclass = ANYOF_DIGIT; break;
2720 case 'D': namedclass = ANYOF_NDIGIT; break;
a0ed51b3
LW
2721 case 'p':
2722 case 'P':
2723 if (*PL_regcomp_parse == '{') {
2724 e = strchr(PL_regcomp_parse++, '}');
2725 if (!e)
2726 FAIL("Missing right brace on \\p{}");
2727 n = e - PL_regcomp_parse;
2728 }
2729 else {
2730 e = PL_regcomp_parse;
2731 n = 1;
2732 }
2733 if (!SIZE_ONLY) {
2734 if (value == 'p')
b8c5462f
JH
2735 Perl_sv_catpvf(aTHX_ listsv,
2736 "+utf8::%.*s\n", n, PL_regcomp_parse);
a0ed51b3 2737 else
cea2e8a9 2738 Perl_sv_catpvf(aTHX_ listsv,
b8c5462f 2739 "!utf8::%.*s\n", n, PL_regcomp_parse);
a0ed51b3
LW
2740 }
2741 PL_regcomp_parse = e + 1;
b8c5462f 2742 lastvalue = OOB_UTF8;
a0ed51b3 2743 continue;
b8c5462f
JH
2744 case 'n': value = '\n'; break;
2745 case 'r': value = '\r'; break;
2746 case 't': value = '\t'; break;
2747 case 'f': value = '\f'; break;
2748 case 'b': value = '\b'; break;
2749 case 'e': value = '\033'; break;
2750 case 'a': value = '\007'; break;
a0ed51b3
LW
2751 case 'x':
2752 if (*PL_regcomp_parse == '{') {
2753 e = strchr(PL_regcomp_parse++, '}');
2754 if (!e)
2755 FAIL("Missing right brace on \\x{}");
b8c5462f
JH
2756 value = scan_hex(PL_regcomp_parse,
2757 e - PL_regcomp_parse,
2758 &numlen);
a0ed51b3
LW
2759 PL_regcomp_parse = e + 1;
2760 }
2761 else {
2762 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2763 PL_regcomp_parse += numlen;
2764 }
2765 break;
2766 case 'c':
2767 value = UCHARAT(PL_regcomp_parse++);
2768 value = toCTRL(value);
2769 break;
2770 case '0': case '1': case '2': case '3': case '4':
2771 case '5': case '6': case '7': case '8': case '9':
2772 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2773 PL_regcomp_parse += numlen;
2774 break;
2775 }
2776 }
b8c5462f
JH
2777 if (!SIZE_ONLY && namedclass > -1) {
2778 switch (namedclass) {
2779 case ANYOF_ALNUM:
2780 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2781 case ANYOF_NALNUM:
2782 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2783 case ANYOF_ALNUMC:
2784 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2785 case ANYOF_NALNUMC:
2786 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2787 case ANYOF_ALPHA:
2788 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2789 case ANYOF_NALPHA:
2790 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2791 case ANYOF_ASCII:
2792 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2793 case ANYOF_NASCII:
2794 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2795 case ANYOF_CNTRL:
2796 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2797 case ANYOF_NCNTRL:
2798 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2799 case ANYOF_GRAPH:
2800 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2801 case ANYOF_NGRAPH:
2802 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2803 case ANYOF_DIGIT:
2804 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2805 case ANYOF_NDIGIT:
2806 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2807 case ANYOF_LOWER:
2808 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2809 case ANYOF_NLOWER:
2810 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2811 case ANYOF_PRINT:
2812 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2813 case ANYOF_NPRINT:
2814 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2815 case ANYOF_PUNCT:
2816 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2817 case ANYOF_NPUNCT:
2818 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2819 case ANYOF_SPACE:
2820 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2821 case ANYOF_NSPACE:
2822 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2823 case ANYOF_UPPER:
2824 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2825 case ANYOF_NUPPER:
2826 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2827 case ANYOF_XDIGIT:
2828 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2829 case ANYOF_NXDIGIT:
2830 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2831 }
2832 }
2833 else
2834 if (range) {
a0ed51b3
LW
2835 if (lastvalue > value)
2836 FAIL("invalid [] range in regexp");
f478a152 2837#ifdef UV_IS_QUAD
b9c39e73 2838 if (!SIZE_ONLY)
f478a152
JH
2839 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\t%04" PERL_PRIx64 "\n", (UV)lastvalue, (UV)value);
2840#else
b9c39e73 2841 if (!SIZE_ONLY)
f478a152
JH
2842 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\n", lastvalue, value);
2843#endif
a0ed51b3
LW
2844 lastvalue = value;
2845 range = 0;
2846 }
2847 else {
2848 lastvalue = value;
2849 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2850 PL_regcomp_parse[1] != ']') {
2851 PL_regcomp_parse++;
2852 range = 1;
2853 continue; /* do it next time */
2854 }
f478a152 2855#ifdef UV_IS_QUAD
b9c39e73 2856 if (!SIZE_ONLY)
f478a152
JH
2857 Perl_sv_catpvf(aTHX_ listsv, "%04" PERL_PRIx64 "\n", (UV)value);
2858#else
b9c39e73 2859 if (!SIZE_ONLY)
cea2e8a9 2860 Perl_sv_catpvf(aTHX_ listsv, "%04x\n", value);
f478a152 2861#endif
a0ed51b3
LW
2862 }
2863 }
a0ed51b3
LW
2864
2865 ret = reganode(ANYOFUTF8, 0);
2866
2867 if (!SIZE_ONLY) {
2868 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2869 SvREFCNT_dec(listsv);
2870 n = add_data(1,"s");
2871 PL_regcomp_rx->data->data[n] = (void*)rv;
2872 ARG1_SET(ret, flags);
2873 ARG2_SET(ret, n);
2874 }
2875
2876 return ret;
2877}
2878
76e3520e 2879STATIC char*
cea2e8a9 2880S_nextchar(pTHX)
a0d0e21e 2881{
5c0ca799 2882 dTHR;
3280af22 2883 char* retval = PL_regcomp_parse++;
a0d0e21e 2884
4633a7c4 2885 for (;;) {
3280af22
NIS
2886 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2887 PL_regcomp_parse[2] == '#') {
2888 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2889 PL_regcomp_parse++;
2890 PL_regcomp_parse++;
4633a7c4
LW
2891 continue;
2892 }
3280af22
NIS
2893 if (PL_regflags & PMf_EXTENDED) {
2894 if (isSPACE(*PL_regcomp_parse)) {
2895 PL_regcomp_parse++;
748a9306
LW
2896 continue;
2897 }
3280af22
NIS
2898 else if (*PL_regcomp_parse == '#') {
2899 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2900 PL_regcomp_parse++;
2901 PL_regcomp_parse++;
748a9306
LW
2902 continue;
2903 }
748a9306 2904 }
4633a7c4 2905 return retval;
a0d0e21e 2906 }
a687059c
LW
2907}
2908
2909/*
c277df42 2910- reg_node - emit a node
a0d0e21e 2911*/
76e3520e 2912STATIC regnode * /* Location. */
cea2e8a9 2913S_reg_node(pTHX_ U8 op)
a687059c 2914{
5c0ca799 2915 dTHR;
c277df42
IZ
2916 register regnode *ret;
2917 register regnode *ptr;
a687059c 2918
3280af22 2919 ret = PL_regcode;
c277df42 2920 if (SIZE_ONLY) {
6b88bc9c 2921 SIZE_ALIGN(PL_regsize);
3280af22 2922 PL_regsize += 1;
a0d0e21e
LW
2923 return(ret);
2924 }
a687059c 2925
c277df42 2926 NODE_ALIGN_FILL(ret);
a0d0e21e 2927 ptr = ret;
c277df42 2928 FILL_ADVANCE_NODE(ptr, op);
3280af22 2929 PL_regcode = ptr;
a687059c 2930
a0d0e21e 2931 return(ret);
a687059c
LW
2932}
2933
2934/*
a0d0e21e
LW
2935- reganode - emit a node with an argument
2936*/
76e3520e 2937STATIC regnode * /* Location. */
cea2e8a9 2938S_reganode(pTHX_ U8 op, U32 arg)
fe14fcc3 2939{
5c0ca799 2940 dTHR;
c277df42
IZ
2941 register regnode *ret;
2942 register regnode *ptr;
fe14fcc3 2943
3280af22 2944 ret = PL_regcode;
c277df42 2945 if (SIZE_ONLY) {
6b88bc9c 2946 SIZE_ALIGN(PL_regsize);
3280af22 2947 PL_regsize += 2;
a0d0e21e
LW
2948 return(ret);
2949 }
fe14fcc3 2950
c277df42 2951 NODE_ALIGN_FILL(ret);
a0d0e21e 2952 ptr = ret;
c277df42 2953 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3280af22 2954 PL_regcode = ptr;
fe14fcc3 2955
a0d0e21e 2956 return(ret);
fe14fcc3
LW
2957}
2958
2959/*
a0ed51b3
LW
2960- regc - emit (if appropriate) a Unicode character
2961*/
2962STATIC void
cea2e8a9 2963S_reguni(pTHX_ UV uv, char* s, I32* lenp)
a0ed51b3 2964{
c485e607 2965 dTHR;
a0ed51b3 2966 if (SIZE_ONLY) {
dfe13c55 2967 U8 tmpbuf[10];
a0ed51b3
LW
2968 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2969 }
2970 else
dfe13c55 2971 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
a0ed51b3
LW
2972
2973}
2974
2975/*
a0d0e21e
LW
2976- regc - emit (if appropriate) a byte of code
2977*/
76e3520e 2978STATIC void
cea2e8a9 2979S_regc(pTHX_ U8 b, char* s)
a687059c 2980{
5c0ca799 2981 dTHR;
c277df42
IZ
2982 if (!SIZE_ONLY)
2983 *s = b;
a687059c
LW
2984}
2985
2986/*
a0d0e21e
LW
2987- reginsert - insert an operator in front of already-emitted operand
2988*
2989* Means relocating the operand.
2990*/
76e3520e 2991STATIC void
cea2e8a9 2992S_reginsert(pTHX_ U8 op, regnode *opnd)
a687059c 2993{
5c0ca799 2994 dTHR;
c277df42
IZ
2995 register regnode *src;
2996 register regnode *dst;
2997 register regnode *place;
2998 register int offset = regarglen[(U8)op];
2999
22c35a8c 3000/* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
c277df42
IZ
3001
3002 if (SIZE_ONLY) {
3280af22 3003 PL_regsize += NODE_STEP_REGNODE + offset;
a0d0e21e
LW
3004 return;
3005 }
a687059c 3006
3280af22
NIS
3007 src = PL_regcode;
3008 PL_regcode += NODE_STEP_REGNODE + offset;
3009 dst = PL_regcode;
a0d0e21e 3010 while (src > opnd)
c277df42 3011 StructCopy(--src, --dst, regnode);
a0d0e21e
LW
3012
3013 place = opnd; /* Op node, where operand used to be. */
c277df42
IZ
3014 src = NEXTOPER(place);
3015 FILL_ADVANCE_NODE(place, op);
3016 Zero(src, offset, regnode);
a687059c
LW
3017}
3018
3019/*
c277df42 3020- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 3021*/
76e3520e 3022STATIC void
cea2e8a9 3023S_regtail(pTHX_ regnode *p, regnode *val)
a687059c 3024{
5c0ca799 3025 dTHR;
c277df42
IZ
3026 register regnode *scan;
3027 register regnode *temp;
a0d0e21e
LW
3028 register I32 offset;
3029
c277df42 3030 if (SIZE_ONLY)
a0d0e21e
LW
3031 return;
3032
3033 /* Find last node. */
3034 scan = p;
3035 for (;;) {
3036 temp = regnext(scan);
3037 if (temp == NULL)
3038 break;
3039 scan = temp;
3040 }
a687059c 3041
c277df42
IZ
3042 if (reg_off_by_arg[OP(scan)]) {
3043 ARG_SET(scan, val - scan);
a0ed51b3
LW
3044 }
3045 else {
c277df42
IZ
3046 NEXT_OFF(scan) = val - scan;
3047 }
a687059c
LW
3048}
3049
3050/*
a0d0e21e
LW
3051- regoptail - regtail on operand of first argument; nop if operandless
3052*/
76e3520e 3053STATIC void
cea2e8a9 3054S_regoptail(pTHX_ regnode *p, regnode *val)
a687059c 3055{
5c0ca799 3056 dTHR;
a0d0e21e 3057 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42
IZ
3058 if (p == NULL || SIZE_ONLY)
3059 return;
22c35a8c 3060 if (PL_regkind[(U8)OP(p)] == BRANCH) {
c277df42 3061 regtail(NEXTOPER(p), val);
a0ed51b3 3062 }
22c35a8c 3063 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
c277df42 3064 regtail(NEXTOPER(NEXTOPER(p)), val);
a0ed51b3
LW
3065 }
3066 else
a0d0e21e 3067 return;
a687059c
LW
3068}
3069
3070/*
3071 - regcurly - a little FSA that accepts {\d+,?\d*}
3072 */
79072805 3073STATIC I32
cea2e8a9 3074S_regcurly(pTHX_ register char *s)
a687059c
LW
3075{
3076 if (*s++ != '{')
3077 return FALSE;
f0fcb552 3078 if (!isDIGIT(*s))
a687059c 3079 return FALSE;
f0fcb552 3080 while (isDIGIT(*s))
a687059c
LW
3081 s++;
3082 if (*s == ',')
3083 s++;
f0fcb552 3084 while (isDIGIT(*s))
a687059c
LW
3085 s++;
3086 if (*s != '}')
3087 return FALSE;
3088 return TRUE;
3089}
3090
a687059c 3091
76e3520e 3092STATIC regnode *
cea2e8a9 3093S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
c277df42 3094{
35ff7856 3095#ifdef DEBUGGING
f248d071 3096 register U8 op = EXACT; /* Arbitrary non-END op. */
c277df42
IZ
3097 register regnode *next, *onode;
3098
3099 while (op != END && (!last || node < last)) {
3100 /* While that wasn't END last time... */
3101
3102 NODE_ALIGN(node);
3103 op = OP(node);
3104 if (op == CLOSE)
3105 l--;
3106 next = regnext(node);
3107 /* Where, what. */
3108 if (OP(node) == OPTIMIZED)
3109 goto after_print;
3110 regprop(sv, node);
54dc92de 3111 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
c277df42
IZ
3112 2*l + 1, "", SvPVX(sv));
3113 if (next == NULL) /* Next ptr. */
3114 PerlIO_printf(Perl_debug_log, "(0)");
3115 else
3116 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3117 (void)PerlIO_putc(Perl_debug_log, '\n');
3118 after_print:
22c35a8c 3119 if (PL_regkind[(U8)op] == BRANCHJ) {
c277df42
IZ
3120 register regnode *nnode = (OP(next) == LONGJMP
3121 ? regnext(next)
3122 : next);
3123 if (last && nnode > last)
3124 nnode = last;
3125 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
a0ed51b3 3126 }
22c35a8c 3127 else if (PL_regkind[(U8)op] == BRANCH) {
c277df42 3128 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
a0ed51b3
LW
3129 }
3130 else if ( op == CURLY) { /* `next' might be very big: optimizer */
c277df42
IZ
3131 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3132 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
a0ed51b3 3133 }
22c35a8c 3134 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
c277df42
IZ
3135 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3136 next, sv, l + 1);
a0ed51b3
LW
3137 }
3138 else if ( op == PLUS || op == STAR) {
c277df42 3139 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
a0ed51b3
LW
3140 }
3141 else if (op == ANYOF) {
c277df42
IZ
3142 node = NEXTOPER(node);
3143 node += ANY_SKIP;
a0ed51b3 3144 }
22c35a8c 3145 else if (PL_regkind[(U8)op] == EXACT) {
c277df42
IZ
3146 /* Literal string, where present. */
3147 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
3148 node = NEXTOPER(node);
a0ed51b3
LW
3149 }
3150 else {
c277df42
IZ
3151 node = NEXTOPER(node);
3152 node += regarglen[(U8)op];
3153 }
3154 if (op == CURLYX || op == OPEN)
3155 l++;
3156 else if (op == WHILEM)
3157 l--;
3158 }
17c3b450 3159#endif /* DEBUGGING */
c277df42
IZ
3160 return node;
3161}
3162
a687059c 3163/*
fd181c75 3164 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c
LW
3165 */
3166void
864dbfa3 3167Perl_regdump(pTHX_ regexp *r)
a687059c 3168{
35ff7856 3169#ifdef DEBUGGING
5c0ca799 3170 dTHR;
46fc3d4c 3171 SV *sv = sv_newmortal();
a687059c 3172
c277df42 3173 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e
LW
3174
3175 /* Header fields of interest. */
c277df42 3176 if (r->anchored_substr)
2c2d71f5 3177 PerlIO_printf(Perl_debug_log, "anchored `%s%.*s%s'%s at %d ",
3280af22 3178 PL_colors[0],
2c2d71f5 3179 SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0),
c277df42 3180 SvPVX(r->anchored_substr),
3280af22 3181 PL_colors[1],
c277df42
IZ
3182 SvTAIL(r->anchored_substr) ? "$" : "",
3183 r->anchored_offset);
3184 if (r->float_substr)
2c2d71f5 3185 PerlIO_printf(Perl_debug_log, "floating `%s%.*s%s'%s at %d..%u ",
3280af22 3186 PL_colors[0],
2c2d71f5
JH
3187 SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0),
3188 SvPVX(r->float_substr),
3280af22 3189 PL_colors[1],
c277df42
IZ
3190 SvTAIL(r->float_substr) ? "$" : "",
3191 r->float_min_offset, r->float_max_offset);
3192 if (r->check_substr)
3193 PerlIO_printf(Perl_debug_log,
3194 r->check_substr == r->float_substr
3195 ? "(checking floating" : "(checking anchored");
3196 if (r->reganch & ROPT_NOSCAN)
3197 PerlIO_printf(Perl_debug_log, " noscan");
3198 if (r->reganch & ROPT_CHECK_ALL)
3199 PerlIO_printf(Perl_debug_log, " isall");
3200 if (r->check_substr)
3201 PerlIO_printf(Perl_debug_log, ") ");
3202
46fc3d4c
PP
3203 if (r->regstclass) {
3204 regprop(sv, r->regstclass);
3205 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3206 }
774d564b
PP
3207 if (r->reganch & ROPT_ANCH) {
3208 PerlIO_printf(Perl_debug_log, "anchored");
3209 if (r->reganch & ROPT_ANCH_BOL)
3210 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42
IZ
3211 if (r->reganch & ROPT_ANCH_MBOL)
3212 PerlIO_printf(Perl_debug_log, "(MBOL)");
cad2e5aa
JH
3213 if (r->reganch & ROPT_ANCH_SBOL)
3214 PerlIO_printf(Perl_debug_log, "(SBOL)");
774d564b
PP
3215 if (r->reganch & ROPT_ANCH_GPOS)
3216 PerlIO_printf(Perl_debug_log, "(GPOS)");
3217 PerlIO_putc(Perl_debug_log, ' ');
3218 }
c277df42
IZ
3219 if (r->reganch & ROPT_GPOS_SEEN)
3220 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 3221 if (r->reganch & ROPT_SKIP)
760ac839 3222 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 3223 if (r->reganch & ROPT_IMPLICIT)
760ac839 3224 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 3225 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02
IZ
3226 if (r->reganch & ROPT_EVAL_SEEN)
3227 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 3228 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 3229#endif /* DEBUGGING */
a687059c
LW
3230}
3231
3232/*
a0d0e21e
LW
3233- regprop - printable representation of opcode
3234*/
46fc3d4c 3235void
864dbfa3 3236Perl_regprop(pTHX_ SV *sv, regnode *o)
a687059c 3237{
35ff7856 3238#ifdef DEBUGGING
5c0ca799 3239 dTHR;
9b155405 3240 register int k;
a0d0e21e 3241
54dc92de 3242 sv_setpvn(sv, "", 0);
9b155405 3243 if (OP(o) >= reg_num) /* regnode.type is unsigned */
a0d0e21e 3244 FAIL("corrupted regexp opcode");
9b155405
IZ
3245 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3246
3247 k = PL_regkind[(U8)OP(o)];
3248
3249 if (k == EXACT)
cea2e8a9 3250 Perl_sv_catpvf(aTHX_ sv, " <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
9b155405
IZ
3251 else if (k == CURLY) {
3252 if (OP(o) == CURLYM || OP(o) == CURLYN)
cea2e8a9
GS
3253 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3254 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 3255 }
2c2d71f5
JH
3256 else if (k == WHILEM && o->flags) /* Ordinal/of */
3257 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
9b155405 3258 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
cea2e8a9 3259 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
9b155405 3260 else if (k == LOGICAL)
cea2e8a9 3261 Perl_sv_catpvf(aTHX_ sv, "[%d]", ARG(o)); /* 2: embedded, otherwise 1 */
9b155405 3262 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
cea2e8a9 3263 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
17c3b450 3264#endif /* DEBUGGING */
35ff7856 3265}
a687059c 3266
cad2e5aa
JH
3267SV *
3268Perl_re_intuit_string(pTHX_ regexp *prog)
3269{ /* Assume that RE_INTUIT is set */
3270 DEBUG_r(
3271 { STRLEN n_a;
3272 char *s = SvPV(prog->check_substr,n_a);
3273
3274 if (!PL_colorset) reginitcolors();
3275 PerlIO_printf(Perl_debug_log,
3276 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3277 PL_colors[4],PL_colors[5],PL_colors[0],
3278 s,
3279 PL_colors[1],
3280 (strlen(s) > 60 ? "..." : ""));
3281 } );
3282
3283 return prog->check_substr;
3284}
3285
2b69d0c2 3286void
864dbfa3 3287Perl_pregfree(pTHX_ struct regexp *r)
a687059c 3288{
5c0ca799 3289 dTHR;
adac82c7 3290 DEBUG_r(if (!PL_colorset) reginitcolors());
cad2e5aa
JH
3291 DEBUG_r(PerlIO_printf(Perl_debug_log,
3292 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3293 PL_colors[4],PL_colors[5],PL_colors[0],
3294 r->precomp,
3295 PL_colors[1],
3296 (strlen(r->precomp) > 60 ? "..." : "")));
3297
3298
c277df42 3299 if (!r || (--r->refcnt > 0))
a0d0e21e 3300 return;
c277df42 3301 if (r->precomp)
a0d0e21e 3302 Safefree(r->precomp);
cf93c79d
IZ
3303 if (RX_MATCH_COPIED(r))
3304 Safefree(r->subbeg);
a193d654
GS
3305 if (r->substrs) {
3306 if (r->anchored_substr)
3307 SvREFCNT_dec(r->anchored_substr);
3308 if (r->float_substr)
3309 SvREFCNT_dec(r->float_substr);
2779dcf1 3310 Safefree(r->substrs);
a193d654 3311 }
c277df42
IZ
3312 if (r->data) {
3313 int n = r->data->count;
dfad63ad
HS
3314 AV* new_comppad = NULL;
3315 AV* old_comppad;
3316 SV** old_curpad;
3317
c277df42
IZ
3318 while (--n >= 0) {
3319 switch (r->data->what[n]) {
3320 case 's':
3321 SvREFCNT_dec((SV*)r->data->data[n]);
3322 break;
dfad63ad
HS
3323 case 'p':
3324 new_comppad = (AV*)r->data->data[n];
3325 break;
c277df42 3326 case 'o':
dfad63ad 3327 if (new_comppad == NULL)
cea2e8a9 3328 Perl_croak(aTHX_ "panic: pregfree comppad");
dfad63ad
HS
3329 old_comppad = PL_comppad;
3330 old_curpad = PL_curpad;
3331 PL_comppad = new_comppad;
3332 PL_curpad = AvARRAY(new_comppad);
c277df42 3333 op_free((OP_4tree*)r->data->data[n]);
dfad63ad
HS
3334 PL_comppad = old_comppad;
3335 PL_curpad = old_curpad;
3336 SvREFCNT_dec((SV*)new_comppad);
3337 new_comppad = NULL;
c277df42
IZ
3338 break;
3339 case 'n':
3340 break;
3341 default:
3342 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3343 }
3344 }
3345 Safefree(r->data->what);
3346 Safefree(r->data);
a0d0e21e
LW
3347 }
3348 Safefree(r->startp);
3349 Safefree(r->endp);
3350 Safefree(r);
a687059c 3351}
c277df42
IZ
3352
3353/*
3354 - regnext - dig the "next" pointer out of a node
3355 *
3356 * [Note, when REGALIGN is defined there are two places in regmatch()
3357 * that bypass this code for speed.]
3358 */
3359regnode *
864dbfa3 3360Perl_regnext(pTHX_ register regnode *p)
c277df42 3361{
5c0ca799 3362 dTHR;
c277df42
IZ
3363 register I32 offset;
3364
3280af22 3365 if (p == &PL_regdummy)
c277df42
IZ
3366 return(NULL);
3367
3368 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3369 if (offset == 0)
3370 return(NULL);
3371
c277df42 3372 return(p+offset);
c277df42
IZ
3373}
3374
01f988be 3375STATIC void
cea2e8a9 3376S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
c277df42
IZ
3377{
3378 va_list args;
3379 STRLEN l1 = strlen(pat1);
3380 STRLEN l2 = strlen(pat2);
3381 char buf[512];
06bf62c7 3382 SV *msv;
c277df42
IZ
3383 char *message;
3384
3385 if (l1 > 510)
3386 l1 = 510;
3387 if (l1 + l2 > 510)
3388 l2 = 510 - l1;
3389 Copy(pat1, buf, l1 , char);
3390 Copy(pat2, buf + l1, l2 , char);
3b818b81
GS
3391 buf[l1 + l2] = '\n';
3392 buf[l1 + l2 + 1] = '\0';
8736538c
AS
3393#ifdef I_STDARG
3394 /* ANSI variant takes additional second argument */
c277df42 3395 va_start(args, pat2);
8736538c
AS
3396#else
3397 va_start(args);
3398#endif
06bf62c7 3399 msv = mess(buf, &args);
c277df42 3400 va_end(args);
06bf62c7 3401 message = SvPV(msv,l1);
c277df42
IZ
3402 if (l1 > 512)
3403 l1 = 512;
3404 Copy(message, buf, l1 , char);
3405 buf[l1] = '\0'; /* Overwrite \n */
cea2e8a9 3406 Perl_croak(aTHX_ "%s", buf);
c277df42 3407}
a0ed51b3
LW
3408
3409/* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3410
3411void
864dbfa3 3412Perl_save_re_context(pTHX)
c485e607
NIS
3413{
3414 dTHR;
a0ed51b3
LW
3415 SAVEPPTR(PL_bostr);
3416 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3417 SAVEI32(PL_regnpar); /* () count. */
3418 SAVEI32(PL_regsize); /* Code size. */
3419 SAVEI16(PL_regflags); /* are we folding, multilining? */
3420 SAVEPPTR(PL_reginput); /* String-input pointer. */
3421 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3422 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3423 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3424 SAVESPTR(PL_regendp); /* Ditto for endp. */
3425 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3426 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3427 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3428 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3429 PL_reg_start_tmp = 0;
3430 SAVEFREEPV(PL_reg_start_tmp);
3431 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3432 PL_reg_start_tmpl = 0;
3433 SAVESPTR(PL_regdata);
3434 SAVEI32(PL_reg_flags); /* from regexec.c */
3435 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3436 SAVEI32(PL_regnarrate); /* from regexec.c */
3437 SAVESPTR(PL_regprogram); /* from regexec.c */
3438 SAVEINT(PL_regindent); /* from regexec.c */
3439 SAVESPTR(PL_regcc); /* from regexec.c */
3440 SAVESPTR(PL_curcop);
3441 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3442 SAVEI32(PL_regseen); /* from regcomp.c */
3443 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3444 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3445 SAVESPTR(PL_regcode); /* Code-emit pointer; &regdummy = don't */
3446 SAVEPPTR(PL_regxend); /* End of input for compile */
3447 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
54b6e2fa
IZ
3448 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3449 SAVESPTR(PL_reg_re); /* from regexec.c */
3450 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3451 SAVESPTR(PL_reg_sv); /* from regexec.c */
3452 SAVESPTR(PL_reg_magic); /* from regexec.c */
3453 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3454 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3455 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3456#ifdef DEBUGGING
3457 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3458#endif
a0ed51b3 3459}
51371543
GS
3460
3461#ifdef PERL_OBJECT
3462#define NO_XSLOCKS
3463#include "XSUB.h"
3464#undef this
3465#define this pPerl
3466#endif
3467
3468static void
3469clear_re(pTHXo_ void *r)
3470{
3471 ReREFCNT_dec((regexp *)r);
3472}
3473