/[pcre]/code/trunk/perltest.pl
ViewVC logotype

Contents of /code/trunk/perltest.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1502 - (hide annotations) (download)
Mon Sep 15 13:56:18 2014 UTC (2 weeks, 1 day ago) by ph10
File MIME type: text/plain
File size: 6273 byte(s)
Files tidied for 8.36-RC1.

1 ph10 97 #! /usr/bin/env perl
2 nigel 3
3     # Program for testing regular expressions with perl to check that PCRE handles
4 ph10 1385 # them the same. This version needs to have "use utf8" at the start for running
5     # the UTF-8 tests, but *not* for the other tests. The only way I've found for
6     # doing this is to cat this line in explicitly in the RunPerlTest script. I've
7     # also used this method to supply "require Encode" for the UTF-8 tests, so that
8     # the main test will still run where Encode is not installed.
9 nigel 3
10 ph10 1046 #use utf8;
11 ph10 871 #require Encode;
12    
13 ph10 1385 # Function for turning a string into a string of printing chars.
14    
15 nigel 3 sub pchars {
16     my($t) = "";
17    
18 nigel 63 if ($utf8)
19 nigel 3 {
20 nigel 63 @p = unpack('U*', $_[0]);
21     foreach $c (@p)
22     {
23     if ($c >= 32 && $c < 127) { $t .= chr $c; }
24 ph10 903 else { $t .= sprintf("\\x{%02x}", $c);
25 ph10 871 }
26 nigel 63 }
27 nigel 3 }
28 nigel 63 else
29     {
30     foreach $c (split(//, $_[0]))
31     {
32     if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
33     else { $t .= sprintf("\\x%02x", ord $c); }
34     }
35     }
36    
37 nigel 3 $t;
38     }
39    
40    
41     # Read lines from named file or stdin and write to named file or stdout; lines
42     # consist of a regular expression, in delimiters and optionally followed by
43     # options, followed by a set of test data, terminated by an empty line.
44    
45     # Sort out the input and output files
46    
47     if (@ARGV > 0)
48     {
49     open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
50     $infile = "INFILE";
51     }
52     else { $infile = "STDIN"; }
53    
54     if (@ARGV > 1)
55     {
56     open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
57     $outfile = "OUTFILE";
58     }
59     else { $outfile = "STDOUT"; }
60    
61 nigel 23 printf($outfile "Perl $] Regular Expressions\n\n");
62 nigel 3
63     # Main loop
64    
65     NEXT_RE:
66     for (;;)
67     {
68     printf " re> " if $infile eq "STDIN";
69     last if ! ($_ = <$infile>);
70     printf $outfile "$_" if $infile ne "STDIN";
71 ph10 1388 next if ($_ =~ /^\s*$/ || $_ =~ /^< forbid/);
72 ph10 1404
73 nigel 3 $pattern = $_;
74    
75     while ($pattern !~ /^\s*(.).*\1/s)
76     {
77     printf " > " if $infile eq "STDIN";
78     last if ! ($_ = <$infile>);
79     printf $outfile "$_" if $infile ne "STDIN";
80     $pattern .= $_;
81     }
82    
83 ph10 1497 chomp($pattern);
84     $pattern =~ s/\s+$//;
85 nigel 3
86 nigel 63 # The private /+ modifier means "print $' afterwards".
87 nigel 41
88 ph10 510 $showrest = ($pattern =~ s/\+(?=[a-zA-Z]*$)//);
89 ph10 654
90 ph10 617 # A doubled version is used by pcretest to print remainders after captures
91 nigel 41
92 ph10 654 $pattern =~ s/\+(?=[a-zA-Z]*$)//;
93    
94 nigel 91 # Remove /8 from a UTF-8 pattern.
95 nigel 63
96 ph10 510 $utf8 = $pattern =~ s/8(?=[a-zA-Z]*$)//;
97 ph10 461
98 ph10 459 # Remove /J from a pattern with duplicate names.
99 nigel 63
100 ph10 510 $pattern =~ s/J(?=[a-zA-Z]*$)//;
101 ph10 461
102 ph10 512 # Remove /K from a pattern (asks pcretest to check MARK data) */
103    
104 ph10 510 $pattern =~ s/K(?=[a-zA-Z]*$)//;
105    
106 ph10 1385 # /W asks pcretest to set PCRE_UCP; change this to /u for Perl
107 ph10 518
108 ph10 1385 $pattern =~ s/W(?=[a-zA-Z]*$)/u/;
109 ph10 518
110 ph10 612 # Remove /S or /SS from a pattern (asks pcretest to study or not to study)
111    
112     $pattern =~ s/S(?=[a-zA-Z]*$)//g;
113    
114 ph10 1399 # Remove /Y and /O from a pattern (disable PCRE optimizations)
115 ph10 836
116 ph10 1399 $pattern =~ s/[YO](?=[a-zA-Z]*$)//;
117 ph10 836
118 nigel 3 # Check that the pattern is valid
119    
120 nigel 91 eval "\$_ =~ ${pattern}";
121 nigel 3 if ($@)
122     {
123 nigel 23 printf $outfile "Error: $@";
124 ph10 1497 if ($infile != "STDIN")
125     {
126     for (;;)
127     {
128     last if ! ($_ = <$infile>);
129 ph10 1502 last if $_ =~ /^\s*$/;
130     }
131     }
132 nigel 3 next NEXT_RE;
133     }
134    
135 nigel 41 # If the /g modifier is present, we want to put a loop round the matching;
136     # otherwise just a single "if".
137    
138     $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
139    
140     # If the pattern is actually the null string, Perl uses the most recently
141     # executed (and successfully compiled) regex is used instead. This is a
142     # nasty trap for the unwary! The PCRE test suite does contain null strings
143     # in places - if they are allowed through here all sorts of weird and
144     # unexpected effects happen. To avoid this, we replace such patterns with
145     # a non-null pattern that has the same effect.
146    
147     $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
148    
149 nigel 3 # Read data lines and test them
150    
151     for (;;)
152     {
153     printf "data> " if $infile eq "STDIN";
154     last NEXT_RE if ! ($_ = <$infile>);
155     chomp;
156     printf $outfile "$_\n" if $infile ne "STDIN";
157    
158 ph10 510 s/\s+$//; # Remove trailing space
159     s/^\s+//; # Remove leading space
160     s/\\Y//g; # Remove \Y (pcretest flag to set PCRE_NO_START_OPTIMIZE)
161 nigel 3
162     last if ($_ eq "");
163 nigel 41 $x = eval "\"$_\""; # To get escapes processed
164 nigel 3
165 ph10 488 # Empty array for holding results, ensure $REGERROR and $REGMARK are
166     # unset, then do the matching.
167 nigel 3
168 nigel 41 @subs = ();
169    
170 nigel 63 $pushes = "push \@subs,\$&;" .
171 nigel 41 "push \@subs,\$1;" .
172     "push \@subs,\$2;" .
173     "push \@subs,\$3;" .
174     "push \@subs,\$4;" .
175     "push \@subs,\$5;" .
176     "push \@subs,\$6;" .
177     "push \@subs,\$7;" .
178     "push \@subs,\$8;" .
179     "push \@subs,\$9;" .
180     "push \@subs,\$10;" .
181     "push \@subs,\$11;" .
182     "push \@subs,\$12;" .
183     "push \@subs,\$13;" .
184     "push \@subs,\$14;" .
185     "push \@subs,\$15;" .
186     "push \@subs,\$16;" .
187     "push \@subs,\$'; }";
188    
189 ph10 488 undef $REGERROR;
190     undef $REGMARK;
191    
192 nigel 91 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
193 nigel 63
194 nigel 3 if ($@)
195     {
196     printf $outfile "Error: $@\n";
197     next NEXT_RE;
198     }
199 nigel 41 elsif (scalar(@subs) == 0)
200 nigel 3 {
201 ph10 488 printf $outfile "No match";
202     if (defined $REGERROR && $REGERROR != 1)
203 ph10 871 { printf $outfile (", mark = %s", &pchars($REGERROR)); }
204 ph10 488 printf $outfile "\n";
205 nigel 3 }
206     else
207     {
208 nigel 41 while (scalar(@subs) != 0)
209 nigel 3 {
210 nigel 41 printf $outfile (" 0: %s\n", &pchars($subs[0]));
211     printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
212     $last_printed = 0;
213     for ($i = 1; $i <= 16; $i++)
214 nigel 3 {
215 nigel 41 if (defined $subs[$i])
216     {
217     while ($last_printed++ < $i-1)
218     { printf $outfile ("%2d: <unset>\n", $last_printed); }
219     printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
220     $last_printed = $i;
221     }
222 nigel 3 }
223 nigel 41 splice(@subs, 0, 18);
224 nigel 3 }
225 ph10 903
226 ph10 871 # It seems that $REGMARK is not marked as UTF-8 even when use utf8 is
227     # set and the input pattern was a UTF-8 string. We can, however, force
228 ph10 903 # it to be so marked.
229    
230 ph10 488 if (defined $REGMARK && $REGMARK != 1)
231 ph10 871 {
232 ph10 903 $xx = $REGMARK;
233     $xx = Encode::decode_utf8($xx) if $utf8;
234     printf $outfile ("MK: %s\n", &pchars($xx));
235 ph10 871 }
236 nigel 3 }
237     }
238     }
239    
240 nigel 75 # printf $outfile "\n";
241 nigel 3
242     # End

Properties

Name Value
svn:executable *

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12