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

Diff of /code/trunk/perltest.pl

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

code/trunk/perltest revision 3 by nigel, Sat Feb 24 21:38:01 2007 UTC code/trunk/perltest.pl revision 518 by ph10, Tue May 18 15:47:01 2010 UTC
# Line 1  Line 1 
1  #! /usr/bin/perl  #! /usr/bin/env 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. However, it needs to
6    # have "use utf8" at the start for running the UTF-8 tests, but *not* for the
7    # other tests. The only way I've found for doing this is to cat this line in
8    # explicitly in the RunPerlTest script.
9    
10    # use locale;  # With this included, \x0b matches \s!
11    
12  # Function for turning a string into a string of printing chars  # Function for turning a string into a string of printing chars. There are
13    # currently problems with UTF-8 strings; this fudges round them.
14    
15  sub pchars {  sub pchars {
16  my($t) = "";  my($t) = "";
17    
18  foreach $c (split(//, @_[0]))  if ($utf8)
19    {    {
20    if (ord $c >= 32 && ord $c < 127) { $t .= $c; }    @p = unpack('U*', $_[0]);
21      else { $t .= sprintf("\\x%02x", ord $c); }    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    
40    
   
41  # Read lines from named file or stdin and write to named file or stdout; lines  # Read lines from named file or stdin and write to named file or stdout; lines
42  # consist of a regular expression, in delimiters and optionally followed by  # consist of a regular expression, in delimiters and optionally followed by
43  # options, followed by a set of test data, terminated by an empty line.  # options, followed by a set of test data, terminated by an empty line.
# Line 39  if (@ARGV > 1) Line 58  if (@ARGV > 1)
58    }    }
59  else { $outfile = "STDOUT"; }  else { $outfile = "STDOUT"; }
60    
61  printf($outfile "Perl Regular Expressions\n\n");  printf($outfile "Perl $] Regular Expressions\n\n");
62    
63  # Main loop  # Main loop
64    
# Line 53  for (;;) Line 72  for (;;)
72    
73    $pattern = $_;    $pattern = $_;
74    
   $delimiter = substr($_, 0, 1);  
75    while ($pattern !~ /^\s*(.).*\1/s)    while ($pattern !~ /^\s*(.).*\1/s)
76      {      {
77      printf "    > " if $infile eq "STDIN";      printf "    > " if $infile eq "STDIN";
# Line 65  for (;;) Line 83  for (;;)
83     chomp($pattern);     chomp($pattern);
84     $pattern =~ s/\s+$//;     $pattern =~ s/\s+$//;
85    
86      # The private /+ modifier means "print $' afterwards".
87    
88      $showrest = ($pattern =~ s/\+(?=[a-zA-Z]*$)//);
89    
90      # Remove /8 from a UTF-8 pattern.
91    
92      $utf8 = $pattern =~ s/8(?=[a-zA-Z]*$)//;
93    
94      # Remove /J from a pattern with duplicate names.
95    
96      $pattern =~ s/J(?=[a-zA-Z]*$)//;
97    
98      # Remove /K from a pattern (asks pcretest to check MARK data) */
99    
100      $pattern =~ s/K(?=[a-zA-Z]*$)//;
101    
102      # Remove /W from a pattern (asks pcretest to set PCRE_UCP)
103    
104      $pattern =~ s/W(?=[a-zA-Z]*$)//;
105    
106    # Check that the pattern is valid    # Check that the pattern is valid
107    
108    eval "\$_ =~ ${pattern}";    eval "\$_ =~ ${pattern}";
109    if ($@)    if ($@)
110      {      {
111      printf $outfile "Error: $@\n";      printf $outfile "Error: $@";
112      next NEXT_RE;      next NEXT_RE;
113      }      }
114    
115      # If the /g modifier is present, we want to put a loop round the matching;
116      # otherwise just a single "if".
117    
118      $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
119    
120      # If the pattern is actually the null string, Perl uses the most recently
121      # executed (and successfully compiled) regex is used instead. This is a
122      # nasty trap for the unwary! The PCRE test suite does contain null strings
123      # in places - if they are allowed through here all sorts of weird and
124      # unexpected effects happen. To avoid this, we replace such patterns with
125      # a non-null pattern that has the same effect.
126    
127      $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
128    
129    # Read data lines and test them    # Read data lines and test them
130    
131    for (;;)    for (;;)
# Line 83  for (;;) Line 135  for (;;)
135      chomp;      chomp;
136      printf $outfile "$_\n" if $infile ne "STDIN";      printf $outfile "$_\n" if $infile ne "STDIN";
137    
138      s/\s+$//;      s/\s+$//;  # Remove trailing space
139      s/^\s+//;      s/^\s+//;  # Remove leading space
140        s/\\Y//g;  # Remove \Y (pcretest flag to set PCRE_NO_START_OPTIMIZE)
141    
142      last if ($_ eq "");      last if ($_ eq "");
143        $x = eval "\"$_\"";   # To get escapes processed
144    
145        # Empty array for holding results, ensure $REGERROR and $REGMARK are
146        # unset, 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      $_ = eval "\"$_\"";   # To get escapes processed      undef $REGERROR;
170        undef $REGMARK;
171    
172      $ok = 0;      eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
     eval "if (\$_ =~ ${pattern}) {" .  
          "\$z = \$&;" .  
          "\$a = \$1;" .  
          "\$b = \$2;" .  
          "\$c = \$3;" .  
          "\$d = \$4;" .  
          "\$e = \$5;" .  
          "\$f = \$6;" .  
          "\$g = \$7;" .  
          "\$h = \$8;" .  
          "\$i = \$9;" .  
          "\$j = \$10;" .  
          "\$k = \$11;" .  
          "\$l = \$12;" .  
          "\$m = \$13;" .  
          "\$n = \$14;" .  
          "\$o = \$15;" .  
          "\$p = \$16;" .  
          "\$ok = 1; }";  
173    
174      if ($@)      if ($@)
175        {        {
176        printf $outfile "Error: $@\n";        printf $outfile "Error: $@\n";
177        next NEXT_RE;        next NEXT_RE;
178        }        }
179      elsif (!$ok)      elsif (scalar(@subs) == 0)
180        {        {
181        printf $outfile "No match\n";        printf $outfile "No match";
182          if (defined $REGERROR && $REGERROR != 1)
183            { print $outfile (", mark = $REGERROR"); }
184          printf $outfile "\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          if (defined $REGMARK && $REGMARK != 1)
206            { print $outfile ("MK: $REGMARK\n"); }
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.518

  ViewVC Help
Powered by ViewVC 1.1.5