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