This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate with Sarathy.
[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) {
57def98f 705 FAIL2("lookbehind longer than %"UVuf" not implemented", (UV)U8_MAX);
c277df42
IZ
706 }
707 scan->flags = minnext;
708 }
709 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
710 pars++;
405ff068 711 if (data && (data_fake.flags & SF_HAS_EVAL))
c277df42 712 data->flags |= SF_HAS_EVAL;
2c2d71f5
JH
713 if (data)
714 data->whilem_c = data_fake.whilem_c;
a0ed51b3
LW
715 }
716 else if (OP(scan) == OPEN) {
c277df42 717 pars++;
a0ed51b3
LW
718 }
719 else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
c277df42
IZ
720 next = regnext(scan);
721
722 if ( next && (OP(next) != WHILEM) && next < last)
c277df42 723 is_par = 0; /* Disable optimization */
a0ed51b3
LW
724 }
725 else if (OP(scan) == EVAL) {
c277df42
IZ
726 if (data)
727 data->flags |= SF_HAS_EVAL;
728 }
0f5d15d6
IZ
729 else if (OP(scan) == LOGICAL && scan->flags == 2) { /* Embedded */
730 if (flags & SCF_DO_SUBSTR) {
731 scan_commit(data);
732 data->longest = &(data->longest_float);
733 }
734 is_inf = is_inf_internal = 1;
735 }
c277df42
IZ
736 /* Else: zero-length, ignore. */
737 scan = regnext(scan);
738 }
739
740 finish:
741 *scanp = scan;
aca2d497 742 *deltap = is_inf_internal ? I32_MAX : delta;
c277df42
IZ
743 if (flags & SCF_DO_SUBSTR && is_inf)
744 data->pos_delta = I32_MAX - data->pos_min;
745 if (is_par > U8_MAX)
746 is_par = 0;
747 if (is_par && pars==1 && data) {
748 data->flags |= SF_IN_PAR;
749 data->flags &= ~SF_HAS_PAR;
a0ed51b3
LW
750 }
751 else if (pars && data) {
c277df42
IZ
752 data->flags |= SF_HAS_PAR;
753 data->flags &= ~SF_IN_PAR;
754 }
755 return min;
756}
757
76e3520e 758STATIC I32
cea2e8a9 759S_add_data(pTHX_ I32 n, char *s)
c277df42 760{
5c0ca799 761 dTHR;
3280af22
NIS
762 if (PL_regcomp_rx->data) {
763 Renewc(PL_regcomp_rx->data,
764 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
c277df42 765 char, struct reg_data);
3280af22
NIS
766 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
767 PL_regcomp_rx->data->count += n;
a0ed51b3
LW
768 }
769 else {
3280af22 770 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
c277df42 771 char, struct reg_data);
3280af22
NIS
772 New(1208, PL_regcomp_rx->data->what, n, U8);
773 PL_regcomp_rx->data->count = n;
c277df42 774 }
3280af22
NIS
775 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
776 return PL_regcomp_rx->data->count - n;
c277df42
IZ
777}
778
d88dccdf 779void
864dbfa3 780Perl_reginitcolors(pTHX)
d88dccdf
IZ
781{
782 dTHR;
783 int i = 0;
784 char *s = PerlEnv_getenv("PERL_RE_COLORS");
785
786 if (s) {
787 PL_colors[0] = s = savepv(s);
788 while (++i < 6) {
789 s = strchr(s, '\t');
790 if (s) {
791 *s = '\0';
792 PL_colors[i] = ++s;
793 }
794 else
c712d376 795 PL_colors[i] = s = "";
d88dccdf
IZ
796 }
797 } else {
798 while (i < 6)
799 PL_colors[i++] = "";
800 }
801 PL_colorset = 1;
802}
803
a687059c 804/*
e50aee73 805 - pregcomp - compile a regular expression into internal code
a687059c
LW
806 *
807 * We can't allocate space until we know how big the compiled form will be,
808 * but we can't compile it (and thus know how big it is) until we've got a
809 * place to put the code. So we cheat: we compile it twice, once with code
810 * generation turned off and size counting turned on, and once "for real".
811 * This also means that we don't allocate space until we are sure that the
812 * thing really will compile successfully, and we never have to move the
813 * code and thus invalidate pointers into it. (Note that it has to be in
814 * one piece because free() must be able to free it all.) [NB: not true in perl]
815 *
816 * Beware that the optimization-preparation code in here knows about some
817 * of the structure of the compiled regexp. [I'll say.]
818 */
819regexp *
864dbfa3 820Perl_pregcomp(pTHX_ char *exp, char *xend, PMOP *pm)
a687059c 821{
5c0ca799 822 dTHR;
a0d0e21e 823 register regexp *r;
c277df42
IZ
824 regnode *scan;
825 SV **longest;
826 SV *longest_fixed;
827 SV *longest_float;
828 regnode *first;
a0d0e21e 829 I32 flags;
a0d0e21e
LW
830 I32 minlen = 0;
831 I32 sawplus = 0;
832 I32 sawopen = 0;
2c2d71f5 833 scan_data_t data;
a0d0e21e
LW
834
835 if (exp == NULL)
c277df42 836 FAIL("NULL regexp argument");
a0d0e21e 837
e24b16f9 838 if (PL_curcop == &PL_compiling ? (PL_hints & HINT_UTF8) : IN_UTF8)
a0ed51b3
LW
839 PL_reg_flags |= RF_utf8;
840 else
841 PL_reg_flags = 0;
842
3280af22 843 PL_regprecomp = savepvn(exp, xend - exp);
35ef4773 844 DEBUG_r(if (!PL_colorset) reginitcolors());
2c2d71f5 845 DEBUG_r(PerlIO_printf(Perl_debug_log, "%sCompiling REx%s `%s%*s%s'\n",
d88dccdf
IZ
846 PL_colors[4],PL_colors[5],PL_colors[0],
847 xend - exp, PL_regprecomp, PL_colors[1]));
3280af22
NIS
848 PL_regflags = pm->op_pmflags;
849 PL_regsawback = 0;
bbce6d69 850
3280af22
NIS
851 PL_regseen = 0;
852 PL_seen_zerolen = *exp == '^' ? -1 : 0;
853 PL_seen_evals = 0;
854 PL_extralen = 0;
c277df42 855
bbce6d69 856 /* First pass: determine size, legality. */
3280af22
NIS
857 PL_regcomp_parse = exp;
858 PL_regxend = xend;
859 PL_regnaughty = 0;
860 PL_regnpar = 1;
861 PL_regsize = 0L;
862 PL_regcode = &PL_regdummy;
2c2d71f5 863 PL_reg_whilem_seen = 0;
cd439c50 864 REGC((U8)REG_MAGIC, (char*)PL_regcode);
a0d0e21e 865 if (reg(0, &flags) == NULL) {
3280af22
NIS
866 Safefree(PL_regprecomp);
867 PL_regprecomp = Nullch;
a0d0e21e
LW
868 return(NULL);
869 }
3280af22 870 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
c277df42 871
c277df42
IZ
872 /* Small enough for pointer-storage convention?
873 If extralen==0, this means that we will not need long jumps. */
3280af22
NIS
874 if (PL_regsize >= 0x10000L && PL_extralen)
875 PL_regsize += PL_extralen;
c277df42 876 else
3280af22 877 PL_extralen = 0;
2c2d71f5
JH
878 if (PL_reg_whilem_seen > 15)
879 PL_reg_whilem_seen = 15;
a0d0e21e 880
bbce6d69 881 /* Allocate space and initialize. */
3280af22 882 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
c277df42 883 char, regexp);
a0d0e21e
LW
884 if (r == NULL)
885 FAIL("regexp out of space");
c277df42 886 r->refcnt = 1;
bbce6d69 887 r->prelen = xend - exp;
3280af22 888 r->precomp = PL_regprecomp;
cf93c79d
IZ
889 r->subbeg = NULL;
890 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
4327152a
IZ
891 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
892
893 r->substrs = 0; /* Useful during FAIL. */
894 r->startp = 0; /* Useful during FAIL. */
895 r->endp = 0; /* Useful during FAIL. */
896
3280af22 897 PL_regcomp_rx = r;
bbce6d69
PP
898
899 /* Second pass: emit code. */
3280af22
NIS
900 PL_regcomp_parse = exp;
901 PL_regxend = xend;
902 PL_regnaughty = 0;
903 PL_regnpar = 1;
904 PL_regcode = r->program;
2cd61cdb 905 /* Store the count of eval-groups for security checks: */
3280af22 906 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
cd439c50 907 REGC((U8)REG_MAGIC, (char*) PL_regcode++);
c277df42 908 r->data = 0;
a0d0e21e
LW
909 if (reg(0, &flags) == NULL)
910 return(NULL);
911
912 /* Dig out information for optimizations. */
cf93c79d 913 r->reganch = pm->op_pmflags & PMf_COMPILETIME; /* Again? */
3280af22 914 pm->op_pmflags = PL_regflags;
a0ed51b3
LW
915 if (UTF)
916 r->reganch |= ROPT_UTF8;
c277df42 917 r->regstclass = NULL;
a0ed51b3
LW
918 if (PL_regnaughty >= 10) /* Probably an expensive pattern. */
919 r->reganch |= ROPT_NAUGHTY;
c277df42 920 scan = r->program + 1; /* First BRANCH. */
2779dcf1
IZ
921
922 /* XXXX To minimize changes to RE engine we always allocate
923 3-units-long substrs field. */
924 Newz(1004, r->substrs, 1, struct reg_substr_data);
925
2c2d71f5 926 StructCopy(&zero_scan_data, &data, scan_data_t);
c277df42 927 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
c277df42 928 I32 fake;
c5254dd6 929 STRLEN longest_float_length, longest_fixed_length;
a0d0e21e
LW
930
931 first = scan;
c277df42 932 /* Skip introductions and multiplicators >= 1. */
a0d0e21e
LW
933 while ((OP(first) == OPEN && (sawopen = 1)) ||
934 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
935 (OP(first) == PLUS) ||
936 (OP(first) == MINMOD) ||
22c35a8c 937 (PL_regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
a0d0e21e
LW
938 if (OP(first) == PLUS)
939 sawplus = 1;
940 else
941 first += regarglen[(U8)OP(first)];
942 first = NEXTOPER(first);
a687059c
LW
943 }
944
a0d0e21e
LW
945 /* Starting-point info. */
946 again:
c277df42 947 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
4d61ec05 948 else if (strchr((char*)PL_simple+4,OP(first)))
a0d0e21e 949 r->regstclass = first;
22c35a8c
GS
950 else if (PL_regkind[(U8)OP(first)] == BOUND ||
951 PL_regkind[(U8)OP(first)] == NBOUND)
a0d0e21e 952 r->regstclass = first;
22c35a8c 953 else if (PL_regkind[(U8)OP(first)] == BOL) {
cad2e5aa
JH
954 r->reganch |= (OP(first) == MBOL
955 ? ROPT_ANCH_MBOL
956 : (OP(first) == SBOL
957 ? ROPT_ANCH_SBOL
958 : ROPT_ANCH_BOL));
a0d0e21e 959 first = NEXTOPER(first);
774d564b
PP
960 goto again;
961 }
962 else if (OP(first) == GPOS) {
963 r->reganch |= ROPT_ANCH_GPOS;
964 first = NEXTOPER(first);
965 goto again;
a0d0e21e
LW
966 }
967 else if ((OP(first) == STAR &&
22c35a8c 968 PL_regkind[(U8)OP(NEXTOPER(first))] == REG_ANY) &&
a0d0e21e
LW
969 !(r->reganch & ROPT_ANCH) )
970 {
971 /* turn .* into ^.* with an implied $*=1 */
cad2e5aa
JH
972 int type = OP(NEXTOPER(first));
973
974 if (type == REG_ANY || type == ANYUTF8)
975 type = ROPT_ANCH_MBOL;
976 else
977 type = ROPT_ANCH_SBOL;
978
979 r->reganch |= type | ROPT_IMPLICIT;
a0d0e21e 980 first = NEXTOPER(first);
774d564b 981 goto again;
a0d0e21e 982 }
cad2e5aa
JH
983 if (sawplus && (!sawopen || !PL_regsawback)
984 && !(PL_regseen & REG_SEEN_EVAL)) /* May examine pos and $& */
985 /* x+ must match at the 1st pos of run of x's */
986 r->reganch |= ROPT_SKIP;
a0d0e21e 987
c277df42
IZ
988 /* Scan is after the zeroth branch, first is atomic matcher. */
989 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
990 first - scan + 1));
a0d0e21e
LW
991 /*
992 * If there's something expensive in the r.e., find the
993 * longest literal string that must appear and make it the
994 * regmust. Resolve ties in favor of later strings, since
995 * the regstart check works with the beginning of the r.e.
996 * and avoiding duplication strengthens checking. Not a
997 * strong reason, but sufficient in the absence of others.
998 * [Now we resolve ties in favor of the earlier string if
c277df42 999 * it happens that c_offset_min has been invalidated, since the
a0d0e21e
LW
1000 * earlier string may buy us something the later one won't.]
1001 */
a0d0e21e 1002 minlen = 0;
a687059c 1003
79cb57f6
GS
1004 data.longest_fixed = newSVpvn("",0);
1005 data.longest_float = newSVpvn("",0);
1006 data.last_found = newSVpvn("",0);
c277df42
IZ
1007 data.longest = &(data.longest_fixed);
1008 first = scan;
1009
3280af22 1010 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
c277df42 1011 &data, SCF_DO_SUBSTR);
3280af22 1012 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
c277df42 1013 && data.last_start_min == 0 && data.last_end > 0
3280af22
NIS
1014 && !PL_seen_zerolen
1015 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
c277df42
IZ
1016 r->reganch |= ROPT_CHECK_ALL;
1017 scan_commit(&data);
1018 SvREFCNT_dec(data.last_found);
1019
a0ed51b3 1020 longest_float_length = CHR_SVLEN(data.longest_float);
c5254dd6 1021 if (longest_float_length
c277df42
IZ
1022 || (data.flags & SF_FL_BEFORE_EOL
1023 && (!(data.flags & SF_FL_BEFORE_MEOL)
3280af22 1024 || (PL_regflags & PMf_MULTILINE)))) {
cf93c79d
IZ
1025 int t;
1026
a0ed51b3 1027 if (SvCUR(data.longest_fixed) /* ok to leave SvCUR */
aca2d497
IZ
1028 && data.offset_fixed == data.offset_float_min
1029 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
1030 goto remove_float; /* As in (a)+. */
1031
c277df42
IZ
1032 r->float_substr = data.longest_float;
1033 r->float_min_offset = data.offset_float_min;
1034 r->float_max_offset = data.offset_float_max;
cf93c79d
IZ
1035 t = (data.flags & SF_FL_BEFORE_EOL /* Can't have SEOL and MULTI */
1036 && (!(data.flags & SF_FL_BEFORE_MEOL)
1037 || (PL_regflags & PMf_MULTILINE)));
1038 fbm_compile(r->float_substr, t ? FBMcf_TAIL : 0);
a0ed51b3
LW
1039 }
1040 else {
aca2d497 1041 remove_float:
c277df42
IZ
1042 r->float_substr = Nullsv;
1043 SvREFCNT_dec(data.longest_float);
c5254dd6 1044 longest_float_length = 0;
a0d0e21e 1045 }
c277df42 1046
a0ed51b3 1047 longest_fixed_length = CHR_SVLEN(data.longest_fixed);
c5254dd6 1048 if (longest_fixed_length
c277df42
IZ
1049 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
1050 && (!(data.flags & SF_FIX_BEFORE_MEOL)
3280af22 1051 || (PL_regflags & PMf_MULTILINE)))) {
cf93c79d
IZ
1052 int t;
1053
c277df42
IZ
1054 r->anchored_substr = data.longest_fixed;
1055 r->anchored_offset = data.offset_fixed;
cf93c79d
IZ
1056 t = (data.flags & SF_FIX_BEFORE_EOL /* Can't have SEOL and MULTI */
1057 && (!(data.flags & SF_FIX_BEFORE_MEOL)
1058 || (PL_regflags & PMf_MULTILINE)));
1059 fbm_compile(r->anchored_substr, t ? FBMcf_TAIL : 0);
a0ed51b3
LW
1060 }
1061 else {
c277df42
IZ
1062 r->anchored_substr = Nullsv;
1063 SvREFCNT_dec(data.longest_fixed);
c5254dd6 1064 longest_fixed_length = 0;
a0d0e21e 1065 }
c277df42
IZ
1066
1067 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
c5254dd6 1068 if (longest_fixed_length > longest_float_length) {
c277df42
IZ
1069 r->check_substr = r->anchored_substr;
1070 r->check_offset_min = r->check_offset_max = r->anchored_offset;
1071 if (r->reganch & ROPT_ANCH_SINGLE)
1072 r->reganch |= ROPT_NOSCAN;
a0ed51b3
LW
1073 }
1074 else {
c277df42
IZ
1075 r->check_substr = r->float_substr;
1076 r->check_offset_min = data.offset_float_min;
1077 r->check_offset_max = data.offset_float_max;
a0d0e21e 1078 }
30382c73
IZ
1079 /* XXXX Currently intuiting is not compatible with ANCH_GPOS.
1080 This should be changed ASAP! */
1081 if (r->check_substr && !(r->reganch & ROPT_ANCH_GPOS)) {
cad2e5aa
JH
1082 r->reganch |= RE_USE_INTUIT;
1083 if (SvTAIL(r->check_substr))
1084 r->reganch |= RE_INTUIT_TAIL;
1085 }
a0ed51b3
LW
1086 }
1087 else {
c277df42
IZ
1088 /* Several toplevels. Best we can is to set minlen. */
1089 I32 fake;
1090
1091 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
1092 scan = r->program + 1;
2c2d71f5 1093 minlen = study_chunk(&scan, &fake, scan + PL_regsize, &data, 0);
c277df42 1094 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
a0d0e21e
LW
1095 }
1096
a0d0e21e 1097 r->minlen = minlen;
3280af22 1098 if (PL_regseen & REG_SEEN_GPOS)
c277df42 1099 r->reganch |= ROPT_GPOS_SEEN;
3280af22 1100 if (PL_regseen & REG_SEEN_LOOKBEHIND)
c277df42 1101 r->reganch |= ROPT_LOOKBEHIND_SEEN;
3280af22 1102 if (PL_regseen & REG_SEEN_EVAL)
ce862d02 1103 r->reganch |= ROPT_EVAL_SEEN;
cf93c79d
IZ
1104 Newz(1002, r->startp, PL_regnpar, I32);
1105 Newz(1002, r->endp, PL_regnpar, I32);
a0d0e21e
LW
1106 DEBUG_r(regdump(r));
1107 return(r);
a687059c
LW
1108}
1109
1110/*
1111 - reg - regular expression, i.e. main body or parenthesized thing
1112 *
1113 * Caller must absorb opening parenthesis.
1114 *
1115 * Combining parenthesis handling with the base level of regular expression
1116 * is a trifle forced, but the need to tie the tails of the branches to what
1117 * follows makes it hard to avoid.
1118 */
76e3520e 1119STATIC regnode *
cea2e8a9 1120S_reg(pTHX_ I32 paren, I32 *flagp)
c277df42 1121 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
a687059c 1122{
5c0ca799 1123 dTHR;
c277df42
IZ
1124 register regnode *ret; /* Will be the head of the group. */
1125 register regnode *br;
1126 register regnode *lastbr;
1127 register regnode *ender = 0;
a0d0e21e 1128 register I32 parno = 0;
3280af22 1129 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
c277df42 1130 char c;
a0d0e21e 1131
821b33a5 1132 *flagp = 0; /* Tentatively. */
a0d0e21e
LW
1133
1134 /* Make an OPEN node, if parenthesized. */
1135 if (paren) {
3280af22 1136 if (*PL_regcomp_parse == '?') {
ca9dfc88
IZ
1137 U16 posflags = 0, negflags = 0;
1138 U16 *flagsp = &posflags;
0f5d15d6 1139 int logical = 0;
ca9dfc88 1140
3280af22
NIS
1141 PL_regcomp_parse++;
1142 paren = *PL_regcomp_parse++;
c277df42 1143 ret = NULL; /* For look-ahead/behind. */
a0d0e21e 1144 switch (paren) {
c277df42 1145 case '<':
3280af22
NIS
1146 PL_regseen |= REG_SEEN_LOOKBEHIND;
1147 if (*PL_regcomp_parse == '!')
c277df42 1148 paren = ',';
3280af22 1149 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
c277df42 1150 goto unknown;
3280af22 1151 PL_regcomp_parse++;
a0d0e21e
LW
1152 case '=':
1153 case '!':
3280af22 1154 PL_seen_zerolen++;
c277df42
IZ
1155 case ':':
1156 case '>':
a0d0e21e
LW
1157 break;
1158 case '$':
1159 case '@':
c277df42 1160 FAIL2("Sequence (?%c...) not implemented", (int)paren);
a0d0e21e
LW
1161 break;
1162 case '#':
3280af22
NIS
1163 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1164 PL_regcomp_parse++;
1165 if (*PL_regcomp_parse != ')')
c277df42 1166 FAIL("Sequence (?#... not terminated");
a0d0e21e
LW
1167 nextchar();
1168 *flagp = TRYAGAIN;
1169 return NULL;
0f5d15d6
IZ
1170 case 'p':
1171 logical = 1;
1172 paren = *PL_regcomp_parse++;
1173 /* FALL THROUGH */
c277df42
IZ
1174 case '{':
1175 {
1176 dTHR;
1177 I32 count = 1, n = 0;
1178 char c;
3280af22 1179 char *s = PL_regcomp_parse;
c277df42
IZ
1180 SV *sv;
1181 OP_4tree *sop, *rop;
1182
3280af22
NIS
1183 PL_seen_zerolen++;
1184 PL_regseen |= REG_SEEN_EVAL;
1185 while (count && (c = *PL_regcomp_parse)) {
1186 if (c == '\\' && PL_regcomp_parse[1])
1187 PL_regcomp_parse++;
c277df42
IZ
1188 else if (c == '{')
1189 count++;
1190 else if (c == '}')
1191 count--;
3280af22 1192 PL_regcomp_parse++;
c277df42 1193 }
3280af22 1194 if (*PL_regcomp_parse != ')')
c277df42
IZ
1195 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1196 if (!SIZE_ONLY) {
1197 AV *av;
1198
3280af22 1199 if (PL_regcomp_parse - 1 - s)
79cb57f6 1200 sv = newSVpvn(s, PL_regcomp_parse - 1 - s);
c277df42 1201 else
79cb57f6 1202 sv = newSVpvn("", 0);
c277df42
IZ
1203
1204 rop = sv_compile_2op(sv, &sop, "re", &av);
1205
dfad63ad 1206 n = add_data(3, "nop");
3280af22 1207 PL_regcomp_rx->data->data[n] = (void*)rop;
dfad63ad
HS
1208 PL_regcomp_rx->data->data[n+1] = (void*)sop;
1209 PL_regcomp_rx->data->data[n+2] = (void*)av;
c277df42 1210 SvREFCNT_dec(sv);
a0ed51b3 1211 }
e24b16f9
GS
1212 else { /* First pass */
1213 if (PL_reginterp_cnt < ++PL_seen_evals
1214 && PL_curcop != &PL_compiling)
2cd61cdb
IZ
1215 /* No compiled RE interpolated, has runtime
1216 components ===> unsafe. */
1217 FAIL("Eval-group not allowed at runtime, use re 'eval'");
3280af22 1218 if (PL_tainted)
cc6b7395 1219 FAIL("Eval-group in insecure regular expression");
c277df42
IZ
1220 }
1221
1222 nextchar();
0f5d15d6
IZ
1223 if (logical) {
1224 ret = reg_node(LOGICAL);
1225 if (!SIZE_ONLY)
1226 ret->flags = 2;
1227 regtail(ret, reganode(EVAL, n));
1228 return ret;
1229 }
c277df42
IZ
1230 return reganode(EVAL, n);
1231 }
1232 case '(':
1233 {
3280af22
NIS
1234 if (PL_regcomp_parse[0] == '?') {
1235 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1236 || PL_regcomp_parse[1] == '<'
1237 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
c277df42
IZ
1238 I32 flag;
1239
1240 ret = reg_node(LOGICAL);
0f5d15d6
IZ
1241 if (!SIZE_ONLY)
1242 ret->flags = 1;
c277df42
IZ
1243 regtail(ret, reg(1, &flag));
1244 goto insert_if;
1245 }
a0ed51b3
LW
1246 }
1247 else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
3280af22 1248 parno = atoi(PL_regcomp_parse++);
c277df42 1249
3280af22
NIS
1250 while (isDIGIT(*PL_regcomp_parse))
1251 PL_regcomp_parse++;
c277df42
IZ
1252 ret = reganode(GROUPP, parno);
1253 if ((c = *nextchar()) != ')')
1254 FAIL2("Switch (?(number%c not recognized", c);
1255 insert_if:
1256 regtail(ret, reganode(IFTHEN, 0));
1257 br = regbranch(&flags, 1);
1258 if (br == NULL)
1259 br = reganode(LONGJMP, 0);
1260 else
1261 regtail(br, reganode(LONGJMP, 0));
1262 c = *nextchar();
d1b80229
IZ
1263 if (flags&HASWIDTH)
1264 *flagp |= HASWIDTH;
c277df42
IZ
1265 if (c == '|') {
1266 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1267 regbranch(&flags, 1);
1268 regtail(ret, lastbr);
d1b80229
IZ
1269 if (flags&HASWIDTH)
1270 *flagp |= HASWIDTH;
c277df42 1271 c = *nextchar();
a0ed51b3
LW
1272 }
1273 else
c277df42
IZ
1274 lastbr = NULL;
1275 if (c != ')')
1276 FAIL("Switch (?(condition)... contains too many branches");
1277 ender = reg_node(TAIL);
1278 regtail(br, ender);
1279 if (lastbr) {
1280 regtail(lastbr, ender);
1281 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
a0ed51b3
LW
1282 }
1283 else
c277df42
IZ
1284 regtail(ret, ender);
1285 return ret;
a0ed51b3
LW
1286 }
1287 else {
3280af22 1288 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
c277df42
IZ
1289 }
1290 }
1b1626e4 1291 case 0:
c277df42 1292 FAIL("Sequence (? incomplete");
1b1626e4 1293 break;
a0d0e21e 1294 default:
3280af22 1295 --PL_regcomp_parse;
ca9dfc88 1296 parse_flags:
3280af22
NIS
1297 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1298 if (*PL_regcomp_parse != 'o')
1299 pmflag(flagsp, *PL_regcomp_parse);
1300 ++PL_regcomp_parse;
ca9dfc88 1301 }
3280af22 1302 if (*PL_regcomp_parse == '-') {
ca9dfc88 1303 flagsp = &negflags;
3280af22 1304 ++PL_regcomp_parse;
ca9dfc88 1305 goto parse_flags;
48c036b1 1306 }
3280af22
NIS
1307 PL_regflags |= posflags;
1308 PL_regflags &= ~negflags;
1309 if (*PL_regcomp_parse == ':') {
1310 PL_regcomp_parse++;
ca9dfc88
IZ
1311 paren = ':';
1312 break;
1313 }
c277df42 1314 unknown:
3280af22
NIS
1315 if (*PL_regcomp_parse != ')')
1316 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
a0d0e21e
LW
1317 nextchar();
1318 *flagp = TRYAGAIN;
1319 return NULL;
1320 }
1321 }
1322 else {
3280af22
NIS
1323 parno = PL_regnpar;
1324 PL_regnpar++;
a0d0e21e 1325 ret = reganode(OPEN, parno);
c277df42 1326 open = 1;
a0d0e21e 1327 }
a0ed51b3
LW
1328 }
1329 else
a0d0e21e
LW
1330 ret = NULL;
1331
1332 /* Pick up the branches, linking them together. */
c277df42 1333 br = regbranch(&flags, 1);
a0d0e21e
LW
1334 if (br == NULL)
1335 return(NULL);
3280af22
NIS
1336 if (*PL_regcomp_parse == '|') {
1337 if (!SIZE_ONLY && PL_extralen) {
c277df42 1338 reginsert(BRANCHJ, br);
a0ed51b3
LW
1339 }
1340 else
c277df42
IZ
1341 reginsert(BRANCH, br);
1342 have_branch = 1;
1343 if (SIZE_ONLY)
3280af22 1344 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
a0ed51b3
LW
1345 }
1346 else if (paren == ':') {
c277df42
IZ
1347 *flagp |= flags&SIMPLE;
1348 }
1349 if (open) { /* Starts with OPEN. */
1350 regtail(ret, br); /* OPEN -> first. */
a0ed51b3
LW
1351 }
1352 else if (paren != '?') /* Not Conditional */
a0d0e21e 1353 ret = br;
821b33a5
IZ
1354 if (flags&HASWIDTH)
1355 *flagp |= HASWIDTH;
a0d0e21e 1356 *flagp |= flags&SPSTART;
c277df42 1357 lastbr = br;
3280af22
NIS
1358 while (*PL_regcomp_parse == '|') {
1359 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1360 ender = reganode(LONGJMP,0);
1361 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1362 }
1363 if (SIZE_ONLY)
3280af22 1364 PL_extralen += 2; /* Account for LONGJMP. */
a0d0e21e 1365 nextchar();
c277df42 1366 br = regbranch(&flags, 0);
a687059c 1367 if (br == NULL)
a0d0e21e 1368 return(NULL);
c277df42
IZ
1369 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1370 lastbr = br;
821b33a5
IZ
1371 if (flags&HASWIDTH)
1372 *flagp |= HASWIDTH;
a687059c 1373 *flagp |= flags&SPSTART;
a0d0e21e
LW
1374 }
1375
c277df42
IZ
1376 if (have_branch || paren != ':') {
1377 /* Make a closing node, and hook it on the end. */
1378 switch (paren) {
1379 case ':':
1380 ender = reg_node(TAIL);
1381 break;
1382 case 1:
1383 ender = reganode(CLOSE, parno);
1384 break;
1385 case '<':
c277df42
IZ
1386 case ',':
1387 case '=':
1388 case '!':
c277df42 1389 *flagp &= ~HASWIDTH;
821b33a5
IZ
1390 /* FALL THROUGH */
1391 case '>':
1392 ender = reg_node(SUCCEED);
c277df42
IZ
1393 break;
1394 case 0:
1395 ender = reg_node(END);
1396 break;
1397 }
1398 regtail(lastbr, ender);
a0d0e21e 1399
c277df42
IZ
1400 if (have_branch) {
1401 /* Hook the tails of the branches to the closing node. */
1402 for (br = ret; br != NULL; br = regnext(br)) {
1403 regoptail(br, ender);
1404 }
1405 }
a0d0e21e 1406 }
c277df42
IZ
1407
1408 {
1409 char *p;
1410 static char parens[] = "=!<,>";
1411
1412 if (paren && (p = strchr(parens, paren))) {
1413 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1414 int flag = (p - parens) > 1;
1415
1416 if (paren == '>')
1417 node = SUSPEND, flag = 0;
1418 reginsert(node,ret);
c277df42 1419 ret->flags = flag;
c277df42
IZ
1420 regtail(ret, reg_node(TAIL));
1421 }
a0d0e21e
LW
1422 }
1423
1424 /* Check for proper termination. */
ce3e6498
HS
1425 if (paren) {
1426 PL_regflags = oregflags;
1427 if (PL_regcomp_parse >= PL_regxend || *nextchar() != ')') {
1428 FAIL("unmatched () in regexp");
1429 }
a0ed51b3
LW
1430 }
1431 else if (!paren && PL_regcomp_parse < PL_regxend) {
3280af22 1432 if (*PL_regcomp_parse == ')') {
a0d0e21e 1433 FAIL("unmatched () in regexp");
a0ed51b3
LW
1434 }
1435 else
a0d0e21e
LW
1436 FAIL("junk on end of regexp"); /* "Can't happen". */
1437 /* NOTREACHED */
1438 }
a687059c 1439
a0d0e21e 1440 return(ret);
a687059c
LW
1441}
1442
1443/*
1444 - regbranch - one alternative of an | operator
1445 *
1446 * Implements the concatenation operator.
1447 */
76e3520e 1448STATIC regnode *
cea2e8a9 1449S_regbranch(pTHX_ I32 *flagp, I32 first)
a687059c 1450{
5c0ca799 1451 dTHR;
c277df42
IZ
1452 register regnode *ret;
1453 register regnode *chain = NULL;
1454 register regnode *latest;
1455 I32 flags = 0, c = 0;
a0d0e21e 1456
c277df42
IZ
1457 if (first)
1458 ret = NULL;
1459 else {
3280af22 1460 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1461 ret = reganode(BRANCHJ,0);
1462 else
1463 ret = reg_node(BRANCH);
1464 }
1465
1466 if (!first && SIZE_ONLY)
3280af22 1467 PL_extralen += 1; /* BRANCHJ */
c277df42
IZ
1468
1469 *flagp = WORST; /* Tentatively. */
a0d0e21e 1470
3280af22 1471 PL_regcomp_parse--;
a0d0e21e 1472 nextchar();
3280af22 1473 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
a0d0e21e
LW
1474 flags &= ~TRYAGAIN;
1475 latest = regpiece(&flags);
1476 if (latest == NULL) {
1477 if (flags & TRYAGAIN)
1478 continue;
1479 return(NULL);
a0ed51b3
LW
1480 }
1481 else if (ret == NULL)
c277df42 1482 ret = latest;
a0d0e21e 1483 *flagp |= flags&HASWIDTH;
c277df42 1484 if (chain == NULL) /* First piece. */
a0d0e21e
LW
1485 *flagp |= flags&SPSTART;
1486 else {
3280af22 1487 PL_regnaughty++;
a0d0e21e 1488 regtail(chain, latest);
a687059c 1489 }
a0d0e21e 1490 chain = latest;
c277df42
IZ
1491 c++;
1492 }
1493 if (chain == NULL) { /* Loop ran zero times. */
1494 chain = reg_node(NOTHING);
1495 if (ret == NULL)
1496 ret = chain;
1497 }
1498 if (c == 1) {
1499 *flagp |= flags&SIMPLE;
a0d0e21e 1500 }
a687059c 1501
a0d0e21e 1502 return(ret);
a687059c
LW
1503}
1504
1505/*
1506 - regpiece - something followed by possible [*+?]
1507 *
1508 * Note that the branching code sequences used for ? and the general cases
1509 * of * and + are somewhat optimized: they use the same NOTHING node as
1510 * both the endmarker for their branch list and the body of the last branch.
1511 * It might seem that this node could be dispensed with entirely, but the
1512 * endmarker role is not redundant.
1513 */
76e3520e 1514STATIC regnode *
cea2e8a9 1515S_regpiece(pTHX_ I32 *flagp)
a687059c 1516{
5c0ca799 1517 dTHR;
c277df42 1518 register regnode *ret;
a0d0e21e
LW
1519 register char op;
1520 register char *next;
1521 I32 flags;
3280af22 1522 char *origparse = PL_regcomp_parse;
a0d0e21e
LW
1523 char *maxpos;
1524 I32 min;
c277df42 1525 I32 max = REG_INFTY;
a0d0e21e
LW
1526
1527 ret = regatom(&flags);
1528 if (ret == NULL) {
1529 if (flags & TRYAGAIN)
1530 *flagp |= TRYAGAIN;
1531 return(NULL);
1532 }
1533
3280af22 1534 op = *PL_regcomp_parse;
a0d0e21e 1535
3280af22
NIS
1536 if (op == '{' && regcurly(PL_regcomp_parse)) {
1537 next = PL_regcomp_parse + 1;
a0d0e21e
LW
1538 maxpos = Nullch;
1539 while (isDIGIT(*next) || *next == ',') {
1540 if (*next == ',') {
1541 if (maxpos)
1542 break;
1543 else
1544 maxpos = next;
a687059c 1545 }
a0d0e21e
LW
1546 next++;
1547 }
1548 if (*next == '}') { /* got one */
1549 if (!maxpos)
1550 maxpos = next;
3280af22
NIS
1551 PL_regcomp_parse++;
1552 min = atoi(PL_regcomp_parse);
a0d0e21e
LW
1553 if (*maxpos == ',')
1554 maxpos++;
1555 else
3280af22 1556 maxpos = PL_regcomp_parse;
a0d0e21e
LW
1557 max = atoi(maxpos);
1558 if (!max && *maxpos != '0')
c277df42
IZ
1559 max = REG_INFTY; /* meaning "infinity" */
1560 else if (max >= REG_INFTY)
1561 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
3280af22 1562 PL_regcomp_parse = next;
a0d0e21e
LW
1563 nextchar();
1564
1565 do_curly:
1566 if ((flags&SIMPLE)) {
3280af22 1567 PL_regnaughty += 2 + PL_regnaughty / 2;
a0d0e21e
LW
1568 reginsert(CURLY, ret);
1569 }
1570 else {
2c2d71f5
JH
1571 regnode *w = reg_node(WHILEM);
1572
1573 w->flags = 0;
1574 regtail(ret, w);
3280af22 1575 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1576 reginsert(LONGJMP,ret);
1577 reginsert(NOTHING,ret);
1578 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1579 }
a0d0e21e 1580 reginsert(CURLYX,ret);
3280af22 1581 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1582 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1583 regtail(ret, reg_node(NOTHING));
1584 if (SIZE_ONLY)
2c2d71f5
JH
1585 PL_reg_whilem_seen++, PL_extralen += 3;
1586 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
a0d0e21e 1587 }
c277df42 1588 ret->flags = 0;
a0d0e21e
LW
1589
1590 if (min > 0)
821b33a5
IZ
1591 *flagp = WORST;
1592 if (max > 0)
1593 *flagp |= HASWIDTH;
a0d0e21e 1594 if (max && max < min)
c277df42
IZ
1595 FAIL("Can't do {n,m} with n > m");
1596 if (!SIZE_ONLY) {
1597 ARG1_SET(ret, min);
1598 ARG2_SET(ret, max);
a687059c 1599 }
a687059c 1600
a0d0e21e 1601 goto nest_check;
a687059c 1602 }
a0d0e21e 1603 }
a687059c 1604
a0d0e21e
LW
1605 if (!ISMULT1(op)) {
1606 *flagp = flags;
a687059c 1607 return(ret);
a0d0e21e 1608 }
bb20fd44 1609
c277df42 1610#if 0 /* Now runtime fix should be reliable. */
bb20fd44 1611 if (!(flags&HASWIDTH) && op != '?')
c277df42
IZ
1612 FAIL("regexp *+ operand could be empty");
1613#endif
bb20fd44 1614
a0d0e21e
LW
1615 nextchar();
1616
821b33a5 1617 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
a0d0e21e
LW
1618
1619 if (op == '*' && (flags&SIMPLE)) {
1620 reginsert(STAR, ret);
c277df42 1621 ret->flags = 0;
3280af22 1622 PL_regnaughty += 4;
a0d0e21e
LW
1623 }
1624 else if (op == '*') {
1625 min = 0;
1626 goto do_curly;
a0ed51b3
LW
1627 }
1628 else if (op == '+' && (flags&SIMPLE)) {
a0d0e21e 1629 reginsert(PLUS, ret);
c277df42 1630 ret->flags = 0;
3280af22 1631 PL_regnaughty += 3;
a0d0e21e
LW
1632 }
1633 else if (op == '+') {
1634 min = 1;
1635 goto do_curly;
a0ed51b3
LW
1636 }
1637 else if (op == '?') {
a0d0e21e
LW
1638 min = 0; max = 1;
1639 goto do_curly;
1640 }
1641 nest_check:
17feb5d5 1642 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY && !(flags&HASWIDTH) && max > REG_INFTY/3) {
cea2e8a9 1643 Perl_warner(aTHX_ WARN_UNSAFE, "%.*s matches null string many times",
3280af22 1644 PL_regcomp_parse - origparse, origparse);
a0d0e21e
LW
1645 }
1646
3280af22 1647 if (*PL_regcomp_parse == '?') {
a0d0e21e
LW
1648 nextchar();
1649 reginsert(MINMOD, ret);
c277df42 1650 regtail(ret, ret + NODE_STEP_REGNODE);
a0d0e21e 1651 }
3280af22 1652 if (ISMULT2(PL_regcomp_parse))
a0d0e21e
LW
1653 FAIL("nested *?+ in regexp");
1654
1655 return(ret);
a687059c
LW
1656}
1657
1658/*
1659 - regatom - the lowest level
1660 *
1661 * Optimization: gobbles an entire sequence of ordinary characters so that
1662 * it can turn them into a single node, which is smaller to store and
1663 * faster to run. Backslashed characters are exceptions, each becoming a
1664 * separate node; the code is simpler that way and it's not worth fixing.
1665 *
1666 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1667 */
76e3520e 1668STATIC regnode *
cea2e8a9 1669S_regatom(pTHX_ I32 *flagp)
a687059c 1670{
5c0ca799 1671 dTHR;
c277df42 1672 register regnode *ret = 0;
a0d0e21e
LW
1673 I32 flags;
1674
1675 *flagp = WORST; /* Tentatively. */
1676
1677tryagain:
3280af22 1678 switch (*PL_regcomp_parse) {
a0d0e21e 1679 case '^':
3280af22 1680 PL_seen_zerolen++;
a0d0e21e 1681 nextchar();
3280af22 1682 if (PL_regflags & PMf_MULTILINE)
c277df42 1683 ret = reg_node(MBOL);
3280af22 1684 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1685 ret = reg_node(SBOL);
a0d0e21e 1686 else
c277df42 1687 ret = reg_node(BOL);
a0d0e21e
LW
1688 break;
1689 case '$':
3280af22
NIS
1690 if (PL_regcomp_parse[1])
1691 PL_seen_zerolen++;
a0d0e21e 1692 nextchar();
3280af22 1693 if (PL_regflags & PMf_MULTILINE)
c277df42 1694 ret = reg_node(MEOL);
3280af22 1695 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1696 ret = reg_node(SEOL);
a0d0e21e 1697 else
c277df42 1698 ret = reg_node(EOL);
a0d0e21e
LW
1699 break;
1700 case '.':
1701 nextchar();
a0ed51b3
LW
1702 if (UTF) {
1703 if (PL_regflags & PMf_SINGLELINE)
1704 ret = reg_node(SANYUTF8);
1705 else
1706 ret = reg_node(ANYUTF8);
1707 *flagp |= HASWIDTH;
1708 }
1709 else {
1710 if (PL_regflags & PMf_SINGLELINE)
1711 ret = reg_node(SANY);
1712 else
22c35a8c 1713 ret = reg_node(REG_ANY);
a0ed51b3
LW
1714 *flagp |= HASWIDTH|SIMPLE;
1715 }
3280af22 1716 PL_regnaughty++;
a0d0e21e
LW
1717 break;
1718 case '[':
3280af22 1719 PL_regcomp_parse++;
a0ed51b3 1720 ret = (UTF ? regclassutf8() : regclass());
a14b48bc
LW
1721 if (*PL_regcomp_parse != ']')
1722 FAIL("unmatched [] in regexp");
1723 nextchar();
a0d0e21e
LW
1724 *flagp |= HASWIDTH|SIMPLE;
1725 break;
1726 case '(':
1727 nextchar();
1728 ret = reg(1, &flags);
1729 if (ret == NULL) {
1730 if (flags & TRYAGAIN)
1731 goto tryagain;
1732 return(NULL);
1733 }
c277df42 1734 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
a0d0e21e
LW
1735 break;
1736 case '|':
1737 case ')':
1738 if (flags & TRYAGAIN) {
1739 *flagp |= TRYAGAIN;
1740 return NULL;
1741 }
3280af22 1742 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
a0d0e21e
LW
1743 /* Supposed to be caught earlier. */
1744 break;
85afd4ae 1745 case '{':
3280af22
NIS
1746 if (!regcurly(PL_regcomp_parse)) {
1747 PL_regcomp_parse++;
85afd4ae
CS
1748 goto defchar;
1749 }
1750 /* FALL THROUGH */
a0d0e21e
LW
1751 case '?':
1752 case '+':
1753 case '*':
3115e423 1754 FAIL("?+*{} follows nothing in regexp");
a0d0e21e
LW
1755 break;
1756 case '\\':
3280af22 1757 switch (*++PL_regcomp_parse) {
a0d0e21e 1758 case 'A':
3280af22 1759 PL_seen_zerolen++;
c277df42 1760 ret = reg_node(SBOL);
a0d0e21e
LW
1761 *flagp |= SIMPLE;
1762 nextchar();
1763 break;
1764 case 'G':
c277df42 1765 ret = reg_node(GPOS);
3280af22 1766 PL_regseen |= REG_SEEN_GPOS;
a0d0e21e
LW
1767 *flagp |= SIMPLE;
1768 nextchar();
1769 break;
1770 case 'Z':
c277df42 1771 ret = reg_node(SEOL);
a0d0e21e
LW
1772 *flagp |= SIMPLE;
1773 nextchar();
1774 break;
b85d18e9
IZ
1775 case 'z':
1776 ret = reg_node(EOS);
1777 *flagp |= SIMPLE;
3280af22 1778 PL_seen_zerolen++; /* Do not optimize RE away */
b85d18e9
IZ
1779 nextchar();
1780 break;
4a2d328f 1781 case 'C':
a0ed51b3
LW
1782 ret = reg_node(SANY);
1783 *flagp |= HASWIDTH|SIMPLE;
1784 nextchar();
1785 break;
1786 case 'X':
1787 ret = reg_node(CLUMP);
1788 *flagp |= HASWIDTH;
1789 nextchar();
1790 if (UTF && !PL_utf8_mark)
dfe13c55 1791 is_utf8_mark((U8*)"~"); /* preload table */
a0ed51b3 1792 break;
a0d0e21e 1793 case 'w':
a0ed51b3
LW
1794 ret = reg_node(
1795 UTF
1796 ? (LOC ? ALNUMLUTF8 : ALNUMUTF8)
1797 : (LOC ? ALNUML : ALNUM));
a0d0e21e
LW
1798 *flagp |= HASWIDTH|SIMPLE;
1799 nextchar();
a0ed51b3 1800 if (UTF && !PL_utf8_alnum)
dfe13c55 1801 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1802 break;
1803 case 'W':
a0ed51b3
LW
1804 ret = reg_node(
1805 UTF
1806 ? (LOC ? NALNUMLUTF8 : NALNUMUTF8)
1807 : (LOC ? NALNUML : NALNUM));
a0d0e21e
LW
1808 *flagp |= HASWIDTH|SIMPLE;
1809 nextchar();
a0ed51b3 1810 if (UTF && !PL_utf8_alnum)
dfe13c55 1811 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1812 break;
1813 case 'b':
3280af22 1814 PL_seen_zerolen++;
f5c9036e 1815 PL_regseen |= REG_SEEN_LOOKBEHIND;
a0ed51b3
LW
1816 ret = reg_node(
1817 UTF
1818 ? (LOC ? BOUNDLUTF8 : BOUNDUTF8)
1819 : (LOC ? BOUNDL : BOUND));
a0d0e21e
LW
1820 *flagp |= SIMPLE;
1821 nextchar();
a0ed51b3 1822 if (UTF && !PL_utf8_alnum)
dfe13c55 1823 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1824 break;
1825 case 'B':
3280af22 1826 PL_seen_zerolen++;
f5c9036e 1827 PL_regseen |= REG_SEEN_LOOKBEHIND;
a0ed51b3
LW
1828 ret = reg_node(
1829 UTF
1830 ? (LOC ? NBOUNDLUTF8 : NBOUNDUTF8)
1831 : (LOC ? NBOUNDL : NBOUND));
a0d0e21e
LW
1832 *flagp |= SIMPLE;
1833 nextchar();
a0ed51b3 1834 if (UTF && !PL_utf8_alnum)
dfe13c55 1835 is_utf8_alnum((U8*)"a"); /* preload table */
a0d0e21e
LW
1836 break;
1837 case 's':
a0ed51b3
LW
1838 ret = reg_node(
1839 UTF
1840 ? (LOC ? SPACELUTF8 : SPACEUTF8)
1841 : (LOC ? SPACEL : SPACE));
a0d0e21e
LW
1842 *flagp |= HASWIDTH|SIMPLE;
1843 nextchar();
a0ed51b3 1844 if (UTF && !PL_utf8_space)
dfe13c55 1845 is_utf8_space((U8*)" "); /* preload table */
a0d0e21e
LW
1846 break;
1847 case 'S':
a0ed51b3
LW
1848 ret = reg_node(
1849 UTF
1850 ? (LOC ? NSPACELUTF8 : NSPACEUTF8)
1851 : (LOC ? NSPACEL : NSPACE));
a0d0e21e
LW
1852 *flagp |= HASWIDTH|SIMPLE;
1853 nextchar();
a0ed51b3 1854 if (UTF && !PL_utf8_space)
dfe13c55 1855 is_utf8_space((U8*)" "); /* preload table */
a0d0e21e
LW
1856 break;
1857 case 'd':
a0ed51b3 1858 ret = reg_node(UTF ? DIGITUTF8 : DIGIT);
a0d0e21e
LW
1859 *flagp |= HASWIDTH|SIMPLE;
1860 nextchar();
a0ed51b3 1861 if (UTF && !PL_utf8_digit)
dfe13c55 1862 is_utf8_digit((U8*)"1"); /* preload table */
a0d0e21e
LW
1863 break;
1864 case 'D':
a0ed51b3 1865 ret = reg_node(UTF ? NDIGITUTF8 : NDIGIT);
a0d0e21e
LW
1866 *flagp |= HASWIDTH|SIMPLE;
1867 nextchar();
a0ed51b3 1868 if (UTF && !PL_utf8_digit)
dfe13c55 1869 is_utf8_digit((U8*)"1"); /* preload table */
a0d0e21e 1870 break;
a14b48bc
LW
1871 case 'p':
1872 case 'P':
1873 { /* a lovely hack--pretend we saw [\pX] instead */
1874 char* oldregxend = PL_regxend;
1875
1876 if (PL_regcomp_parse[1] == '{') {
1877 PL_regxend = strchr(PL_regcomp_parse, '}');
1878 if (!PL_regxend)
1879 FAIL("Missing right brace on \\p{}");
1880 PL_regxend++;
1881 }
1882 else
1883 PL_regxend = PL_regcomp_parse + 2;
1884 PL_regcomp_parse--;
1885
1886 ret = regclassutf8();
1887
1888 PL_regxend = oldregxend;
1889 PL_regcomp_parse--;
1890 nextchar();
1891 *flagp |= HASWIDTH|SIMPLE;
1892 }
1893 break;
a0d0e21e
LW
1894 case 'n':
1895 case 'r':
1896 case 't':
1897 case 'f':
1898 case 'e':
1899 case 'a':
1900 case 'x':
1901 case 'c':
1902 case '0':
1903 goto defchar;
1904 case '1': case '2': case '3': case '4':
1905 case '5': case '6': case '7': case '8': case '9':
1906 {
3280af22 1907 I32 num = atoi(PL_regcomp_parse);
a0d0e21e 1908
3280af22 1909 if (num > 9 && num >= PL_regnpar)
a0d0e21e
LW
1910 goto defchar;
1911 else {
3280af22 1912 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
ef64f398 1913 FAIL("reference to nonexistent group");
3280af22 1914 PL_regsawback = 1;
a0ed51b3
LW
1915 ret = reganode(FOLD
1916 ? (LOC ? REFFL : REFF)
c8756f30 1917 : REF, num);
a0d0e21e 1918 *flagp |= HASWIDTH;
3280af22
NIS
1919 while (isDIGIT(*PL_regcomp_parse))
1920 PL_regcomp_parse++;
1921 PL_regcomp_parse--;
a0d0e21e
LW
1922 nextchar();
1923 }
1924 }
1925 break;
1926 case '\0':
3280af22 1927 if (PL_regcomp_parse >= PL_regxend)
a0d0e21e
LW
1928 FAIL("trailing \\ in regexp");
1929 /* FALL THROUGH */
1930 default:
c9f97d15
IZ
1931 /* Do not generate `unrecognized' warnings here, we fall
1932 back into the quick-grab loop below */
a0d0e21e
LW
1933 goto defchar;
1934 }
1935 break;
4633a7c4
LW
1936
1937 case '#':
3280af22
NIS
1938 if (PL_regflags & PMf_EXTENDED) {
1939 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1940 if (PL_regcomp_parse < PL_regxend)
4633a7c4
LW
1941 goto tryagain;
1942 }
1943 /* FALL THROUGH */
1944
a0d0e21e
LW
1945 default: {
1946 register I32 len;
a0ed51b3 1947 register UV ender;
a0d0e21e 1948 register char *p;
c277df42 1949 char *oldp, *s;
a0d0e21e
LW
1950 I32 numlen;
1951
3280af22 1952 PL_regcomp_parse++;
a0d0e21e
LW
1953
1954 defchar:
a0ed51b3
LW
1955 ret = reg_node(FOLD
1956 ? (LOC ? EXACTFL : EXACTF)
bbce6d69 1957 : EXACT);
cd439c50 1958 s = STRING(ret);
3280af22
NIS
1959 for (len = 0, p = PL_regcomp_parse - 1;
1960 len < 127 && p < PL_regxend;
a0d0e21e
LW
1961 len++)
1962 {
1963 oldp = p;
5b5a24f7 1964
3280af22
NIS
1965 if (PL_regflags & PMf_EXTENDED)
1966 p = regwhite(p, PL_regxend);
a0d0e21e
LW
1967 switch (*p) {
1968 case '^':
1969 case '$':
1970 case '.':
1971 case '[':
1972 case '(':
1973 case ')':
1974 case '|':
1975 goto loopdone;
1976 case '\\':
1977 switch (*++p) {
1978 case 'A':
1979 case 'G':
1980 case 'Z':
b85d18e9 1981 case 'z':
a0d0e21e
LW
1982 case 'w':
1983 case 'W':
1984 case 'b':
1985 case 'B':
1986 case 's':
1987 case 'S':
1988 case 'd':
1989 case 'D':
a14b48bc
LW
1990 case 'p':
1991 case 'P':
a0d0e21e
LW
1992 --p;
1993 goto loopdone;
1994 case 'n':
1995 ender = '\n';
1996 p++;
a687059c 1997 break;
a0d0e21e
LW
1998 case 'r':
1999 ender = '\r';
2000 p++;
a687059c 2001 break;
a0d0e21e
LW
2002 case 't':
2003 ender = '\t';
2004 p++;
a687059c 2005 break;
a0d0e21e
LW
2006 case 'f':
2007 ender = '\f';
2008 p++;
a687059c 2009 break;
a0d0e21e
LW
2010 case 'e':
2011 ender = '\033';
2012 p++;
a687059c 2013 break;
a0d0e21e
LW
2014 case 'a':
2015 ender = '\007';
2016 p++;
a687059c 2017 break;
a0d0e21e 2018 case 'x':
a0ed51b3
LW
2019 if (*++p == '{') {
2020 char* e = strchr(p, '}');
2021
2022 if (!e)
2023 FAIL("Missing right brace on \\x{}");
2024 else if (UTF) {
2025 ender = scan_hex(p + 1, e - p, &numlen);
2026 if (numlen + len >= 127) { /* numlen is generous */
2027 p--;
2028 goto loopdone;
2029 }
2030 p = e + 1;
2031 }
2032 else
2033 FAIL("Can't use \\x{} without 'use utf8' declaration");
2034 }
2035 else {
2036 ender = scan_hex(p, 2, &numlen);
2037 p += numlen;
2038 }
a687059c 2039 break;
a0d0e21e
LW
2040 case 'c':
2041 p++;
bbce6d69
PP
2042 ender = UCHARAT(p++);
2043 ender = toCTRL(ender);
a687059c 2044 break;
a0d0e21e
LW
2045 case '0': case '1': case '2': case '3':case '4':
2046 case '5': case '6': case '7': case '8':case '9':
2047 if (*p == '0' ||
3280af22 2048 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
a0d0e21e
LW
2049 ender = scan_oct(p, 3, &numlen);
2050 p += numlen;
2051 }
2052 else {
2053 --p;
2054 goto loopdone;
a687059c
LW
2055 }
2056 break;
a0d0e21e 2057 case '\0':
3280af22 2058 if (p >= PL_regxend)
a687059c
LW
2059 FAIL("trailing \\ in regexp");
2060 /* FALL THROUGH */
a0d0e21e 2061 default:
c9f97d15 2062 if (!SIZE_ONLY && ckWARN(WARN_UNSAFE) && isALPHA(*p))
cea2e8a9 2063 Perl_warner(aTHX_ WARN_UNSAFE,
c9f97d15
IZ
2064 "/%.127s/: Unrecognized escape \\%c passed through",
2065 PL_regprecomp,
2066 *p);
a0ed51b3 2067 goto normal_default;
a0d0e21e
LW
2068 }
2069 break;
a687059c 2070 default:
a0ed51b3
LW
2071 normal_default:
2072 if ((*p & 0xc0) == 0xc0 && UTF) {
dfe13c55 2073 ender = utf8_to_uv((U8*)p, &numlen);
a0ed51b3
LW
2074 p += numlen;
2075 }
2076 else
2077 ender = *p++;
a0d0e21e 2078 break;
a687059c 2079 }
3280af22
NIS
2080 if (PL_regflags & PMf_EXTENDED)
2081 p = regwhite(p, PL_regxend);
a0ed51b3
LW
2082 if (UTF && FOLD) {
2083 if (LOC)
2084 ender = toLOWER_LC_uni(ender);
2085 else
2086 ender = toLOWER_uni(ender);
2087 }
a0d0e21e
LW
2088 if (ISMULT2(p)) { /* Back off on ?+*. */
2089 if (len)
2090 p = oldp;
a0ed51b3
LW
2091 else if (ender >= 0x80 && UTF) {
2092 reguni(ender, s, &numlen);
2093 s += numlen;
2094 len += numlen;
2095 }
a0d0e21e
LW
2096 else {
2097 len++;
cd439c50 2098 REGC(ender, s++);
a0d0e21e
LW
2099 }
2100 break;
a687059c 2101 }
a0ed51b3
LW
2102 if (ender >= 0x80 && UTF) {
2103 reguni(ender, s, &numlen);
2104 s += numlen;
2105 len += numlen - 1;
2106 }
2107 else
cd439c50 2108 REGC(ender, s++);
a0d0e21e
LW
2109 }
2110 loopdone:
3280af22 2111 PL_regcomp_parse = p - 1;
a0d0e21e
LW
2112 nextchar();
2113 if (len < 0)
2114 FAIL("internal disaster in regexp");
2115 if (len > 0)
2116 *flagp |= HASWIDTH;
2117 if (len == 1)
2118 *flagp |= SIMPLE;
c277df42 2119 if (!SIZE_ONLY)
cd439c50
IZ
2120 STR_LEN(ret) = len;
2121 if (SIZE_ONLY)
2122 PL_regsize += STR_SZ(len);
2123 else
2124 PL_regcode += STR_SZ(len);
a687059c 2125 }
a0d0e21e
LW
2126 break;
2127 }
a687059c 2128
a0d0e21e 2129 return(ret);
a687059c
LW
2130}
2131
873ef191 2132STATIC char *
cea2e8a9 2133S_regwhite(pTHX_ char *p, char *e)
5b5a24f7
CS
2134{
2135 while (p < e) {
2136 if (isSPACE(*p))
2137 ++p;
2138 else if (*p == '#') {
2139 do {
2140 p++;
2141 } while (p < e && *p != '\n');
2142 }
2143 else
2144 break;
2145 }
2146 return p;
2147}
2148
b8c5462f
JH
2149/* Parse POSIX character classes: [[:foo:]], [[=foo=]], [[.foo.]].
2150 Character classes ([:foo:]) can also be negated ([:^foo:]).
2151 Returns a named class id (ANYOF_XXX) if successful, -1 otherwise.
2152 Equivalence classes ([=foo=]) and composites ([.foo.]) are parsed,
2153 but trigger warnings because they are currently unimplemented. */
2154STATIC I32
cea2e8a9 2155S_regpposixcc(pTHX_ I32 value)
620e46c5 2156{
11b8faa4 2157 dTHR;
620e46c5 2158 char *posixcc = 0;
b8c5462f 2159 I32 namedclass = -1;
620e46c5
JH
2160
2161 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2162 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2163 (*PL_regcomp_parse == ':' ||
2164 *PL_regcomp_parse == '=' ||
2165 *PL_regcomp_parse == '.')) {
2166 char c = *PL_regcomp_parse;
2167 char* s = PL_regcomp_parse++;
2168
2169 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2170 PL_regcomp_parse++;
2171 if (PL_regcomp_parse == PL_regxend)
2172 /* Grandfather lone [:, [=, [. */
2173 PL_regcomp_parse = s;
2174 else {
b8c5462f
JH
2175 char* t = PL_regcomp_parse++; /* skip over the c */
2176
2177 if (*PL_regcomp_parse == ']') {
2178 PL_regcomp_parse++; /* skip over the ending ] */
2179 posixcc = s + 1;
2180 if (*s == ':') {
2181 I32 complement = *posixcc == '^' ? *posixcc++ : 0;
2182 I32 skip = 5; /* the most common skip */
2183
2184 switch (*posixcc) {
2185 case 'a':
2186 if (strnEQ(posixcc, "alnum", 5))
2187 namedclass =
2188 complement ? ANYOF_NALNUMC : ANYOF_ALNUMC;
2189 else if (strnEQ(posixcc, "alpha", 5))
2190 namedclass =
2191 complement ? ANYOF_NALPHA : ANYOF_ALPHA;
2192 else if (strnEQ(posixcc, "ascii", 5))
2193 namedclass =
2194 complement ? ANYOF_NASCII : ANYOF_ASCII;
2195 break;
2196 case 'c':
2197 if (strnEQ(posixcc, "cntrl", 5))
2198 namedclass =
2199 complement ? ANYOF_NCNTRL : ANYOF_CNTRL;
2200 break;
2201 case 'd':
2202 if (strnEQ(posixcc, "digit", 5))
2203 namedclass =
2204 complement ? ANYOF_NDIGIT : ANYOF_DIGIT;
2205 break;
2206 case 'g':
2207 if (strnEQ(posixcc, "graph", 5))
2208 namedclass =
2209 complement ? ANYOF_NGRAPH : ANYOF_GRAPH;
2210 break;
2211 case 'l':
2212 if (strnEQ(posixcc, "lower", 5))
2213 namedclass =
2214 complement ? ANYOF_NLOWER : ANYOF_LOWER;
2215 break;
2216 case 'p':
2217 if (strnEQ(posixcc, "print", 5))
2218 namedclass =
2219 complement ? ANYOF_NPRINT : ANYOF_PRINT;
2220 else if (strnEQ(posixcc, "punct", 5))
2221 namedclass =
2222 complement ? ANYOF_NPUNCT : ANYOF_PUNCT;
2223 break;
2224 case 's':
2225 if (strnEQ(posixcc, "space", 5))
2226 namedclass =
2227 complement ? ANYOF_NSPACE : ANYOF_SPACE;
2228 case 'u':
2229 if (strnEQ(posixcc, "upper", 5))
2230 namedclass =
2231 complement ? ANYOF_NUPPER : ANYOF_UPPER;
2232 break;
2233 case 'w': /* this is not POSIX, this is the Perl \w */
2234 if (strnEQ(posixcc, "word", 4)) {
2235 namedclass =
2236 complement ? ANYOF_NALNUM : ANYOF_ALNUM;
2237 skip = 4;
2238 }
2239 break;
2240 case 'x':
2241 if (strnEQ(posixcc, "xdigit", 6)) {
2242 namedclass =
2243 complement ? ANYOF_NXDIGIT : ANYOF_XDIGIT;
2244 skip = 6;
2245 }
2246 break;
2247 }
93733859 2248 if ((namedclass == OOB_NAMEDCLASS ||
b8c5462f
JH
2249 !(posixcc + skip + 2 < PL_regxend &&
2250 (posixcc[skip] == ':' &&
2251 posixcc[skip + 1] == ']'))))
2252 Perl_croak(aTHX_ "Character class [:%.*s:] unknown",
2253 t - s - 1, s + 1);
2254 } else if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2255 /* [[=foo=]] and [[.foo.]] are still future. */
cea2e8a9 2256 Perl_warner(aTHX_ WARN_UNSAFE,
b8c5462f
JH
2257 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2258 } else {
2259 /* Maternal grandfather:
2260 * "[:" ending in ":" but not in ":]" */
767d463e
HS
2261 PL_regcomp_parse = s;
2262 }
620e46c5
JH
2263 }
2264 }
2265
b8c5462f
JH
2266 return namedclass;
2267}
2268
2269STATIC void
2270S_checkposixcc(pTHX)
2271{
2272 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY &&
2273 (*PL_regcomp_parse == ':' ||
2274 *PL_regcomp_parse == '=' ||
2275 *PL_regcomp_parse == '.')) {
2276 char *s = PL_regcomp_parse;
93733859 2277 char c = *s++;
b8c5462f
JH
2278
2279 while(*s && isALNUM(*s))
2280 s++;
2281 if (*s && c == *s && s[1] == ']') {
2282 Perl_warner(aTHX_ WARN_UNSAFE,
2283 "Character class syntax [%c %c] belongs inside character classes", c, c);
2284 if (c == '=' || c == '.')
2285 Perl_warner(aTHX_ WARN_UNSAFE,
2286 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2287 }
2288 }
620e46c5
JH
2289}
2290
76e3520e 2291STATIC regnode *
cea2e8a9 2292S_regclass(pTHX)
a687059c 2293{
5c0ca799 2294 dTHR;
c277df42 2295 register char *opnd, *s;
a0ed51b3 2296 register I32 value;
b8c5462f 2297 register I32 lastvalue = OOB_CHAR8;
a0d0e21e 2298 register I32 range = 0;
c277df42 2299 register regnode *ret;
a0d0e21e
LW
2300 register I32 def;
2301 I32 numlen;
b8c5462f 2302 I32 namedclass;
a0d0e21e 2303
cd439c50 2304 s = opnd = MASK(PL_regcode);
c277df42 2305 ret = reg_node(ANYOF);
b8c5462f 2306 for (value = 0; value < ANYOF_SIZE; value++)
cd439c50 2307 REGC(0, s++);
3280af22
NIS
2308 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2309 PL_regnaughty++;
2310 PL_regcomp_parse++;
c277df42 2311 if (!SIZE_ONLY)
b8c5462f 2312 ANYOF_FLAGS(opnd) |= ANYOF_INVERT;
bbce6d69 2313 }
c277df42 2314 if (!SIZE_ONLY) {
3280af22 2315 PL_regcode += ANY_SKIP;
a0ed51b3 2316 if (FOLD)
b8c5462f 2317 ANYOF_FLAGS(opnd) |= ANYOF_FOLD;
a0ed51b3 2318 if (LOC)
b8c5462f 2319 ANYOF_FLAGS(opnd) |= ANYOF_LOCALE;
a0ed51b3
LW
2320 }
2321 else {
3280af22 2322 PL_regsize += ANY_SKIP;
a0d0e21e 2323 }
b8c5462f
JH
2324
2325 checkposixcc();
2326
3280af22 2327 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
a0d0e21e 2328 goto skipcond; /* allow 1st char to be ] or - */
3280af22 2329 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
a0d0e21e 2330 skipcond:
93733859 2331 namedclass = OOB_NAMEDCLASS;
a0ed51b3 2332 value = UCHARAT(PL_regcomp_parse++);
620e46c5 2333 if (value == '[')
b8c5462f 2334 namedclass = regpposixcc(value);
620e46c5 2335 else if (value == '\\') {
a0ed51b3
LW
2336 value = UCHARAT(PL_regcomp_parse++);
2337 switch (value) {
b8c5462f
JH
2338 case 'w': namedclass = ANYOF_ALNUM; break;
2339 case 'W': namedclass = ANYOF_NALNUM; break;
2340 case 's': namedclass = ANYOF_SPACE; break;
2341 case 'S': namedclass = ANYOF_NSPACE; break;
2342 case 'd': namedclass = ANYOF_DIGIT; break;
2343 case 'D': namedclass = ANYOF_NDIGIT; break;
2344 case 'n': value = '\n'; break;
2345 case 'r': value = '\r'; break;
2346 case 't': value = '\t'; break;
2347 case 'f': value = '\f'; break;
2348 case 'b': value = '\b'; break;
2349 case 'e': value = '\033'; break;
2350 case 'a': value = '\007'; break;
2351 case 'x':
2352 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2353 PL_regcomp_parse += numlen;
2354 break;
2355 case 'c':
2356 value = UCHARAT(PL_regcomp_parse++);
2357 value = toCTRL(value);
2358 break;
2359 case '0': case '1': case '2': case '3': case '4':
2360 case '5': case '6': case '7': case '8': case '9':
2361 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2362 PL_regcomp_parse += numlen;
2363 break;
2364 }
2365 }
93733859 2366 if (!SIZE_ONLY && namedclass > OOB_NAMEDCLASS) {
1209ba90
JH
2367 if (range) {
2368 ANYOF_BITMAP_SET(opnd, lastvalue);
2369 ANYOF_BITMAP_SET(opnd, '-');
2370 }
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 2612 }
1209ba90
JH
2613 if (range && namedclass > OOB_NAMEDCLASS)
2614 range = 0; /* [a-\d], [a-[:digit:]], not a true range. */
a0d0e21e 2615 if (range) {
a0ed51b3 2616 if (lastvalue > value)
93733859 2617 FAIL("invalid [] range in regexp"); /* [b-a] */
a0d0e21e
LW
2618 range = 0;
2619 }
2620 else {
a0ed51b3 2621 lastvalue = value;
3280af22 2622 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
93733859 2623 PL_regcomp_parse[1] != ']') {
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 2781 if (!SIZE_ONLY && namedclass > OOB_NAMEDCLASS) {
1209ba90
JH
2782 if (range) /* [a-\d], [a-[:digit:]] */
2783 Perl_sv_catpvf(aTHX_ listsv, /* 0x002D is Unicode for '-' */
2784 "%04"UVxf"\n%002D\n", (UV)lastvalue);
2785 switch (namedclass) {
b8c5462f
JH
2786 case ANYOF_ALNUM:
2787 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsWord\n"); break;
2788 case ANYOF_NALNUM:
2789 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsWord\n"); break;
2790 case ANYOF_ALNUMC:
2791 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlnum\n"); break;
2792 case ANYOF_NALNUMC:
2793 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlnum\n"); break;
2794 case ANYOF_ALPHA:
2795 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsAlpha\n"); break;
2796 case ANYOF_NALPHA:
2797 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsAlpha\n"); break;
2798 case ANYOF_ASCII:
2799 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsASCII\n"); break;
2800 case ANYOF_NASCII:
2801 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsASCII\n"); break;
2802 case ANYOF_CNTRL:
2803 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsCntrl\n"); break;
2804 case ANYOF_NCNTRL:
2805 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsCntrl\n"); break;
2806 case ANYOF_GRAPH:
2807 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsGraph\n"); break;
2808 case ANYOF_NGRAPH:
2809 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsGraph\n"); break;
2810 case ANYOF_DIGIT:
2811 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsDigit\n"); break;
2812 case ANYOF_NDIGIT:
2813 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsDigit\n"); break;
2814 case ANYOF_LOWER:
2815 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsLower\n"); break;
2816 case ANYOF_NLOWER:
2817 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsLower\n"); break;
2818 case ANYOF_PRINT:
2819 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPrint\n"); break;
2820 case ANYOF_NPRINT:
2821 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPrint\n"); break;
2822 case ANYOF_PUNCT:
2823 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsPunct\n"); break;
2824 case ANYOF_NPUNCT:
2825 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsPunct\n"); break;
2826 case ANYOF_SPACE:
2827 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsSpace\n"); break;
2828 case ANYOF_NSPACE:
2829 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsSpace\n"); break;
2830 case ANYOF_UPPER:
2831 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsUpper\n"); break;
2832 case ANYOF_NUPPER:
2833 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsUpper\n"); break;
2834 case ANYOF_XDIGIT:
2835 Perl_sv_catpvf(aTHX_ listsv, "+utf8::IsXDigit\n"); break;
2836 case ANYOF_NXDIGIT:
2837 Perl_sv_catpvf(aTHX_ listsv, "!utf8::IsXDigit\n"); break;
2838 }
93733859 2839 continue;
b8c5462f 2840 }
1209ba90
JH
2841 if (range && namedclass > OOB_NAMEDCLASS)
2842 range = 0; /* [a-\d], [a-[:digit:]], not a true range. */
b8c5462f 2843 if (range) {
a0ed51b3 2844 if (lastvalue > value)
93733859 2845 FAIL("invalid [] range in regexp"); /* [b-a] */
b9c39e73 2846 if (!SIZE_ONLY)
57def98f 2847 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\t%04"UVxf"\n", (UV)lastvalue, (UV)value);
a0ed51b3
LW
2848 range = 0;
2849 }
2850 else {
2851 lastvalue = value;
2852 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
93733859 2853 PL_regcomp_parse[1] != ']') {
a0ed51b3
LW
2854 PL_regcomp_parse++;
2855 range = 1;
2856 continue; /* do it next time */
2857 }
93733859
JH
2858 }
2859 /* now is the next time */
93733859 2860 if (!SIZE_ONLY)
57def98f 2861 Perl_sv_catpvf(aTHX_ listsv, "%04"UVxf"\n", (UV)value);
93733859 2862 range = 0;
a0ed51b3 2863 }
a0ed51b3
LW
2864
2865 ret = reganode(ANYOFUTF8, 0);
2866
2867 if (!SIZE_ONLY) {
2868 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2869 SvREFCNT_dec(listsv);
2870 n = add_data(1,"s");
2871 PL_regcomp_rx->data->data[n] = (void*)rv;
2872 ARG1_SET(ret, flags);
2873 ARG2_SET(ret, n);
2874 }
2875
2876 return ret;
2877}
2878
76e3520e 2879STATIC char*
cea2e8a9 2880S_nextchar(pTHX)
a0d0e21e 2881{
5c0ca799 2882 dTHR;
3280af22 2883 char* retval = PL_regcomp_parse++;
a0d0e21e 2884
4633a7c4 2885 for (;;) {
3280af22
NIS
2886 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2887 PL_regcomp_parse[2] == '#') {
2888 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2889 PL_regcomp_parse++;
2890 PL_regcomp_parse++;
4633a7c4
LW
2891 continue;
2892 }
3280af22
NIS
2893 if (PL_regflags & PMf_EXTENDED) {
2894 if (isSPACE(*PL_regcomp_parse)) {
2895 PL_regcomp_parse++;
748a9306
LW
2896 continue;
2897 }
3280af22
NIS
2898 else if (*PL_regcomp_parse == '#') {
2899 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2900 PL_regcomp_parse++;
2901 PL_regcomp_parse++;
748a9306
LW
2902 continue;
2903 }
748a9306 2904 }
4633a7c4 2905 return retval;
a0d0e21e 2906 }
a687059c
LW
2907}
2908
2909/*
c277df42 2910- reg_node - emit a node
a0d0e21e 2911*/
76e3520e 2912STATIC regnode * /* Location. */
cea2e8a9 2913S_reg_node(pTHX_ U8 op)
a687059c 2914{
5c0ca799 2915 dTHR;
c277df42
IZ
2916 register regnode *ret;
2917 register regnode *ptr;
a687059c 2918
3280af22 2919 ret = PL_regcode;
c277df42 2920 if (SIZE_ONLY) {
6b88bc9c 2921 SIZE_ALIGN(PL_regsize);
3280af22 2922 PL_regsize += 1;
a0d0e21e
LW
2923 return(ret);
2924 }
a687059c 2925
c277df42 2926 NODE_ALIGN_FILL(ret);
a0d0e21e 2927 ptr = ret;
c277df42 2928 FILL_ADVANCE_NODE(ptr, op);
3280af22 2929 PL_regcode = ptr;
a687059c 2930
a0d0e21e 2931 return(ret);
a687059c
LW
2932}
2933
2934/*
a0d0e21e
LW
2935- reganode - emit a node with an argument
2936*/
76e3520e 2937STATIC regnode * /* Location. */
cea2e8a9 2938S_reganode(pTHX_ U8 op, U32 arg)
fe14fcc3 2939{
5c0ca799 2940 dTHR;
c277df42
IZ
2941 register regnode *ret;
2942 register regnode *ptr;
fe14fcc3 2943
3280af22 2944 ret = PL_regcode;
c277df42 2945 if (SIZE_ONLY) {
6b88bc9c 2946 SIZE_ALIGN(PL_regsize);
3280af22 2947 PL_regsize += 2;
a0d0e21e
LW
2948 return(ret);
2949 }
fe14fcc3 2950
c277df42 2951 NODE_ALIGN_FILL(ret);
a0d0e21e 2952 ptr = ret;
c277df42 2953 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3280af22 2954 PL_regcode = ptr;
fe14fcc3 2955
a0d0e21e 2956 return(ret);
fe14fcc3
LW
2957}
2958
2959/*
cd439c50 2960- reguni - emit (if appropriate) a Unicode character
a0ed51b3
LW
2961*/
2962STATIC void
cea2e8a9 2963S_reguni(pTHX_ UV uv, char* s, I32* lenp)
a0ed51b3 2964{
c485e607 2965 dTHR;
a0ed51b3 2966 if (SIZE_ONLY) {
dfe13c55 2967 U8 tmpbuf[10];
a0ed51b3
LW
2968 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2969 }
2970 else
dfe13c55 2971 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
a0ed51b3
LW
2972
2973}
2974
2975/*
a0d0e21e
LW
2976- reginsert - insert an operator in front of already-emitted operand
2977*
2978* Means relocating the operand.
2979*/
76e3520e 2980STATIC void
cea2e8a9 2981S_reginsert(pTHX_ U8 op, regnode *opnd)
a687059c 2982{
5c0ca799 2983 dTHR;
c277df42
IZ
2984 register regnode *src;
2985 register regnode *dst;
2986 register regnode *place;
2987 register int offset = regarglen[(U8)op];
2988
22c35a8c 2989/* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
c277df42
IZ
2990
2991 if (SIZE_ONLY) {
3280af22 2992 PL_regsize += NODE_STEP_REGNODE + offset;
a0d0e21e
LW
2993 return;
2994 }
a687059c 2995
3280af22
NIS
2996 src = PL_regcode;
2997 PL_regcode += NODE_STEP_REGNODE + offset;
2998 dst = PL_regcode;
a0d0e21e 2999 while (src > opnd)
c277df42 3000 StructCopy(--src, --dst, regnode);
a0d0e21e
LW
3001
3002 place = opnd; /* Op node, where operand used to be. */
c277df42
IZ
3003 src = NEXTOPER(place);
3004 FILL_ADVANCE_NODE(place, op);
3005 Zero(src, offset, regnode);
a687059c
LW
3006}
3007
3008/*
c277df42 3009- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 3010*/
76e3520e 3011STATIC void
cea2e8a9 3012S_regtail(pTHX_ regnode *p, regnode *val)
a687059c 3013{
5c0ca799 3014 dTHR;
c277df42
IZ
3015 register regnode *scan;
3016 register regnode *temp;
a0d0e21e
LW
3017 register I32 offset;
3018
c277df42 3019 if (SIZE_ONLY)
a0d0e21e
LW
3020 return;
3021
3022 /* Find last node. */
3023 scan = p;
3024 for (;;) {
3025 temp = regnext(scan);
3026 if (temp == NULL)
3027 break;
3028 scan = temp;
3029 }
a687059c 3030
c277df42
IZ
3031 if (reg_off_by_arg[OP(scan)]) {
3032 ARG_SET(scan, val - scan);
a0ed51b3
LW
3033 }
3034 else {
c277df42
IZ
3035 NEXT_OFF(scan) = val - scan;
3036 }
a687059c
LW
3037}
3038
3039/*
a0d0e21e
LW
3040- regoptail - regtail on operand of first argument; nop if operandless
3041*/
76e3520e 3042STATIC void
cea2e8a9 3043S_regoptail(pTHX_ regnode *p, regnode *val)
a687059c 3044{
5c0ca799 3045 dTHR;
a0d0e21e 3046 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42
IZ
3047 if (p == NULL || SIZE_ONLY)
3048 return;
22c35a8c 3049 if (PL_regkind[(U8)OP(p)] == BRANCH) {
c277df42 3050 regtail(NEXTOPER(p), val);
a0ed51b3 3051 }
22c35a8c 3052 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
c277df42 3053 regtail(NEXTOPER(NEXTOPER(p)), val);
a0ed51b3
LW
3054 }
3055 else
a0d0e21e 3056 return;
a687059c
LW
3057}
3058
3059/*
3060 - regcurly - a little FSA that accepts {\d+,?\d*}
3061 */
79072805 3062STATIC I32
cea2e8a9 3063S_regcurly(pTHX_ register char *s)
a687059c
LW
3064{
3065 if (*s++ != '{')
3066 return FALSE;
f0fcb552 3067 if (!isDIGIT(*s))
a687059c 3068 return FALSE;
f0fcb552 3069 while (isDIGIT(*s))
a687059c
LW
3070 s++;
3071 if (*s == ',')
3072 s++;
f0fcb552 3073 while (isDIGIT(*s))
a687059c
LW
3074 s++;
3075 if (*s != '}')
3076 return FALSE;
3077 return TRUE;
3078}
3079
a687059c 3080
76e3520e 3081STATIC regnode *
cea2e8a9 3082S_dumpuntil(pTHX_ regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
c277df42 3083{
35ff7856 3084#ifdef DEBUGGING
f248d071 3085 register U8 op = EXACT; /* Arbitrary non-END op. */
c277df42
IZ
3086 register regnode *next, *onode;
3087
3088 while (op != END && (!last || node < last)) {
3089 /* While that wasn't END last time... */
3090
3091 NODE_ALIGN(node);
3092 op = OP(node);
3093 if (op == CLOSE)
3094 l--;
3095 next = regnext(node);
3096 /* Where, what. */
3097 if (OP(node) == OPTIMIZED)
3098 goto after_print;
3099 regprop(sv, node);
54dc92de 3100 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
c277df42
IZ
3101 2*l + 1, "", SvPVX(sv));
3102 if (next == NULL) /* Next ptr. */
3103 PerlIO_printf(Perl_debug_log, "(0)");
3104 else
3105 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
3106 (void)PerlIO_putc(Perl_debug_log, '\n');
3107 after_print:
22c35a8c 3108 if (PL_regkind[(U8)op] == BRANCHJ) {
c277df42
IZ
3109 register regnode *nnode = (OP(next) == LONGJMP
3110 ? regnext(next)
3111 : next);
3112 if (last && nnode > last)
3113 nnode = last;
3114 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
a0ed51b3 3115 }
22c35a8c 3116 else if (PL_regkind[(U8)op] == BRANCH) {
c277df42 3117 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
a0ed51b3
LW
3118 }
3119 else if ( op == CURLY) { /* `next' might be very big: optimizer */
c277df42
IZ
3120 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3121 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
a0ed51b3 3122 }
22c35a8c 3123 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
c277df42
IZ
3124 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
3125 next, sv, l + 1);
a0ed51b3
LW
3126 }
3127 else if ( op == PLUS || op == STAR) {
c277df42 3128 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
a0ed51b3
LW
3129 }
3130 else if (op == ANYOF) {
c277df42
IZ
3131 node = NEXTOPER(node);
3132 node += ANY_SKIP;
a0ed51b3 3133 }
22c35a8c 3134 else if (PL_regkind[(U8)op] == EXACT) {
c277df42 3135 /* Literal string, where present. */
cd439c50 3136 node += NODE_SZ_STR(node) - 1;
c277df42 3137 node = NEXTOPER(node);
a0ed51b3
LW
3138 }
3139 else {
c277df42
IZ
3140 node = NEXTOPER(node);
3141 node += regarglen[(U8)op];
3142 }
3143 if (op == CURLYX || op == OPEN)
3144 l++;
3145 else if (op == WHILEM)
3146 l--;
3147 }
17c3b450 3148#endif /* DEBUGGING */
c277df42
IZ
3149 return node;
3150}
3151
a687059c 3152/*
fd181c75 3153 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c
LW
3154 */
3155void
864dbfa3 3156Perl_regdump(pTHX_ regexp *r)
a687059c 3157{
35ff7856 3158#ifdef DEBUGGING
5c0ca799 3159 dTHR;
46fc3d4c 3160 SV *sv = sv_newmortal();
a687059c 3161
c277df42 3162 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e
LW
3163
3164 /* Header fields of interest. */
c277df42 3165 if (r->anchored_substr)
2c2d71f5 3166 PerlIO_printf(Perl_debug_log, "anchored `%s%.*s%s'%s at %d ",
3280af22 3167 PL_colors[0],
2c2d71f5 3168 SvCUR(r->anchored_substr) - (SvTAIL(r->anchored_substr)!=0),
c277df42 3169 SvPVX(r->anchored_substr),
3280af22 3170 PL_colors[1],
c277df42
IZ
3171 SvTAIL(r->anchored_substr) ? "$" : "",
3172 r->anchored_offset);
3173 if (r->float_substr)
2c2d71f5 3174 PerlIO_printf(Perl_debug_log, "floating `%s%.*s%s'%s at %d..%u ",
3280af22 3175 PL_colors[0],
2c2d71f5
JH
3176 SvCUR(r->float_substr) - (SvTAIL(r->float_substr)!=0),
3177 SvPVX(r->float_substr),
3280af22 3178 PL_colors[1],
c277df42
IZ
3179 SvTAIL(r->float_substr) ? "$" : "",
3180 r->float_min_offset, r->float_max_offset);
3181 if (r->check_substr)
3182 PerlIO_printf(Perl_debug_log,
3183 r->check_substr == r->float_substr
3184 ? "(checking floating" : "(checking anchored");
3185 if (r->reganch & ROPT_NOSCAN)
3186 PerlIO_printf(Perl_debug_log, " noscan");
3187 if (r->reganch & ROPT_CHECK_ALL)
3188 PerlIO_printf(Perl_debug_log, " isall");
3189 if (r->check_substr)
3190 PerlIO_printf(Perl_debug_log, ") ");
3191
46fc3d4c
PP
3192 if (r->regstclass) {
3193 regprop(sv, r->regstclass);
3194 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
3195 }
774d564b
PP
3196 if (r->reganch & ROPT_ANCH) {
3197 PerlIO_printf(Perl_debug_log, "anchored");
3198 if (r->reganch & ROPT_ANCH_BOL)
3199 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42
IZ
3200 if (r->reganch & ROPT_ANCH_MBOL)
3201 PerlIO_printf(Perl_debug_log, "(MBOL)");
cad2e5aa
JH
3202 if (r->reganch & ROPT_ANCH_SBOL)
3203 PerlIO_printf(Perl_debug_log, "(SBOL)");
774d564b
PP
3204 if (r->reganch & ROPT_ANCH_GPOS)
3205 PerlIO_printf(Perl_debug_log, "(GPOS)");
3206 PerlIO_putc(Perl_debug_log, ' ');
3207 }
c277df42
IZ
3208 if (r->reganch & ROPT_GPOS_SEEN)
3209 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 3210 if (r->reganch & ROPT_SKIP)
760ac839 3211 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 3212 if (r->reganch & ROPT_IMPLICIT)
760ac839 3213 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 3214 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02
IZ
3215 if (r->reganch & ROPT_EVAL_SEEN)
3216 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 3217 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 3218#endif /* DEBUGGING */
a687059c
LW
3219}
3220
3221/*
a0d0e21e
LW
3222- regprop - printable representation of opcode
3223*/
46fc3d4c 3224void
864dbfa3 3225Perl_regprop(pTHX_ SV *sv, regnode *o)
a687059c 3226{
35ff7856 3227#ifdef DEBUGGING
5c0ca799 3228 dTHR;
9b155405 3229 register int k;
a0d0e21e 3230
54dc92de 3231 sv_setpvn(sv, "", 0);
9b155405 3232 if (OP(o) >= reg_num) /* regnode.type is unsigned */
a0d0e21e 3233 FAIL("corrupted regexp opcode");
9b155405
IZ
3234 sv_catpv(sv, (char*)reg_name[OP(o)]); /* Take off const! */
3235
3236 k = PL_regkind[(U8)OP(o)];
3237
3238 if (k == EXACT)
7821416a 3239 Perl_sv_catpvf(aTHX_ sv, " <%s%.*s%s>", PL_colors[0],
cd439c50 3240 STR_LEN(o), STRING(o), PL_colors[1]);
9b155405
IZ
3241 else if (k == CURLY) {
3242 if (OP(o) == CURLYM || OP(o) == CURLYN)
cea2e8a9
GS
3243 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* Parenth number */
3244 Perl_sv_catpvf(aTHX_ sv, " {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 3245 }
2c2d71f5
JH
3246 else if (k == WHILEM && o->flags) /* Ordinal/of */
3247 Perl_sv_catpvf(aTHX_ sv, "[%d/%d]", o->flags & 0xf, o->flags>>4);
9b155405 3248 else if (k == REF || k == OPEN || k == CLOSE || k == GROUPP )
cea2e8a9 3249 Perl_sv_catpvf(aTHX_ sv, "%d", ARG(o)); /* Parenth number */
9b155405 3250 else if (k == LOGICAL)
04ebc1ab 3251 Perl_sv_catpvf(aTHX_ sv, "[%d]", o->flags); /* 2: embedded, otherwise 1 */
9b155405 3252 else if (k == BRANCHJ && (OP(o) == UNLESSM || OP(o) == IFMATCH))
cea2e8a9 3253 Perl_sv_catpvf(aTHX_ sv, "[-%d]", o->flags);
17c3b450 3254#endif /* DEBUGGING */
35ff7856 3255}
a687059c 3256
cad2e5aa
JH
3257SV *
3258Perl_re_intuit_string(pTHX_ regexp *prog)
3259{ /* Assume that RE_INTUIT is set */
3260 DEBUG_r(
3261 { STRLEN n_a;
3262 char *s = SvPV(prog->check_substr,n_a);
3263
3264 if (!PL_colorset) reginitcolors();
3265 PerlIO_printf(Perl_debug_log,
3266 "%sUsing REx substr:%s `%s%.60s%s%s'\n",
3267 PL_colors[4],PL_colors[5],PL_colors[0],
3268 s,
3269 PL_colors[1],
3270 (strlen(s) > 60 ? "..." : ""));
3271 } );
3272
3273 return prog->check_substr;
3274}
3275
2b69d0c2 3276void
864dbfa3 3277Perl_pregfree(pTHX_ struct regexp *r)
a687059c 3278{
5c0ca799 3279 dTHR;
adac82c7 3280 DEBUG_r(if (!PL_colorset) reginitcolors());
7821416a
IZ
3281
3282 if (!r || (--r->refcnt > 0))
3283 return;
cad2e5aa
JH
3284 DEBUG_r(PerlIO_printf(Perl_debug_log,
3285 "%sFreeing REx:%s `%s%.60s%s%s'\n",
3286 PL_colors[4],PL_colors[5],PL_colors[0],
3287 r->precomp,
3288 PL_colors[1],
3289 (strlen(r->precomp) > 60 ? "..." : "")));
3290
c277df42 3291 if (r->precomp)
a0d0e21e 3292 Safefree(r->precomp);
cf93c79d
IZ
3293 if (RX_MATCH_COPIED(r))
3294 Safefree(r->subbeg);
a193d654
GS
3295 if (r->substrs) {
3296 if (r->anchored_substr)
3297 SvREFCNT_dec(r->anchored_substr);
3298 if (r->float_substr)
3299 SvREFCNT_dec(r->float_substr);
2779dcf1 3300 Safefree(r->substrs);
a193d654 3301 }
c277df42
IZ
3302 if (r->data) {
3303 int n = r->data->count;
dfad63ad
HS
3304 AV* new_comppad = NULL;
3305 AV* old_comppad;
3306 SV** old_curpad;
3307
c277df42
IZ
3308 while (--n >= 0) {
3309 switch (r->data->what[n]) {
3310 case 's':
3311 SvREFCNT_dec((SV*)r->data->data[n]);
3312 break;
dfad63ad
HS
3313 case 'p':
3314 new_comppad = (AV*)r->data->data[n];
3315 break;
c277df42 3316 case 'o':
dfad63ad 3317 if (new_comppad == NULL)
cea2e8a9 3318 Perl_croak(aTHX_ "panic: pregfree comppad");
dfad63ad
HS
3319 old_comppad = PL_comppad;
3320 old_curpad = PL_curpad;
3321 PL_comppad = new_comppad;
3322 PL_curpad = AvARRAY(new_comppad);
c277df42 3323 op_free((OP_4tree*)r->data->data[n]);
dfad63ad
HS
3324 PL_comppad = old_comppad;
3325 PL_curpad = old_curpad;
3326 SvREFCNT_dec((SV*)new_comppad);
3327 new_comppad = NULL;
c277df42
IZ
3328 break;
3329 case 'n':
3330 break;
3331 default:
3332 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3333 }
3334 }
3335 Safefree(r->data->what);
3336 Safefree(r->data);
a0d0e21e
LW
3337 }
3338 Safefree(r->startp);
3339 Safefree(r->endp);
3340 Safefree(r);
a687059c 3341}
c277df42
IZ
3342
3343/*
3344 - regnext - dig the "next" pointer out of a node
3345 *
3346 * [Note, when REGALIGN is defined there are two places in regmatch()
3347 * that bypass this code for speed.]
3348 */
3349regnode *
864dbfa3 3350Perl_regnext(pTHX_ register regnode *p)
c277df42 3351{
5c0ca799 3352 dTHR;
c277df42
IZ
3353 register I32 offset;
3354
3280af22 3355 if (p == &PL_regdummy)
c277df42
IZ
3356 return(NULL);
3357
3358 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3359 if (offset == 0)
3360 return(NULL);
3361
c277df42 3362 return(p+offset);
c277df42
IZ
3363}
3364
01f988be 3365STATIC void
cea2e8a9 3366S_re_croak2(pTHX_ const char* pat1,const char* pat2,...)
c277df42
IZ
3367{
3368 va_list args;
3369 STRLEN l1 = strlen(pat1);
3370 STRLEN l2 = strlen(pat2);
3371 char buf[512];
06bf62c7 3372 SV *msv;
c277df42
IZ
3373 char *message;
3374
3375 if (l1 > 510)
3376 l1 = 510;
3377 if (l1 + l2 > 510)
3378 l2 = 510 - l1;
3379 Copy(pat1, buf, l1 , char);
3380 Copy(pat2, buf + l1, l2 , char);
3b818b81
GS
3381 buf[l1 + l2] = '\n';
3382 buf[l1 + l2 + 1] = '\0';
8736538c
AS
3383#ifdef I_STDARG
3384 /* ANSI variant takes additional second argument */
c277df42 3385 va_start(args, pat2);
8736538c
AS
3386#else
3387 va_start(args);
3388#endif
5a844595 3389 msv = vmess(buf, &args);
c277df42 3390 va_end(args);
06bf62c7 3391 message = SvPV(msv,l1);
c277df42
IZ
3392 if (l1 > 512)
3393 l1 = 512;
3394 Copy(message, buf, l1 , char);
3395 buf[l1] = '\0'; /* Overwrite \n */
cea2e8a9 3396 Perl_croak(aTHX_ "%s", buf);
c277df42 3397}
a0ed51b3
LW
3398
3399/* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3400
3401void
864dbfa3 3402Perl_save_re_context(pTHX)
c485e607
NIS
3403{
3404 dTHR;
a0ed51b3
LW
3405 SAVEPPTR(PL_bostr);
3406 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3407 SAVEI32(PL_regnpar); /* () count. */
3408 SAVEI32(PL_regsize); /* Code size. */
3409 SAVEI16(PL_regflags); /* are we folding, multilining? */
3410 SAVEPPTR(PL_reginput); /* String-input pointer. */
3411 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3412 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3413 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3414 SAVESPTR(PL_regendp); /* Ditto for endp. */
3415 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3416 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3417 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3418 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3419 PL_reg_start_tmp = 0;
3420 SAVEFREEPV(PL_reg_start_tmp);
3421 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3422 PL_reg_start_tmpl = 0;
3423 SAVESPTR(PL_regdata);
3424 SAVEI32(PL_reg_flags); /* from regexec.c */
3425 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3426 SAVEI32(PL_regnarrate); /* from regexec.c */
3427 SAVESPTR(PL_regprogram); /* from regexec.c */
3428 SAVEINT(PL_regindent); /* from regexec.c */
3429 SAVESPTR(PL_regcc); /* from regexec.c */
3430 SAVESPTR(PL_curcop);
3431 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3432 SAVEI32(PL_regseen); /* from regcomp.c */
3433 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3434 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3435 SAVESPTR(PL_regcode); /* Code-emit pointer; &regdummy = don't */
3436 SAVEPPTR(PL_regxend); /* End of input for compile */
3437 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
54b6e2fa
IZ
3438 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3439 SAVESPTR(PL_reg_re); /* from regexec.c */
3440 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3441 SAVESPTR(PL_reg_sv); /* from regexec.c */
3442 SAVESPTR(PL_reg_magic); /* from regexec.c */
3443 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3444 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3445 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3446#ifdef DEBUGGING
3447 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3448#endif
a0ed51b3 3449}
51371543
GS
3450
3451#ifdef PERL_OBJECT
3452#define NO_XSLOCKS
3453#include "XSUB.h"
3454#undef this
3455#define this pPerl
3456#endif
3457
3458static void
3459clear_re(pTHXo_ void *r)
3460{
3461 ReREFCNT_dec((regexp *)r);
3462}
3463