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