1 |
#! /usr/bin/perl |
#! /usr/bin/perl |
2 |
|
|
3 |
# Program for testing regular expressions with perl to check that PCRE handles |
# Program for testing regular expressions with perl to check that PCRE handles |
4 |
# them the same. |
# 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 |
|
|
11 |
# Function for turning a string into a string of printing chars |
# Function for turning a string into a string of printing chars. There are |
12 |
|
# currently problems with UTF-8 strings; this fudges round them. |
13 |
|
|
14 |
sub pchars { |
sub pchars { |
15 |
my($t) = ""; |
my($t) = ""; |
16 |
|
|
17 |
foreach $c (split(//, @_[0])) |
if ($utf8) |
18 |
{ |
{ |
19 |
if (ord $c >= 32 && ord $c < 127) { $t .= $c; } |
# use utf8; <=============== For UTF-8 in Perl 5.6 |
20 |
else { $t .= sprintf("\\x%02x", ord $c); } |
@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 |
|
} |
27 |
|
|
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 |
$t; |
$t; |
38 |
} |
} |
39 |
|
|
59 |
} |
} |
60 |
else { $outfile = "STDOUT"; } |
else { $outfile = "STDOUT"; } |
61 |
|
|
62 |
printf($outfile "Perl Regular Expressions\n\n"); |
printf($outfile "Perl $] Regular Expressions\n\n"); |
63 |
|
|
64 |
# Main loop |
# Main loop |
65 |
|
|
73 |
|
|
74 |
$pattern = $_; |
$pattern = $_; |
75 |
|
|
|
$delimiter = substr($_, 0, 1); |
|
76 |
while ($pattern !~ /^\s*(.).*\1/s) |
while ($pattern !~ /^\s*(.).*\1/s) |
77 |
{ |
{ |
78 |
printf " > " if $infile eq "STDIN"; |
printf " > " if $infile eq "STDIN"; |
84 |
chomp($pattern); |
chomp($pattern); |
85 |
$pattern =~ s/\s+$//; |
$pattern =~ s/\s+$//; |
86 |
|
|
87 |
|
# The private /+ modifier means "print $' afterwards". |
88 |
|
|
89 |
|
$showrest = ($pattern =~ s/\+(?=[a-z]*$)//); |
90 |
|
|
91 |
|
# The private /8 modifier means "operate in UTF-8". Currently, Perl |
92 |
|
# has bugs that we try to work around using this flag. |
93 |
|
|
94 |
|
$utf8 = ($pattern =~ s/8(?=[a-z]*$)//); |
95 |
|
|
96 |
# Check that the pattern is valid |
# Check that the pattern is valid |
97 |
|
|
98 |
eval "\$_ =~ ${pattern}"; |
if ($utf8) |
99 |
|
{ |
100 |
|
# use utf8; <=============== For UTF-8 in Perl 5.6 |
101 |
|
eval "\$_ =~ ${pattern}"; |
102 |
|
} |
103 |
|
else |
104 |
|
{ |
105 |
|
eval "\$_ =~ ${pattern}"; |
106 |
|
} |
107 |
|
|
108 |
if ($@) |
if ($@) |
109 |
{ |
{ |
110 |
printf $outfile "Error: $@\n"; |
printf $outfile "Error: $@"; |
111 |
next NEXT_RE; |
next NEXT_RE; |
112 |
} |
} |
113 |
|
|
114 |
|
# If the /g modifier is present, we want to put a loop round the matching; |
115 |
|
# otherwise just a single "if". |
116 |
|
|
117 |
|
$cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if"; |
118 |
|
|
119 |
|
# If the pattern is actually the null string, Perl uses the most recently |
120 |
|
# executed (and successfully compiled) regex is used instead. This is a |
121 |
|
# nasty trap for the unwary! The PCRE test suite does contain null strings |
122 |
|
# in places - if they are allowed through here all sorts of weird and |
123 |
|
# unexpected effects happen. To avoid this, we replace such patterns with |
124 |
|
# a non-null pattern that has the same effect. |
125 |
|
|
126 |
|
$pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/); |
127 |
|
|
128 |
# Read data lines and test them |
# Read data lines and test them |
129 |
|
|
130 |
for (;;) |
for (;;) |
139 |
|
|
140 |
last if ($_ eq ""); |
last if ($_ eq ""); |
141 |
|
|
142 |
$_ = eval "\"$_\""; # To get escapes processed |
$x = eval "\"$_\""; # To get escapes processed |
143 |
|
|
144 |
|
# Empty array for holding results, then do the matching. |
145 |
|
|
146 |
$ok = 0; |
@subs = (); |
147 |
eval "if (\$_ =~ ${pattern}) {" . |
|
148 |
"\$z = \$&;" . |
$pushes = "push \@subs,\$&;" . |
149 |
"\$a = \$1;" . |
"push \@subs,\$1;" . |
150 |
"\$b = \$2;" . |
"push \@subs,\$2;" . |
151 |
"\$c = \$3;" . |
"push \@subs,\$3;" . |
152 |
"\$d = \$4;" . |
"push \@subs,\$4;" . |
153 |
"\$e = \$5;" . |
"push \@subs,\$5;" . |
154 |
"\$f = \$6;" . |
"push \@subs,\$6;" . |
155 |
"\$g = \$7;" . |
"push \@subs,\$7;" . |
156 |
"\$h = \$8;" . |
"push \@subs,\$8;" . |
157 |
"\$i = \$9;" . |
"push \@subs,\$9;" . |
158 |
"\$j = \$10;" . |
"push \@subs,\$10;" . |
159 |
"\$k = \$11;" . |
"push \@subs,\$11;" . |
160 |
"\$l = \$12;" . |
"push \@subs,\$12;" . |
161 |
"\$m = \$13;" . |
"push \@subs,\$13;" . |
162 |
"\$n = \$14;" . |
"push \@subs,\$14;" . |
163 |
"\$o = \$15;" . |
"push \@subs,\$15;" . |
164 |
"\$p = \$16;" . |
"push \@subs,\$16;" . |
165 |
"\$ok = 1; }"; |
"push \@subs,\$'; }"; |
166 |
|
|
167 |
|
if ($utf8) |
168 |
|
{ |
169 |
|
# use utf8; <=============== For UTF-8 in Perl 5.6 |
170 |
|
eval "${cmd} (\$x =~ ${pattern}) {" . $pushes; |
171 |
|
} |
172 |
|
else |
173 |
|
{ |
174 |
|
eval "${cmd} (\$x =~ ${pattern}) {" . $pushes; |
175 |
|
} |
176 |
|
|
177 |
if ($@) |
if ($@) |
178 |
{ |
{ |
179 |
printf $outfile "Error: $@\n"; |
printf $outfile "Error: $@\n"; |
180 |
next NEXT_RE; |
next NEXT_RE; |
181 |
} |
} |
182 |
elsif (!$ok) |
elsif (scalar(@subs) == 0) |
183 |
{ |
{ |
184 |
printf $outfile "No match\n"; |
printf $outfile "No match\n"; |
185 |
} |
} |
186 |
else |
else |
187 |
{ |
{ |
188 |
@subs = ($z,$a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n,$o,$p); |
while (scalar(@subs) != 0) |
|
$last_printed = 0; |
|
|
for ($i = 0; $i <= 17; $i++) |
|
189 |
{ |
{ |
190 |
if ($i == 0 || defined $subs[$i]) |
printf $outfile (" 0: %s\n", &pchars($subs[0])); |
191 |
|
printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest; |
192 |
|
$last_printed = 0; |
193 |
|
for ($i = 1; $i <= 16; $i++) |
194 |
{ |
{ |
195 |
while ($last_printed++ < $i-1) |
if (defined $subs[$i]) |
196 |
{ printf $outfile ("%2d: <unset>\n", $last_printed); } |
{ |
197 |
printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i])); |
while ($last_printed++ < $i-1) |
198 |
$last_printed = $i; |
{ printf $outfile ("%2d: <unset>\n", $last_printed); } |
199 |
|
printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i])); |
200 |
|
$last_printed = $i; |
201 |
|
} |
202 |
} |
} |
203 |
|
splice(@subs, 0, 18); |
204 |
} |
} |
205 |
} |
} |
206 |
} |
} |