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