lib/App/Greple/Grep.pm

filter: Statementstmt 87.6% 376 / 429 Branchbran 74.3% 122 / 164 Conditioncond 64.9% 50 / 77 MC/DCmcdc 38.6% 17 / 44 Subroutinesub 84.6% 55 / 65 Podpod 22.7% 5 / 22 totaltotal 78.0% 625 / 801 CC 124 SCAR 29.9
CC
124
Cov
82%
prepare
46.9
parallel_match
30.0
borders
22.7
CRAP 19.8
« script/greple ↑ Summary lib/App/Greple/debug.pm »
1 package App::Greple::Grep;
2  
3 171 ▶use v5.24;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 171 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 171 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 171 our @EXPORT = qw(FILELABEL);
8 171 our %EXPORT_TAGS = ();
9 171 our @EXPORT_OK = qw();
10  
11 171 our @ISA = qw(App::Greple::Text);
12  
13 171 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 171 ▶use List::Util qw(min max reduce sum);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 171 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 171 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 171 ▶use Getopt::EX::Func qw(callable);
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 171 ▶use App::Greple::Common qw(%debug &FILELABEL);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 171 ▶use App::Greple::Regions;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 171 ▶use App::Greple::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 171 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 171 MATCH_NEGATIVE => 0,
26 MATCH_POSITIVE => 1,
27 MATCH_MUST => 2,
28 171 };
29 171 push @EXPORT, qw(
30 MATCH_NEGATIVE
31 MATCH_POSITIVE
32 MATCH_MUST
33 );
34  
35 171 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
36 171 POSI_BASE => 0, POSI_POSI => 0, POSI_NEGA => 1, POSI_LIST => 2,
37 NEGA_BASE => 3, NEGA_POSI => 3, NEGA_NEGA => 4, NEGA_LIST => 5,
38 MUST_BASE => 6, MUST_POSI => 6, MUST_NEGA => 7, MUST_LIST => 8,
39 INDX_POSI => 0, INDX_NEGA => 1, INDX_LIST => 2,
40 171 };
41  
42 171 my @match_base;
43 171 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
44 171 $match_base[MATCH_POSITIVE] = POSI_BASE;
45 171 $match_base[MATCH_NEGATIVE] = NEGA_BASE;
46 171 $match_base[MATCH_MUST] = MUST_BASE;
47 }
48  
49 209 ▶sub category {
Subroutine category: called CC=3 SCAR=11.0
Pod undocumented
50 209 my $obj = shift;
51 209 ▶ return MATCH_MUST if $obj->is_required;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->is_required 12 197 209
52 197 ▶ return MATCH_NEGATIVE if $obj->is_negative;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->is_negative 6 191 197
53 191 return MATCH_POSITIVE;
54 }
55  
56 161 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
57 161 my $class = shift;
58 161 my $obj = bless { @_ }, $class;
59 161 $obj;
60 }
61  
62 161 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
63 161 my $self = shift;
64 161 $self->prepare->compose;
65 }
66  
67 171 package App::Greple::Grep::Block {
68 171 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 223 ▶ sub min :lvalue { $_[0]->[0] }
Subroutine min: called CC=1 SCAR=0.0
70 4 ▶ sub max :lvalue { $_[0]->[1] }
Subroutine max: called CC=1 SCAR=0.0
71 1267 ▶ sub number :lvalue { $_[0]->[2] }
Subroutine number: called CC=1 SCAR=0.0
72 }
73  
74 171 package App::Greple::Grep::Match {
75 171 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 620 ▶ sub min :lvalue { $_[0]->[0] }
Subroutine min: called CC=1 SCAR=0.0
77 578 ▶ sub max :lvalue { $_[0]->[1] }
Subroutine max: called CC=1 SCAR=0.0
78 623 ▶ sub index :lvalue { $_[0]->[2] }
Subroutine index: called CC=1 SCAR=0.0
79 3 ▶ sub callback :lvalue { $_[0]->[3] }
Subroutine callback: called CC=1 SCAR=0.0
80 }
81  
82 171 package App::Greple::Grep::Result {
83 171 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 530 ▶ sub block { $_[0]->[0] }
Subroutine block: called CC=1 SCAR=0.0
85 267 ▶ sub matched { $_[0]->@[ 1 .. $#{$_[0]} ] }
Subroutine matched: called CC=1 SCAR=0.0
86 0 ▶ sub min { $_[0]->block->min }
Subroutine min: not called CC=1 SCAR=6.9
87 0 ▶ sub max { $_[0]->block->max }
Subroutine max: not called CC=1 SCAR=6.9
88 0 ▶ sub number { $_[0]->block->number }
Subroutine number: not called CC=1 SCAR=6.9
89 }
90  
91 ##
92 ## Match multiple patterns in parallel using child processes.
93 ## Each child scans $_ (shared by copy-on-write) with a single
94 ## pattern and returns [from, to, index] triplets in packed binary
95 ## format. Returns a list indexed by pattern number; undef elements
96 ## mean the pattern was not processed and should be matched in the
97 ## calling process.
98 ##
99 171 ▶use constant NO_INDEX => ~0;
Subroutine BEGIN: called CC=1 SCAR=0.0
100  
101 171 our $default_threshold = 1024 * 1024;
102  
103 161 ▶sub parallel_match {
Subroutine parallel_match: called CC=19 SCAR=34.0
Pod undocumented
104 161 my $self = shift;
105 161 my $patlist = shift;
106 161 ▶ my $max = $self->{parallel} // 0;
Condition100%2/2
$self->{"parallel"} // 0
l!l
16145
MC/DC100%1/1
$self->{"parallel"} // 0
$self->{"parallel"}
Truth table100%2/2
A
A= $self->{"parallel"}
Aresult
11
00
107 161 ▶ return () if $max < 2;
Branch: 1 branch
BranchTrueFalseTotal
if $max < 2 145 16 161
108 16 ▶ my $threshold = $ENV{GREPLE_PARALLEL_THRESHOLD} // $default_threshold;
Condition33%1/3
$ENV{"GREPLE_PARALLEL_THRESHOLD"} // $App::Greple::Grep::default_threshold
l!l&&r!l&&!r
1600
MC/DC0%0/2
$ENV{"GREPLE_PARALLEL_THRESHOLD"} // $App::Greple::Grep::default_threshold
$ENV{"GREPLE_PARALLEL_THRESHOLD"} $App::Greple::Grep::default_threshold
Truth table33%1/3
A // B
A= $ENV{"GREPLE_PARALLEL_THRESHOLD"}B= $App::Greple::Grep::default_threshold
ABresult
1X1
011
000
109 16 ▶ return () if length() < $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < $threshold 0 16 16
110 16 my @eligible = grep { not $patlist->[$_]->is_function } keys @$patlist;
111 16 ▶ return () if @eligible < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @eligible < 2 2 14 16
112 warn sprintf("parallel_match: %d patterns, max %d processes\n",
113 14 ▶ scalar @eligible, $max) if $debug{m};
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::Grep::debug{"m"} 0 14 14
114 14 my @result;
115 14 ▶ while (my @chunk = splice @eligible, 0, $max) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@chunk) = splice(@eligible, 0, $max)) 14 14 28
116 14 my @child;
117 14 for my $i (@chunk) {
118 30 my $pat = $patlist->[$i];
119 30 ▶ pipe(my $r, my $w) or last;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or last 30 0 30
120 30 my $pid = fork;
121 30 ▶ if (not defined $pid) { # fall back to sequential
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 30 30
122 0 close $r; close $w;
123 0 last;
124 }
125 30 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 30 30
126 0 close $r;
127 0 binmode $w;
128 my @p = match_regions(pattern => $pat->regex,
129 group => $self->{group_index},
130 0 index => $self->{group_index} >= 2);
131 my $data = pack 'J*',
132 0 ▶ map { ($_->[0], $_->[1], $_->[2] // NO_INDEX) } @p;
Condition0%0/2
$_->[2] // '18446744073709551615'
l!l
00
MC/DC0%0/1
$_->[2] // '18446744073709551615'
$_->[2]
Truth table0%0/2
A
A= $_->[2]
Aresult
11
00
133 0 ▶ syswrite $w, $data if length $data;
Branch: 1 branch
BranchTrueFalseTotal
if length $data 0 0 0
134 0 close $w;
135 0 POSIX::_exit(0);
136 }
137 30 close $w;
138 30 binmode $r;
139 30 push @child, [ $i, $pid, $r ];
140 }
141 14 for (@child) {
142 30 my($i, $pid, $r) = @$_;
143 30 my $data = do { local $/; <$r> };
144 30 close $r;
145 30 waitpid $pid, 0;
146 30 ▶ next if $? != 0; # fall back to sequential
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 30 30
147 30 ▶ my @v = unpack 'J*', $data // '';
Condition50%1/2
$data // ""
l!l
300
MC/DC0%0/1
$data // ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
148 30 my @p;
149 30 ▶ for (my $j = 0; $j < @v; $j += 3) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @v) 27 30 57
150 27 ▶ push @p, $v[$j+2] == NO_INDEX
Branch: 1 branch
BranchTrueFalseTotal
$v[$j + 2] == 18446744073709551615 ? : 27 0 27
151 ? [ @v[$j, $j+1] ] : [ @v[$j .. $j+2] ];
152 }
153 30 $result[$i] = \@p;
154 }
155 }
156 14 @result;
157 }
158  
159 ##
160 ## Compute line borders in a child process, overlapping with pattern
161 ## matching in the calling process. Border positions are independent
162 ## from the match result and required only after the search.
163 ##
164 14 ▶sub start_borders {
Subroutine start_borders: called CC=10 SCAR=30.7
Pod undocumented
165 14 my $self = shift;
166 14 ▶ return if $self->{BORDERS_CHILD}; # already started
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"BORDERS_CHILD"} 0 14 14
167 14 ▶ return if $self->{block}->@*; # borders not used for --block
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"block"}} 0 14 14
168 14 ▶ my $threshold = $ENV{GREPLE_PARALLEL_THRESHOLD} // $default_threshold;
Condition33%1/3
$ENV{"GREPLE_PARALLEL_THRESHOLD"} // $App::Greple::Grep::default_threshold
l!l&&r!l&&!r
1400
MC/DC0%0/2
$ENV{"GREPLE_PARALLEL_THRESHOLD"} // $App::Greple::Grep::default_threshold
$ENV{"GREPLE_PARALLEL_THRESHOLD"} $App::Greple::Grep::default_threshold
Truth table33%1/3
A // B
A= $ENV{"GREPLE_PARALLEL_THRESHOLD"}B= $App::Greple::Grep::default_threshold
ABresult
1X1
011
000
169 14 ▶ return if length() < $threshold;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ < $threshold 0 14 14
170 14 ▶ pipe(my $r, my $w) or return;
Branch: 1 branch
BranchTrueFalseTotal
pipe my $r, my $w or return 14 0 14
171 14 my $pid = fork;
172 14 ▶ if (not defined $pid) { # fall back to sequential
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 14 14
173 0 close $r; close $w;
174 0 return;
175 }
176 14 ▶ if ($pid == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pid == 0) 0 14 14
177 0 close $r;
178 0 binmode $w;
179 0 my $data = pack 'J*', match_borders $self->{border};
180 0 ▶ syswrite $w, $data if length $data;
Branch: 1 branch
BranchTrueFalseTotal
if length $data 0 0 0
181 0 close $w;
182 0 POSIX::_exit(0);
183 }
184 14 close $w;
185 14 binmode $r;
186 14 $self->{BORDERS_CHILD} = [ $pid, $r ];
187 14 ▶ warn "started borders child process\n" if $debug{m};
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::Grep::debug{"m"} 0 14 14
188 }
189  
190 13 ▶sub read_borders {
Subroutine read_borders: called CC=3 SCAR=11.1
Pod undocumented
191 13 my $self = shift;
192 13 my($pid, $r) = @{delete $self->{BORDERS_CHILD}};
193 13 my $data = do { local $/; <$r> };
194 13 close $r;
195 13 waitpid $pid, 0;
196 13 ▶ return if $? != 0; # fall back to sequential
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 13 13
197 13 ▶ unpack 'J*', $data // '';
Condition50%1/2
$data // ""
l!l
130
MC/DC0%0/1
$data // ""
$data
Truth table50%1/2
A
A= $data
Aresult
11
00
198 }
199  
200 161 ▶sub discard_borders {
Subroutine discard_borders: called CC=2 SCAR=6.9
Pod undocumented
201 161 my $self = shift;
202 161 ▶ if (my $child = delete $self->{BORDERS_CHILD}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = delete $self->{"BORDERS_CHILD"}) 1 160 161
203 1 my($pid, $r) = @$child;
204 1 kill 'TERM', $pid;
205 1 close $r;
206 1 waitpid $pid, 0;
207 }
208 161 $self;
209 }
210  
211 161 ▶sub prepare {
Subroutine prepare: called CC=44 SCAR=38.5
Pod undocumented
212 161 my $self = shift;
213  
214 161 local *_ = $self->{text};
215 161 my $pat_holder = $self->{pattern};
216 161 my @blocks;
217  
218 161 $self->{RESULT} = [];
219 161 $self->{BLOCKS} = [];
220  
221 ##
222 ## build match result list
223 ##
224 161 my @result;
225 161 my $positive_count = 0;
226 161 my $group_index_offset = 0;
227 161 my @patlist = $pat_holder->patterns;
228 161 my @parallel = $self->parallel_match(\@patlist);
229 161 ▶ while (my($i, $pat) = each @patlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @patlist) 224 161 385
230 224 my($func, @args) = do {
231 224 ▶ if ($pat->is_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat->is_function) { } 0 224 224
232 0 $pat->function;
233 } else {
234 Getopt::EX::Func->new(\&match_regions,
235 pattern => $pat->regex,
236 group => $self->{group_index},
237 224 index => $self->{group_index} >= 2,
238 );
239 }
240 };
241 my @p = $parallel[$i]
242 30 ? @{$parallel[$i]}
243 224 ▶ : $func->call(@args, &FILELABEL => $self->{filename});
Branch: 1 branch
BranchTrueFalseTotal
$parallel[$i] ? : 30 194 224
244 224 ▶ if (@p == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@p == 0) { } 16 208 224
245 ##
246 ## $self->{need} can be negative value, which means
247 ## required pattern can be compromised upto that number.
248 ##
249 16 ▶ return $self->discard_borders if $pat->is_required and $self->{need} >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pat->is_required and $self->{"need"} >= 0 0 16 16
Condition33%1/3
$pat->is_required and $self->{"need"} >= 0
!ll&&!rl&&r
1600
MC/DC0%0/2
$pat->is_required and $self->{"need"} >= 0
$pat->is_required $self->{"need"} >= 0
Truth table33%1/3
A and B
A= $pat->is_requiredB= $self->{"need"} >= 0
ABresult
0X0
100
111
250 ##
251 ## Update offset even when no match for --ci=G
252 ##
253 16 ▶ if ($self->{group_index} == 2 and not $pat->is_function) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"group_index"} == 2 and not $pat->is_function) 1 15 16
Condition66%2/3
$self->{"group_index"} == 2 and not $pat->is_function
!ll&&!rl&&r
1501
MC/DC50%1/2
$self->{"group_index"} == 2 and not $pat->is_function
$self->{"group_index"} == 2 not $pat->is_function
Truth table66%2/3
A and not(B)
A= $self->{"group_index"} == 2B= not $pat->is_function
ABresult
0X0
100
111
254 1 ▶ $group_index_offset += $pat->group_count //= do {
Condition33%1/3
$pat->group_count //= do { "" =~ /@{[$pat->regex];}?/u; $#+ or 1 }
l!l&&r!l&&!r
010
MC/DC0%0/2
$pat->group_count //= do { "" =~ /@{[$pat->regex];}?/u; $#+ or 1 }
$pat->group_count do { "" =~ /@{[$pat->regex];}?/u; $#+ or 1 }
Truth table33%1/3
A //= B
A= $pat->group_countB= do { "" =~ /@{[$pat->regex];}?/u; $#+ or 1 }
ABresult
1X1
011
000
255 1 "" =~ /@{[$pat->regex]}?/;
256 1 ▶ $#+ || 1;
Condition50%1/2
$#+ || 1
l!l
10
MC/DC0%0/1
$#+ || 1
$#+
Truth table50%1/2
A
A= $#+
Aresult
11
00
257 };
258 }
259 } else {
260 208 bless $_, 'App::Greple::Grep::Match' for @p;
261 208 ▶ if ($pat->is_positive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pat->is_positive) { } 202 6 208
262 ##
263 ## Borders are required only when something matched.
264 ## Do not fork a child process for unmatched files.
265 ##
266 202 ▶ $self->start_borders if $self->{parallel} and not @blocks;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parallel"} and not @blocks 14 188 202
Condition100%3/3
$self->{"parallel"} and not @blocks
!ll&&!rl&&r
1761214
MC/DC100%2/2
$self->{"parallel"} and not @blocks
$self->{"parallel"} not @blocks
Truth table100%3/3
A and not(B)
A= $self->{"parallel"}B= not @blocks
ABresult
0X0
100
111
267 202 push @blocks, @{clone(\@p)};
268 202 $self->{stat}->{match_positive} += @p;
269 202 $positive_count++;
270 }
271 else {
272 6 $self->{stat}->{match_negative} += @p;
273 }
274 ##
275 ## Adjust group index for --ci=G option
276 ## group_index: 0=off, 1=group, 2=sequential, 3=per-pattern
277 ##
278 208 ▶ if ($self->{group_index} == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"group_index"} == 2) 4 204 208
279 4 my $max_index = 0;
280 4 for (@p) {
281 6 ▶ if (defined $_->index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_->index) 6 0 6
282 6 $_->index += $group_index_offset;
283 6 ▶ $max_index = $_->index + 1 if $_->index >= $max_index;
Branch: 1 branch
BranchTrueFalseTotal
if $_->index >= $max_index 6 0 6
284 }
285 }
286 4 $group_index_offset = $max_index;
287 }
288 208 ▶ $_->index //= $i for @p;
Condition66%2/3
$_->index //= $i
l!l&&r!l&&!r
95900
MC/DC0%0/2
$_->index //= $i
$_->index $i
Truth table66%2/3
A //= B
A= $_->indexB= $i
ABresult
1X1
011
000
289 208 ▶ if (my $n = $self->{callback}->@*) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = @{$self->{"callback"}}) 3 205 208
290 3 ▶ if (my $callback = $self->{callback}->[ $i % $n ]) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $callback = $self->{"callback"}[$i % $n]) 3 0 3
291 3 ▶ $_->callback //= $callback for @p;
Condition33%1/3
$_->callback //= $callback
l!l&&r!l&&!r
030
MC/DC0%0/2
$_->callback //= $callback
$_->callback $callback
Truth table33%1/3
A //= B
A= $_->callbackB= $callback
ABresult
1X1
011
000
292 }
293 }
294 }
295 224 push @result, \@p;
296 }
297 161 $self->{stat}->{match_block} += @blocks;
298  
299 ##
300 ## optimization for inadequate match
301 ##
302 161 ▶ return $self->discard_borders if $positive_count < $self->{need} + $self->{must};
Branch: 1 branch
BranchTrueFalseTotal
if $positive_count < $self->{"need"} + $self->{"must"} 9 152 161
303  
304 ##
305 ## --inside, --outside
306 ##
307 152 ▶ if (my @reg_union = $self->{regions}->union) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@reg_union) = $self->{"regions"}->union) 9 143 152
308 9 my @tmp = map { [] } @result;
309 9 ▶ while (my($regi, $reg) = each @reg_union) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @reg_union) 9 9 18
310 9 my @select = get_regions($self->{filename}, \$_, $reg->spec);
311 9 ▶ @select or next if $reg->is_inside;
Branch: 1 branch
BranchTrueFalseTotal
if $reg->is_inside 8 1 9
Condition60%3/5
$reg->is_inside && (@select || (next))
!ll&&!rl&&r
180
@select or next
l!l
80
MC/DC0%0/2
$reg->is_inside && (@select || (next))
$reg->is_inside @select
Truth table33%1/3
A && B
A= $reg->is_insideB= @select
ABresult
0X0
100
111
312 9 ▶ while (my($resi, $r) = each @result) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @result) 11 9 20
313 my @l = select_regions({ strict => $self->{strict} },
314 11 $r, \@select, $reg->flag);
315 11 ▶ if ($self->{region_index} // @result == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"region_index"} // @result == 1) 7 4 11
Condition50%1/2
$self->{"region_index"} // @result == 1
l!l
011
MC/DC0%0/1
$self->{"region_index"} // @result == 1
$self->{"region_index"}
Truth table50%1/2
A
A= $self->{"region_index"}
Aresult
11
00
316 7 map { $_->[2] = $regi } @l;
317 }
318 11 push @{$tmp[$resi]}, @l;
319 }
320 }
321 9 @result = map { [ merge_regions { nojoin => 1, destructive => 1 }, @$_ ] } @tmp;
322 }
323  
324 ##
325 ## --include, --exclude
326 ##
327 152 for my $reg ($self->{regions}->intersect) {
328 2 my @select = get_regions($self->{filename}, \$_, $reg->spec);
329 2 ▶ @select or next if not $reg->is_outside;
Branch: 1 branch
BranchTrueFalseTotal
unless $reg->is_outside 1 1 2
Condition60%3/5
$reg->is_outside || (@select || (next))
l!l&&r!l&&!r
110
@select or next
l!l
10
MC/DC0%0/2
$reg->is_outside || (@select || (next))
$reg->is_outside @select
Truth table66%2/3
A || B
A= $reg->is_outsideB= @select
ABresult
1X1
011
000
330 2 for my $r (@result) {
331 @$r = select_regions({ strict => $self->{strict} },
332 2 $r, \@select, $reg->flag);
333 }
334 }
335  
336 ##
337 ## Setup BLOCKS
338 ##
339 152 my $bp = $self->{BLOCKS} = [ do {
340 152 ▶ if ($self->{block}->@*) { # --block
Branch: 2 branches
BranchTrueFalseTotal
if (@{$self->{'block'}}) { } 7 145 152
elsif (@blocks) { } 140 5 145
341 7 my $text = \$_;
342 merge_regions { nojoin => 1, destructive => 1 }, map {
343 7 get_regions($self->{filename}, $text, $_);
344 7 } $self->{block}->@*;
345 }
346 elsif (@blocks) { # from matched range
347 my %opt = ( A => $self->{after},
348 B => $self->{before},
349 140 border => [ $self->borders ] );
350 140 my $blocker = smart_blocker(\%opt);
351 merge_regions { nojoin => 1, destructive => 1 }, map {
352 140 [ $blocker->(\%opt, $_->min, $_->max) ]
353 } @blocks;
354 }
355 else {
356 5 ( [ 0, length ] ); # nothing matched
357 }
358 } ];
359 152 $self->discard_borders; # no-op if consumed by borders()
360 152 ▶ while (my($i, $blk) = each @$bp) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$bp) 1267 152 1419
361 1267 bless $blk, 'App::Greple::Grep::Block';
362 # set 1-origined block number in the 3rd entry
363 1267 $blk->number = $i + 1;
364 }
365  
366 ##
367 ## build match table
368 ##
369 152 my @match_table = map { [ 0, 0, [], 0, 0, [], 0, 0, [] ] } @$bp;
370 152 ▶ while (my($ri, $r) = each @result) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @result) 209 152 361
371 209 my $base = $match_base[category($patlist[$ri])];
372 209 my @b = classify_regions({ strict => $self->{strict} }, $r, $bp);
373 209 ▶ while (my($bi, $b) = each @b) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @b) 1370 209 1579
374 1370 my $t = $match_table[$bi];
375 1370 ▶ if (@$b) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$b) { } 377 993 1370
376 377 ${$t}[$base + INDX_POSI]++;
377 377 push @{$t->[$base + INDX_LIST]}, @$b;
378 } else {
379 993 ${$t}[$base + INDX_NEGA]++;
380 }
381 }
382 }
383  
384 152 ▶ show_match_table(\@match_table) if $debug{g};
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::Grep::debug{"g"} 0 152 152
385  
386 152 $self->{MATCH_TABLE} = \@match_table;
387  
388 152 $self;
389 }
390  
391 161 ▶sub compose {
Subroutine compose: called CC=22 SCAR=31.1
Pod undocumented
392 161 my $self = shift;
393 161 my $bp = $self->{BLOCKS};
394 161 my $mp = $self->{MATCH_TABLE};
395  
396 ##
397 ## now it is quite easy to get effective blocks
398 ##
399 161 ▶ my $compromize = $self->{need} < 0 ? abs($self->{need}) : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'need'} < 0 ? : 0 161 161
400 my @effective_index = grep(
401 $mp->[$_][MUST_NEGA] <= $compromize &&
402 $mp->[$_][POSI_POSI] >= $self->{need} &&
403 $mp->[$_][NEGA_POSI] <= $self->{allow},
404 161 ▶ keys @$bp)
Branch: 1 branch
BranchTrueFalseTotal
my(@effective_index) = grep(($mp->[$_][7] <= $compromize && $mp->[$_][0] >= $self->{"need"} && $mp->[$_][3] <= $self->{"allow"}), keys @$bp) or return $self 129 32 161
Condition100%6/6
$mp->[$_][7] <= $compromize && $mp->[$_][0] >= $self->{'need'} && $mp->[$_][3] <= $self->{'allow'}
!ll&&!rl&&r
9634300
$mp->[$_][7] <= $compromize and $mp->[$_][0] >= $self->{"need"}
!ll&&!rl&&r
7956304
MC/DC100%3/3
$mp->[$_][7] <= $compromize && $mp->[$_][0] >= $self->{'need'} && $mp->[$_][3] <= $self->{'allow'}
$mp->[$_][7] <= $compromize $mp->[$_][0] >= $self->{"need"} $mp->[$_][3] <= $self->{'allow'}
Truth table100%4/4
A and B && C
A= $mp->[$_][7] <= $compromizeB= $mp->[$_][0] >= $self->{"need"}C= $mp->[$_][3] <= $self->{'allow'}
ABCresult
0XX0
10X0
1100
1111
405 or return $self;
406  
407 ##
408 ## --matchcount
409 ##
410 129 ▶ if (my $countcheck = $self->{countcheck}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $countcheck = $self->{"countcheck"}) 8 121 129
411 8 ▶ @effective_index = do {
Branch: 1 branch
BranchTrueFalseTotal
@effective_index = do { grep {&$countcheck(int @{$mp->[$_][2]});} @effective_index } or return $self 8 0 8
412 8 grep { $countcheck->(int($mp->[$_][POSI_LIST]->@*)) }
413 @effective_index;
414 }
415 or return $self;
416 }
417  
418 ##
419 ## --block with -ABC option
420 ##
421 129 ▶ if ($self->{block}->@* and ($self->{after} or $self->{before})) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"block"}} and $self->{"after"} || $self->{"before"}) 3 126 129
Condition83%5/6
@{$self->{"block"}} and $self->{"after"} || $self->{"before"}
!ll&&!rl&&r
12243
$self->{"after"} or $self->{"before"}
l!l&&r!l&&!r
304
MC/DC66%2/3
@{$self->{"block"}} and $self->{"after"} || $self->{"before"}
@{$self->{"block"}} $self->{"after"} $self->{"before"}
Truth table75%3/4
A and B or C
A= @{$self->{"block"}}B= $self->{"after"}C= $self->{"before"}
ABCresult
0XX0
1000
11X1
1011
422 3 my @mark;
423 3 for my $i (@effective_index) {
424 9 ▶ map { $mark[$_] = 1 if $_ >= 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $_ >= 0 8 1 9
425 3 $i - $self->{before} .. $i + $self->{after};
426 }
427 3 @effective_index = grep $mark[$_], keys @$bp;
428 }
429  
430 ##
431 ## compose the result
432 ##
433 129 my @list = ();
434 129 for my $bi (@effective_index) {
435 253 my @matched = merge_regions({ nojoin => 1, destructive => 1 },
436 $mp->[$bi][MUST_LIST]->@*,
437 $mp->[$bi][POSI_LIST]->@*,
438 $mp->[$bi][NEGA_LIST]->@*);
439 253 ▶ if ($self->{stretch}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"stretch"}) 1 252 253
440 1 my $b = $bp->[$bi];
441 1 my $m = $matched[0];
442 1 ▶ my $i = min map { $_->[2] // 0 } @matched;
Condition50%1/2
$_->[2] // 0
l!l
20
MC/DC0%0/1
$_->[2] // 0
$_->[2]
Truth table50%1/2
A
A= $_->[2]
Aresult
11
00
443 1 @matched = [ $b->min, $b->max, $i, $m->[3] ];
444 }
445 253 ▶ if ($self->{only}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'only'}) { } 29 224 253
elsif ($self->{'all'}) { } 6 218 224
446 29 push @list, map({ [ $_, $_ ] } @matched);
447 } elsif ($self->{all}) {
448 6 ▶ push @list, [ [ 0, length ] ] if @list == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @list == 0 6 0 6
449 6 push @{$list[0]}, @matched;
450 } else {
451 218 push @list, [ $bp->[$bi], @matched ];
452 }
453 }
454 129 for my $r (@list) {
455 267 bless $r, 'App::Greple::Grep::Result';
456 267 bless $r->block, 'App::Greple::Grep::Block';
457 267 for my $m ($r->matched) {
458 339 bless $m, 'App::Greple::Grep::Match';
459 }
460 }
461  
462 ##
463 ## --join-blocks
464 ##
465 129 ▶ if ($self->{join_blocks} and @list > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"join_blocks"} and @list > 1) 1 128 129
Condition66%2/3
$self->{"join_blocks"} and @list > 1
!ll&&!rl&&r
12801
MC/DC50%1/2
$self->{"join_blocks"} and @list > 1
$self->{"join_blocks"} @list > 1
Truth table66%2/3
A and B
A= $self->{"join_blocks"}B= @list > 1
ABresult
0X0
100
111
466 1 ▶ reduce {
Subroutine __ANON__: called CC=2 SCAR=7.2
467 1 ▶ if ($a->[-1][0]->max == $b->[0]->min) {
Branch: 1 branch
BranchTrueFalseTotal
if ($App::Greple::Grep::a->[-1][0]->max == $App::Greple::Grep::b->[0]->min) { } 1 0 1
468 1 $a->[-1][0]->max = $b->[0]->max;
469 1 push @{$a->[-1]}, splice @$b, 1;
470 } else {
471 0 push @$a, $b;
472 }
473 1 $a;
474 1 } \@list, splice @list, 1;
475 }
476  
477 ##
478 ## ( [ [blockstart, blockend, number ], [start, end], [start, end], ... ],
479 ## [ [blockstart, blockend, number ], [start, end], [start, end], ... ], ... )
480 ##
481 129 $self->{RESULT} = \@list;
482  
483 129 $self;
484 }
485  
486 140 ▶sub borders {
Subroutine borders: called CC=9 SCAR=31.2
Pod undocumented
487 140 my $self = shift;
488 140 local $SIG{ALRM};
489 140 my $alarm_start;
490 140 ▶ if ($self->{alert_size} and length >= $self->{alert_size}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"alert_size"} and length $_ >= $self->{"alert_size"}) 0 140 140
Condition33%1/3
$self->{"alert_size"} and length $_ >= $self->{"alert_size"}
!ll&&!rl&&r
01400
MC/DC0%0/2
$self->{"alert_size"} and length $_ >= $self->{"alert_size"}
$self->{"alert_size"} length $_ >= $self->{"alert_size"}
Truth table33%1/3
A and B
A= $self->{"alert_size"}B= length $_ >= $self->{"alert_size"}
ABresult
0X0
100
111
491 0 $alarm_start = time;
492 0 ▶ $SIG{ALRM} = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
493 0 $SIG{ALRM} = undef;
494 STDERR->printflush(
495 $self->{filename} .
496 0 ": Counting lines, and it may take longer...\n");
497 0 };
498 0 alarm $self->{alert_time};
499 0 ▶ warn "alert timer start ($alarm_start)\n" if $debug{a};
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::Grep::debug{"a"} 0 0 0
500 }
501 140 my @borders = do {
502 140 ▶ if ($self->{BORDERS_CHILD}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'BORDERS_CHILD'}) { } 13 127 140
503 13 my @b = $self->read_borders;
504 13 ▶ @b ? @b : match_borders $self->{border};
Branch: 1 branch
BranchTrueFalseTotal
@b ? : 13 0 13
505 } else {
506 127 match_borders $self->{border};
507 }
508 };
509 140 ▶ if (defined $alarm_start) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $alarm_start) 0 140 140
510 0 ▶ if ($SIG{ALRM}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($SIG{'ALRM'}) { } 0 0 0
511 0 alarm 0;
512 0 ▶ warn "reset alert timer\n" if $debug{a};
Branch: 1 branch
BranchTrueFalseTotal
if $App::Greple::Grep::debug{"a"} 0 0 0
513 } else {
514 0 STDERR->printflush(sprintf("Count %d lines in %d seconds.\n",
515 @borders - 1,
516 time - $alarm_start));
517 }
518 }
519 140 @borders;
520 }
521  
522 160 ▶sub result_ref {
Subroutine result_ref: called CC=1 SCAR=0.0
Pod documented
523 160 my $obj = shift;
524 160 $obj->{RESULT};
525 }
526  
527 417 ▶sub result {
Subroutine result: called CC=1 SCAR=0.0
Pod documented
528 417 my $obj = shift;
529 417 @{ $obj->{RESULT} };
530 }
531  
532 161 ▶sub matched {
Subroutine matched: called CC=2 SCAR=6.9
Pod documented
533 161 my $obj = shift;
534 161 ▶ sum(map { $_->@* - 1 } $obj->result) // 0;
Condition100%2/2
&sum(map({@$_ - 1;} $obj->result)) // 0
l!l
12932
MC/DC100%1/1
&sum(map({@$_ - 1;} $obj->result)) // 0
&sum(map({@$_ - 1;} $obj->result))
Truth table100%2/2
A
A= &sum(map({@$_ - 1;} $obj->result))
Aresult
11
00
535 }
536  
537 0 ▶sub blocks {
Subroutine blocks: not called CC=1 SCAR=6.9
Pod documented
538 0 my $obj = shift;
539 0 $obj->{BLOCKS}->@*;
540 }
541  
542 ##
543 ## Cut all result blocks (and gaps between them) from the text in a
544 ## single pass. Returns ( gap0, block0, gap1, block1, ..., rest ),
545 ## or an empty list when not applicable. Character based substr on
546 ## a large utf8 string requires linear scan for every call to convert
547 ## character offset to byte offset, so cutting each block separately
548 ## makes the output routine quadratic.
549 ##
550 127 ▶sub slice_blocks {
Subroutine slice_blocks: called CC=4 SCAR=13.9
Pod undocumented
551 127 my $self = shift;
552 127 my @blocks = map { $_->block } $self->result;
553 127 ▶ return () if @blocks < 2;
Branch: 1 branch
BranchTrueFalseTotal
if @blocks < 2 83 44 127
554 44 my $pos = 0;
555 44 for (@blocks) {
556 180 ▶ return () if $_->[0] < $pos; # not in order
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] < $pos 0 180 180
557 180 $pos = $_->[1];
558 }
559 44 my $template = unpack_template(\@blocks, 0);
560 44 unpack $template, ${ $self->{text} };
561 }
562  
563 263 ▶sub slice_result {
Subroutine slice_result: called CC=2 SCAR=6.9
Pod documented
564 263 my $obj = shift;
565 263 my $result = shift;
566 263 my($block, @list) = @$result;
567 263 ▶ my $text = @_ ? shift : $obj->cut(@$block);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 180 83 263
568 263 my $template = unpack_template(\@list, $block->min);
569 263 unpack($template, $text);
570 }
571  
572 0 ▶sub slice_index {
Subroutine slice_index: not called CC=1 SCAR=6.9
Pod undocumented
573 0 my $obj = shift;
574 0 my $result = shift;
575 0 my($block, @list) = @$result;
576 0 map { $_ * 2 + 1 } keys @list;
577 }
578  
579 307 ▶sub unpack_template {
Subroutine unpack_template: called CC=3 SCAR=11.0
Pod undocumented
580 ##
581 ## make template to split result text into matched and unmatched parts
582 ##
583 307 my($matched, $offset) = @_;
584 307 my @len;
585 307 for (@$matched) {
586 516 my($s, $e) = @$_;
587 516 ▶ $s = $offset if $s < $offset;
Branch: 1 branch
BranchTrueFalseTotal
if $s < $offset 3 513 516
588 516 push @len, $s - $offset, $e - $s;
589 516 $offset = $e;
590 }
591 307 join '', map "a$_", @len, '*';
592 }
593  
594 0 ▶sub show_match_table {
Subroutine show_match_table: not called CC=2 SCAR=17.9
Pod undocumented
595 0 my $table = shift;
596 0 local $Data::Dumper::Terse = 1;
597 0 ▶ while (my($i, $e) = each @$table) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each @$table) 0 0 0
598 0 printf STDERR
599 "%4d %s", $i++, Dumper($e) =~ s/\s+(?!$)/ /gsr;
600 }
601 }
602  
603 18 ▶sub get_regions {
Subroutine get_regions: called CC=2 SCAR=7.2
Pod undocumented
604 18 my $file = shift;
605 18 local *_ = shift;
606 18 my $pattern = shift;
607  
608 ## func object
609 18 ▶ if (callable $pattern) {
Branch: 1 branch
BranchTrueFalseTotal
if (callable($pattern)) { } 0 18 18
610 0 $pattern->call(&FILELABEL => $file);
611 }
612 ## pattern
613 else {
614 18 match_regions(pattern => $pattern);
615 }
616 }
617  
618 140 ▶sub smart_blocker {
Subroutine smart_blocker: called CC=3 SCAR=11.0
Pod undocumented
619 140 my $opt = shift;
620 140 ▶ return \&blocker if $opt->{A} or $opt->{B};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"A"} or $opt->{"B"} 6 134 140
Condition100%3/3
$opt->{"A"} or $opt->{"B"}
l!l&&r!l&&!r
51134
MC/DC100%2/2
$opt->{"A"} or $opt->{"B"}
$opt->{"A"} $opt->{"B"}
Truth table100%3/3
A or B
A= $opt->{"A"}B= $opt->{"B"}
ABresult
1X1
011
000
621 134 my $from = my $to = -1;
622 570 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
623 570 ▶ if ($from <= $_[1] and $_[2] < $to) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from <= $_[1] and $_[2] < $to) 203 367 570
Condition100%3/3
$from <= $_[1] and $_[2] < $to
!ll&&!rl&&r
6361203
MC/DC100%2/2
$from <= $_[1] and $_[2] < $to
$from <= $_[1] $_[2] < $to
Truth table100%3/3
A and B
A= $from <= $_[1]B= $_[2] < $to
ABresult
0X0
100
111
624 203 return($from, $to);
625 }
626 367 ($from, $to) = &blocker;
627 }
628 134 }
629  
630 171 ▶use List::BinarySearch qw(binsearch_pos);
Subroutine BEGIN: called CC=1 SCAR=0.0
631  
632 375 ▶sub blocker {
Subroutine blocker: called CC=6 SCAR=17.9
Pod undocumented
633 375 my($opt, $from, $to) = @_;
634 375 my $border = $opt->{border};
635  
636 375 ▶ my $bi = binsearch_pos { $a <=> $b } $from, @$border;
Subroutine __ANON__: called CC=1 SCAR=0.0
637 375 ▶ $bi-- if $border->[$bi] != $from;
Branch: 1 branch
BranchTrueFalseTotal
if $border->[$bi] != $from 266 109 375
638 375 ▶ $bi = max 0, $bi - $opt->{B} if $opt->{B};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"B"} 6 369 375
639  
640 375 ▶ my $ei = binsearch_pos { $a <=> $b } $to, @$border;
Subroutine __ANON__: called CC=1 SCAR=0.0
641 375 ▶ $ei++ if $ei == $bi and $ei < $#{$border};
Branch: 1 branch
BranchTrueFalseTotal
if $ei == $bi and $ei < $#{$border;} 56 319 375
Condition100%3/3
$ei == $bi and $ei < $#{$border;}
!ll&&!rl&&r
317256
MC/DC100%2/2
$ei == $bi and $ei < $#{$border;}
$ei == $bi $ei < $#{$border;}
Truth table100%3/3
A and B
A= $ei == $biB= $ei < $#{$border;}
ABresult
0X0
100
111
642 375 ▶ $ei = min $#{$border}, $ei + $opt->{A} if $opt->{A};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"A"} 7 368 375
643  
644 375 @$border[ $bi, $ei ];
645 }
646  
647 171 package App::Greple::Text {
648 171 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
649 171 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
650 0 ▶ use overload '""' => sub { ${ $_[0]->{text} } };
Subroutine BEGIN: called CC=1 SCAR=0.6
Subroutine __ANON__: not called CC=1 SCAR=0.6
651 0 ▶ sub new {
Subroutine new: not called CC=1 SCAR=6.9
652 0 my $class = shift;
653 0 bless { text => \$_[0] }, $class;
654 }
655 0 ▶ sub text { ${ $_[0]->{text} } }
Subroutine text: not called CC=1 SCAR=6.9
656 171 ▶ sub cut {
Subroutine cut: called CC=1 SCAR=0.0
657 171 my($obj, $from, $to) = @_;
658 171 substr ${ $obj->{text} }, $from, $to - $from;
659 }
660 }
661  
662 171 1;
663  
664 __END__
« script/greple ↑ Summary lib/App/Greple/debug.pm »