| 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::ListMatch; | ||||||||||||||||||||||||||||
| 11 | 3 | $File::Globstar::ListMatch::VERSION = 'v1.0.0'; | |||||||||||||||||||||||||||
| 12 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Locale::TextDomain qw(File-Globstar); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 3 | ▶ | use Scalar::Util 1.21 qw(reftype); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use File::Globstar qw(translatestar pnmatchstar); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use constant RE_NONE => File::Globstar::RE_NONE(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use constant RE_NEGATED => File::Globstar::RE_NEGATED(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use constant RE_FULL_MATCH => File::Globstar::RE_FULL_MATCH; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant RE_DIRECTORY => File::Globstar::RE_DIRECTORY; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||
| 26 | 40 | ▶ | my ($class, $input, %options) = @_; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 40 | my $self = {}; | |||||||||||||||||||||||||||
| 29 | 40 | bless $self, $class; | |||||||||||||||||||||||||||
| 30 | 40 | $self->{__ignore_case} = delete $options{ignoreCase}; | |||||||||||||||||||||||||||
| 31 | 40 | $self->{__filename} = delete $options{filename}; | |||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 40 | ▶ | if (ref $input) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | 38 | my $type = reftype $input; | |||||||||||||||||||||||||||
| 35 | 38 | ▶ | if ('SCALAR' eq $type) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 36 | 36 | $self->_readString($$input); | |||||||||||||||||||||||||||
| 37 | } elsif ('ARRAY' eq $type) { | ||||||||||||||||||||||||||||
| 38 | 1 | $self->_readArray($input); | |||||||||||||||||||||||||||
| 39 | } else { | ||||||||||||||||||||||||||||
| 40 | 1 | $self->_readFileHandle($input); | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | } elsif ("GLOB" eq reftype \$input) { | ||||||||||||||||||||||||||||
| 43 | 1 | $self->_readFileHandle(\$input, ); | |||||||||||||||||||||||||||
| 44 | } else { | ||||||||||||||||||||||||||||
| 45 | 1 | $self->_readFile($input); | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 40 | return $self; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | sub __match { | ||||||||||||||||||||||||||||
| 52 | 89 | ▶ | my ($self, $imode, $path, $is_directory) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__match: called CC=10 SCAR=23.0
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 89 | my $match; | |||||||||||||||||||||||||||
| 55 | 89 | foreach my $pattern ($self->patterns) { | |||||||||||||||||||||||||||
| 56 | 136 | my $type = ref $pattern; | |||||||||||||||||||||||||||
| 57 | 136 | my $negated; | |||||||||||||||||||||||||||
| 58 | 136 | ▶ | if ($type & RE_NEGATED) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 47 | ▶ | next if !$match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 44 | $negated = 1; | |||||||||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||||||||||
| 62 | 89 | ▶ | next if $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 133 | $match = pnmatchstar $pattern, $path, isDirectory => $is_directory; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 89 | ▶ | return 1 if $match; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # Check that none of its parent directories has been ignored. | ||||||||||||||||||||||||||||
| 71 | 53 | ▶ | if (!$imode) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 35 | $path =~ s{/$}{}; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 35 | ▶ | while ($path =~ s{/[^/]*$}{} && length $path) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$path =~ s[/[^/]*$][] and length $path
MC/DC100%2/2
$path =~ s[/[^/]*$][] and length $path
$path =~ s[/[^/]*$][]
length $path
Truth table100%3/3
A and B
A= $path =~ s[/[^/]*$][]B= length $path
| |||||||||||||||||||||||||||||
| 75 | 15 | ▶ | return 1 if $self->__match(undef, $path, 1); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 50 | return; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | sub match { | ||||||||||||||||||||||||||||
| 83 | 43 | ▶ | my ($self) = shift @_; | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 43 | return $self->__match(undef, @_); | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub matchExclude { | ||||||||||||||||||||||||||||
| 89 | 2 | ▶ | &match; | ||||||||||||||||||||||||||
|
Subroutine
matchExclude: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub matchInclude { | ||||||||||||||||||||||||||||
| 93 | 31 | ▶ | my ($self) = shift @_; | ||||||||||||||||||||||||||
|
Subroutine
matchInclude: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 31 | return $self->__match(1, @_); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | sub patterns { | ||||||||||||||||||||||||||||
| 99 | 100 | ▶ | return @{shift->{__patterns}}; | ||||||||||||||||||||||||||
|
Subroutine
patterns: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub _readArray { | ||||||||||||||||||||||||||||
| 103 | 40 | ▶ | my ($self, $lines) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_readArray: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 40 | my @patterns; | |||||||||||||||||||||||||||
| 106 | 40 | $self->{__patterns} = \@patterns; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 40 | my $ignore_case = $self->{__ignore_case}; | |||||||||||||||||||||||||||
| 109 | 40 | foreach my $line (@$lines) { | |||||||||||||||||||||||||||
| 110 | 78 | my $transpiled = eval { | |||||||||||||||||||||||||||
| 111 | 78 | translatestar $line, | |||||||||||||||||||||||||||
| 112 | ignoreCase => $ignore_case, | ||||||||||||||||||||||||||||
| 113 | pathMode => 1 | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # Why a slash? When matching, we discard a trailing slash from the | ||||||||||||||||||||||||||||
| 117 | # string to match. The regex '/$' can therefore never match. And the | ||||||||||||||||||||||||||||
| 118 | # leading caret is there in order to save Perl at least reading the | ||||||||||||||||||||||||||||
| 119 | # string to the end. | ||||||||||||||||||||||||||||
| 120 | 78 | ▶ | $transpiled = qr{^/$} if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | 78 | push @patterns, $transpiled; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 40 | return $self; | |||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | sub _readString { | ||||||||||||||||||||||||||||
| 128 | 39 | ▶ | my ($self, $string) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_readString: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 39 | my @lines; | |||||||||||||||||||||||||||
| 131 | 39 | foreach my $line (split /\n/, $string) { | |||||||||||||||||||||||||||
| 132 | 92 | ▶ | next if $line =~ /^#/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | # If the string contains trailing whitespace we have to count the | ||||||||||||||||||||||||||||
| 135 | # number of backslashes in front of the first whitespace character. | ||||||||||||||||||||||||||||
| 136 | 86 | ▶ | if ($line =~ s/(\\*)([\x{9}-\x{13} ])[\x{9}-\x{13} ]*$//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 12 | my ($bs, $first) = ($1, $2); | |||||||||||||||||||||||||||
| 138 | 12 | ▶ | if ($bs) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 4 | $line .= $bs; | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 4 | my $num_bs = $bs =~ y/\\/\\/; | |||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # If the number of backslashes is odd, the last space was | ||||||||||||||||||||||||||||
| 144 | # escaped. | ||||||||||||||||||||||||||||
| 145 | 4 | ▶ | $line .= $first if $num_bs & 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | 86 | ▶ | next if '' eq $line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 75 | push @lines, $line; | |||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 39 | return $self->_readArray(\@lines); | |||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub _readFileHandle { | ||||||||||||||||||||||||||||
| 157 | 3 | ▶ | my ($self, $fh) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_readFileHandle: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 3 | my $filename = $self->{__filename}; | |||||||||||||||||||||||||||
| 160 | 3 | ▶ | $filename = __["in memory string"] if File::Globstar::empty($filename); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 3 | $fh->clearerr; | |||||||||||||||||||||||||||
| 163 | 3 | my @lines = $fh->getlines; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 3 | ▶ | die __x("Error reading '{filename}': {error}!\n", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | filename => $filename, error => $!) if $fh->error; | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | 3 | return $self->_readString(join '', @lines); | |||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub _readFile { | ||||||||||||||||||||||||||||
| 172 | 1 | ▶ | my ($self, $filename) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_readFile: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | $self->{__filename} = $filename | ||||||||||||||||||||||||||||
| 175 | 1 | ▶ | if File::Globstar::empty($self->{__filename}); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | ▶ | open my $fh, '<', $filename | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | or die __x("Error reading '{filename}': {error}!\n", | ||||||||||||||||||||||||||||
| 179 | filename => $filename, error => $!); | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | 1 | return $self->_readFileHandle($fh); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 3 | 1; | |||||||||||||||||||||||||||