/vendor/pcre/perltest.pl

http://github.com/feyeleanor/RubyGoLightly · Perl · 191 lines · 122 code · 39 blank · 30 comment · 37 complexity · 6d1fb4b72bcdcc48b8fd9486128fe4d2 MD5 · raw file

  1. #! /usr/bin/env perl
  2. # Program for testing regular expressions with perl to check that PCRE handles
  3. # them the same. This is the version that supports /8 for UTF-8 testing. As it
  4. # stands, it requires at least Perl 5.8 for UTF-8 support. However, it needs to
  5. # have "use utf8" at the start for running the UTF-8 tests, but *not* for the
  6. # other tests. The only way I've found for doing this is to cat this line in
  7. # explicitly in the RunPerlTest script.
  8. # use locale; # With this included, \x0b matches \s!
  9. # Function for turning a string into a string of printing chars. There are
  10. # currently problems with UTF-8 strings; this fudges round them.
  11. sub pchars {
  12. my($t) = "";
  13. if ($utf8)
  14. {
  15. @p = unpack('U*', $_[0]);
  16. foreach $c (@p)
  17. {
  18. if ($c >= 32 && $c < 127) { $t .= chr $c; }
  19. else { $t .= sprintf("\\x{%02x}", $c); }
  20. }
  21. }
  22. else
  23. {
  24. foreach $c (split(//, $_[0]))
  25. {
  26. if (ord $c >= 32 && ord $c < 127) { $t .= $c; }
  27. else { $t .= sprintf("\\x%02x", ord $c); }
  28. }
  29. }
  30. $t;
  31. }
  32. # Read lines from named file or stdin and write to named file or stdout; lines
  33. # consist of a regular expression, in delimiters and optionally followed by
  34. # options, followed by a set of test data, terminated by an empty line.
  35. # Sort out the input and output files
  36. if (@ARGV > 0)
  37. {
  38. open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
  39. $infile = "INFILE";
  40. }
  41. else { $infile = "STDIN"; }
  42. if (@ARGV > 1)
  43. {
  44. open(OUTFILE, ">$ARGV[1]") || die "Failed to open $ARGV[1]\n";
  45. $outfile = "OUTFILE";
  46. }
  47. else { $outfile = "STDOUT"; }
  48. printf($outfile "Perl $] Regular Expressions\n\n");
  49. # Main loop
  50. NEXT_RE:
  51. for (;;)
  52. {
  53. printf " re> " if $infile eq "STDIN";
  54. last if ! ($_ = <$infile>);
  55. printf $outfile "$_" if $infile ne "STDIN";
  56. next if ($_ eq "");
  57. $pattern = $_;
  58. while ($pattern !~ /^\s*(.).*\1/s)
  59. {
  60. printf " > " if $infile eq "STDIN";
  61. last if ! ($_ = <$infile>);
  62. printf $outfile "$_" if $infile ne "STDIN";
  63. $pattern .= $_;
  64. }
  65. chomp($pattern);
  66. $pattern =~ s/\s+$//;
  67. # The private /+ modifier means "print $' afterwards".
  68. $showrest = ($pattern =~ s/\+(?=[a-z]*$)//);
  69. # Remove /8 from a UTF-8 pattern.
  70. $utf8 = $pattern =~ s/8(?=[a-z]*$)//;
  71. # Check that the pattern is valid
  72. eval "\$_ =~ ${pattern}";
  73. if ($@)
  74. {
  75. printf $outfile "Error: $@";
  76. next NEXT_RE;
  77. }
  78. # If the /g modifier is present, we want to put a loop round the matching;
  79. # otherwise just a single "if".
  80. $cmd = ($pattern =~ /g[a-z]*$/)? "while" : "if";
  81. # If the pattern is actually the null string, Perl uses the most recently
  82. # executed (and successfully compiled) regex is used instead. This is a
  83. # nasty trap for the unwary! The PCRE test suite does contain null strings
  84. # in places - if they are allowed through here all sorts of weird and
  85. # unexpected effects happen. To avoid this, we replace such patterns with
  86. # a non-null pattern that has the same effect.
  87. $pattern = "/(?#)/$2" if ($pattern =~ /^(.)\1(.*)$/);
  88. # Read data lines and test them
  89. for (;;)
  90. {
  91. printf "data> " if $infile eq "STDIN";
  92. last NEXT_RE if ! ($_ = <$infile>);
  93. chomp;
  94. printf $outfile "$_\n" if $infile ne "STDIN";
  95. s/\s+$//;
  96. s/^\s+//;
  97. last if ($_ eq "");
  98. $x = eval "\"$_\""; # To get escapes processed
  99. # Empty array for holding results, then do the matching.
  100. @subs = ();
  101. $pushes = "push \@subs,\$&;" .
  102. "push \@subs,\$1;" .
  103. "push \@subs,\$2;" .
  104. "push \@subs,\$3;" .
  105. "push \@subs,\$4;" .
  106. "push \@subs,\$5;" .
  107. "push \@subs,\$6;" .
  108. "push \@subs,\$7;" .
  109. "push \@subs,\$8;" .
  110. "push \@subs,\$9;" .
  111. "push \@subs,\$10;" .
  112. "push \@subs,\$11;" .
  113. "push \@subs,\$12;" .
  114. "push \@subs,\$13;" .
  115. "push \@subs,\$14;" .
  116. "push \@subs,\$15;" .
  117. "push \@subs,\$16;" .
  118. "push \@subs,\$'; }";
  119. eval "${cmd} (\$x =~ ${pattern}) {" . $pushes;
  120. if ($@)
  121. {
  122. printf $outfile "Error: $@\n";
  123. next NEXT_RE;
  124. }
  125. elsif (scalar(@subs) == 0)
  126. {
  127. printf $outfile "No match\n";
  128. }
  129. else
  130. {
  131. while (scalar(@subs) != 0)
  132. {
  133. printf $outfile (" 0: %s\n", &pchars($subs[0]));
  134. printf $outfile (" 0+ %s\n", &pchars($subs[17])) if $showrest;
  135. $last_printed = 0;
  136. for ($i = 1; $i <= 16; $i++)
  137. {
  138. if (defined $subs[$i])
  139. {
  140. while ($last_printed++ < $i-1)
  141. { printf $outfile ("%2d: <unset>\n", $last_printed); }
  142. printf $outfile ("%2d: %s\n", $i, &pchars($subs[$i]));
  143. $last_printed = $i;
  144. }
  145. }
  146. splice(@subs, 0, 18);
  147. }
  148. }
  149. }
  150. }
  151. # printf $outfile "\n";
  152. # End