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

Contents of /code/trunk/perltest.pl

Parent Directory Parent Directory | Revision Log Revision Log


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

1 #! /usr/bin/perl
2
3 # Program for testing regular expressions with perl to check that PCRE handles
4 # 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
10 use locale;
11
12
13 # 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
16 sub pchars {
17 my($t) = "";
18
19 if ($utf8)
20 {
21 # 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 }
29
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 $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 printf($outfile "Perl $] Regular Expressions\n\n");
65
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 # The private /+ modifier means "print $' afterwards".
90
91 $showrest = ($pattern =~ s/\+(?=[a-z]*$)//);
92
93 # 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 # Check that the pattern is valid
99
100 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 if ($@)
111 {
112 printf $outfile "Error: $@";
113 next NEXT_RE;
114 }
115
116 # 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 # 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 $x = eval "\"$_\""; # To get escapes processed
145
146 # Empty array for holding results, then do the matching.
147
148 @subs = ();
149
150 $pushes = "push \@subs,\$&;" .
151 "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 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 if ($@)
180 {
181 printf $outfile "Error: $@\n";
182 next NEXT_RE;
183 }
184 elsif (scalar(@subs) == 0)
185 {
186 printf $outfile "No match\n";
187 }
188 else
189 {
190 while (scalar(@subs) != 0)
191 {
192 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 {
197 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 }
205 splice(@subs, 0, 18);
206 }
207 }
208 }
209 }
210
211 # printf $outfile "\n";
212
213 # End

Properties

Name Value
svn:executable *

webmaster@exim.org
ViewVC Help
Powered by ViewVC 1.1.12