-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathregexec.c
12396 lines (10890 loc) · 474 KB
/
regexec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* regexec.c
*/
/*
* One Ring to rule them all, One Ring to find them
*
* [p.v of _The Lord of the Rings_, opening poem]
* [p.50 of _The Lord of the Rings_, I/iii: "The Shadow of the Past"]
* [p.254 of _The Lord of the Rings_, II/ii: "The Council of Elrond"]
*/
/* This file contains functions for executing a regular expression. See
* also regcomp.c which funnily enough, contains functions for compiling
* a regular expression.
*
* This file is also copied at build time to ext/re/re_exec.c, where
* it's built with -DPERL_EXT_RE_BUILD -DPERL_EXT_RE_DEBUG -DPERL_EXT.
* This causes the main functions to be compiled under new names and with
* debugging support added, which makes "use re 'debug'" work.
*/
/* NOTE: this is derived from Henry Spencer's regexp code, and should not
* confused with the original package (see point 3 below). Thanks, Henry!
*/
/* Additional note: this code is very heavily munged from Henry's version
* in places. In some spots I've traded clarity for efficiency, so don't
* blame Henry for some of the lack of readability.
*/
/* The names of the functions have been changed from regcomp and
* regexec to pregcomp and pregexec in order to avoid conflicts
* with the POSIX routines of the same names.
*/
#ifdef PERL_EXT_RE_BUILD
#include "re_top.h"
#endif
/*
* pregcomp and pregexec -- regsub and regerror are not used in perl
*
* Copyright (c) 1986 by University of Toronto.
* Written by Henry Spencer. Not derived from licensed software.
*
* Permission is granted to anyone to use this software for any
* purpose on any computer system, and to redistribute it freely,
* subject to the following restrictions:
*
* 1. The author is not responsible for the consequences of use of
* this software, no matter how awful, even if they arise
* from defects in it.
*
* 2. The origin of this software must not be misrepresented, either
* by explicit claim or by omission.
*
* 3. Altered versions must be plainly marked as such, and must not
* be misrepresented as being the original software.
*
**** Alterations to Henry's code are...
****
**** Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
**** 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
**** by Larry Wall and others
****
**** You may distribute under the terms of either the GNU General Public
**** License or the Artistic License, as specified in the README file.
*
* Beware that some of this code is subtly aware of the way operator
* precedence is structured in regular expressions. Serious changes in
* regular-expression syntax might require a total rethink.
*/
#include "EXTERN.h"
#define PERL_IN_REGEX_ENGINE
#define PERL_IN_REGEXEC_C
#include "perl.h"
#ifdef PERL_IN_XSUB_RE
# include "re_comp.h"
#else
# include "regcomp.h"
#endif
#include "invlist_inline.h"
#include "unicode_constants.h"
static const char b_utf8_locale_required[] =
"Use of \\b{} or \\B{} for non-UTF-8 locale is wrong."
" Assuming a UTF-8 locale";
#define CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_BOUND \
STMT_START { \
if (! IN_UTF8_CTYPE_LOCALE) { \
Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), \
b_utf8_locale_required); \
} \
} STMT_END
static const char sets_utf8_locale_required[] =
"Use of (?[ ]) for non-UTF-8 locale is wrong. Assuming a UTF-8 locale";
#define CHECK_AND_WARN_NON_UTF8_CTYPE_LOCALE_IN_SETS(n) \
STMT_START { \
if (! IN_UTF8_CTYPE_LOCALE && (FLAGS(n) & ANYOFL_UTF8_LOCALE_REQD)){\
Perl_ck_warner(aTHX_ packWARN(WARN_LOCALE), \
sets_utf8_locale_required); \
} \
} STMT_END
#ifdef DEBUGGING
/* At least one required character in the target string is expressible only in
* UTF-8. */
static const char non_utf8_target_but_utf8_required[]
= "Can't match, because target string needs to be in UTF-8\n";
#endif
#define NON_UTF8_TARGET_BUT_UTF8_REQUIRED(target) STMT_START { \
DEBUG_EXECUTE_r(Perl_re_printf( aTHX_ "%s", non_utf8_target_but_utf8_required));\
goto target; \
} STMT_END
#ifndef STATIC
#define STATIC static
#endif
/*
* Forwards.
*/
#define CHR_SVLEN(sv) (utf8_target ? sv_len_utf8(sv) : SvCUR(sv))
#define HOPc(pos,off) \
(char *)(reginfo->is_utf8_target \
? reghop3((U8*)pos, off, \
(U8*)(off >= 0 ? reginfo->strend : reginfo->strbeg)) \
: (U8*)(pos + off))
/* like HOPMAYBE3 but backwards. lim must be +ve. Returns NULL on overshoot */
#define HOPBACK3(pos, off, lim) \
(reginfo->is_utf8_target \
? reghopmaybe3((U8*)pos, (SSize_t)0-off, (U8*)(lim)) \
: (pos - off >= lim) \
? (U8*)pos - off \
: NULL)
#define HOPBACKc(pos, off) ((char*)HOPBACK3(pos, off, reginfo->strbeg))
#define HOP3(pos,off,lim) (reginfo->is_utf8_target ? reghop3((U8*)(pos), off, (U8*)(lim)) : (U8*)(pos + off))
#define HOP3c(pos,off,lim) ((char*)HOP3(pos,off,lim))
/* lim must be +ve. Returns NULL on overshoot */
#define HOPMAYBE3(pos,off,lim) \
(reginfo->is_utf8_target \
? reghopmaybe3((U8*)pos, off, (U8*)(lim)) \
: ((U8*)pos + off <= lim) \
? (U8*)pos + off \
: NULL)
/* like HOP3, but limits the result to <= lim even for the non-utf8 case.
* off must be >=0; args should be vars rather than expressions */
#define HOP3lim(pos,off,lim) (reginfo->is_utf8_target \
? reghop3((U8*)(pos), off, (U8*)(lim)) \
: (U8*)((pos + off) > lim ? lim : (pos + off)))
#define HOP3clim(pos,off,lim) ((char*)HOP3lim(pos,off,lim))
#define HOP4(pos,off,llim, rlim) (reginfo->is_utf8_target \
? utf8_hop_safe((U8*)(pos), off, (U8*)(llim), (U8*)(rlim)) \
: (U8*)(pos + off))
#define HOP4c(pos,off,llim, rlim) ((char*)HOP4(pos,off,llim, rlim))
#define PLACEHOLDER /* Something for the preprocessor to grab onto */
/* TODO: Combine JUMPABLE and HAS_TEXT to cache OP(rn) */
/* for use after a quantifier and before an EXACT-like node -- japhy */
/* it would be nice to rework regcomp.sym to generate this stuff. sigh
*
* NOTE that *nothing* that affects backtracking should be in here, specifically
* VERBS must NOT be included. JUMPABLE is used to determine if we can ignore a
* node that is in between two EXACT like nodes when ascertaining what the required
* "follow" character is. This should probably be moved to regex compile time
* although it may be done at run time because of the REF possibility - more
* investigation required. -- demerphq
*/
#define JUMPABLE(rn) ( \
OP(rn) == OPEN || \
(OP(rn) == CLOSE && \
!EVAL_CLOSE_PAREN_IS(cur_eval,PARNO(rn)) ) || \
OP(rn) == EVAL || \
OP(rn) == SUSPEND || OP(rn) == IFMATCH || \
OP(rn) == PLUS || OP(rn) == MINMOD || \
OP(rn) == KEEPS || \
(REGNODE_TYPE(OP(rn)) == CURLY && ARG1i(rn) > 0) \
)
#define IS_EXACT(rn) (REGNODE_TYPE(OP(rn)) == EXACT)
#define HAS_TEXT(rn) ( IS_EXACT(rn) || REGNODE_TYPE(OP(rn)) == REF )
/*
Search for mandatory following text node; for lookahead, the text must
follow but for lookbehind (FLAGS(rn) != 0) we skip to the next step.
*/
#define FIND_NEXT_IMPT(rn) STMT_START { \
while (JUMPABLE(rn)) { \
const OPCODE type = OP(rn); \
if (type == SUSPEND || REGNODE_TYPE(type) == CURLY) \
rn = REGNODE_AFTER_opcode(rn,type); \
else if (type == PLUS) \
rn = REGNODE_AFTER_type(rn,tregnode_PLUS); \
else if (type == IFMATCH) \
rn = (FLAGS(rn) == 0) ? REGNODE_AFTER_type(rn,tregnode_IFMATCH) : rn + ARG1u(rn); \
else rn += NEXT_OFF(rn); \
} \
} STMT_END
#define SLAB_FIRST(s) (&(s)->states[0])
#define SLAB_LAST(s) (&(s)->states[PERL_REGMATCH_SLAB_SLOTS-1])
static void S_setup_eval_state(pTHX_ regmatch_info *const reginfo);
static void S_cleanup_regmatch_info_aux(pTHX_ void *arg);
static regmatch_state * S_push_slab(pTHX);
#define REGCP_OTHER_ELEMS 3
#define REGCP_FRAME_ELEMS 1
/* REGCP_FRAME_ELEMS are not part of the REGCP_OTHER_ELEMS and
* are needed for the regexp context stack bookkeeping. */
STATIC CHECKPOINT
S_regcppush(pTHX_ const regexp *rex, I32 parenfloor, U32 maxopenparen comma_pDEPTH)
{
const int retval = PL_savestack_ix;
/* Number of bytes about to be stored in the stack */
const SSize_t paren_bytes_to_push = sizeof(*RXp_OFFSp(rex)) * (maxopenparen - parenfloor);
/* Number of savestack[] entries to be filled by the paren data */
/* Rounding is performed in case we are few elements short */
const int paren_elems_to_push = (paren_bytes_to_push + sizeof(*PL_savestack) - 1) / sizeof(*PL_savestack);
const UV total_elems = paren_elems_to_push + REGCP_OTHER_ELEMS;
const UV elems_shifted = total_elems << SAVE_TIGHT_SHIFT;
DECLARE_AND_GET_RE_DEBUG_FLAGS;
PERL_ARGS_ASSERT_REGCPPUSH;
if (paren_elems_to_push < 0)
Perl_croak(aTHX_ "panic: paren_elems_to_push, %i < 0, maxopenparen: %i parenfloor: %i",
(int)paren_elems_to_push, (int)maxopenparen,
(int)parenfloor);
if ((elems_shifted >> SAVE_TIGHT_SHIFT) != total_elems)
Perl_croak(aTHX_ "panic: paren_elems_to_push offset %" UVuf
" out of range (%lu-%ld)",
total_elems,
(unsigned long)maxopenparen,
(long)parenfloor);
DEBUG_BUFFERS_r(
if ((int)maxopenparen > (int)parenfloor)
Perl_re_exec_indentf( aTHX_
"rex = 0x%" UVxf " offs = 0x%" UVxf ": saving capture indices:\n",
depth,
PTR2UV(rex),
PTR2UV(RXp_OFFSp(rex))
);
);
SSGROW(total_elems + REGCP_FRAME_ELEMS);
assert((IV)PL_savestack_max > (IV)(total_elems + REGCP_FRAME_ELEMS));
/* memcpy the offs inside the stack - it's faster than for loop */
memcpy(&PL_savestack[PL_savestack_ix], RXp_OFFSp(rex) + parenfloor + 1, paren_bytes_to_push);
PL_savestack_ix += paren_elems_to_push;
DEBUG_BUFFERS_r({
I32 p;
for (p = parenfloor + 1; p <= (I32)maxopenparen; p++) {
Perl_re_exec_indentf(aTHX_
" \\%" UVuf " %" IVdf " (%" IVdf ") .. %" IVdf " (regcppush)\n",
depth,
(UV)p,
(IV)RXp_OFFSp(rex)[p].start,
(IV)RXp_OFFSp(rex)[p].start_tmp,
(IV)RXp_OFFSp(rex)[p].end
);
}
});
/* REGCP_OTHER_ELEMS are pushed in any case, parentheses or no. */
SSPUSHINT(maxopenparen);
SSPUSHINT(RXp_LASTPAREN(rex));
SSPUSHINT(RXp_LASTCLOSEPAREN(rex));
SSPUSHUV(SAVEt_REGCONTEXT | elems_shifted); /* Magic cookie. */
DEBUG_BUFFERS_r({
Perl_re_exec_indentf(aTHX_
"finished regcppush returning %" IVdf " cur: %" IVdf "\n",
depth, retval, PL_savestack_ix);
});
return retval;
}
/* These are needed since we do not localize EVAL nodes: */
#define REGCP_SET(cp) \
DEBUG_STATE_r( \
Perl_re_exec_indentf( aTHX_ \
"Setting an EVAL scope, savestack = %" IVdf ",\n", \
depth, (IV)PL_savestack_ix \
) \
); \
cp = PL_savestack_ix
#define REGCP_UNWIND(cp) \
DEBUG_STATE_r( \
if (cp != PL_savestack_ix) \
Perl_re_exec_indentf( aTHX_ \
"Clearing an EVAL scope, savestack = %" \
IVdf "..%" IVdf "\n", \
depth, (IV)(cp), (IV)PL_savestack_ix \
) \
); \
regcpblow(cp)
/* set the start and end positions of capture ix */
#define CLOSE_ANY_CAPTURE(rex, ix, s, e) \
RXp_OFFSp(rex)[(ix)].start = (s); \
RXp_OFFSp(rex)[(ix)].end = (e)
#define CLOSE_CAPTURE(rex, ix, s, e) \
CLOSE_ANY_CAPTURE(rex, ix, s, e); \
if (ix > RXp_LASTPAREN(rex)) \
RXp_LASTPAREN(rex) = (ix); \
RXp_LASTCLOSEPAREN(rex) = (ix); \
DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_ \
"CLOSE: rex = 0x%" UVxf " offs = 0x%" UVxf ": \\%" UVuf ": set %" IVdf " .. %" IVdf " max: %" UVuf "\n", \
depth, \
PTR2UV(rex), \
PTR2UV(RXp_OFFSp(rex)), \
(UV)(ix), \
(IV)RXp_OFFSp(rex)[ix].start, \
(IV)RXp_OFFSp(rex)[ix].end, \
(UV)RXp_LASTPAREN(rex) \
))
/* the lp and lcp args match the relevant members of the
* regexp structure, but in practice they should all be U16
* instead as we have a hard limit of U16_MAX parens. See
* line 4003 or so of regcomp.c where we parse OPEN parens
* of various types. */
PERL_STATIC_INLINE void
S_unwind_paren(pTHX_ regexp *rex, U32 lp, U32 lcp comma_pDEPTH) {
PERL_ARGS_ASSERT_UNWIND_PAREN;
U32 n;
DECLARE_AND_GET_RE_DEBUG_FLAGS;
DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
"UNWIND_PAREN: rex = 0x%" UVxf " offs = 0x%" UVxf
": invalidate (%" UVuf " .. %" UVuf ") set lcp: %" UVuf "\n",
depth,
PTR2UV(rex),
PTR2UV(RXp_OFFSp(rex)),
(UV)(lp),
(UV)(RXp_LASTPAREN(rex)),
(UV)(lcp)
));
for (n = RXp_LASTPAREN(rex); n > lp; n--) {
RXp_OFFSp(rex)[n].end = -1;
}
RXp_LASTPAREN(rex) = n;
RXp_LASTCLOSEPAREN(rex) = lcp;
}
#define UNWIND_PAREN(lp,lcp) unwind_paren(rex,lp,lcp)
PERL_STATIC_INLINE void
S_capture_clear(pTHX_ regexp *rex, U16 from_ix, U16 to_ix, const char *str comma_pDEPTH) {
PERL_ARGS_ASSERT_CAPTURE_CLEAR;
PERL_UNUSED_ARG(str); /* only used for debugging */
U16 my_ix;
DECLARE_AND_GET_RE_DEBUG_FLAGS;
for ( my_ix = from_ix; my_ix <= to_ix; my_ix++ ) {
DEBUG_BUFFERS_r(Perl_re_exec_indentf( aTHX_
"CAPTURE_CLEAR %s \\%" IVdf ": "
"%" IVdf "(%" IVdf ") .. %" IVdf
" => "
"%" IVdf "(%" IVdf ") .. %" IVdf
"\n",
depth, str, (IV)my_ix,
(IV)RXp_OFFSp(rex)[my_ix].start,
(IV)RXp_OFFSp(rex)[my_ix].start_tmp,
(IV)RXp_OFFSp(rex)[my_ix].end,
(IV)-1, (IV)-1, (IV)-1));
RXp_OFFSp(rex)[my_ix].start = -1;
RXp_OFFSp(rex)[my_ix].start_tmp = -1;
RXp_OFFSp(rex)[my_ix].end = -1;
}
}
#define CAPTURE_CLEAR(from_ix, to_ix, str) \
if (from_ix) capture_clear(rex,from_ix, to_ix, str)
STATIC void
S_regcppop(pTHX_ regexp *rex, U32 *maxopenparen_p comma_pDEPTH)
{
UV i;
U32 paren;
DECLARE_AND_GET_RE_DEBUG_FLAGS;
PERL_ARGS_ASSERT_REGCPPOP;
DEBUG_BUFFERS_r({
Perl_re_exec_indentf(aTHX_
"starting regcppop at %" IVdf "\n",
depth, PL_savestack_ix);
});
/* Pop REGCP_OTHER_ELEMS before the parentheses loop starts. */
i = SSPOPUV;
assert((i & SAVE_MASK) == SAVEt_REGCONTEXT); /* Check that the magic cookie is there. */
i >>= SAVE_TIGHT_SHIFT; /* Parentheses elements to pop. */
RXp_LASTCLOSEPAREN(rex) = SSPOPINT;
RXp_LASTPAREN(rex) = SSPOPINT;
*maxopenparen_p = SSPOPINT;
i -= REGCP_OTHER_ELEMS;
/* Now restore the parentheses context. */
DEBUG_BUFFERS_r(
if (i || RXp_LASTPAREN(rex) + 1 <= rex->nparens)
Perl_re_exec_indentf( aTHX_
"rex = 0x%" UVxf " offs = 0x%" UVxf ": restoring capture indices to:\n",
depth,
PTR2UV(rex),
PTR2UV(RXp_OFFSp(rex))
);
);
/* substract remaining elements from the stack */
PL_savestack_ix -= i;
/* static assert that offs struc size is not less than stack elem size */
STATIC_ASSERT_STMT(sizeof(*RXp_OFFSp(rex)) >= sizeof(*PL_savestack));
/* calculate actual number of offs/capture groups stored */
/* by doing integer division (leaving potential alignment aside) */
i = (i * sizeof(*PL_savestack)) / sizeof(*RXp_OFFSp(rex));
/* calculate paren starting point */
/* i is our number of entries which we are subtracting from *maxopenparen_p */
/* and we are storing + 1 this to get the beginning */
paren = *maxopenparen_p - i + 1;
/* restore them */
memcpy(RXp_OFFSp(rex) + paren, &PL_savestack[PL_savestack_ix], i * sizeof(*RXp_OFFSp(rex)));
DEBUG_BUFFERS_r(
for (; paren <= *maxopenparen_p; ++paren) {
Perl_re_exec_indentf(aTHX_
" \\%" UVuf " %" IVdf "(%" IVdf ") .. %" IVdf " %s (regcppop)\n",
depth,
(UV)paren,
(IV)RXp_OFFSp(rex)[paren].start,
(IV)RXp_OFFSp(rex)[paren].start_tmp,
(IV)RXp_OFFSp(rex)[paren].end,
(paren > RXp_LASTPAREN(rex) ? "(skipped)" : ""));
}
);
#if 1
/* It would seem that the similar code in regtry()
* already takes care of this, and in fact it is in
* a better location to since this code can #if 0-ed out
* but the code in regtry() is needed or otherwise tests
* requiring null fields (pat.t#187 and split.t#{13,14}
* (as of patchlevel 7877) will fail. Then again,
* this code seems to be necessary or otherwise
* this erroneously leaves $1 defined: "1" =~ /^(?:(\d)x)?\d$/
* --jhi updated by dapm */
for (i = RXp_LASTPAREN(rex) + 1; i <= rex->nparens; i++) {
if (i > *maxopenparen_p) {
RXp_OFFSp(rex)[i].start = -1;
}
RXp_OFFSp(rex)[i].end = -1;
DEBUG_BUFFERS_r( Perl_re_exec_indentf( aTHX_
" \\%" UVuf ": %s ..-1 undeffing (regcppop)\n",
depth,
(UV)i,
(i > *maxopenparen_p) ? "-1" : " "
));
}
#endif
DEBUG_BUFFERS_r({
Perl_re_exec_indentf(aTHX_
"finished regcppop at %" IVdf "\n",
depth, PL_savestack_ix);
});
}
/* restore the parens and associated vars at savestack position ix,
* but without popping the stack */
STATIC void
S_regcp_restore(pTHX_ regexp *rex, I32 ix, U32 *maxopenparen_p comma_pDEPTH)
{
I32 tmpix = PL_savestack_ix;
PERL_ARGS_ASSERT_REGCP_RESTORE;
PL_savestack_ix = ix;
regcppop(rex, maxopenparen_p);
PL_savestack_ix = tmpix;
}
#define regcpblow(cp) LEAVE_SCOPE(cp) /* Ignores regcppush()ed data. */
STATIC bool
S_isFOO_lc(pTHX_ const U8 classnum, const U8 character)
{
/* Returns a boolean as to whether or not 'character' is a member of the
* Posix character class given by 'classnum' that should be equivalent to a
* value in the typedef 'char_class_number_'.
*
* Ideally this could be replaced by a just an array of function pointers
* to the C library functions that implement the macros this calls.
* However, to compile, the precise function signatures are required, and
* these may vary from platform to platform. To avoid having to figure
* out what those all are on each platform, I (khw) am using this method,
* which adds an extra layer of function call overhead (unless the C
* optimizer strips it away). But we don't particularly care about
* performance with locales anyway. */
if (IN_UTF8_CTYPE_LOCALE) {
return cBOOL(generic_isCC_(character, classnum));
}
switch ((char_class_number_) classnum) {
case CC_ENUM_ALPHANUMERIC_: return isU8_ALPHANUMERIC_LC(character);
case CC_ENUM_ALPHA_: return isU8_ALPHA_LC(character);
case CC_ENUM_ASCII_: return isU8_ASCII_LC(character);
case CC_ENUM_BLANK_: return isU8_BLANK_LC(character);
case CC_ENUM_CASED_: return isU8_CASED_LC(character);
case CC_ENUM_CNTRL_: return isU8_CNTRL_LC(character);
case CC_ENUM_DIGIT_: return isU8_DIGIT_LC(character);
case CC_ENUM_GRAPH_: return isU8_GRAPH_LC(character);
case CC_ENUM_LOWER_: return isU8_LOWER_LC(character);
case CC_ENUM_PRINT_: return isU8_PRINT_LC(character);
case CC_ENUM_PUNCT_: return isU8_PUNCT_LC(character);
case CC_ENUM_SPACE_: return isU8_SPACE_LC(character);
case CC_ENUM_UPPER_: return isU8_UPPER_LC(character);
case CC_ENUM_WORDCHAR_: return isU8_WORDCHAR_LC(character);
case CC_ENUM_XDIGIT_: return isU8_XDIGIT_LC(character);
default: /* VERTSPACE should never occur in locales */
break;
}
Perl_croak(aTHX_
"panic: isFOO_lc() has an unexpected character class '%d'",
classnum);
NOT_REACHED; /* NOTREACHED */
return FALSE;
}
PERL_STATIC_INLINE I32
S_foldEQ_latin1_s2_folded(pTHX_ const char *s1, const char *s2, I32 len)
{
/* Compare non-UTF-8 using Unicode (Latin1) semantics. s2 must already be
* folded. Works on all folds representable without UTF-8, except for
* LATIN_SMALL_LETTER_SHARP_S, and does not check for this. Nor does it
* check that the strings each have at least 'len' characters.
*
* There is almost an identical API function where s2 need not be folded:
* Perl_foldEQ_latin1() */
const U8 *a = (const U8 *)s1;
const U8 *b = (const U8 *)s2;
PERL_ARGS_ASSERT_FOLDEQ_LATIN1_S2_FOLDED;
assert(len >= 0);
while (len--) {
assert(! isUPPER_L1(*b));
if (toLOWER_L1(*a) != *b) {
return 0;
}
a++, b++;
}
return 1;
}
STATIC bool
S_isFOO_utf8_lc(pTHX_ const U8 classnum, const U8* character, const U8* e)
{
/* Returns a boolean as to whether or not the (well-formed) UTF-8-encoded
* 'character' is a member of the Posix character class given by 'classnum'
* that should be equivalent to a value in the typedef
* 'char_class_number_'.
*
* This just calls isFOO_lc on the code point for the character if it is in
* the range 0-255. Outside that range, all characters use Unicode
* rules, ignoring any locale. So use the Unicode function if this class
* requires an inversion list, and use the Unicode macro otherwise. */
PERL_ARGS_ASSERT_ISFOO_UTF8_LC;
if (UTF8_IS_INVARIANT(*character)) {
return isFOO_lc(classnum, *character);
}
else if (UTF8_IS_DOWNGRADEABLE_START(*character)) {
return isFOO_lc(classnum,
EIGHT_BIT_UTF8_TO_NATIVE(*character, *(character + 1)));
}
_CHECK_AND_OUTPUT_WIDE_LOCALE_UTF8_MSG(character, e);
switch ((char_class_number_) classnum) {
case CC_ENUM_SPACE_: return is_XPERLSPACE_high(character);
case CC_ENUM_BLANK_: return is_HORIZWS_high(character);
case CC_ENUM_XDIGIT_: return is_XDIGIT_high(character);
case CC_ENUM_VERTSPACE_: return is_VERTWS_high(character);
default:
return _invlist_contains_cp(PL_XPosix_ptrs[classnum],
utf8_to_uvchr_buf(character, e, NULL));
}
NOT_REACHED; /* NOTREACHED */
}
STATIC U8 *
S_find_span_end(U8 * s, const U8 * send, const U8 span_byte)
{
/* Returns the position of the first byte in the sequence between 's' and
* 'send-1' inclusive that isn't 'span_byte'; returns 'send' if none found.
* */
PERL_ARGS_ASSERT_FIND_SPAN_END;
assert(send >= s);
if ((STRLEN) (send - s) >= PERL_WORDSIZE
+ PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
- (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
{
PERL_UINTMAX_T span_word;
/* Process per-byte until reach word boundary. XXX This loop could be
* eliminated if we knew that this platform had fast unaligned reads */
while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
if (*s != span_byte) {
return s;
}
s++;
}
/* Create a word filled with the bytes we are spanning */
span_word = PERL_COUNT_MULTIPLIER * span_byte;
/* Process per-word as long as we have at least a full word left */
do {
/* Keep going if the whole word is composed of 'span_byte's */
if ((* (PERL_UINTMAX_T *) s) == span_word) {
s += PERL_WORDSIZE;
continue;
}
/* Here, at least one byte in the word isn't 'span_byte'. */
#ifdef EBCDIC
break;
#else
/* This xor leaves 1 bits only in those non-matching bytes */
span_word ^= * (PERL_UINTMAX_T *) s;
/* Make sure the upper bit of each non-matching byte is set. This
* makes each such byte look like an ASCII platform variant byte */
span_word |= span_word << 1;
span_word |= span_word << 2;
span_word |= span_word << 4;
/* That reduces the problem to what this function solves */
return s + variant_byte_number(span_word);
#endif
} while (s + PERL_WORDSIZE <= send);
}
/* Process the straggler bytes beyond the final word boundary */
while (s < send) {
if (*s != span_byte) {
return s;
}
s++;
}
return s;
}
STATIC U8 *
S_find_next_masked(U8 * s, const U8 * send, const U8 byte, const U8 mask)
{
/* Returns the position of the first byte in the sequence between 's'
* and 'send-1' inclusive that when ANDed with 'mask' yields 'byte';
* returns 'send' if none found. It uses word-level operations instead of
* byte to speed up the process */
PERL_ARGS_ASSERT_FIND_NEXT_MASKED;
assert(send >= s);
assert((byte & mask) == byte);
#ifndef EBCDIC
if ((STRLEN) (send - s) >= PERL_WORDSIZE
+ PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
- (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
{
PERL_UINTMAX_T word, mask_word;
while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
if (((*s) & mask) == byte) {
return s;
}
s++;
}
word = PERL_COUNT_MULTIPLIER * byte;
mask_word = PERL_COUNT_MULTIPLIER * mask;
do {
PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
/* If 'masked' contains bytes with the bit pattern of 'byte' within
* it, xoring with 'word' will leave each of the 8 bits in such
* bytes be 0, and no byte containing any other bit pattern will be
* 0. */
masked ^= word;
/* This causes the most significant bit to be set to 1 for any
* bytes in the word that aren't completely 0 */
masked |= masked << 1;
masked |= masked << 2;
masked |= masked << 4;
/* The msbits are the same as what marks a byte as variant, so we
* can use this mask. If all msbits are 1, the word doesn't
* contain 'byte' */
if ((masked & PERL_VARIANTS_WORD_MASK) == PERL_VARIANTS_WORD_MASK) {
s += PERL_WORDSIZE;
continue;
}
/* Here, the msbit of bytes in the word that aren't 'byte' are 1,
* and any that are, are 0. Complement and re-AND to swap that */
masked = ~ masked;
masked &= PERL_VARIANTS_WORD_MASK;
/* This reduces the problem to that solved by this function */
s += variant_byte_number(masked);
return s;
} while (s + PERL_WORDSIZE <= send);
}
#endif
while (s < send) {
if (((*s) & mask) == byte) {
return s;
}
s++;
}
return s;
}
STATIC U8 *
S_find_span_end_mask(U8 * s, const U8 * send, const U8 span_byte, const U8 mask)
{
/* Returns the position of the first byte in the sequence between 's' and
* 'send-1' inclusive that when ANDed with 'mask' isn't 'span_byte'.
* 'span_byte' should have been ANDed with 'mask' in the call of this
* function. Returns 'send' if none found. Works like find_span_end(),
* except for the AND */
PERL_ARGS_ASSERT_FIND_SPAN_END_MASK;
assert(send >= s);
assert((span_byte & mask) == span_byte);
if ((STRLEN) (send - s) >= PERL_WORDSIZE
+ PERL_WORDSIZE * PERL_IS_SUBWORD_ADDR(s)
- (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK))
{
PERL_UINTMAX_T span_word, mask_word;
while (PTR2nat(s) & PERL_WORD_BOUNDARY_MASK) {
if (((*s) & mask) != span_byte) {
return s;
}
s++;
}
span_word = PERL_COUNT_MULTIPLIER * span_byte;
mask_word = PERL_COUNT_MULTIPLIER * mask;
do {
PERL_UINTMAX_T masked = (* (PERL_UINTMAX_T *) s) & mask_word;
if (masked == span_word) {
s += PERL_WORDSIZE;
continue;
}
#ifdef EBCDIC
break;
#else
masked ^= span_word;
masked |= masked << 1;
masked |= masked << 2;
masked |= masked << 4;
return s + variant_byte_number(masked);
#endif
} while (s + PERL_WORDSIZE <= send);
}
while (s < send) {
if (((*s) & mask) != span_byte) {
return s;
}
s++;
}
return s;
}
/*
* pregexec and friends
*/
#ifndef PERL_IN_XSUB_RE
/*
- pregexec - match a regexp against a string
*/
I32
Perl_pregexec(pTHX_ REGEXP * const prog, char* stringarg, char *strend,
char *strbeg, SSize_t minend, SV *screamer, U32 nosave)
/* stringarg: the point in the string at which to begin matching */
/* strend: pointer to null at end of string */
/* strbeg: real beginning of string */
/* minend: end of match must be >= minend bytes after stringarg. */
/* screamer: SV being matched: only used for utf8 flag, pos() etc; string
* itself is accessed via the pointers above */
/* nosave: For optimizations. */
{
PERL_ARGS_ASSERT_PREGEXEC;
return
regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
nosave ? 0 : REXEC_COPY_STR);
}
#endif
/* re_intuit_start():
*
* Based on some optimiser hints, try to find the earliest position in the
* string where the regex could match.
*
* rx: the regex to match against
* sv: the SV being matched: only used for utf8 flag; the string
* itself is accessed via the pointers below. Note that on
* something like an overloaded SV, SvPOK(sv) may be false
* and the string pointers may point to something unrelated to
* the SV itself.
* strbeg: real beginning of string
* strpos: the point in the string at which to begin matching
* strend: pointer to the byte following the last char of the string
* flags currently unused; set to 0
* data: currently unused; set to NULL
*
* The basic idea of re_intuit_start() is to use some known information
* about the pattern, namely:
*
* a) the longest known anchored substring (i.e. one that's at a
* constant offset from the beginning of the pattern; but not
* necessarily at a fixed offset from the beginning of the
* string);
* b) the longest floating substring (i.e. one that's not at a constant
* offset from the beginning of the pattern);
* c) Whether the pattern is anchored to the string; either
* an absolute anchor: /^../, or anchored to \n: /^.../m,
* or anchored to pos(): /\G/;
* d) A start class: a real or synthetic character class which
* represents which characters are legal at the start of the pattern;
*
* to either quickly reject the match, or to find the earliest position
* within the string at which the pattern might match, thus avoiding
* running the full NFA engine at those earlier locations, only to
* eventually fail and retry further along.
*
* Returns NULL if the pattern can't match, or returns the address within
* the string which is the earliest place the match could occur.
*
* The longest of the anchored and floating substrings is called 'check'
* and is checked first. The other is called 'other' and is checked
* second. The 'other' substring may not be present. For example,
*
* /(abc|xyz)ABC\d{0,3}DEFG/
*
* will have
*
* check substr (float) = "DEFG", offset 6..9 chars
* other substr (anchored) = "ABC", offset 3..3 chars
* stclass = [ax]
*
* Be aware that during the course of this function, sometimes 'anchored'
* refers to a substring being anchored relative to the start of the
* pattern, and sometimes to the pattern itself being anchored relative to
* the string. For example:
*
* /\dabc/: "abc" is anchored to the pattern;
* /^\dabc/: "abc" is anchored to the pattern and the string;
* /\d+abc/: "abc" is anchored to neither the pattern nor the string;
* /^\d+abc/: "abc" is anchored to neither the pattern nor the string,
* but the pattern is anchored to the string.
*/
char *
Perl_re_intuit_start(pTHX_
REGEXP * const rx,
SV *sv,
const char * const strbeg,
char *strpos,
char *strend,
const U32 flags,
re_scream_pos_data *data)
{
struct regexp *const prog = ReANY(rx);
SSize_t start_shift = prog->check_offset_min;
/* Should be nonnegative! */
SSize_t end_shift = 0;
/* current lowest pos in string where the regex can start matching */
char *rx_origin = strpos;
SV *check;
const bool utf8_target = (sv && SvUTF8(sv)) ? 1 : 0; /* if no sv we have to assume bytes */
U8 other_ix = 1 - prog->substrs->check_ix;
bool ml_anch = 0;
char *other_last = strpos;/* latest pos 'other' substr already checked to */
char *check_at = NULL; /* check substr found at this pos */
const I32 multiline = prog->extflags & RXf_PMf_MULTILINE;
RXi_GET_DECL(prog,progi);
regmatch_info reginfo_buf; /* create some info to pass to find_byclass */
regmatch_info *const reginfo = ®info_buf;
DECLARE_AND_GET_RE_DEBUG_FLAGS;
PERL_ARGS_ASSERT_RE_INTUIT_START;
PERL_UNUSED_ARG(flags);
PERL_UNUSED_ARG(data);
DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
"Intuit: trying to determine minimum start position...\n"));
/* for now, assume that all substr offsets are positive. If at some point
* in the future someone wants to do clever things with lookbehind and
* -ve offsets, they'll need to fix up any code in this function
* which uses these offsets. See the thread beginning
*/
assert(prog->substrs->data[0].min_offset >= 0);
assert(prog->substrs->data[0].max_offset >= 0);
assert(prog->substrs->data[1].min_offset >= 0);
assert(prog->substrs->data[1].max_offset >= 0);
assert(prog->substrs->data[2].min_offset >= 0);
assert(prog->substrs->data[2].max_offset >= 0);
/* for now, assume that if both present, that the floating substring
* doesn't start before the anchored substring.
* If you break this assumption (e.g. doing better optimisations
* with lookahead/behind), then you'll need to audit the code in this
* function carefully first
*/
assert(
! ( (prog->anchored_utf8 || prog->anchored_substr)
&& (prog->float_utf8 || prog->float_substr))
|| (prog->float_min_offset >= prog->anchored_offset));
/* byte rather than char calculation for efficiency. It fails
* to quickly reject some cases that can't match, but will reject
* them later after doing full char arithmetic */
if (prog->minlen > strend - strpos) {
DEBUG_EXECUTE_r(Perl_re_printf( aTHX_
" String too short...\n"));
goto fail;
}