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

Contents of /code/trunk/perltest.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 87 - (hide annotations) (download)
Sat Feb 24 21:41:21 2007 UTC (7 years, 8 months ago) by nigel
Original Path: code/trunk/perltest
File size: 5247 byte(s)
Load pcre-6.5 into code/trunk.

1 nigel 3 #! /usr/bin/perl
2    
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     # stands, it requires at least Perl 5.8 for UTF-8 support. For Perl 5.6, it
6     # can be used as is for non-UTF-8 testing, but you have to uncomment the
7     # "use utf8" lines in order to to UTF-8 stuff (and you mustn't uncomment them
8     # for non-UTF-8 use).
9 nigel 3
10 nigel 87 use locale;
11 nigel 3
12 nigel 87
13 nigel 63 # Function for turning a string into a string of printing chars. There are
14     # currently problems with UTF-8 strings; this fudges round them.
15 nigel 3
16     sub pchars {
17     my($t) = "";
18    
19 nigel 63 if ($utf8)
20 nigel 3 {
21 nigel 63 # use utf8; <=============== For UTF-8 in Perl 5.6
22     @p = unpack('U*', $_[0]);
23     foreach $c (@p)
24     {
25     if ($c >= 32 && $c < 127) { $t .= chr $c; }
26     else { $t .= sprintf("\\x{%02x}", $c); }
27     }
28 nigel 3 }
29 nigel 63
30     else
31     {
32     foreach $c (split(//, $_[0]))
33     {
34     if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
35     else { $t .= sprintf("\\x%02x", ord $c); }
36     }
37     }
38    
39 nigel 3 $t;
40     }
41    
42    
43    
44     # Read lines from named file or stdin and write to named file or stdout; lines
45     # consist of a regular expression, in delimiters and optionally followed by
46     # options, followed by a set of test data, terminated by an empty line.
47    
48     # Sort out the input and output files
49    
50     if (@ARGV > 0)
51     {
52     open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
53     $infile = "INFILE";
54     }
55     else { $infile = "STDIN"; }
56    
57     if (@ARGV > 1)
58     {
59     open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
60     $outfile = "OUTFILE";
61     }
62     else { $outfile = "STDOUT"; }
63    
64 nigel 23 printf($outfile "Perl $] Regular Expressions\n\n");
65 nigel 3
66     # Main loop
67    
68     NEXT_RE:
69     for (;;)
70     {
71     printf " re> " if $infile eq "STDIN";
72     last if ! ($_ = <$infile>);
73     printf $outfile "$_" if $infile ne "STDIN";
74     next if ($_ eq "");
75    
76     $pattern = $_;
77    
78     while ($pattern !~ /^\s*(.).*\1/s)
79     {
80     printf " > " if $infile eq "STDIN";
81     last if ! ($_ = <$infile>);
82     printf $outfile "$_" if $infile ne "STDIN";
83     $pattern .= $_;
84     }
85    
86     chomp($pattern);
87     $pattern =~ s/\s+$//;
88    
89 nigel 63 # The private /+ modifier means "print $' afterwards".
90 nigel 41
91     $showrest = ($pattern =~ s/\+(?=[a-z]*$)//);
92    
93 nigel 63 # The private /8 modifier means "operate in UTF-8". Currently, Perl
94     # has bugs that we try to work around using this flag.
95    
96     $utf8 = ($pattern =~ s/8(?=[a-z]*$)//);
97    
98 nigel 3 # Check that the pattern is valid
99    
100 nigel 63 if ($utf8)
101     {
102     # use utf8; <=============== For UTF-8 in Perl 5.6
103     eval "\$_ =~ ${pattern}";
104     }
105     else
106     {
107     eval "\$_ =~ ${pattern}";
108     }
109    
110 nigel 3 if ($@)
111     {
112 nigel 23 printf $outfile "Error: $@";
113 nigel 3 next NEXT_RE;
114     }
115    
116 nigel 41 # If the /g modifier is present, we want to put a loop round the matching;
117     # otherwise just a single "if".
118    
119     $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
120    
121     # If the pattern is actually the null string, Perl uses the most recently
122     # executed (and successfully compiled) regex is used instead. This is a
123     # nasty trap for the unwary! The PCRE test suite does contain null strings
124     # in places - if they are allowed through here all sorts of weird and
125     # unexpected effects happen. To avoid this, we replace such patterns with
126     # a non-null pattern that has the same effect.
127    
128     $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
129    
130 nigel 3 # Read data lines and test them
131    
132     for (;;)
133     {
134     printf "data> " if $infile eq "STDIN";
135     last NEXT_RE if ! ($_ = <$infile>);
136     chomp;
137     printf $outfile "$_\n" if $infile ne "STDIN";
138    
139     s/\s+$//;
140     s/^\s+//;
141    
142     last if ($_ eq "");
143    
144 nigel 41 $x = eval "\"$_\""; # To get escapes processed
145 nigel 3
146 nigel 41 # Empty array for holding results, then do the matching.
147 nigel 3
148 nigel 41 @subs = ();
149    
150 nigel 63 $pushes = "push \@subs,\$&;" .
151 nigel 41 "push \@subs,\$1;" .
152     "push \@subs,\$2;" .
153     "push \@subs,\$3;" .
154     "push \@subs,\$4;" .
155     "push \@subs,\$5;" .
156     "push \@subs,\$6;" .
157     "push \@subs,\$7;" .
158     "push \@subs,\$8;" .
159     "push \@subs,\$9;" .
160     "push \@subs,\$10;" .
161     "push \@subs,\$11;" .
162     "push \@subs,\$12;" .
163     "push \@subs,\$13;" .
164     "push \@subs,\$14;" .
165     "push \@subs,\$15;" .
166     "push \@subs,\$16;" .
167     "push \@subs,\$'; }";
168    
169 nigel 63 if ($utf8)
170     {
171     # use utf8; <=============== For UTF-8 in Perl 5.6
172     eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
173     }
174     else
175     {
176     eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
177     }
178    
179 nigel 3 if ($@)
180     {
181     printf $outfile "Error: $@\n";
182     next NEXT_RE;
183     }
184 nigel 41 elsif (scalar(@subs) == 0)
185 nigel 3 {
186     printf $outfile "No match\n";
187     }
188     else
189     {
190 nigel 41 while (scalar(@subs) != 0)
191 nigel 3 {
192 nigel 41 printf $outfile (" 0: %s\n", &pchars($subs[0]));
193     printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
194     $last_printed = 0;
195     for ($i = 1; $i <= 16; $i++)
196 nigel 3 {
197 nigel 41 if (defined $subs[$i])
198     {
199     while ($last_printed++ < $i-1)
200     { printf $outfile ("%2d: <unset>\n", $last_printed); }
201     printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
202     $last_printed = $i;
203     }
204 nigel 3 }
205 nigel 41 splice(@subs, 0, 18);
206 nigel 3 }
207     }
208     }
209     }
210    
211 nigel 75 # printf $outfile "\n";
212 nigel 3
213     # End

Properties

Name Value
svn:executable *

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12