blib/lib/Regexp/Log/DateRange.pm

filter: Statementstmt 96.8% 91 / 94 Branchbran 84.3% 54 / 64 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 5 totaltotal 89.9% 161 / 179 CC 37 SCAR 23.0
CC
37
Cov
92%
new
13.2
range2tree
13.0
match_range
8.0
CRAP 10.0
↑ Summary
1 # $Id: DateRange.pm,v 1.2 2005/04/19 15:33:02 dk Exp $
2  
3 package Regexp::Log::DateRange;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use vars qw($VERSION %templates);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 $VERSION = '0.02';
8  
9 1 %templates = (
10 syslog => [
11 [ '\\s+', 1, 12, [ qw(. jan feb mar apr may jun jul aug sep oct nov dec)]],
12 [ '\\s+', 1, 31, undef, '0?'],
13 [ '\\:', 0, 23, undef, '0?' ],
14 [ '\\:', 0, 59, undef, '0?' ],
15 ],
16 );
17  
18 sub new
19 200 ▶{
Subroutine new: called CC=12 SCAR=25.8
Pod undocumented
20 200 my ( $class, $template, $date1, $date2) = @_;
21  
22 200 ▶ unless ( ref($template)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $template) 200 0 200
23 die "Template '$template' doesn't exist\n"
24 200 ▶ unless $templates{$template};
Branch: 1 branch
BranchTrueFalseTotal
unless $Regexp::Log::DateRange::templates{$template} 0 200 200
25 200 $template = $templates{$template};
26 }
27  
28 # some sanity checks
29 200 my $n = @$template;
30 200 ▶ die "template is empty\n" unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 200 200
31 200 ▶ die "date [@$date1] is not valid\n" unless $n == @$date1;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$date1 0 200 200
32 200 ▶ die "date [@$date2] is not valid\n" unless $n == @$date2;
Branch: 1 branch
BranchTrueFalseTotal
unless $n == @$date2 0 200 200
33  
34 200 ▶ for ( my $i = 0; $i < $n; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $n) 216 0 216
35 216 ▶ next if $date1->[$i] == $date2->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $date1->[$i] == $date2->[$i] 16 200 216
36 200 ▶ last if $date1->[$i] < $date2->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $date1->[$i] < $date2->[$i] 200 0 200
37 0 ( $date2, $date1) = ( $date1, $date2);
38 0 last;
39 }
40
41 # build 'alignment' vectors; for example, for the right range,
42 # 2499 would give 0111, for the left, 1150 would give 0001
43 200 my ( @w1, @w2);
44 200 my ( $last1, $last2) = ( 1, 1);
45 200 ▶ for ( my $i = $#$date1; $i > 0; $i--) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i > 0) 600 200 800
46 600 ▶ $w1[$i-1] = $last1 & (( $date1->[$i] == $template->[$i]->[1] ) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$date1->[$i] == $template->[$i][1] ? : 310 290 600
47 600 ▶ $w2[$i-1] = $last2 & (( $date2->[$i] == $template->[$i]->[2] ) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$date2->[$i] == $template->[$i][2] ? : 307 293 600
48 600 $last1 = $w1[$i-1];
49 600 $last2 = $w2[$i-1];
50 }
51 200 my $tree = range2tree( $template, $date1, $date2, \@w1, \@w2, 0);
52 200 return tree2re( $template, $tree, 0);
53 }
54  
55 # 1,2,3 => (?:1|2|3)
56 sub re_group
57 1957 ▶{
Subroutine re_group: called CC=3 SCAR=11.4
Pod undocumented
58 1957 ▶ if ( 0 == @_) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == @_) { } 0 1957 1957
elsif (1 == @_) { } 805 1152 1957
59 0 return '';
60 } elsif ( 1 == @_) {
61 805 return $_[0];
62 } else {
63 1152 return '(?:'.join('|', @_).')';
64 }
65 }
66  
67 # 8 .. 13 => (?:0?8|9)|1[0123]
68 sub match_range
69 979 ▶{
Subroutine match_range: called CC=8 SCAR=20.8
Pod undocumented
70 979 my ( $from, $to, $digit_prefix) = @_;
71  
72 979 my @tens;
73 979 for my $x ( $from .. $to) {
74 11143 my $ten = int( $x / 10);
75 11143 ▶ unless ( defined $tens[$ten]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tens[$ten]) { } 9267 1876 11143
76 1876 my $mod = int( $x % 10);
77 1876 $tens[$ten] = [ $mod, $mod];
78 } else {
79 9267 $tens[$ten]->[1]++;
80 }
81 }
82
83 979 my @q;
84 979 my $last_range = '';
85 979 my @branges;
86 979 ▶ for ( my $i = 0; $i < @tens; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @tens) 2645 979 3624
87 2645 ▶ next unless defined $tens[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tens[$i] 769 1876 2645
88 1876 ▶ my $range = ( $tens[$i]->[0] == $tens[$i]->[1] ) ?
Branch: 1 branch
BranchTrueFalseTotal
$tens[$i][0] == $tens[$i][1] ? : 457 1419 1876
89 $tens[$i]->[0] :
90 "[$tens[$i]->[0]-$tens[$i]->[1]]";
91 1876 ▶ if ( $i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i) { } 1327 549 1876
92 1327 ▶ if ( $range eq $last_range) {
Branch: 1 branch
BranchTrueFalseTotal
if ($range eq $last_range) { } 282 1045 1327
93 282 push @branges, $i;
94 282 $q[-1] = "[$branges[0]-$branges[-1]]$range";
95 } else {
96 1045 $last_range = $range;
97 1045 push @q, "$i$range";
98 1045 @branges = ($i);
99 }
100 } else {
101 549 push @q, "$digit_prefix$range";
102 }
103 }
104  
105
106 979 my $ret = re_group(@q);
107 979 $ret =~ s/\[0-9\]/\\d/g;
108 979 return $ret;
109 }
110  
111 # Convert date range into a max-3-branch tree, where each branch is an alternative
112 # expansion rule, and is either a range or a value leaf; the value leaves can
113 # point deeper. For example, if matching date range 1 Apr - 3 June, the corresponding
114 # structure would be something like
115 #
116 # range (Apr-May)
117 # value (June,
118 # range(1-3)
119 # )
120 #
121 sub range2tree
122 794 ▶{
Subroutine range2tree: called CC=13 SCAR=25.7
Pod undocumented
123 794 my ( $template, $d1, $d2, $w1, $w2, $depth) = @_;
124  
125 794 my ( $i, $left, $center, $right);
126 794 my ( $r1, $r2) = ( $d1-> [$depth], $d2-> [$depth]);
127  
128 # print +(' ' x $depth), "$depth: $r1 $r2\n";
129  
130 794 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if ($w1->[$depth] and $w2->[$depth] or $depth >= $d1->$#*) { } 200 594 794
elsif ($r1 < $r2) { } 569 25 594
Condition100%6/6
$w1->[$depth] and $w2->[$depth] or $depth >= $d1->$#*
l!l&&r!l&&!r
5195594
$w1->[$depth] and $w2->[$depth]
!ll&&!rl&&r
3933965
MC/DC100%3/3
$w1->[$depth] and $w2->[$depth] or $depth >= $d1->$#*
$w1->[$depth] $w2->[$depth] $depth >= $d1->$#*
Truth table100%5/5
A and B or C
A= $w1->[$depth]B= $w2->[$depth]C= $depth >= $d1->$#*
ABCresult
11X1
0X11
1011
0X00
1000
131 ( $w1->[$depth] and $w2->[$depth])
132 or $depth >= $#$d1
133 ) {
134 200 $center = {
135 range => [ $r1 , $r2 ],
136 };
137 # print +(' ' x $depth), "T\n";
138 } elsif ( $r1 < $r2) {
139 569 my ( @d1, @d2);
140 # if, say, in '123' vs '145', '2' < '4' where depth = 1,
141 # then d1 = 129 and d2 = 140
142 569 ▶ for ( $i = 0; $i <= $depth; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $depth) 1145 569 1714
143 1145 $d1[$i] = $d1->[$i];
144 1145 $d2[$i] = $d2->[$i];
145 }
146 569 ▶ for ( $i = $depth + 1; $i < @$d1; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$d1) 1131 569 1700
147 1131 $d1[$i] = $template-> [$i]->[2];
148 1131 $d2[$i] = $template-> [$i]->[1];
149 }
150 569 ▶ if ( $w1->[$depth]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w1->[$depth]) { } 285 284 569
151 285 $r1--;
152 # print +(' ' x $depth), "LT\n";
153 } else {
154 # print +(' ' x $depth), "$depth L > @$d1 : @d1 [@$w1]\n";
155 284 $left = {
156 next => range2tree( $template, $d1, \@d1, $w1, [(1) x @d1], $depth + 1),
157 value => $r1,
158 };
159 # print +(' ' x $depth), "$depth L <\n";
160 }
161  
162 569 ▶ if ( $w2->[$depth]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w2->[$depth]) { } 284 285 569
163 # print +(' ' x $depth), "RT\n";
164 284 $r2++;
165 } else {
166 # print +(' ' x $depth), "$depth R > @d2 : @$d2 [@$w2]\n";
167 285 $right = {
168 next => range2tree( $template, \@d2, $d2, [(1) x @d2], $w2, $depth + 1),
169 value => $r2,
170 };
171 # print +(' ' x $depth), "$depth R <\n";
172 }
173 569 ▶ if ( $r1 + 1 < $r2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r1 + 1 < $r2) 569 0 569
174 569 $center = {
175 range => [ $r1 + 1 , $r2 - 1 ],
176 };
177 # print +(' ' x $depth), "$depth CT [ ", $r1+1, ' .. ', $r2-1, " ]\n";
178 }
179 } else {
180 25 $center = {
181 next => range2tree( $template, $d1, $d2, $w1, $w2, $depth + 1),
182 value => $r1,
183 }
184 }
185  
186 return [
187 794 ▶ $left ? $left : (),
Branch: 3 branches
BranchTrueFalseTotal
$left ? : 284 510 794
$center ? : 794 0 794
$right ? : 285 509 794
188 $center ? $center : (),
189 $right ? $right : ()
190 ];
191 }
192  
193 # converts a tree into a regexp
194 sub tree2re
195 794 ▶{
Subroutine tree2re: called CC=5 SCAR=16.1
Pod undocumented
196 794 my ( $template, $tree, $depth) = @_;
197 794 my @q;
198 794 my $t = $template-> [$depth];
199 794 for my $hash ( @$tree) {
200 1363 ▶ if ( exists $hash-> {value}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{'value'}) { } 594 769 1363
201 my $v = $t->[3] ?
202 $t->[3]->[$hash->{value}] :
203 594 ▶ match_range( $hash->{value}, $hash->{value}, $t->[4]);
Branch: 1 branch
BranchTrueFalseTotal
$t->[3] ? : 200 394 594
204 push @q, $v .
205 $t->[0] .
206 594 tree2re( $template, $hash-> {next}, $depth + 1);
207 } else {
208 my $r = $t->[3] ?
209 1043 re_group( map { $t->[3]->[$_] }
210 $hash-> {range}-> [0] .. $hash-> {range}-> [1] ) :
211 769 ▶ match_range( @{$hash-> {range}}, $t->[4]);
Branch: 1 branch
BranchTrueFalseTotal
$t->[3] ? : 184 585 769
212 769 push @q, $r . $t->[0];
213 }
214 }
215  
216 794 return re_group(@q);
217 }
218  
219 1 1;
220  
221 __END__
↑ Summary