blib/lib/Regexp/Wildcards.pm

filter: Statementstmt 100% 154 / 154 Branchbran 98.0% 49 / 50 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 29 / 29 Podpod 100% 5 / 5 totaltotal 99.6% 260 / 261 CC 40 SCAR 16.1
CC
40
Cov
100%
_validate
13.0
convert
8.0
new
5.0
CRAP 5.0
↑ Summary
1 package Regexp::Wildcards;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Carp qw<croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Scalar::Util qw<blessed>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Text::Balanced qw<extract_bracketed>;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Regexp::Wildcards - Converts wildcard expressions to Perl regular expressions.
13  
14 =head1 VERSION
15  
16 Version 1.05
17  
18 =cut
19  
20 9 ▶use vars qw<$VERSION>;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 $VERSION = '1.05';
23 }
24  
25 =head1 SYNOPSIS
26  
27 use Regexp::Wildcards;
28  
29 my $rw = Regexp::Wildcards->new(type => 'unix');
30  
31 my $re;
32 $re = $rw->convert('a{b?,c}*'); # Do it Unix shell style.
33 $re = $rw->convert('a?,b*', 'win32'); # Do it Windows shell style.
34 $re = $rw->convert('*{x,y}?', 'jokers'); # Process the jokers and
35 # escape the rest.
36 $re = $rw->convert('%a_c%', 'sql'); # Turn SQL wildcards into
37 # regexps.
38  
39 $rw = Regexp::Wildcards->new(
40 do => [ qw<jokers brackets> ], # Do jokers and brackets.
41 capture => [ qw<any greedy> ], # Capture *'s greedily.
42 );
43  
44 $rw->do(add => 'groups'); # Don't escape groups.
45 $rw->capture(rem => [ qw<greedy> ]); # Actually we want non-greedy
46 # matches.
47 $re = $rw->convert('*a{,(b)?}?c*'); # '(.*?)a(?:|(b).).c(.*?)'
48 $rw->capture(); # No more captures.
49  
50 =head1 DESCRIPTION
51  
52 In many situations, users may want to specify patterns to match but don't need the full power of regexps.
53 Wildcards make one of those sets of simplified rules.
54 This module converts wildcard expressions to Perl regular expressions, so that you can use them for matching.
55  
56 It handles the C<*> and C<?> jokers, as well as Unix bracketed alternatives C<{,}>, but also C<%> and C<_> SQL wildcards.
57 If required, it can also keep original C<(...)> groups or C<^> and C<$> anchors.
58 Backspace (C<\>) is used as an escape character.
59  
60 Typesets that mimic the behaviour of Windows and Unix shells are also provided.
61  
62 =head1 METHODS
63  
64 =cut
65  
66 468 ▶sub _check_self {
Subroutine _check_self: called CC=3 SCAR=11.0
67 468 ▶ croak 'First argument isn\'t a valid ' . __PACKAGE__ . ' object'
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) 8 460 468
Condition100%3/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
44460
MC/DC100%2/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table100%3/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
68 unless blessed $_[0] and $_[0]->isa(__PACKAGE__);
69 }
70  
71 9 my %types = (
72 jokers => [ qw<jokers> ],
73 sql => [ qw<sql> ],
74 commas => [ qw<commas> ],
75 brackets => [ qw<brackets> ],
76 unix => [ qw<jokers brackets> ],
77 win32 => [ qw<jokers commas> ],
78 );
79 9 $types{$_} = $types{win32} for qw<dos os2 MSWin32 cygwin>;
80 9 $types{$_} = $types{unix} for qw<linux
81 darwin machten next
82 aix irix hpux dgux dynixptx
83 bsdos freebsd openbsd
84 svr4 solaris sunos dec_osf
85 sco_sv unicos unicosmk>;
86  
87 9 my %escapes = (
88 jokers => '?*',
89 sql => '_%',
90 commas => ',',
91 brackets => '{},',
92 groups => '()',
93 anchors => '^$',
94 );
95  
96 my %captures = (
97 31 ▶ single => sub { $_[1] ? '(.)' : '.' },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 9 22 31
98 31 ▶ any => sub { $_[1] ? ($_[0]->{greedy} ? '(.*)'
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 2 branches
BranchTrueFalseTotal
$_[1] ? : 12 19 31
$_[0]{'greedy'} ? : 6 6 12
99 : '(.*?)')
100 : '.*' },
101 31 ▶ brackets => sub { $_[1] ? '(' : '(?:'; },
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
BranchTrueFalseTotal
$_[1] ? : 0 31 31
102 9 greedy => undef,
103 );
104  
105 66 ▶sub _validate {
Subroutine _validate: called CC=13 SCAR=25.6
106 66 my $self = shift;
107 66 _check_self $self;
108 66 my $valid = shift;
109 66 my $old = shift;
110 66 ▶ $old = { } unless defined $old;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old 26 40 66
111  
112 66 my %opts;
113 66 ▶ if (@_ <= 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ <= 1) { } 47 19 66
elsif (@_ % 2) { } 2 17 19
114 47 ▶ $opts{set} = defined $_[0] ? $_[0] : { };
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 30 17 47
115 } elsif (@_ % 2) {
116 2 croak 'Arguments must be passed as an unique scalar or as key => value pairs';
117 } else {
118 17 %opts = @_;
119 }
120  
121 64 my %checked;
122 64 for (qw<set add rem>) {
123 184 my $opt = $opts{$_};
124 184 ▶ next unless defined $opt;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opt 120 64 184
125  
126 my $cb = {
127 15 ▶ '' => sub { +{ ($_[0] => 1) x (exists $valid->{$_[0]}) } },
Subroutine __ANON__: called CC=1 SCAR=0.0
128 25 ▶ 'ARRAY' => sub { +{ map { ($_ => 1) x (exists $valid->{$_}) } @{$_[0]} } },
Subroutine __ANON__: called CC=1 SCAR=0.0
129 6 ▶ 'HASH' => sub { +{ map { ($_ => $_[0]->{$_}) x (exists $valid->{$_}) }
Subroutine __ANON__: called CC=1 SCAR=0.0
130 20 keys %{$_[0]} } }
131 64 }->{ ref $opt };
132 64 ▶ croak 'Wrong option set' unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 4 60 64
133 60 $checked{$_} = $cb->($opt);
134 }
135  
136 60 ▶ my $config = (exists $checked{set}) ? $checked{set} : $old;
Branch: 1 branch
BranchTrueFalseTotal
exists $checked{'set'} ? : 49 11 60
137 60 $config->{$_} = $checked{add}->{$_} for grep $checked{add}->{$_},
138 60 ▶ keys %{$checked{add} || {}};
Condition100%2/2
$checked{'add'} || {}
l!l
753
MC/DC100%1/1
$checked{'add'} || {}
$checked{'add'}
Truth table100%2/2
A
A= $checked{'add'}
Aresult
11
00
139 60 delete $config->{$_} for grep $checked{rem}->{$_},
140 60 ▶ keys %{$checked{rem} || {}};
Condition100%2/2
$checked{'rem'} || {}
l!l
456
MC/DC100%1/1
$checked{'rem'} || {}
$checked{'rem'}
Truth table100%2/2
A
A= $checked{'rem'}
Aresult
11
00
141  
142 60 $config;
143 }
144  
145 32 ▶sub _do {
Subroutine _do: called CC=2 SCAR=6.9
146 32 my $self = shift;
147  
148 32 my $config;
149 32 $config->{do} = $self->_validate(\%escapes, $self->{do}, @_);
150 29 $config->{escape} = '';
151 29 $config->{escape} .= $escapes{$_} for keys %{$config->{do}};
152 29 $config->{escape} = quotemeta $config->{escape};
153  
154 29 $config;
155 }
156  
157 18 ▶sub do {
Subroutine do: called CC=2 SCAR=6.9
Pod documented
158 18 my $self = shift;
159 18 _check_self $self;
160  
161 16 my $config = $self->_do(@_);
162 13 $self->{$_} = $config->{$_} for keys %$config;
163  
164 13 $self;
165 }
166  
167 34 ▶sub _capture {
Subroutine _capture: called CC=3 SCAR=11.0
168 34 my $self = shift;
169  
170 34 my $config;
171 34 $config->{capture} = $self->_validate(\%captures, $self->{capture}, @_);
172 31 $config->{greedy} = delete $config->{capture}->{greedy};
173 31 for (keys %captures) {
174 $config->{'c_' . $_} = $captures{$_}->($config, $config->{capture}->{$_})
175 124 ▶ if $captures{$_}; # Skip 'greedy'
Branch: 1 branch
BranchTrueFalseTotal
if $captures{$_} 93 31 124
176 }
177  
178 31 $config;
179 }
180  
181 36 ▶sub capture {
Subroutine capture: called CC=2 SCAR=6.9
Pod documented
182 36 my $self = shift;
183 36 _check_self $self;
184  
185 34 my $config = $self->_capture(@_);
186 31 $self->{$_} = $config->{$_} for keys %$config;
187  
188 31 $self;
189 }
190  
191 18 ▶sub _type {
Subroutine _type: called CC=3 SCAR=11.0
192 18 my ($self, $type) = @_;
193 18 ▶ $type = 'unix' unless defined $type;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type 9 9 18
194 18 ▶ croak 'Wrong type' unless exists $types{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $types{$type} 2 16 18
195  
196 16 my $config = $self->_do($types{$type});
197 16 $config->{type} = $type;
198  
199 16 $config;
200 }
201  
202 16 ▶sub type {
Subroutine type: called CC=2 SCAR=6.9
Pod documented
203 16 my $self = shift;
204 16 _check_self $self;
205  
206 14 my $config = $self->_type(@_);
207 13 $self->{$_} = $config->{$_} for keys %$config;
208  
209 13 $self;
210 }
211  
212 14 ▶sub new {
Subroutine new: called CC=5 SCAR=16.1
Pod documented
213 14 my $class = shift;
214 14 ▶ $class = blessed($class) || $class || __PACKAGE__;
Condition100%5/5
builtin::blessed($class) || $class || __PACKAGE__
l!l
131
builtin::blessed($class) or $class
l!l&&r!l&&!r
1121
MC/DC100%2/2
builtin::blessed($class) || $class || __PACKAGE__
builtin::blessed($class) $class
Truth table100%3/3
A or B
A= builtin::blessed($class)B= $class
ABresult
1X1
011
000
215  
216 14 ▶ croak 'Optional arguments must be passed as key => value pairs' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 13 14
217 13 my %args = @_;
218  
219 13 my $self = bless { }, $class;
220  
221 13 ▶ if (defined $args{do}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'do'}) { } 4 9 13
222 4 $self->do($args{do});
223 } else {
224 9 $self->type($args{type});
225 }
226  
227 13 $self->capture($args{capture});
228 }
229  
230 =head2 C<new>
231  
232 my $rw = Regexp::Wildcards->new(do => $what, capture => $capture);
233 my $rw = Regexp::Wildcards->new(type => $type, capture => $capture);
234  
235 Constructs a new L<Regexp::Wildcard> object.
236  
237 C<do> lists all features that should be enabled when converting wildcards to regexps.
238 Refer to L</do> for details on what can be passed in C<$what>.
239  
240 The C<type> specifies a predefined set of C<do> features to use.
241 See L</type> for details on which types are valid.
242 The C<do> option overrides C<type>.
243  
244 C<capture> lists which atoms should be capturing.
245 Refer to L</capture> for more details.
246  
247 =head2 C<do>
248  
249 $rw->do($what);
250 $rw->do(set => $c1);
251 $rw->do(add => $c2);
252 $rw->do(rem => $c3);
253  
254 Specifies the list of metacharacters to convert or to prevent for escaping.
255 They fit into six classes :
256  
257 =over 4
258  
259 =item *
260  
261 C<'jokers'>
262  
263 Converts C<?> to C<.> and C<*> to C<.*>.
264  
265 'a**\\*b??\\?c' ==> 'a.*\\*b..\\?c'
266  
267 =item *
268  
269 C<'sql'>
270  
271 Converts C<_> to C<.> and C<%> to C<.*>.
272  
273 'a%%\\%b__\\_c' ==> 'a.*\\%b..\\_c'
274  
275 =item *
276  
277 C<'commas'>
278  
279 Converts all C<,> to C<|> and puts the complete resulting regular expression inside C<(?: ... )>.
280  
281 'a,b{c,d},e' ==> '(?:a|b\\{c|d\\}|e)'
282  
283 =item *
284  
285 C<'brackets'>
286  
287 Converts all matching C<{ ... , ... }> brackets to C<(?: ... | ... )> alternations.
288 If some brackets are unbalanced, it tries to substitute as many of them as possible, and then escape the remaining unmatched C<{> and C<}>.
289 Commas outside of any bracket-delimited block are also escaped.
290  
291 'a,b{c,d},e' ==> 'a\\,b(?:c|d)\\,e'
292 '{a\\{b,c}d,e}' ==> '(?:a\\{b|c)d\\,e\\}'
293 '{a{b,c\\}d,e}' ==> '\\{a\\{b\\,c\\}d\\,e\\}'
294  
295 =item *
296  
297 C<'groups'>
298  
299 Keeps the parenthesis C<( ... )> of the original string without escaping them.
300 Currently, no check is done to ensure that the parenthesis are matching.
301  
302 'a(b(c))d\\(\\)' ==> (no change)
303  
304 =item *
305  
306 C<'anchors'>
307  
308 Prevents the I<beginning-of-line> C<^> and I<end-of-line> C<$> anchors to be escaped.
309 Since C<[...]> character class are currently escaped, a C<^> will always be interpreted as I<beginning-of-line>.
310  
311 'a^b$c' ==> (no change)
312  
313 =back
314  
315 Each C<$c> can be any of :
316  
317 =over 4
318  
319 =item *
320  
321 A hash reference, with wanted metacharacter group names (described above) as keys and booleans as values ;
322  
323 =item *
324  
325 An array reference containing the list of wanted metacharacter classes ;
326  
327 =item *
328  
329 A plain scalar, when only one group is required.
330  
331 =back
332  
333 When C<set> is present, the classes given as its value replace the current object options.
334 Then the C<add> classes are added, and the C<rem> classes removed.
335  
336 Passing a sole scalar C<$what> is equivalent as passing C<< set => $what >>.
337 No argument means C<< set => [ ] >>.
338  
339 $rw->do(set => 'jokers'); # Only translate jokers.
340 $rw->do('jokers'); # Same.
341 $rw->do(add => [ qw<sql commas> ]); # Translate also SQL and commas.
342 $rw->do(rem => 'jokers'); # Specifying both 'sql' and
343 # 'jokers' is useless.
344 $rw->do(); # Translate nothing.
345  
346 The C<do> method returns the L<Regexp::Wildcards> object.
347  
348 =head2 C<type>
349  
350 $rw->type($type);
351  
352 Notifies to convert the metacharacters that corresponds to the predefined type C<$type>.
353 C<$type> can be any of :
354  
355 =over 4
356  
357 =item *
358  
359 C<'jokers'>, C<'sql'>, C<'commas'>, C<'brackets'>
360  
361 Singleton types that enable the corresponding C<do> classes.
362  
363 =item *
364  
365 C<'unix'>
366  
367 Covers typical Unix shell globbing features (effectively C<'jokers'> and C<'brackets'>).
368  
369 =item *
370  
371 C<$^O> values for common Unix systems
372  
373 Wrap to C<'unix'> (see L<perlport> for the list).
374  
375 =item *
376  
377 C<undef>
378  
379 Defaults to C<'unix'>.
380  
381 =item *
382  
383 C<'win32'>
384  
385 Covers typical Windows shell globbing features (effectively C<'jokers'> and C<'commas'>).
386  
387 =item *
388  
389 C<'dos'>, C<'os2'>, C<'MSWin32'>, C<'cygwin'>
390  
391 Wrap to C<'win32'>.
392  
393 =back
394  
395 In particular, you can usually pass C<$^O> as the C<$type> and get the corresponding shell behaviour.
396  
397 $rw->type('win32'); # Set type to win32.
398 $rw->type($^O); # Set type to unix on Unices and win32 on Windows
399 $rw->type(); # Set type to unix.
400  
401 The C<type> method returns the L<Regexp::Wildcards> object.
402  
403 =head2 C<capture>
404  
405 $rw->capture($captures);
406 $rw->capture(set => $c1);
407 $rw->capture(add => $c2);
408 $rw->capture(rem => $c3);
409  
410 Specifies the list of atoms to capture.
411 This method works like L</do>, except that the classes are different :
412  
413 =over 4
414  
415 =item *
416  
417 C<'single'>
418  
419 Captures all unescaped I<"exactly one"> metacharacters, i.e. C<?> for wildcards or C<_> for SQL.
420  
421 'a???b\\??' ==> 'a(.)(.)(.)b\\?(.)'
422 'a___b\\__' ==> 'a(.)(.)(.)b\\_(.)'
423  
424 =item *
425  
426 C<'any'>
427  
428 Captures all unescaped I<"any"> metacharacters, i.e. C<*> for wildcards or C<%> for SQL.
429  
430 'a***b\\**' ==> 'a(.*)b\\*(.*)'
431 'a%%%b\\%%' ==> 'a(.*)b\\%(.*)'
432  
433 =item *
434  
435 C<'greedy'>
436  
437 When used in conjunction with C<'any'>, it makes the C<'any'> captures greedy (by default they are not).
438  
439 'a***b\\**' ==> 'a(.*?)b\\*(.*?)'
440 'a%%%b\\%%' ==> 'a(.*?)b\\%(.*?)'
441  
442 =item *
443  
444 C<'brackets'>
445  
446 Capture matching C<{ ... , ... }> alternations.
447  
448 'a{b\\},\\{c}' ==> 'a(b\\}|\\{c)'
449  
450 =back
451  
452 $rw->capture(set => 'single'); # Only capture "exactly one"
453 # metacharacters.
454 $rw->capture('single'); # Same.
455 $rw->capture(add => [ qw<any greedy> ]); # Also greedily capture
456 # "any" metacharacters.
457 $rw->capture(rem => 'greedy'); # No more greed please.
458 $rw->capture(); # Capture nothing.
459  
460 The C<capture> method returns the L<Regexp::Wildcards> object.
461  
462 =head2 C<convert>
463  
464 my $rx = $rw->convert($wc);
465 my $rx = $rw->convert($wc, $type);
466  
467 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.
468 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>.
469  
470 =cut
471  
472 332 ▶sub convert {
Subroutine convert: called CC=8 SCAR=20.8
Pod documented
473 332 my ($self, $wc, $type) = @_;
474 332 _check_self $self;
475  
476 330 ▶ my $config = (defined $type) ? $self->_type($type) : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $type ? : 4 326 330
477 329 ▶ return unless defined $wc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $wc 1 328 329
478  
479 328 my $e = $config->{escape};
480 # Escape :
481 # - an even number of \ that doesn't protect a regexp/wildcard metachar
482 # - an odd number of \ that doesn't protect a wildcard metachar
483 328 $wc =~ s/
484 (?<!\\)(
485 (?:\\\\)*
486 (?:
487 [^\w\s\\$e]
488 |
489 \\
490 (?: [^\W$e] | \s | $ )
491 )
492 )
493 /\\$1/gx;
494  
495 328 my $do = $config->{do};
496 328 ▶ $wc = $self->_jokers($wc) if $do->{jokers};
Branch: 1 branch
BranchTrueFalseTotal
if $do->{"jokers"} 201 127 328
497 328 ▶ $wc = $self->_sql($wc) if $do->{sql};
Branch: 1 branch
BranchTrueFalseTotal
if $do->{"sql"} 175 153 328
498 328 ▶ if ($do->{brackets}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($do->{'brackets'}) { } 40 288 328
elsif ($do->{'commas'} and $wc =~ /(?<!\\)(?:\\\\)*,/) { } 7 561 568
499 40 $wc = $self->_bracketed($wc);
500 ▶ } elsif ($do->{commas} and $wc =~ /(?<!\\)(?:\\\\)*,/) {
Condition100%3/3
$do->{"commas"} and $wc =~ /(?<!\\)(?:\\\\)*,/
!ll&&!rl&&r
28017
MC/DC100%2/2
$do->{"commas"} and $wc =~ /(?<!\\)(?:\\\\)*,/
$do->{"commas"} $wc =~ /(?<!\\)(?:\\\\)*,/
Truth table100%3/3
A and B
A= $do->{"commas"}B= $wc =~ /(?<!\\)(?:\\\\)*,/
ABresult
0X0
100
111
501 7 $wc = $self->{'c_brackets'} . $self->_commas($wc) . ')';
502 }
503  
504 $wc
505 328 }
506  
507 =head1 EXPORT
508  
509 An object module shouldn't export any function, and so does this one.
510  
511 =head1 DEPENDENCIES
512  
513 L<Carp> (core module since perl 5), L<Scalar::Util>, L<Text::Balanced> (since 5.7.3).
514  
515 =head1 CAVEATS
516  
517 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).
518 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.
519  
520 =head1 SEE ALSO
521  
522 L<Text::Glob>.
523  
524 =head1 AUTHOR
525  
526 Vincent Pit, C<< <perl at profvince.com> >>, L<http://www.profvince.com>.
527  
528 You can contact me by mail or on C<irc.perl.org> (vincent).
529  
530 =head1 BUGS
531  
532 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>.
533 I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
534  
535 =head1 SUPPORT
536  
537 You can find documentation for this module with the perldoc command.
538  
539 perldoc Regexp::Wildcards
540  
541 Tests code coverage report is available at L<http://www.profvince.com/perl/cover/Regexp-Wildcards>.
542  
543 =head1 COPYRIGHT & LICENSE
544  
545 Copyright 2007,2008,2009,2013 Vincent Pit, all rights reserved.
546  
547 This program is free software; you can redistribute it and/or modify it
548 under the same terms as Perl itself.
549  
550 =cut
551  
552 134 ▶sub _extract ($) { extract_bracketed $_[0], '{', qr/.*?(?<!\\)(?:\\\\)*(?={)/ }
Subroutine _extract: called CC=1 SCAR=0.0
553  
554 201 ▶sub _jokers {
Subroutine _jokers: called CC=1 SCAR=0.0
555 201 my $self = shift;
556 201 local $_ = $_[0];
557  
558 # substitute ? preceded by an even number of \
559 201 my $s = $self->{c_single};
560 201 s/(?<!\\)((?:\\\\)*)\?/$1$s/g;
561 # substitute * preceded by an even number of \
562 201 $s = $self->{c_any};
563 201 s/(?<!\\)((?:\\\\)*)\*+/$1$s/g;
564  
565 201 $_
566 }
567  
568 175 ▶sub _sql {
Subroutine _sql: called CC=1 SCAR=0.0
569 175 my $self = shift;
570 175 local $_ = $_[0];
571  
572 # substitute _ preceded by an even number of \
573 175 my $s = $self->{c_single};
574 175 s/(?<!\\)((?:\\\\)*)_/$1$s/g;
575 # substitute % preceded by an even number of \
576 175 $s = $self->{c_any};
577 175 s/(?<!\\)((?:\\\\)*)%+/$1$s/g;
578  
579 175 $_
580 }
581  
582 67 ▶sub _commas {
Subroutine _commas: called CC=1 SCAR=0.0
583 67 local $_ = $_[1];
584  
585 # substitute , preceded by an even number of \
586 67 s/(?<!\\)((?:\\\\)*),/$1|/g;
587  
588 67 $_
589 }
590  
591 47 ▶sub _brackets {
Subroutine _brackets: called CC=2 SCAR=6.9
592 47 my ($self, $rest) = @_;
593  
594 47 substr $rest, 0, 1, '';
595 47 chop $rest;
596  
597 47 my ($re, $bracket, $prefix) = ('');
598 47 ▶ while (do { ($bracket, $rest, $prefix) = _extract $rest; $bracket }) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { ($bracket, $rest, $prefix) = &_extract($rest); $bracket }) 13 47 60
599 13 $re .= $self->_commas($prefix) . $self->_brackets($bracket);
600 }
601 47 $re .= $self->_commas($rest);
602  
603 47 $self->{c_brackets} . $re . ')';
604 }
605  
606 40 ▶sub _bracketed {
Subroutine _bracketed: called CC=2 SCAR=6.9
607 40 my ($self, $rest) = @_;
608  
609 40 my ($re, $bracket, $prefix) = ('');
610 40 ▶ while (do { ($bracket, $rest, $prefix) = _extract $rest; $bracket }) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { ($bracket, $rest, $prefix) = &_extract($rest); $bracket }) 34 40 74
611 34 $re .= $prefix . $self->_brackets($bracket);
612 }
613 40 $re .= $rest;
614  
615 40 $re =~ s/(?<!\\)((?:\\\\)*[\{\},])/\\$1/g;
616  
617 40 $re;
618 }
619  
620 9 1; # End of Regexp::Wildcards
↑ Summary