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. It
5 # requires at least Perl 5.6.
8 # Function for turning a string into a string of printing chars. There are
9 # currently problems with UTF-8 strings; this fudges round them.
17 @p = unpack('U*', $_[0]);
20 if ($c >= 32 && $c < 127) { $t .= chr $c; }
21 else { $t .= sprintf("\\x{%02x}", $c); }
27 foreach $c (split(//, $_[0]))
29 if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
30 else { $t .= sprintf("\\x%02x", ord $c); }
39 # Read lines from named file or stdin and write to named file or stdout; lines
40 # consist of a regular expression, in delimiters and optionally followed by
41 # options, followed by a set of test data, terminated by an empty line.
43 # Sort out the input and output files
47 open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
50 else { $infile = "STDIN"; }
54 open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
57 else { $outfile = "STDOUT"; }
59 printf($outfile "Perl $] Regular Expressions\n\n");
66 printf " re> " if $infile eq "STDIN";
67 last if ! ($_ = <$infile>);
68 printf $outfile "$_" if $infile ne "STDIN";
73 while ($pattern !~ /^\s*(.).*\1/s)
75 printf " > " if $infile eq "STDIN";
76 last if ! ($_ = <$infile>);
77 printf $outfile "$_" if $infile ne "STDIN";
84 # The private /+ modifier means "print $' afterwards".
86 $showrest = ($pattern =~ s/\+(?=[a-z]*$)//);
88 # The private /8 modifier means "operate in UTF-8". Currently, Perl
89 # has bugs that we try to work around using this flag.
91 $utf8 = ($pattern =~ s/8(?=[a-z]*$)//);
93 # Check that the pattern is valid
98 eval "\$_ =~ ${pattern}";
102 eval "\$_ =~ ${pattern}";
107 printf $outfile "Error: $@";
111 # If the /g modifier is present, we want to put a loop round the matching;
112 # otherwise just a single "if".
114 $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
116 # If the pattern is actually the null string, Perl uses the most recently
117 # executed (and successfully compiled) regex is used instead. This is a
118 # nasty trap for the unwary! The PCRE test suite does contain null strings
119 # in places - if they are allowed through here all sorts of weird and
120 # unexpected effects happen. To avoid this, we replace such patterns with
121 # a non-null pattern that has the same effect.
123 $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
125 # Read data lines and test them
129 printf "data> " if $infile eq "STDIN";
130 last NEXT_RE if ! ($_ = <$infile>);
132 printf $outfile "$_\n" if $infile ne "STDIN";
139 $x = eval "\"$_\""; # To get escapes processed
141 # Empty array for holding results, then do the matching.
145 $pushes = "push \@subs,\$&;" .
155 "push \@subs,\$10;" .
156 "push \@subs,\$11;" .
157 "push \@subs,\$12;" .
158 "push \@subs,\$13;" .
159 "push \@subs,\$14;" .
160 "push \@subs,\$15;" .
161 "push \@subs,\$16;" .
162 "push \@subs,\$'; }";
167 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
171 eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
176 printf $outfile "Error: $@\n";
179 elsif (scalar(@subs) == 0)
181 printf $outfile "No match\n";
185 while (scalar(@subs) != 0)
187 printf $outfile (" 0: %s\n", &pchars($subs[0]));
188 printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
190 for ($i = 1; $i <= 16; $i++)
192 if (defined $subs[$i])
194 while ($last_printed++ < $i-1)
195 { printf $outfile ("%2d: <unset>\n", $last_printed); }
196 printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
200 splice(@subs, 0, 18);
206 printf $outfile "\n";