blib/lib/DDC/Format/Kwic.pm

filter: Statementstmt 13.4% 12 / 89 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 44.4% 4 / 9 Podpod 60.0% 3 / 5 totaltotal 10.0% 19 / 189 CC 33 SCAR 61.1
CC
33
Cov
8%
toString
702.0
wsStr
30.0
maxlen
12.0
CRAP 450.1
↑ Summary blib/lib/DDC/Client.pm »
1 ##-*- Mode: CPerl -*-
2  
3 ## File: DDC::Format::Kwic.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description:
6 ## + DDC Query utilities: output formatting: keywords-in-context
7 ##======================================================================
8  
9 package DDC::Format::Kwic;
10 26 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 26 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 26 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 ##======================================================================
15 ## Globals
16 26 our @ISA = qw(DDC::Format);
17  
18 26 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 26 *isa = \&UNIVERSAL::isa;
20 }
21  
22 ##======================================================================
23 ## Constructors, etc.
24  
25 ## $fmt = $CLASS_OR_OBJ->new(%args)
26 ## + %args:
27 ## (
28 ## start=>$previous_hit_num, ##-- pre-initial hit number (default=0)
29 ## highlight=>[$pre,$post], ##-- highlighting substrings
30 ## width=>$nchars, ##-- context width; default=32
31 ## useMatchIds=>$bool, ##-- whether to use match-ids if available; undef (default) if non-trivial match-ids are specified
32 ## wsAttr=>$wskey, ##-- token attribute to use for preceding whitespace (default='ws')
33 ## )
34 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
35 0 my $that = shift;
36 0 ▶ return bless {
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
37 highlight=>['__','__'],
38 width=>32,
39 useMatchIds=>undef,
40 wsAttr=>'ws',
41 @_
42 }, ref($that)||$that;
43 }
44  
45 ## $fmt = $fmt->reset()
46 ## + reset counters, etc.
47 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
48 0 $_[0]{start}=0;
49 0 return $_[0]->SUPER::reset();
50 }
51  
52 ##======================================================================
53 ## Helper functions
54  
55 ## $len = maxlen(@strings)
56 0 ▶sub maxlen {
Subroutine maxlen: not called CC=3 SCAR=24.8
Pod undocumented
57 0 my $l = 0;
58 0 ▶ do { $l=length($_) if (length($_) > $l) } foreach (@_);
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $l 0 0 0
59 0 return $l;
60 }
61  
62 ## $wsStr = $fmt->wsStr($token)
63 0 ▶sub wsStr {
Subroutine wsStr: not called CC=5 SCAR=34.0
Pod undocumented
64 0 my ($fmt,$w) = @_;
65 0 ▶ return (!$fmt->{wsAttr} || !ref($w) || !defined($w->{$fmt->{wsAttr}}) || $w->{$fmt->{wsAttr}} ? ' ' : '');
Branch: 1 branch
BranchTrueFalseTotal
!($fmt->{'wsAttr'} && ref $w && defined $$w{$fmt->{'wsAttr'}}) || $$w{$fmt->{'wsAttr'}} ? : 0 0 0
Condition0%0/9
not $fmt->{"wsAttr"} && ref $w && defined $$w{$fmt->{"wsAttr"}} or $$w{$fmt->{"wsAttr"}}
l!l&&r!l&&!r
000
$fmt->{'wsAttr'} && ref $w && defined $$w{$fmt->{'wsAttr'}}
!ll&&!rl&&r
000
$fmt->{"wsAttr"} and ref $w
!ll&&!rl&&r
000
MC/DC0%0/4
not $fmt->{"wsAttr"} && ref $w && defined $$w{$fmt->{"wsAttr"}} or $$w{$fmt->{"wsAttr"}}
$fmt->{"wsAttr"} ref $w defined $$w{$fmt->{'wsAttr'}} $$w{$fmt->{"wsAttr"}}
Truth table0%0/5
not(A and B && C) or D
A= $fmt->{"wsAttr"}B= ref $wC= defined $$w{$fmt->{'wsAttr'}}D= $$w{$fmt->{"wsAttr"}}
ABCDresult
0XXX1
10XX1
110X1
11111
11100
66 }
67  
68 ##======================================================================
69 ## API
70  
71 ## $str = $fmt->toString($hitList)
72 0 ▶sub toString {
Subroutine toString: not called CC=26 SCAR=65.5
Pod documented
73 0 my ($fmt,$hits) = @_;
74  
75 0 ▶ if ($hits->{counts_} && @{$hits->{counts_}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hits->{"counts_"} and @{$hits->{"counts_"};}) 0 0 0
Condition0%0/3
$hits->{"counts_"} and @{$hits->{"counts_"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$hits->{"counts_"} and @{$hits->{"counts_"};}
$hits->{"counts_"} @{$hits->{"counts_"};}
Truth table0%0/3
A and B
A= $hits->{"counts_"}B= @{$hits->{"counts_"};}
ABresult
0X0
100
111
76 ##-- count-query: format as text
77 0 my ($i);
78 0 my @lens = map {$i=$_; maxlen(map {$_->[$i]} @{$hits->{counts_}})} (0..$#{$hits->{counts_}[0]});
79 0 my $fmt = join("\t", map {"%-${_}s"} @lens)."\n";
80 0 return join('', map {sprintf($fmt,@$_)} @{$hits->{counts_}});
81 }
82  
83 0 ▶ my $xlen = $fmt->{width} || 2**31;
Condition0%0/2
$fmt->{'width'} || 2147483648.0
l!l
00
MC/DC0%0/1
$fmt->{'width'} || 2147483648.0
$fmt->{'width'}
Truth table0%0/2
A
A= $fmt->{'width'}
Aresult
11
00
84 0 my $hnum = $hits->{start};
85 0 ▶ my $useMatchIds = defined($fmt->{useMatchIds}) ? $fmt->{useMatchIds} : (grep {$_>0 && $_!=1} map {$_->{hl_}} map {@{$_->{ctx_}[1]}} @{$hits->{hits_}});
Branch: 1 branch
BranchTrueFalseTotal
defined $fmt->{'useMatchIds'} ? : 0 0 0
Condition0%0/3
$_ > 0 && $_ != 1
!ll&&!rl&&r
000
MC/DC0%0/2
$_ > 0 && $_ != 1
$_ > 0 $_ != 1
Truth table0%0/3
A && B
A= $_ > 0B= $_ != 1
ABresult
0X0
100
111
86  
87 0 my (@hits);
88 0 foreach my $hit (@{$hits->{hits_}}) {
89 ##-- hit key: number + file basename + page
90 0 my $f = basename($hit->{meta_}{file_});
91 0 $f =~ s/\..*$//;
92 0 ▶ my $p = defined($hit->{meta_}{page_}) ? $hit->{meta_}{page_} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $hit->{'meta_'}{'page_'} ? : 0 0 0
93 0 my $pagei = (grep {$hit->{meta_}{indices_}[$_] eq 'page'} (0..$#{$hit->{meta_}{indices_}}))[0];
94 0 ▶ my $targetMatchId = $useMatchIds ? (sort {$a<=>$b} grep {$_} map {$_->{hl_}} @{$hit->{ctx_}[1]})[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$useMatchIds ? : 0 0 0
95  
96 ##-- hit context
97 0 ▶ my $fkey = $hits->{defaultField} || $hit->{meta_}{indices_}[0] || 'w';
Condition0%0/5
$hits->{'defaultField'} || $hit->{'meta_'}{'indices_'}[0] || 'w'
l!l
00
$hits->{"defaultField"} or $hit->{"meta_"}{"indices_"}[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$hits->{'defaultField'} || $hit->{'meta_'}{'indices_'}[0] || 'w'
$hits->{"defaultField"} $hit->{"meta_"}{"indices_"}[0]
Truth table0%0/3
A or B
A= $hits->{"defaultField"}B= $hit->{"meta_"}{"indices_"}[0]
ABresult
1X1
011
000
98 0 my (@l,@c,@r);
99 0 my $ary = \@l;
100 0 my $hl = '__';
101 0 foreach (map {@$_} @{$hit->{ctx_}}) {
102 0 ▶ if ($ary eq \@l && ref($_) && ($useMatchIds ? $_->{hl_}==$targetMatchId : $_->{hl_})) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ary eq \@l and ref $_ and $useMatchIds ? $_->{'hl_'} == $targetMatchId : $_->{'hl_'}) { } 0 0 0
elsif ($ary eq \@c) { } 0 0 0
$useMatchIds ? : 0 0 0
Condition0%0/6
$ary eq \@l and ref $_ and $useMatchIds ? $_->{"hl_"} == $targetMatchId : $_->{"hl_"}
!ll&&!rl&&r
000
$ary eq \@l and ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
$ary eq \@l and ref $_ and $useMatchIds ? $_->{"hl_"} == $targetMatchId : $_->{"hl_"}
$ary eq \@l ref $_ $useMatchIds ? $_->{"hl_"} == $targetMatchId : $_->{"hl_"}
Truth table0%0/4
A and B and C
A= $ary eq \@lB= ref $_C= $useMatchIds ? $_->{"hl_"} == $targetMatchId : $_->{"hl_"}
ABCresult
0XX0
10X0
1100
1111
103 0 $ary=\@c;
104 0 ▶ $p=$_->{page} if (!$p && defined($_->{page}));
Branch: 1 branch
BranchTrueFalseTotal
if not $p and defined $_->{"page"} 0 0 0
Condition0%0/3
not $p and defined $_->{"page"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $p and defined $_->{"page"}
not $p defined $_->{"page"}
Truth table0%0/3
not(A) and B
A= not $pB= defined $_->{"page"}
ABresult
0X0
100
111
105 }
106 elsif ($ary eq \@c) {
107 0 $ary = \@r;
108 }
109 0 ▶ $hl = ($ary eq \@c ? '__' : '_');
Branch: 1 branch
BranchTrueFalseTotal
$ary eq \@c ? : 0 0 0
110 push(@$ary, ($fmt->wsStr($_)
111 .(ref($_)
112 ? ($_->{hl_}
113 ? ($hl.$_->{$fkey}.$hl.($useMatchIds ? "/$_->{hl_}" : ''))
114 0 ▶ : $_->{$fkey})
Branch: 3 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
$_->{'hl_'} ? : 0 0 0
$useMatchIds ? : 0 0 0
115 : $_)));
116 }
117  
118 0 my $ls = join('', @l);
119 0 my $rs = join('', @r);
120 0 ▶ substr($ls, 0, length($ls)-$xlen+3, '...') if (length($ls) > $xlen);
Branch: 1 branch
BranchTrueFalseTotal
if length $ls > $xlen 0 0 0
121 0 ▶ substr($rs, $xlen-3, length($rs)-$xlen+3, '...') if (length($rs) > $xlen);
Branch: 1 branch
BranchTrueFalseTotal
if length $rs > $xlen 0 0 0
122  
123 0 push(@hits,[$hnum++, "[$f:$p]", $ls, join('',@c), $rs]);
124 }
125  
126 0 my $ln = maxlen(map {$_->[0]} @hits);
127 0 my $lf = maxlen(map {$_->[1]} @hits);
128 0 my $ll = maxlen(map {$_->[2]} @hits);
129 0 my $lc = maxlen(map {$_->[3]} @hits);
130 0 my $lr = maxlen(map {$_->[4]} @hits);
131 return (
132 "# Hit(s) $hits[0][0]-$hits[$#hits][0] of $hits->{nhits_}"
133 .($hits->{hint_} ? " <HINT: $hits->{hint_}>" : '')
134 ."\n"
135 0 ▶ .join('', map {sprintf("%${ln}d: %-${lf}s %${ll}s %-${lc}s %-${lr}s\n", @$_)} @hits)
Branch: 1 branch
BranchTrueFalseTotal
$hits->{'hint_'} ? : 0 0 0
136 );
137 }
138  
139 26 1; ##-- be happy
140  
141 __END__
↑ Summary blib/lib/DDC/Client.pm »