]> git.vpit.fr Git - perl/modules/Regexp-Wildcards.git/blob - lib/Regexp/Wildcards.pm
72fa0c385ee5a39cc7d3f1324377355ecf8ad45b
[perl/modules/Regexp-Wildcards.git] / lib / Regexp / Wildcards.pm
1 package Regexp::Wildcards;
2
3 use strict;
4 use warnings;
5
6 use Carp qw/croak/;
7 use Text::Balanced qw/extract_bracketed/;
8
9 =head1 NAME
10
11 Regexp::Wildcards - Converts wildcard expressions to Perl regular expressions.
12
13 =head1 VERSION
14
15 Version 0.08
16
17 =cut
18
19 our $VERSION = '0.08';
20
21 =head1 SYNOPSIS
22
23     use Regexp::Wildcards;
24
25     my $rw = Regexp::Wildcards->new(type => 'unix');
26
27     my $re;
28     $re = $rw->convert('a{b?,c}*');          # Do it Unix shell style.
29     $re = $rw->convert('a?,b*',   'win32');  # Do it Windows shell style.
30     $re = $rw->convert('*{x,y}?', 'jokers'); # Process the jokers and escape the rest.
31     $re = $rw->convert('%a_c%',   'sql');   # Turn SQL wildcards into regexps.
32
33     $rw = Regexp::Wildcards->new(
34      do      => [ qw/jokers brackets/ ], # Do jokers and brackets.
35      capture => [ qw/any greedy/ ],      # Capture *'s greedily.
36     );
37
38     $rw->do(add => 'groups');            # Don't escape groups.
39     $rw->capture(rem => [ qw/greedy/ ]); # Actually we want non-greedy matches.
40     $re = $rw->convert('*a{,(b)?}?c*');  # '(.*?)a(?:|(b).).c(.*?)'
41     $rw->capture();                      # No more captures.
42
43 =head1 DESCRIPTION
44
45 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.
46
47 It handles the C<*> and C<?> jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards. It can also keep original C<(...)> groups. Backspace (C<\>) is used as an escape character.
48
49 Typesets that mimic the behaviour of Windows and Unix shells are also provided.
50
51 =head1 METHODS
52
53 =cut
54
55 sub _check_self {
56  croak 'First argument isn\'t a valid ' . __PACKAGE__ . ' object'
57   unless ref $_[0] and $_[0]->isa(__PACKAGE__);
58 }
59
60 my %types = (
61  jokers   => [ qw/jokers/ ],
62  sql      => [ qw/sql/ ],
63  commas   => [ qw/commas/ ],
64  brackets => [ qw/brackets/ ],
65  unix     => [ qw/jokers brackets/ ],
66  win32    => [ qw/jokers commas/ ],
67 );
68 $types{$_} = $types{win32} for qw/dos os2 MSWin32 cygwin/;
69
70 my %escapes = (
71  jokers   => '?*',
72  sql      => '_%',
73  commas   => ',',
74  brackets => '{},',
75  groups   => '()',
76 );
77
78 my %captures = (
79  single   => sub { $_[1] ? '(.)' : '.' },
80  any      => sub { $_[1] ? ($_[0]->{greedy} ? '(.*)'
81                                             : '(.*?)')
82                          : '.*' },
83  brackets => sub { $_[1] ? '(' : '(?:'; },
84  greedy   => undef
85 );
86
87 sub _validate {
88  my $self  = shift;
89  _check_self $self;
90  my $valid = shift;
91  my $old   = shift;
92  $old = { } unless defined $old;
93  my $c;
94  if (@_ <= 1) {
95   $c = { set => $_[0] };
96  } elsif (@_ % 2) {
97   croak 'Arguments must be passed as an unique scalar or as key => value pairs';
98  } else {
99   my %args = @_;
100   $c = { map { (exists $args{$_}) ? ($_ => $args{$_}) : () } qw/set add rem/ };
101  }
102  for (qw/set add rem/) {
103   my $v = $c->{$_};
104   next unless defined $v;
105   my $cb = {
106    ''      => sub { +{ ($_[0] => 1) x (exists $valid->{$_[0]}) } },
107    'ARRAY' => sub { +{ map { ($_ => 1) x (exists $valid->{$_}) } @{$_[0]} } },
108    'HASH'  => sub { +{ map { ($_ => $_[0]->{$_}) x (exists $valid->{$_}) }
109                         keys %{$_[0]} } }
110   }->{ ref $v };
111   croak 'Wrong option set' unless $cb;
112   $c->{$_} = $cb->($v);
113  }
114  my $config = (exists $c->{set}) ? $c->{set} : $old;
115  $config->{$_} = $c->{add}->{$_} for grep $c->{add}->{$_},
116                                                 keys %{$c->{add} || {}};
117  delete $config->{$_} for grep $c->{rem}->{$_}, keys %{$c->{rem} || {}};
118  $config;
119 }
120
121 sub _do {
122  my $self = shift;
123  my $config;
124  $config->{do} = $self->_validate(\%escapes, $self->{do}, @_);
125  $config->{escape} = '';
126  $config->{escape} .= $escapes{$_} for keys %{$config->{do}};
127  $config->{escape} = quotemeta $config->{escape};
128  $config;
129 }
130
131 sub do {
132  my $self = shift;
133  _check_self $self;
134  my $config = $self->_do(@_);
135  $self->{$_} = $config->{$_} for keys %$config;
136  $self;
137 }
138
139 sub _capture {
140  my $self = shift;
141  my $config;
142  $config->{capture} = $self->_validate(\%captures, $self->{capture}, @_);
143  $config->{greedy}  = delete $config->{capture}->{greedy};
144  for (keys %captures) {
145   $config->{'c_' . $_} = $captures{$_}->($config, $config->{capture}->{$_})
146                                                if $captures{$_}; # Skip 'greedy'
147  }
148  $config;
149 }
150
151 sub capture {
152  my $self = shift;
153  _check_self $self;
154  my $config = $self->_capture(@_);
155  $self->{$_} = $config->{$_} for keys %$config;
156  $self;
157 }
158
159 sub _type {
160  my ($self, $type) = @_;
161  $type = 'unix'      unless defined $type;
162  croak 'Wrong type'  unless exists $types{$type};
163  my $config = $self->_do($types{$type});
164  $config->{type} = $type;
165  $config;
166 }
167
168 sub type {
169  my $self = shift;
170  _check_self $self;
171  my $config = $self->_type(@_);
172  $self->{$_} = $config->{$_} for keys %$config;
173  $self;
174 }
175
176 sub new {
177  my $class = shift;
178  $class = ref($class) || $class || __PACKAGE__;
179  croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
180  my %args = @_;
181  my $self = { };
182  bless $self, $class;
183  if (defined $args{do}) {
184   $self->do($args{do});
185  } else {
186   $self->type($args{type});
187  }
188  $self->capture($args{capture});
189 }
190
191 =head2 C<< new [ do => $what E<verbar> type => $type ], capture => $captures >>
192
193 Constructs a new L<Regexp::Wildcard> object.
194
195 C<do> lists all features that should be enabled when converting wildcards to regexps. Refer to L</do> for details on what can be passed in C<$what>.
196
197 The C<type> specifies a predefined set of C<do> features to use. See L</type> for details on which types are valid. The C<do> option overrides C<type>.
198
199 C<capture> lists which atoms should be capturing. Refer to L</capture> for more details.
200
201 =head2 C<< do [ $what E<verbar> set => $c1, add => $c2, rem => $c3 ] >>
202
203 Specifies the list of metacharacters to convert.
204 They are classified into five classes :
205
206 =over 4
207
208 =item C<'jokers'> converts C<?> to C<.> and C<*> to C<.*> ;
209
210     'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c'
211
212 =item C<'sql'> converts C<_> to C<.> and C<%> to C<.*> ;
213
214     'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c'
215
216 =item C<'commas'> converts all C<,> to C<|> and puts the complete resulting regular expression inside C<(?: ... )> ;
217
218     'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)'
219
220 =item C<'brackets'> converts all matching C<{ ... ,  ... }> brackets to C<(?: ... | ... )> alternations. If some brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining unmatched C<{> and C<}>. Commas outside of any bracket-delimited block are also escaped ;
221
222     'a,b{c,d},e'    ==> 'a\\,b(?:c|d)\\,e'
223     '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}'
224     '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}'
225
226 =item C<'groups'> keeps the parenthesis C<( ... )> of the original string without escaping them. Currently, no check is done to ensure that the parenthesis are matching.
227
228     'a(b(c))d\\(\\)' ==> (no change)
229
230 =back
231
232 Each C<$c> can be any of :
233
234 =over 4
235
236 =item A hash reference, with wanted metacharacter group names (described above) as keys and booleans as values ;
237
238 =item An array reference containing the list of wanted metacharacter classes ;
239
240 =item A plain scalar, when only one group is required.
241
242 =back
243
244 When C<set> is present, the classes given as its value replace the current object options. Then the C<add> classes are added, and the C<rem> classes removed.
245
246 Passing a sole scalar C<$what> is equivalent as passing C<< set => $what >>.
247 No argument means C<< set => [ ] >>.
248
249     $rw->do(set => 'jokers');           # Only translate jokers.
250     $rw->do('jokers');                  # Same.
251     $rw->do(add => [ qw/sql commas/ ]); # Translate also SQL and commas.
252     $rw->do(rem => 'jokers');           # Specifying both 'sql' and 'jokers' is useless.
253     $rw->do();                          # Translate nothing.
254
255 =head2 C<type $type>
256
257 Notifies to convert the metacharacters that corresponds to the predefined type C<$type>. C<$type> can be any of C<'jokers'>, C<'sql'>, C<'commas'>, C<'brackets'>, C<'win32'> or C<'unix'>. An unknown or undefined value defaults to C<'unix'>, except for C<'dos'>, C<'os2'>, C<'MSWin32'> and C<'cygwin'> that default to C<'win32'>. This means that you can pass C<$^O> as the C<$type> and get the corresponding shell behaviour. Returns the object.
258
259     $rw->type('win32'); # Set type to win32.
260     $rw->type();        # Set type to unix.
261
262 =head2 C<< capture [ $captures E<verbar> set => $c1, add => $c2, rem => $c3 ] >>
263
264 Specifies the list of atoms to capture.
265 This method works like L</do>, except that the classes are different :
266
267 =over 4
268
269 =item C<'single'> will capture all unescaped I<"exactly one"> metacharacters, i.e. C<?> for wildcards or C<_> for SQL ;
270
271     'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)'
272     'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)'
273
274 =item C<'any'> will capture all unescaped I<"any"> metacharacters, i.e. C<*> for wildcards or C<%> for SQL ;
275
276     'a***b\\**' ==> 'a(.*)b\\*(.*)'
277     'a%%%b\\%%' ==> 'a(.*)b\\%(.*)'
278
279 =item C<'greedy'>, when used in conjunction with C<'any'>, will make the C<'any'> captures greedy (by default they are not) ;
280
281     'a***b\\**' ==> 'a(.*?)b\\*(.*?)'
282     'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)'
283
284 =item C<'brackets'> will capture matching C<{ ... , ... }> alternations.
285
286     'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)'
287
288 =back
289
290     $rw->capture(set => 'single');           # Only capture "exactly one" metacharacters.
291     $rw->capture('single');                  # Same.
292     $rw->capture(add => [ qw/any greedy/ ]); # Also greedily capture "any" metacharacters.
293     $rw->capture(rem => 'greedy');           # No more greed please.
294     $rw->capture();                          # Capture nothing.
295
296 =head2 C<convert $wc [ , $type ]>
297
298 Converts the wildcard expression C<$wc> into a regular expression according to the options stored into the L<Regexp::Wildcards> object, or to C<$type> if it's supplied. It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, then replace C<'jokers'> or C<'sql'> and C<'commas'> or C<'brackets'> (depending on the L</do> or L</type> options), all of this by applying the C<'capture'> rules specified in the constructor or by L</capture>.
299
300 =cut
301
302 sub convert {
303  my ($self, $wc, $type) = @_;
304  _check_self $self;
305  my $config;
306  if (defined $type) {
307   $config = $self->_type($type);
308  } else {
309   $config = $self;
310  }
311  return unless defined $wc;
312  my $do = $config->{do};
313  my $e  = $config->{escape};
314  $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s\\$e])/\\$1/g;
315  return $self->_sql($wc)   if $do->{sql};
316  $wc = $self->_jokers($wc) if $do->{jokers};
317  if ($do->{brackets}) {
318   $wc = $self->_bracketed($wc);
319  } elsif ($do->{commas}) {
320   if ($wc =~ /(?<!\\)(?:\\\\)*,/) { # win32 allows comma-separated lists
321    $wc = $self->{'c_brackets'} . $self->_commas($wc) . ')';
322   }
323  }
324  return $wc;
325 }
326
327 =head1 EXPORT
328
329 An object module shouldn't export any function, and so does this one.
330
331 =head1 DEPENDENCIES
332
333 L<Text::Balanced>, which is bundled with perl since version 5.7.3
334
335 =head1 CAVEATS
336
337 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.
338
339 =head1 AUTHOR
340
341 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
342
343 You can contact me by mail or on #perl @ FreeNode (vincent or Prof_Vince).
344
345 =head1 BUGS
346
347 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.
348
349 =head1 SUPPORT
350
351 You can find documentation for this module with the perldoc command.
352
353     perldoc Regexp::Wildcards
354
355 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Regexp-Wildcards>.
356
357 =head1 COPYRIGHT & LICENSE
358
359 Copyright 2007-2008 Vincent Pit, all rights reserved.
360
361 This program is free software; you can redistribute it and/or modify it
362 under the same terms as Perl itself.
363
364 =cut
365
366 sub _extract ($) { extract_bracketed $_[0], '{',  qr/.*?(?<!\\)(?:\\\\)*(?={)/ }
367
368 sub _jokers {
369  my $self = shift;
370  local $_ = $_[0];
371  # escape an odd number of \ that doesn't protect a regexp/wildcard special char
372  s/(?<!\\)((?:\\\\)*\\(?:[\w\s]|$))/\\$1/g;
373  # substitute ? preceded by an even number of \
374  my $s = $self->{c_single};
375  s/(?<!\\)((?:\\\\)*)\?/$1$s/g;
376  # substitute * preceded by an even number of \
377  $s = $self->{c_any};
378  s/(?<!\\)((?:\\\\)*)\*+/$1$s/g;
379  return $_;
380 }
381
382 sub _sql {
383  my $self = shift;
384  local $_ = $_[0];
385  # escape an odd number of \ that doesn't protect a regexp/wildcard special char
386  s/(?<!\\)((?:\\\\)*\\(?:[^\W_]|\s|$))/\\$1/g;
387  # substitute _ preceded by an even number of \
388  my $s = $self->{c_single};
389  s/(?<!\\)((?:\\\\)*)_/$1$s/g;
390  # substitute % preceded by an even number of \
391  $s = $self->{c_any};
392  s/(?<!\\)((?:\\\\)*)%+/$1$s/g;
393  return $_;
394 }
395
396 sub _commas {
397  local $_ = $_[1];
398  # substitute , preceded by an even number of \
399  s/(?<!\\)((?:\\\\)*),/$1|/g;
400  return $_;
401 }
402
403 sub _brackets {
404  my ($self, $rest) = @_;
405  substr $rest, 0, 1, '';
406  chop $rest;
407  my ($re, $bracket, $prefix) = ('');
408  while (do { ($bracket, $rest, $prefix) = _extract $rest; $bracket }) {
409   $re .= $self->_commas($prefix) . $self->_brackets($bracket);
410  }
411  $re .= $self->_commas($rest);
412  return $self->{c_brackets} . $re . ')';
413 }
414
415 sub _bracketed {
416  my ($self, $rest) = @_;
417  my ($re, $bracket, $prefix) = ('');
418  while (do { ($bracket, $rest, $prefix) = _extract $rest; $bracket }) {
419   $re .= $prefix . $self->_brackets($bracket);
420  }
421  $re .= $rest;
422  $re =~ s/(?<!\\)((?:\\\\)*[\{\},])/\\$1/g;
423  return $re;
424 }
425
426 1; # End of Regexp::Wildcards