=head2 C<convert $wc [ , $type ]>
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>.
+It successively escapes all unprotected regexp special characters that doesn't hold any meaning for wildcards, then replace C<'jokers'>, 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>.
=cut
_check_self $self;
my $config = (defined $type) ? $self->_type($type) : $self;
return unless defined $wc;
+
+ my $e = $config->{escape};
+ # Escape :
+ # - an even number of \ that doesn't protect a regexp/wildcard metachar
+ # - an odd number of \ that doesn't protect a wildcard metachar
+ $wc =~ s/
+ (?<!\\)(
+ (?:\\\\)*
+ (?:
+ [^\w\s\\$e]
+ |
+ \\
+ (?: [^\W$e] | \s | $ )
+ )
+ )
+ /\\$1/gx;
+
my $do = $config->{do};
- my $e = $config->{escape};
- $wc =~ s/(?<!\\)((?:\\\\)*[^\w\s\\$e])/\\$1/g;
- if ($do->{jokers}) {
- $wc = $self->_jokers($wc);
- } elsif ($do->{sql}) {
- $wc = $self->_sql($wc);
- }
+ $wc = $self->_jokers($wc) if $do->{jokers};
+ $wc = $self->_sql($wc) if $do->{sql};
if ($do->{brackets}) {
$wc = $self->_bracketed($wc);
- } elsif ($do->{commas}) {
- if ($wc =~ /(?<!\\)(?:\\\\)*,/) { # win32 allows comma-separated lists
- $wc = $self->{'c_brackets'} . $self->_commas($wc) . ')';
- }
+ } elsif ($do->{commas} and $wc =~ /(?<!\\)(?:\\\\)*,/) {
+ $wc = $self->{'c_brackets'} . $self->_commas($wc) . ')';
}
+
return $wc;
}
sub _jokers {
my $self = shift;
local $_ = $_[0];
- # 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 = $self->{c_single};
s/(?<!\\)((?:\\\\)*)\?/$1$s/g;
sub _sql {
my $self = shift;
local $_ = $_[0];
- # 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 = $self->{c_single};
s/(?<!\\)((?:\\\\)*)_/$1$s/g;