blib/lib/File/Globstar.pm

filter: Statementstmt 98.3% 177 / 180 Branchbran 94.7% 91 / 96 Conditioncond 86.6% 13 / 15 MC/DCmcdc 70.0% 7 / 10 Subroutinesub 100% 23 / 23 Podpod 83.3% 5 / 6 totaltotal 95.7% 316 / 330 SCAR 41.8
CC
65
Cov
97%
_globstar
24.2
translatestar
16.0
pnmatchstar
12.0
CRAP 65.2
↑ Summary blib/lib/File/Globstar/ListMatch.pm »
1 # Copyright (C) 2016-2023 Guido Flohr <guido.flohr@cantanea.com>,
2 # all rights reserved.
3  
4 # This file is distributed under the same terms and conditions as
5 # Perl itself.
6  
7 # This next lines is here to make Dist::Zilla happy.
8 # ABSTRACT: Perl Globstar (double asterisk globbing) and utils
9  
10 package File::Globstar;
11 9 $File::Globstar::VERSION = 'v1.0.0';
12 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 9 ▶use Locale::TextDomain qw(File-Globstar);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶use File::Glob qw(bsd_glob);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 9 ▶use Scalar::Util 1.21 qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶use File::Find;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 9 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
20 9 ▶use vars qw(@EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 @EXPORT_OK = qw(globstar fnmatchstar translatestar quotestar pnmatchstar);
22  
23 9 ▶use constant RE_NONE => 0x0;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 9 ▶use constant RE_NEGATED => 0x1;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 9 ▶use constant RE_FULL_MATCH => 0x2;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 9 ▶use constant RE_DIRECTORY => 0x4;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 # Remember what Scalar::Util::reftype() returns for a compiled regular
29 # expression. It should normally be 'REGEXP' but with Perl 5.10 (or
30 # maybe older) this seems to be an empty string. In this case, the
31 # check in pnmatchstar() whether it received a compiled regex will be
32 # rather weak ...
33 9 my $test_re = qr/./;
34 9 my $regex_type = reftype $test_re;
35  
36 sub _globstar;
37 sub pnmatchstar;
38  
39 sub empty($) {
40 25 ▶ my ($what) = @_;
Subroutine empty: called CC=3 SCAR=11.0
Pod undocumented
41  
42 25 ▶ return if defined $what && length $what;
Branch: 1 branch
BranchTrueFalseTotal
if defined $what and length $what 17 8 25
Condition100%3/3
defined $what and length $what
!ll&&!rl&&r
1717
MC/DC100%2/2
defined $what and length $what
defined $what length $what
Truth table100%3/3
A and B
A= defined $whatB= length $what
ABresult
0X0
100
111
43  
44 8 return 1;
45 }
46  
47 sub _find_directories($) {
48 15 ▶ my ($directory) = @_;
Subroutine _find_directories: called CC=3 SCAR=11.0
49  
50 15 my $empty = empty $directory;
51 15 ▶ $directory = '.' if $empty;
Branch: 1 branch
BranchTrueFalseTotal
if $empty 5 10 15
52  
53 15 my @hits;
54 File::Find::find sub {
55 171 ▶ return if !-d $_;
Subroutine __ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
unless -d $_ 126 45 171
56 45 ▶ return if '.' eq substr $_, 0, 1;
Branch: 1 branch
BranchTrueFalseTotal
if "." eq substr($_, 0, 1) 15 30 45
57 30 push @hits, $File::Find::name;
58 15 }, $directory;
59  
60 15 ▶ if ($empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty) 5 10 15
61 5 @hits = map { substr $_, 2 } @hits;
62 }
63  
64 15 return @hits;
65 }
66  
67 sub _find_all($) {
68 6 ▶ my ($directory) = @_;
Subroutine _find_all: called CC=3 SCAR=11.0
69  
70 6 my $empty = empty $directory;
71 6 ▶ $directory = '.' if $empty;
Branch: 1 branch
BranchTrueFalseTotal
if $empty 2 4 6
72  
73 6 my @hits;
74 File::Find::find sub {
75 66 ▶ return if '.' eq substr $_, 0, 1;
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if "." eq substr($_, 0, 1) 6 60 66
76 60 push @hits, $File::Find::name;
77 6 }, $directory;
78  
79 6 ▶ if ($empty) {
Branch: 1 branch
BranchTrueFalseTotal
if ($empty) 2 4 6
80 2 @hits = map { substr $_, 2 } @hits;
81 }
82  
83 6 return @hits;
84 }
85  
86 sub _globstar($$;$) {
87 35 ▶ my ($pattern, $directory, $flags) = @_;
Subroutine _globstar: called CC=24 SCAR=31.9
88  
89 35 ▶ $directory = '' if !defined $directory;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $directory 0 35 35
90 35 ▶ $pattern = $_ if !@_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 35 35
91  
92 35 ▶ if ('**' eq $pattern) {
Branch: 3 branches
BranchTrueFalseTotal
if ('**' eq $pattern) { } 2 33 35
elsif ('**/' eq $pattern) { } 2 31 33
elsif ($pattern =~ s[^\*\*/][]) { } 3 28 31
93 2 return _find_all $directory;
94 } elsif ('**/' eq $pattern) {
95 2 return map { $_ . '/' } _find_directories $directory;
96 } elsif ($pattern =~ s{^\*\*/}{}) {
97 3 my %found_files;
98 3 foreach my $directory ('', _find_directories $directory) {
99 12 foreach my $file (_globstar $pattern, $directory, $flags) {
100 18 $found_files{$file} = 1;
101 }
102 }
103 3 return keys %found_files;
104 }
105  
106 28 my $current = $directory;
107  
108 # This is a quotemeta() that does not escape the slash and the
109 # colon. Escaped slashes confuse bsd_glob() and escaping colons
110 # may make a full port to Windows harder.
111 28 $current =~ s{([\x00-\x2d\x3b-\x40\x5b-\x5e\x60\x7b-\x7f])}{\\$1}g;
112 28 ▶ if ($directory ne '' && '/' ne substr $directory, -1, 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($directory ne "" and "/" ne substr($directory, -1, 1)) 9 19 28
Condition100%3/3
$directory ne "" and "/" ne substr($directory, -1, 1)
!ll&&!rl&&r
1369
MC/DC100%2/2
$directory ne "" and "/" ne substr($directory, -1, 1)
$directory ne "" "/" ne substr($directory, -1, 1)
Truth table100%3/3
A and B
A= $directory ne ""B= "/" ne substr($directory, -1, 1)
ABresult
0X0
100
111
113 9 $current .= '/';
114 }
115 28 ▶ while ($pattern =~ s/(.)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pattern =~ s/(.)//s) 189 20 209
116 189 ▶ if ($1 eq '\\') {
Branch: 3 branches
BranchTrueFalseTotal
if ($1 eq '\\') { } 0 189 189
elsif ('/' eq $1 and $pattern =~ s[^\*\*/][]) { } 4 185 189
elsif ('**' eq $pattern) { } 4 181 185
117 0 $pattern =~ s/(..?)//s;
118 0 $current .= $1;
119 ▶ } elsif ('/' eq $1 && $pattern =~ s{^\*\*/}{}) {
Condition100%3/3
"/" eq $1 and $pattern =~ s[^\*\*/][]
!ll&&!rl&&r
17784
MC/DC100%2/2
"/" eq $1 and $pattern =~ s[^\*\*/][]
"/" eq $1 $pattern =~ s[^\*\*/][]
Truth table100%3/3
A and B
A= "/" eq $1B= $pattern =~ s[^\*\*/][]
ABresult
0X0
100
111
120 4 $current .= '/';
121  
122 # Expand until here.
123 4 my @directories = bsd_glob $current, $flags;
124  
125 # And search in every subdirectory;
126 4 my %found_dirs;
127 4 foreach my $directory (@directories) {
128 4 $found_dirs{$directory} = 1;
129 4 foreach my $subdirectory (_find_directories $directory) {
130 8 $found_dirs{$subdirectory . '/'} = 1;
131 }
132 }
133  
134 4 ▶ if ('' eq $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if ("" eq $pattern) 2 2 4
135 2 my %found_subdirs;
136 2 foreach my $directory (keys %found_dirs) {
137 6 $found_subdirs{$directory} = 1;
138 6 foreach my $subdirectory (_find_directories $directory) {
139 6 $found_subdirs{$subdirectory . '/'} = 1;
140 }
141 }
142 2 return keys %found_subdirs;
143 }
144 2 my %found_files;
145 2 foreach my $directory (keys %found_dirs) {
146 6 foreach my $hit (_globstar $pattern, $directory, $flags) {
147 18 $found_files{$hit} = 1;
148 }
149 }
150 2 return keys %found_files;
151 } elsif ('**' eq $pattern) {
152 4 my %found_files;
153 4 foreach my $directory (bsd_glob $current, $flags) {
154 4 $found_files{$directory . '/'} = 1;
155 4 foreach my $file (_find_all $directory) {
156 28 $found_files{$file} = 1;
157 }
158 }
159 4 return keys %found_files;
160 } else {
161 181 $current .= $1;
162 }
163 }
164  
165 # Pattern without globstar. Just return the normal expansion.
166 20 return bsd_glob $current, $flags;
167 }
168  
169 sub globstar {
170 17 ▶ my ($pattern, $flags) = @_;
Subroutine globstar: called CC=4 SCAR=14.2
Pod documented
171  
172 # The double asterisk can only be used in place of a directory.
173 # It is illegal everywhere else.
174 17 my @parts = split /\//, $pattern;
175 17 foreach my $part (@parts) {
176 34 ▶ $part ne '**' and 0 <= index $part, '**' and return;
Branch: 1 branch
BranchTrueFalseTotal
$part ne "**" and index($part, "**") != -1 and return 0 34 34
Condition66%2/3
$part ne "**" and index($part, "**") != -1
!ll&&!rl&&r
15190
MC/DC0%0/2
$part ne "**" and index($part, "**") != -1
$part ne "**" index($part, "**") != -1
Truth table66%2/3
A and B
A= $part ne "**"B= index($part, "**") != -1
ABresult
0X0
100
111
177 }
178  
179 17 return _globstar $pattern, '', $flags;
180 }
181  
182 sub quotestar {
183 8 ▶ my ($string, $listmatch) = @_;
Subroutine quotestar: called CC=2 SCAR=6.9
Pod documented
184  
185 8 $string =~ s/([\\\[\]*?])/\\$1/g;
186 8 ▶ $string =~ s/^!/\\!/ if $listmatch;
Branch: 1 branch
BranchTrueFalseTotal
if $listmatch 2 6 8
187  
188 8 return $string;
189 }
190  
191 sub _transpile_range($) {
192 35 ▶ my ($range) = @_;
Subroutine _transpile_range: called CC=1 SCAR=0.0
193  
194 # Strip-off enclosing brackets.
195 35 $range = substr $range, 1, -2 + length $range;
196  
197 # Replace leading exclamation mark with caret.
198 35 $range =~ s/^!/^/;
199  
200 # Backslashes escape inside Perl ranges but not in ours. Escape them:
201 35 $range =~ s/\\/\\\\/g;
202  
203 # Quote dots and equal sign to prevent Perl from interpreting
204 # equivalence and collating classes.
205 35 $range =~ s/\./\\\./g;
206 35 $range =~ s/\=/\\\=/g;
207  
208 35 return "[$range]";
209 }
210  
211 sub translatestar {
212 153 ▶ my ($pattern, %options) = @_;
Subroutine translatestar: called CC=16 SCAR=27.7
Pod documented
213  
214 153 ▶ die __x("invalid pattern '{pattern}'\n", pattern => $pattern)
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ m[^/+$] 2 151 153
215 if $pattern =~ m{^/+$};
216  
217 151 my $blessing = RE_NONE;
218  
219 151 ▶ if ($options{pathMode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options{"pathMode"}) 91 60 151
220 91 ▶ $blessing |= RE_NEGATED if $pattern =~ s/^!//;
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ s/^!// 21 70 91
221 91 ▶ $blessing |= RE_DIRECTORY if $pattern =~ s{/$}{};
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ s[/$][] 15 76 91
222 91 ▶ $blessing |= RE_FULL_MATCH if $pattern =~ m{/};
Branch: 1 branch
BranchTrueFalseTotal
if $pattern =~ m[/] 22 69 91
223 91 $pattern =~ s{^/}{};
224 }
225  
226 # xgettext doesn't parse Perl code in regexes.
227 151 my $invalid_msg = __"invalid use of double asterisk";
228  
229 151 $pattern =~ s
230 {
231 (.*?) # Anything, followed by ...
232 (
233 \\. # escaped character
234 | # or
235 \A\*\*(?=/) # leading **/
236 | # or
237 /\*\*(?=/|\z) # /**/ or /** at end of string
238 | # or
239 \*\*. # invalid
240 | # or
241 .\*\* # invalid
242 | # or
243 \. # a dot
244 | # or
245 \* # an asterisk
246 |
247 \? # a question mark
248 |
249 \[ # opening bracket
250 !?
251 \]? # possible (literal) closing bracket
252 (?:
253 \\. # escaped character
254 |
255 \[:[a-z]+:\] # character class
256 |
257 [^\\\]]+ # non-backslash or closing bracket
258 )+
259 \]
260 )?
261 }{
262 1823 my $translated = quotemeta $1;
263 1823 ▶ if ('\\' eq substr $2, 0, 1) {
Branch: 8 branches
BranchTrueFalseTotal
if ('\\' eq substr($2, 0, 1)) { } 8 1815 1823
elsif ('**' eq $2) { } 2 1813 1815
elsif ('/**' eq $2) { } 8 1805 1813
elsif ('.' eq $2) { } 16 1789 1805
elsif ('*' eq $2) { } 13 1776 1789
elsif ('?' eq $2) { } 4 1772 1776
elsif ('[' eq substr($2, 0, 1)) { } 35 1737 1772
elsif (length $2) { } 2 1735 1737
264 8 $translated .= quotemeta substr $2, 1, 1;
265 } elsif ('**' eq $2) {
266 2 $translated .= '.*';
267 } elsif ('/**' eq $2) {
268 8 $translated .= '(?:/.*)?';
269 } elsif ('.' eq $2) {
270 16 $translated .= '\\.';
271 } elsif ('*' eq $2) {
272 13 $translated .= '[^/]*';
273 } elsif ('?' eq $2) {
274 4 $translated .= '[^/]';
275 } elsif ('[' eq substr $2, 0, 1) {
276 35 $translated .= _transpile_range $2;
277 } elsif (length $2) {
278 2 ▶ if ($2 =~ /\*\*/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 =~ /\*\*/) 2 0 2
279 2 die $invalid_msg;
280 }
281 0 die "should not happen: $2";
282 }
283 1821 $translated;
284 }gsex;
285  
286 149 ▶ my $re = $options{ignoreCase} ? qr/^$pattern$/i : qr/^$pattern$/;
Branch: 1 branch
BranchTrueFalseTotal
$options{'ignoreCase'} ? : 3 146 149
287  
288 149 bless $re, $blessing;
289 }
290  
291 sub fnmatchstar {
292 31 ▶ my ($pattern, $string, %options) = @_;
Subroutine fnmatchstar: called CC=3 SCAR=11.0
Pod documented
293  
294 31 my $transpiled = eval { translatestar $pattern, %options };
295 31 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 30 31
296  
297 30 ▶ $string =~ $transpiled or return;
Branch: 1 branch
BranchTrueFalseTotal
$string =~ /$transpiled/ or return 8 22 30
298  
299 22 return 1;
300 }
301  
302 sub pnmatchstar {
303 204 ▶ my ($pattern, $string, %options) = @_;
Subroutine pnmatchstar: called CC=12 SCAR=24.9
Pod documented
304  
305 204 ▶ $options{isDirectory} = 1 if $string =~ s{/$}{};
Branch: 1 branch
BranchTrueFalseTotal
if $string =~ s[/$][] 7 197 204
306  
307 204 my $full_path = $string;
308  
309 # Check whether the regular expression is compiled.
310 # (ref $pattern) may be false here because it can be 0.
311 204 my $reftype = reftype $pattern;
312 204 ▶ unless (defined $reftype && $regex_type eq $reftype) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $reftype and $regex_type eq $reftype) 14 190 204
Condition66%2/3
defined $reftype and $regex_type eq $reftype
!ll&&!rl&&r
140190
MC/DC50%1/2
defined $reftype and $regex_type eq $reftype
defined $reftype $regex_type eq $reftype
Truth table66%2/3
A and B
A= defined $reftypeB= $regex_type eq $reftype
ABresult
0X0
100
111
313 14 $pattern = eval { translatestar $pattern, %options, pathMode => 1 };
314 14 ▶ return if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 1 13 14
315 }
316  
317 203 my $flags = ref $pattern;
318 203 ▶ $string =~ s{.*/}{} unless $flags & RE_FULL_MATCH;
Branch: 1 branch
BranchTrueFalseTotal
unless $flags & 2 138 65 203
319  
320 203 my $match = $string =~ $pattern;
321 203 ▶ if ($flags & RE_DIRECTORY) {
Branch: 1 branch
BranchTrueFalseTotal
if ($flags & 4) 30 173 203
322 30 ▶ undef $match if !$options{isDirectory};
Branch: 1 branch
BranchTrueFalseTotal
unless $options{"isDirectory"} 10 20 30
323 }
324  
325 203 my $negated = $flags & RE_NEGATED;
326  
327 203 ▶ if ($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) 115 88 203
328 115 ▶ if ($negated) {
Branch: 1 branch
BranchTrueFalseTotal
if ($negated) { } 35 80 115
329 35 return;
330 } else {
331 80 return 1;
332 }
333 }
334  
335 88 ▶ if ($full_path =~ s{/[^/]*$}{}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($full_path =~ s[/[^/]*$][]) 52 36 88
336 52 return pnmatchstar $pattern, $full_path, %options, isDirectory => 1;
337 }
338  
339 36 ▶ return 1 if $negated;
Branch: 1 branch
BranchTrueFalseTotal
if $negated 10 26 36
340  
341 26 return;
342 }
343  
344 9 1;
↑ Summary blib/lib/File/Globstar/ListMatch.pm »