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