]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blobdiff - lib/Regexp/Wildcards.pm
Importing Regexp-Wildcards-0.08.tar.gz
[perl/modules/Regexp-Wildcards.git] / lib / Regexp / Wildcards.pm
index c7b49a6196d50610938309fe98eae260d59b4be6..329e77a1ffcad15b1ae6dec9ec7838bf1138a14a 100644 (file)
@@ -7,64 +7,152 @@ 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.08
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.08';
 
 =head1 SYNOPSIS
 
     use Regexp::Wildcards qw/wc2re/;
 
     my $re;
-    $re = wc2re 'a{b.,c}*' => 'unix';   # Do it Unix style.
-    $re = wc2re 'a.,b*'    => 'win32';  # Do it Windows style.
-    $re = wc2re '*{x,y}.'  => 'jokers'; # Process the jokers & escape the rest.
+    $re = wc2re 'a{b?,c}*' => 'unix';   # Do it Unix style.
+    $re = wc2re 'a?,b*'    => 'win32';  # Do it Windows style.
+    $re = wc2re '*{x,y}?'  => 'jokers'; # Process the jokers & escape the rest.
+    $re = wc2re '%a_c%'    => 'sql';    # Turn SQL wildcards into regexps.
 
 =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.
+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<?> shell jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards. Backspace (C<\>) is used as an escape character. Wrappers are provided to mimic the behaviour of Windows and Unix shells.
 
-=head1 EXPORT
+=head1 VARIABLES
+
+These variables control if the wildcards jokers and brackets must capture their match. They can be globally set by writing in your program
+
+    $Regexp::Wildcards::CaptureSingle = 1;
+    # From then, "exactly one" wildcards are capturing
+
+or can be locally specified via C<local>
+
+    {
+     local $Regexp::Wildcards::CaptureSingle = 1;
+     # In this block, "exactly one" wildcards are capturing.
+     ...
+    }
+    # Back to the situation from before the block
+
+This section describes also how those elements are translated by the L<functions|/FUNCTIONS>.
 
-Four functions are exported only on request : C<wc2re>, C<wc2re_unix>, C<wc2re_win32> and C<wc2re_jokers>.
+=head2 C<$CaptureSingle>
+
+When this variable is true, each occurence of unescaped I<"exactly one"> wildcards (i.e. C<?> jokers or C<_> for SQL wildcards) are made capturing in the resulting regexp (they are be replaced by C<(.)>). Otherwise, they are just replaced by C<.>. Default is the latter.
+
+    For jokers :
+    'a???b\\??' is translated to 'a(.)(.)(.)b\\?(.)' if $CaptureSingle is true
+                                 'a...b\\?.'         otherwise (default)
+
+    For SQL wildcards :
+    'a___b\\__' is translated to 'a(.)(.)(.)b\\_(.)' if $CaptureSingle is true
+                                 'a...b\\_.'         otherwise (default)
 
 =cut
 
-use base qw/Exporter/;
+our $CaptureSingle = 0;
 
-my %types = (
- 'jokers' => \&wc2re_jokers,
- 'unix'   => \&wc2re_unix,
- 'win32'  => \&wc2re_win32
-);
+sub capture_single {
+ return $CaptureSingle ? '(.)'
+                       : '.';
+}
+
+=head2 C<$CaptureAny>
+
+By default this variable is false, and successions of unescaped I<"any"> wildcards (i.e. C<*> jokers or C<%> for SQL wildcards) are replaced by B<one> single C<.*>. When it evalutes to true, those sequences of I<"any"> wildcards are made into B<one> capture, which is greedy (C<(.*)>) for C<$CaptureAny E<gt> 0> and otherwise non-greedy (C<(.*?)>).
+
+    For jokers :
+    'a***b\\**' is translated to 'a.*b\\*.*'       if $CaptureAny is false (default)
+                                 'a(.*)b\\*(.*)'   if $CaptureAny > 0
+                                 'a(.*?)b\\*(.*?)' otherwise
+
+    For SQL wildcards :
+    'a%%%b\\%%' is translated to 'a.*b\\%.*'       if $CaptureAny is false (default)
+                                 'a(.*)b\\%(.*)'   if $CaptureAny > 0
+                                 'a(.*?)b\\%(.*?)' otherwise
+
+=cut
+
+our $CaptureAny = 0;
+
+sub capture_any {
+ return $CaptureAny ? (($CaptureAny > 0) ? '(.*)'
+                                         : '(.*?)')
+                    : '.*';
+}
+
+=head2 C<$CaptureBrackets>
+
+If this variable is set to true, valid brackets constructs are made into C<( | )> captures, and otherwise they are replaced by non-capturing alternations (C<(?: | >)), which is the default.
+
+    'a{b\\},\\{c}' is translated to 'a(b\\}|\\{c)'   if $CaptureBrackets is true
+                                    'a(?:b\\}|\\{c)' otherwise (default)
+
+=cut
 
