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