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