-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 ] );
+our $CaptureBrackets = 0;
+
+sub capture_brackets {
+ return $CaptureBrackets ? '('
+                         : '(?:';
+}
 
 =head1 FUNCTIONS
 
+=head2 C<wc2re_jokers>
+
+This function takes as its only argument the wildcard string to process, and returns the corresponding regular expression where the jokers C<?> (I<"exactly one">) and C<*> (I<"any">) have been translated into their regexp equivalents (see L</VARIABLES> for more details). All other unprotected regexp metacharacters are escaped.
+
+    # Everything is escaped.
+    print 'ok' if wc2re_jokers('{a{b,c}d,e}') eq '\\{a\\{b\\,c\\}d\\,e\\}';
+
+=cut
+
+sub wc2re_jokers {
+ my ($wc) = @_;
+ $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\])/\\$1/g;
+ return do_jokers($wc);
+}
+
+=head2 C<wc2re_sql>
+
+Similar to the precedent, but for the SQL wildcards C<_> (I<"exactly one">) and C<%> (I<"any">). All other unprotected regexp metacharacters are escaped.
+=cut
+  
+sub wc2re_sql {
+ my ($wc) = @_;
+ $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s%\\])/\\$1/g;
+ return do_sql($wc);
+}
+
 =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 conforms to standard Unix shell wildcard rules. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, turns C<?> and C<*> jokers into their regexp equivalents (see L</wc2re_jokers>), and changes bracketed blocks into (possibly capturing) alternations as described in L</VARIABLES>. If brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped.
+
+    # 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)';
 
-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 :
+The function handles unbalanced bracket expressions, by escaping everything it can't recognize. For example :
 
-    # 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 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 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\\}';
+    # 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 +165,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.
+This one works just like the one before, but for Windows wildcards. Bracketed blocks are no longer handled (which means that brackets are 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
 
@@ -87,61 +178,98 @@ sub wc2re_win32 {
  $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\,])/\\$1/g;
  my $re = do_jokers($wc);
  if ($re =~ /(?<!\\)(?:\\\\)*,/) { # win32 allows comma-separated lists
-  $re = '(?:' . do_commas($re) . ')';
+  $re = capture_brackets . do_commas($re) . ')';
  }
  return $re;
 }
 
-=head2 C<wc2re_jokers>
+=head2 C<wc2re>
 
-This one only handles the C<?> and C<*> jokers. All other unquoted regexp metacharacters will be quoted.
+A generic function that wraps around all the different rules. The first argument is the wildcard expression, and the second one is the type of rules to apply which can be :
 
-=cut
+=over 4
 
-sub wc2re_jokers {
- my ($wc) = @_;
- $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s?*\\])/\\$1/g;
- return do_jokers($wc);
-}
+=item C<'unix'>, C<'win32'>, C<'jokers'>, C<'sql'>
 
-=head2 C<wc2re>
+For one of those raw rule names, C<wc2re> simply maps to C<wc2re_unix>, C<wc2re_win32>, C<wc2re_jokers> and C<wc2re_sql> respectively.
+
+=item C<$^O>
 
-A generic function that wraps around all the different rules. The first argument is the wildcard expression, and the second one is the type of rules to apply, currently either C<unix>, C<win32> or C<jokers>. If the type is undefined, it defaults to C<unix>.
+If you supply the Perl operating system name, the call is deferred to C<wc2re_win32> for C< $^O> equal to C<'dos'>, C<'os2'>, C<'MSWin32'> or C<'cygwin'>, and to C<wc2re_unix> in all the other cases.
+
+=back
+
+If the type is undefined or not supported, it defaults to C<'unix'>.
+
+     # Wraps to wc2re_jokers ($re eq 'a\\{b\\,c\\}.*').
+     $re = wc2re 'a{b,c}*' => 'jokers';
+
+     # Wraps to wc2re_win32 ($re eq '(?:a\\{b|c\\}.*)')
+     #       or wc2re_unix  ($re eq 'a(?:b|c).*')       depending on $^O.
+     $re = wc2re 'a{b,c}*' => $^O;
 
 =cut
 
