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

Contents of /code/trunk/perltest.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 617 - (hide annotations) (download)
Tue Jul 12 11:00:10 2011 UTC (3 years, 4 months ago) by ph10
File MIME type: text/plain
File size: 5774 byte(s)
Small code and comment tidy

1 ph10 97 #! /usr/bin/env perl
2 nigel 3
3     # Program for testing regular expressions with perl to check that PCRE handles
4 nigel 63 # them the same. This is the version that supports /8 for UTF-8 testing. As it
5 nigel 91 # stands, it requires at least Perl 5.8 for UTF-8 support. However, it needs to
6     # have "use utf8" at the start for running the UTF-8 tests, but *not* for the
7     # other tests. The only way I've found for doing this is to cat this line in
8     # explicitly in the RunPerlTest script.
9 nigel 3
10 nigel 91 # use locale; # With this included, \x0b matches \s!
11 nigel 3
12 nigel 63 # Function for turning a string into a string of printing chars. There are
13     # currently problems with UTF-8 strings; this fudges round them.
14 nigel 3
15     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     else { $t .= sprintf("\\x{%02x}", $c); }
25     }
26 nigel 3 }
27 nigel 63
28     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     next if ($_ eq "");
72    
73     $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     chomp($pattern);
84     $pattern =~ s/\s+$//;
85    
86 nigel 63 # The private /+ modifier means "print $' afterwards".
87 nigel 41
88 ph10 510 $showrest = ($pattern =~ s/\+(?=[a-zA-Z]*$)//);
89 ph10 617
90     # A doubled version is used by pcretest to print remainders after captures
91    
92     $pattern =~ s/\+(?=[a-zA-Z]*$)//;
93 nigel 41
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 518 # Remove /W from a pattern (asks pcretest to set PCRE_UCP)
107    
108     $pattern =~ s/W(?=[a-zA-Z]*$)//;
109    
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 nigel 3 # Check that the pattern is valid
115    
116 nigel 91 eval "\$_ =~ ${pattern}";
117 nigel 3 if ($@)
118     {
119 nigel 23 printf $outfile "Error: $@";
120 nigel 3 next NEXT_RE;
121     }
122    
123 nigel 41 # If the /g modifier is present, we want to put a loop round the matching;
124     # otherwise just a single "if".
125    
126     $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
127    
128     # If the pattern is actually the null string, Perl uses the most recently
129     # executed (and successfully compiled) regex is used instead. This is a
130     # nasty trap for the unwary! The PCRE test suite does contain null strings
131     # in places - if they are allowed through here all sorts of weird and
132     # unexpected effects happen. To avoid this, we replace such patterns with
133     # a non-null pattern that has the same effect.
134    
135     $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
136    
137 nigel 3 # Read data lines and test them
138    
139     for (;;)
140     {
141     printf "data> " if $infile eq "STDIN";
142     last NEXT_RE if ! ($_ = <$infile>);
143     chomp;
144     printf $outfile "$_\n" if $infile ne "STDIN";
145    
146 ph10 510 s/\s+$//; # Remove trailing space
147     s/^\s+//; # Remove leading space
148     s/\\Y//g; # Remove \Y (pcretest flag to set PCRE_NO_START_OPTIMIZE)
149 nigel 3
150     last if ($_ eq "");
151 nigel 41 $x = eval "\"$_\""; # To get escapes processed
152 nigel 3
153 ph10 488 # Empty array for holding results, ensure $REGERROR and $REGMARK are
154     # unset, then do the matching.
155 nigel 3
156 nigel 41 @subs = ();
157    
158 nigel 63 $pushes = "push \@subs,\$&;" .
159 nigel 41 "push \@subs,\$1;" .
160     "push \@subs,\$2;" .
161     "push \@subs,\$3;" .
162     "push \@subs,\$4;" .
163     "push \@subs,\$5;" .
164     "push \@subs,\$6;" .
165     "push \@subs,\$7;" .
166     "push \@subs,\$8;" .
167     "push \@subs,\$9;" .
168     "push \@subs,\$10;" .
169     "push \@subs,\$11;" .
170     "push \@subs,\$12;" .
171     "push \@subs,\$13;" .
172     "push \@subs,\$14;" .
173     "push \@subs,\$15;" .
174     "push \@subs,\$16;" .
175     "push \@subs,\$'; }";
176    
177 ph10 488 undef $REGERROR;
178     undef $REGMARK;
179    
180 nigel 91 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
181 nigel 63
182 nigel 3 if ($@)
183     {
184     printf $outfile "Error: $@\n";
185     next NEXT_RE;
186     }
187 nigel 41 elsif (scalar(@subs) == 0)
188 nigel 3 {
189 ph10 488 printf $outfile "No match";
190     if (defined $REGERROR && $REGERROR != 1)
191     { print $outfile (", mark = $REGERROR"); }
192     printf $outfile "\n";
193 nigel 3 }
194     else
195     {
196 nigel 41 while (scalar(@subs) != 0)
197 nigel 3 {
198 nigel 41 printf $outfile (" 0: %s\n", &pchars($subs[0]));
199     printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
200     $last_printed = 0;
201     for ($i = 1; $i <= 16; $i++)
202 nigel 3 {
203 nigel 41 if (defined $subs[$i])
204     {
205     while ($last_printed++ < $i-1)
206     { printf $outfile ("%2d: <unset>\n", $last_printed); }
207     printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
208     $last_printed = $i;
209     }
210 nigel 3 }
211 nigel 41 splice(@subs, 0, 18);
212 nigel 3 }
213 ph10 488 if (defined $REGMARK && $REGMARK != 1)
214     { print $outfile ("MK: $REGMARK\n"); }
215 nigel 3 }
216     }
217     }
218    
219 nigel 75 # printf $outfile "\n";
220 nigel 3
221     # End

Properties

Name Value
svn:executable *

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12