3 # Program for testing regular expressions with perl to check that PCRE handles
7 # Function for turning a string into a string of printing chars
12 foreach $c (split(//, $_[0]))
14 if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
15 else { $t .= sprintf("\\x%02x", ord $c); }
22 # Read lines from named file or stdin and write to named file or stdout; lines
23 # consist of a regular expression, in delimiters and optionally followed by
24 # options, followed by a set of test data, terminated by an empty line.
26 # Sort out the input and output files
30 open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
33 else { $infile = "STDIN"; }
37 open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
40 else { $outfile = "STDOUT"; }
42 printf($outfile "Perl $] Regular Expressions\n\n");
49 printf " re> " if $infile eq "STDIN";
50 last if ! ($_ = <$infile>);
51 printf $outfile "$_" if $infile ne "STDIN";
56 while ($pattern !~ /^\s*(.).*\1/s)
58 printf " > " if $infile eq "STDIN";
59 last if ! ($_ = <$infile>);
60 printf $outfile "$_" if $infile ne "STDIN";
67 # The private /+ modifier means "print $' afterwards". We use it
68 # only on the end of patterns to make it easy to chop off here.
70 $showrest = ($pattern =~ s/\+(?=[a-z]*$)//);
72 # Check that the pattern is valid
74 eval "\$_ =~ ${pattern}";
77 printf $outfile "Error: $@";
81 # If the /g modifier is present, we want to put a loop round the matching;
82 # otherwise just a single "if".
84 $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
86 # If the pattern is actually the null string, Perl uses the most recently
87 # executed (and successfully compiled) regex is used instead. This is a
88 # nasty trap for the unwary! The PCRE test suite does contain null strings
89 # in places - if they are allowed through here all sorts of weird and
90 # unexpected effects happen. To avoid this, we replace such patterns with
91 # a non-null pattern that has the same effect.
93 $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
95 # Read data lines and test them
99 printf "data> " if $infile eq "STDIN";
100 last NEXT_RE if ! ($_ = <$infile>);
102 printf $outfile "$_\n" if $infile ne "STDIN";
109 $x = eval "\"$_\""; # To get escapes processed
111 # Empty array for holding results, then do the matching.
115 eval "${cmd} (\$x =~ ${pattern}) {" .
126 "push \@subs,\$10;" .
127 "push \@subs,\$11;" .
128 "push \@subs,\$12;" .
129 "push \@subs,\$13;" .
130 "push \@subs,\$14;" .
131 "push \@subs,\$15;" .
132 "push \@subs,\$16;" .
133 "push \@subs,\$'; }";
137 printf $outfile "Error: $@\n";
140 elsif (scalar(@subs) == 0)
142 printf $outfile "No match\n";
146 while (scalar(@subs) != 0)
148 printf $outfile (" 0: %s\n", &pchars($subs[0]));
149 printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
151 for ($i = 1; $i <= 16; $i++)
153 if (defined $subs[$i])
155 while ($last_printed++ < $i-1)
156 { printf $outfile ("%2d: <unset>\n", $last_printed); }
157 printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
161 splice(@subs, 0, 18);
167 printf $outfile "\n";