+my %types = (
+ 'jokers'    => \&wc2re_jokers,
+ 'sql'       => \&wc2re_sql,
+ 'unix'      => \&wc2re_unix,
+ map { lc $_ => \&wc2re_win32 } qw/win32 dos os2 MSWin32 cygwin/
+);
+
 sub wc2re {
  my ($wc, $type) = @_;
  return unless defined $wc;
- $type ||= 'unix';
- return $types{lc $type}($wc);
+ $type = $type ? lc $type : 'unix';
+ $type = 'unix' unless exists $types{$type};
+ return $types{$type}($wc);
 }
 
+=head1 EXPORT
+
+These five functions are exported only on request : C<wc2re>, C<wc2re_unix>, C<wc2re_win32>, C<wc2re_jokers> and C<wc2re_sql>. The variables are not exported.
+
+=cut
+
+use base qw/Exporter/;
+
+our @EXPORT         = ();
+our %EXPORT_TAGS    = (
+ 'funcs' =>  [ 'wc2re', map { 'wc2re_'.$_ } keys %types ],
+);
+our @EXPORT_OK      = map { @$_ } values %EXPORT_TAGS;
+our @EXPORT_FAIL    = qw/extract/,
+                    (map { 'do_'.$_ } qw/jokers sql commas brackets bracketed/),
+                    (map { 'capture_'.$_ } qw/single any brackets/);
+$EXPORT_TAGS{'all'} = [ @EXPORT_OK ];
+
 =head1 DEPENDENCIES
 
 L<Text::Balanced>, which is bundled with perl since version 5.7.3
 
+=head1 CAVEATS
+
+This module does not implement the strange behaviours of Windows shell that result from the special handling of the three last characters (for the file extension). For example, Windows XP shell matches C<*a> like C<.*a>, C<*a?> like C<.*a.?>, C<*a??> like C<.*a.{0,2}> and so on.
+
 =head1 SEE ALSO
 
 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.
 
 =head1 AUTHOR
 
-Vincent Pit, C<< <perl at profvince.com> >>
+Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
+
+You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
 
 =head1 BUGS
 
-Please report any bugs or feature requests to
-C<bug-regexp-wildcards at rt.cpan.org>, or through the web interface at
-L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>.
-I will be notified, and then you'll automatically be notified of progress on
-your bug as I make changes.
+Please report any bugs or feature requests to C<bug-regexp-wildcards at rt.cpan.org>, or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Regexp-Wildcards>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
 
 =head1 SUPPORT
 
@@ -151,23 +279,38 @@ You can find documentation for this module with the perldoc command.
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2007 Vincent Pit, all rights reserved.
+Copyright 2007-2008 Vincent Pit, all rights reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the same terms as Perl itself.
 
 =cut
 
-sub extract { extract_bracketed shift, '{',  qr/.*?(?:(?<!\\)(?:\\\\)*)(?={)/; }
+sub extract { extract_bracketed shift, '{',  qr/.*?(?<!\\)(?:\\\\)*(?={)/; }
 
 sub do_jokers {
  local $_ = shift;
  # escape an odd number of \ that doesn't protect a regexp/wildcard special char
  s/(?<!\\)((?:\\\\)*\\(?:[\w\s]|$))/\\$1/g;
  # substitute ? preceded by an even number of \
- s/(?<!\\)((?:\\\\)*)\?/$1./g;
+ my $s = capture_single;
+ s/(?<!\\)((?:\\\\)*)\?/$1$s/g;
+ # substitute * preceded by an even number of \
+ $s = capture_any;
+ s/(?<!\\)((?:\\\\)*)\*+/$1$s/g;
+ return $_;
+}
+
+sub do_sql {
+ local $_ = shift;
+ # escape an odd number of \ that doesn't protect a regexp/wildcard special char
+ s/(?<!\\)((?:\\\\)*\\(?:[^\W_]|\s|$))/\\$1/g;
+ # substitute _ preceded by an even number of \
+ my $s = capture_single;
+ s/(?<!\\)((?:\\\\)*)_/$1$s/g;
  # substitute * preceded by an even number of \
- s/(?<!\\)((?:\\\\)*)\*+/$1.*/g;
+ $s = capture_any;
+ s/(?<!\\)((?:\\\\)*)%+/$1$s/g;
  return $_;
 }
 
@@ -187,19 +330,17 @@ sub do_brackets {
   $re .= do_commas($prefix) . do_brackets($bracket);
  }
  $re .= do_commas($rest);
- return '(?:' . $re . ')';
+ return capture_brackets . $re . ')';
 }
 
 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;
 }