]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blobdiff - lib/Regexp/Wildcards.pm
Importing Regexp-Wildcards-0.03.tar.gz
[perl/modules/Regexp-Wildcards.git] / lib / Regexp / Wildcards.pm
index c7b49a6196d50610938309fe98eae260d59b4be6..a077628649fcd51bade94a1ed3112fc1ce2e58dc 100644 (file)
@@ -7,15 +7,15 @@ use Text::Balanced qw/extract_bracketed/;
 
 =head1 NAME
 
-Regexp::Wildcards - Converts wildcards expressions to Perl regular expressions.
+Regexp::Wildcards - Converts wildcard expressions to Perl regular expressions.
 
 =head1 VERSION
 
-Version 0.01
+Version 0.03
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.03';
 
 =head1 SYNOPSIS
 
@@ -28,43 +28,24 @@ our $VERSION = '0.01';
 
 =head1 DESCRIPTION
 
-In many situations, users may want to specify patterns to match but don't need the full power of regexps. Wildcards make one of those sets of simplified rules. This module converts wildcards expressions to Perl regular expressions, so that you can use them for matching. It handles the C<*> and C<?> jokers, as well as Unix bracketed alternatives C<{,}>, and uses the backspace (C<\>) as an escape character. Wrappers are provided to mimic the behaviour of Windows and Unix shells.
-
-=head1 EXPORT
-
-Four functions are exported only on request : C<wc2re>, C<wc2re_unix>, C<wc2re_win32> and C<wc2re_jokers>.
-
-=cut
-
-use base qw/Exporter/;
-
-my %types = (
- 'jokers' => \&wc2re_jokers,
- 'unix'   => \&wc2re_unix,
- 'win32'  => \&wc2re_win32
-);
-
-our @EXPORT      = ();
-our @EXPORT_OK   = ('wc2re', map { 'wc2re_' . $_ } keys %types);
-our @EXPORT_FAIL = qw/extract do_jokers do_commas do_brackets do_bracketed/; 
-our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
+In many situations, users may want to specify patterns to match but don't need the full power of regexps. Wildcards make one of those sets of simplified rules. This module converts wildcard expressions to Perl regular expressions, so that you can use them for matching. It handles the C<*> and C<?> jokers, as well as Unix bracketed alternatives C<{,}>, and uses the backspace (C<\>) as an escape character. Wrappers are provided to mimic the behaviour of Windows and Unix shells.
 
 =head1 FUNCTIONS
 
 =head2 C<wc2re_unix>
 
-This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression (or C<undef> if the source is invalid) according to standard Unix wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns jokers into their regexp equivalents, and changes bracketed blocks into C<(?:|)> alternations. If brackets are unbalanced, it will try to substitute as many of them as possible, and then escape the remaining C<{> and C<}>.
+This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression according to standard Unix wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns jokers into their regexp equivalents, and changes bracketed blocks into C<(?:|)> alternations. If brackets are unbalanced, it will try to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block will also be escaped.
 
-Unbalanced bracket expressions can always be rescued, but it may change completely its meaning. As a side effect, commas that first appear to be between brackets can be taken at the uppermost level, which invalidates the pattern. For example :
+    # This is a valid bracket expression, and is completely translated.
+    print 'ok' if wc2re_unix('{a{b,c}d,e}') eq '(?:a(?:b|c)d|e)';
 
-    # The last orphaned } gets escaped, and the first comma is replaced.
-    # We also need to escape the comma because unix doesn't allow them out
-    # of brackets.
-    print 'ok' if wc2re_unix('{a\\{b,c}d\\,e}') eq '(?:a\\{b|c)d\\,e\\}';
+The function handles unbalanced bracket expressions, by escaping everything it can't recognize. For example :
 
-    # All of the unprotected brackets are escaped, which means that we must
-    # escape all the commas.
-    print 'ok' if wc2re_unix('{a{b\\,c\\}d\\,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
+    # The first comma is replaced, and the remaining brackets and comma are escaped.
+    print 'ok' if wc2re_unix('{a\\{b,c}d,e}') eq '(?:a\\{b|c)d\\,e\\}';
+
+    # All the brackets and commas are escaped.
+    print 'ok' if wc2re_unix('{a{b,c\\}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
 
 =cut
 
@@ -77,7 +58,10 @@ sub wc2re_unix {
 
 =head2 C<wc2re_win32>
 
-Similar to the precedent, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets will be escaped), but you can still provide a comma-separated list of items.
+Similar to the precedent, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets will be escaped), but you can provide a comma-separated list of items.
+
+    # All the brackets are escaped, and commas are seen as list delimiters.
+    print 'ok' if wc2re_win32('{a{b,c}d,e}') eq '(?:\\{a\\{b|c\\}d|e\\})';
 
 =cut
 
@@ -94,7 +78,10 @@ sub wc2re_win32 {
 
 =head2 C<wc2re_jokers>
 
-This one only handles the C<?> and C<*> jokers. All other unquoted regexp metacharacters will be quoted.
+This one only handles the C<?> and C<*> jokers. All other unquoted regexp metacharacters will be escaped.
+
+    # Everything is escaped.
+    print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
 
 =cut
 
@@ -110,6 +97,12 @@ A generic function that wraps around all the different rules. The first argument
 
 =cut
 
+my %types = (
+ 'jokers' => \&wc2re_jokers,
+ 'unix'   => \&wc2re_unix,
+ 'win32'  => \&wc2re_win32
+);
+
 sub wc2re {
  my ($wc, $type) = @_;
  return unless defined $wc;
@@ -117,6 +110,19 @@ sub wc2re {
  return $types{lc $type}($wc);
 }
 
+=head1 EXPORT
+
+These four functions are exported only on request : C<wc2re>, C<wc2re_unix>, C<wc2re_win32> and C<wc2re_jokers>.
+
+=cut
+
+use base qw/Exporter/;
+
+our @EXPORT      = ();
+our @EXPORT_OK   = ('wc2re', map { 'wc2re_' . $_ } keys %types);
+our @EXPORT_FAIL = qw/extract do_jokers do_commas do_brackets do_bracketed/; 
+our %EXPORT_TAGS = ( all => [ @EXPORT_OK ] );
+
 =head1 DEPENDENCIES
 
 L<Text::Balanced>, which is bundled with perl since version 5.7.3
@@ -127,7 +133,7 @@ Some modules provide incomplete alternatives as helper functions :
 
 L<Net::FTPServer> has a method for that. Only jokers are translated, and escaping won't preserve them.
 
-L<File::Find::Match::Util> has a C<wildcar> function that compiles a matcher. Only handles C<*>.
+L<File::Find::Match::Util> has a C<wildcard> function that compiles a matcher. It only handles C<*>.
 
 L<Text::Buffer> has the C<convertWildcardToRegex> class method that handles jokers.
 
@@ -194,12 +200,10 @@ sub do_bracketed {
  my $rest = shift;
  my ($re, $bracket, $prefix) = ('');
  while (($bracket, $rest, $prefix) = extract $rest and $bracket) {
-  return undef if $prefix =~ /(?<!\\)((?:\\\\)*),/;
   $re .= $prefix . do_brackets($bracket);
  }
- return undef if $rest =~ /(?<!\\)((?:\\\\)*),/;
  $re .= $rest;
- $re =~ s/(?<!\\)((?:\\\\)*[\{\}])/\\$1/g;
+ $re =~ s/(?<!\\)((?:\\\\)*[\{\},])/\\$1/g;
  return $re;
 }