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

Diff of /code/trunk/perltest.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3 by nigel, Sat Feb 24 21:38:01 2007 UTC revision 87 by nigel, Sat Feb 24 21:41:21 2007 UTC
# Line 1  Line 1 
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    use locale;
11    
12  # Function for turning a string into a string of printing chars  
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 {  sub pchars {
17  my($t) = "";  my($t) = "";
18    
19  foreach $c (split(//, @_[0]))  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    if (ord $c >= 32 && ord $c < 127) { $t .= $c; }    foreach $c (split(//, $_[0]))
33      else { $t .= sprintf("\\x%02x", ord $c); }      {
34        if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
35          else { $t .= sprintf("\\x%02x", ord $c); }
36        }
37    }    }
38    
39  $t;  $t;
40  }  }
41    
# Line 39  if (@ARGV > 1) Line 61  if (@ARGV > 1)
61    }    }
62  else { $outfile = "STDOUT"; }  else { $outfile = "STDOUT"; }
63    
64  printf($outfile "Perl Regular Expressions\n\n");  printf($outfile "Perl $] Regular Expressions\n\n");
65    
66  # Main loop  # Main loop
67    
# Line 53  for (;;) Line 75  for (;;)
75    
76    $pattern = $_;    $pattern = $_;
77    
   $delimiter = substr($_, 0, 1);  
78    while ($pattern !~ /^\s*(.).*\1/s)    while ($pattern !~ /^\s*(.).*\1/s)
79      {      {
80      printf "    > " if $infile eq "STDIN";      printf "    > " if $infile eq "STDIN";
# Line 65  for (;;) Line 86  for (;;)
86     chomp($pattern);     chomp($pattern);
87     $pattern =~ s/\s+$//;     $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    # Check that the pattern is valid
99    
100    eval "\$_ =~ ${pattern}";    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 ($@)    if ($@)
111      {      {
112      printf $outfile "Error: $@\n";      printf $outfile "Error: $@";
113      next NEXT_RE;      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    # Read data lines and test them
131    
132    for (;;)    for (;;)
# Line 88  for (;;) Line 141  for (;;)
141    
142      last if ($_ eq "");      last if ($_ eq "");
143    
144      $_ = eval "\"$_\"";   # To get escapes processed      $x = eval "\"$_\"";   # To get escapes processed
145    
146        # Empty array for holding results, then do the matching.
147    
148      $ok = 0;      @subs = ();
149      eval "if (\$_ =~ ${pattern}) {" .  
150           "\$z = \$&;" .      $pushes = "push \@subs,\$&;" .
151           "\$a = \$1;" .           "push \@subs,\$1;" .
152           "\$b = \$2;" .           "push \@subs,\$2;" .
153           "\$c = \$3;" .           "push \@subs,\$3;" .
154           "\$d = \$4;" .           "push \@subs,\$4;" .
155           "\$e = \$5;" .           "push \@subs,\$5;" .
156           "\$f = \$6;" .           "push \@subs,\$6;" .
157           "\$g = \$7;" .           "push \@subs,\$7;" .
158           "\$h = \$8;" .           "push \@subs,\$8;" .
159           "\$i = \$9;" .           "push \@subs,\$9;" .
160           "\$j = \$10;" .           "push \@subs,\$10;" .
161           "\$k = \$11;" .           "push \@subs,\$11;" .
162           "\$l = \$12;" .           "push \@subs,\$12;" .
163           "\$m = \$13;" .           "push \@subs,\$13;" .
164           "\$n = \$14;" .           "push \@subs,\$14;" .
165           "\$o = \$15;" .           "push \@subs,\$15;" .
166           "\$p = \$16;" .           "push \@subs,\$16;" .
167           "\$ok = 1; }";           "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 ($@)      if ($@)
180        {        {
181        printf $outfile "Error: $@\n";        printf $outfile "Error: $@\n";
182        next NEXT_RE;        next NEXT_RE;
183        }        }
184      elsif (!$ok)      elsif (scalar(@subs) == 0)
185        {        {
186        printf $outfile "No match\n";        printf $outfile "No match\n";
187        }        }
188      else      else
189        {        {
190        @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++)  
191          {          {
192          if ($i == 0 || defined $subs[$i])          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            while ($last_printed++ < $i-1)            if (defined $subs[$i])
198              { printf $outfile ("%2d: <unset>\n", $last_printed); }              {
199            printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));              while ($last_printed++ < $i-1)
200            $last_printed = $i;                { 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";  # printf $outfile "\n";
212    
213  # End  # End

Legend:
Removed from v.3  
changed lines
  Added in v.87

  ViewVC Help
Powered by ViewVC 1.1.5