Range/Gather.pm

filter: Statementstmt 15.5% 40 / 258 Branchbran 0.0% 0 / 132 Conditioncond 0.0% 0 / 37 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 44.4% 12 / 27 Podpod 0.0% 0 / 6 totaltotal 10.8% 52 / 480 CC 81 SCAR 67.1
CC
81
Cov
9%
_gather
1892.0
flatten
306.0
unpack_range_size
72.0
CRAP 819.5
↑ Summary Range/Split.pm »
1  
2 package Data::HexDump::Range ; ## no critic (Modules::RequireFilenameMatchesPackage)
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp ;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 BEGIN
9 {
10  
11 2 ▶use Sub::Exporter -setup =>
Subroutine BEGIN: called CC=1 SCAR=0.0
12 {
13 exports => [ qw() ],
14 groups =>
15 {
16 all => [ qw() ],
17 }
18 2 };
19
20 2 ▶use vars qw ($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
21 }
22  
23 #-------------------------------------------------------------------------------
24  
25 2 ▶use English qw( -no_match_vars ) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 2 ▶use Readonly ;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 2 Readonly my $EMPTY_STRING => q{} ;
29 2 Readonly my $SCALAR_TYPE=> q{} ;
30  
31 2 Readonly my $RANGE_DEFINITON_FIELDS => 4 ;
32  
33 2 ▶use Carp qw(carp croak confess) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 #-------------------------------------------------------------------------------
36  
37 =head1 NAME
38  
39 Data::HexDump::Range::Gather - Handles gathering of binary data for Data::HexDump::Range
40  
41 =head1 SUBROUTINES/METHODS
42  
43 Subroutines prefixed with B<[P]> are not part of the public API and shall not be used directly.
44  
45 =cut
46  
47 #-------------------------------------------------------------------------------
48  
49 sub _gather
50 0 ▶{
Subroutine _gather: not called CC=43 SCAR=75.5
51  
52 =head2 [P] _gather($range_description, $data, $offset, $size)
53  
54 Creates an internal data structure from the data to dump.
55  
56 $hdr->_gather($container, $range_description, $data, $size)
57  
58 I<Arguments> - See L<gather>
59  
60 =over 2
61  
62 =item * $container - an array reference or undef - where the gathered data
63  
64 =item * $range_description - See L<gather>
65  
66 =item * $data - See L<gather>
67  
68 =item * $offset - See L<gather>
69  
70 =item * $size - See L<gather>
71  
72 =back
73  
74 I<Returns> -
75  
76 =over 2
77  
78 =item * $container - the gathered data
79  
80 =item * $used_data - integer - the location in the data where the dumping ended
81  
82 =back
83  
84 I<Exceptions> dies if passed invalid parameters
85  
86 =cut
87  
88 0 my ($self, $collected_data, $range_description, $data, $offset, $size) = @_ ;
89  
90 0 my $location = "$self->{FILE}:$self->{LINE}" ;
91  
92 0 ▶my $used_data = $offset || 0 ;
Condition0%0/2
$offset || 0
l!l
00
MC/DC0%0/1
$offset || 0
$offset
Truth table0%0/2
A
A= $offset
Aresult
11
00
93 0 ▶$self->{INTERACTION}{DIE}("Error: Invalid negative offset at '$location'.\n") if($used_data < 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $used_data < 0 0 0 0
94  
95 0 my $data_size = length($data) ;
96  
97 0 ▶$self->{INTERACTION}{DIE}("Error: offset greater than data size at '$location'.\n") if($data_size <= $used_data) ;
Branch: 1 branch
BranchTrueFalseTotal
if $data_size <= $used_data 0 0 0
98  
99 0 ▶$size = defined $size ? min($size, $data_size - $used_data) : $data_size - $used_data ;
Branch: 1 branch
BranchTrueFalseTotal
defined $size ? : 0 0 0
100 0 ▶$self->{INTERACTION}{DIE}("Error: Invalid negative size at '$location'.\n") if($size < 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $size < 0 0 0 0
101 0 ▶$self->{INTERACTION}{DIE}("Error: Invalid size '0' at '$location'.\n") if($size == 0) ;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 0 0
102  
103 0 my $skip_remaining_ranges = 0 ;
104 0 my $last_data = '' ;
105  
106 0 my $range_provider = $self->create_range_provider($range_description);
107  
108 0 ▶while(my $range = $range_provider->($self, $data, $used_data))
Branch: 1 branch
BranchTrueFalseTotal
if (my $range = &$range_provider($self, $data, $used_data)) 0 0 0
109 {
110 0 ▶ if($self->{DUMP_ORIGINAL_RANGE_DESCRIPTION})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DUMP_ORIGINAL_RANGE_DESCRIPTION"}) 0 0 0
111 {
112 $self->{INTERACTION}{INFO}
113 (
114 0 DumpTree $range, 'Original range description', QUOTE_VALUES => 1, DISPLAY_ADDRESS => 0, DISPLAY_PERL_DATA => 1, DISPLAY_INHERITANCE => 1
115 ) ;
116 }
117
118 0 ▶ if('CODE' eq ref($range->[0]) && ! defined $range->[1] && ! defined $range->[2] && ! defined $range->[3]) # eek!
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $range->[0] and not defined $range->[1] and not defined $range->[2] and not defined $range->[3]) 0 0 0
Condition0%0/9
"CODE" eq ref $range->[0] and not defined $range->[1] and not defined $range->[2] and not defined $range->[3]
!ll&&!rl&&r
000
"CODE" eq ref $range->[0] and not defined $range->[1] and not defined $range->[2]
!ll&&!rl&&r
000
"CODE" eq ref $range->[0] and not defined $range->[1]
!ll&&!rl&&r
000
MC/DC0%0/4
"CODE" eq ref $range->[0] and not defined $range->[1] and not defined $range->[2] and not defined $range->[3]
"CODE" eq ref $range->[0] not defined $range->[1] not defined $range->[2] not defined $range->[3]
Truth table0%0/5
A and not(B) and not(C) and not(D)
A= "CODE" eq ref $range->[0]B= not defined $range->[1]C= not defined $range->[2]D= not defined $range->[3]
ABCDresult
0XXX0
10XX0
110X0
11100
11111
119 {
120 0 my ($range_from_sub, $comment) = $range->[0]($self, $data, $used_data) ;
121
122 0 ▶ if(defined $range_from_sub)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range_from_sub) { } 0 0 0
123 {
124 0 ▶ if('ARRAY' eq ref($range))
Branch: 1 branch
BranchTrueFalseTotal
if ('ARRAY' eq ref $range) { } 0 0 0
125 {
126 0 ▶ if( @{$range} == 4)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$range;} == 4) { } 0 0 0
127 {
128 0 $range = $range_from_sub ;
129 }
130 else
131 {
132 $self->{INTERACTION}{DIE}->
133 (
134 "Error: Sub range definition did not return 4 elements array reference, ["
135 0 ▶ . join(', ', map {defined $_ ? $_ : 'undef'}@{$range_from_sub})
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
136 . "] at '$location'."
137 ) ;
138 }
139 }
140 else
141 {
142 0 $self->{INTERACTION}{DIE}->("Error: Sub range definition did not return an array reference at '$location'." ) ;
143 }
144 }
145 else
146 {
147 0 ▶ if($self->{DUMP_RANGE_DESCRIPTION})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DUMP_RANGE_DESCRIPTION"}) 0 0 0
148 {
149 0 ▶ $comment ||= 'No comment returned from sub' ;
Condition0%0/2
$comment ||= "No comment returned from sub"
l!l
00
MC/DC0%0/1
$comment ||= "No comment returned from sub"
$comment
Truth table0%0/2
A
A= $comment
Aresult
11
00
150 0 $self->{INTERACTION}{INFO}("Information: Sub range definition returned no range at '$location'. $comment.\n" ) ;
151 }
152
153 0 next ;
154 }
155 }
156
157 0 my ($range_name, $range_size_definition, $range_color, $range_user_information) = @{$range} ;
158 0 my $range_size = $range_size_definition;
159  
160 0 for my $range_field ($range_name, $range_size, $range_color, $range_user_information)
161 {
162 0 ▶ $range_field = $range_field->($self, $data, $used_data, $size, $range) if 'CODE' eq ref($range_field) ;
Branch: 1 branch
BranchTrueFalseTotal
if "CODE" eq ref $range_field 0 0 0
163 }
164  
165 0 my ($is_header, $is_comment, $is_bitfield, $is_skip, $unpack_format) ;
166  
167 # handle maximum_size
168 0 ▶ if($SCALAR_TYPE eq ref($range_size))
Branch: 2 branches
BranchTrueFalseTotal
if ($SCALAR_TYPE eq ref $range_size) { } 0 0 0
elsif ('CODE' eq ref $range_size) { } 0 0 0
169 {
170 0 ($is_header, $is_comment, $is_bitfield, $is_skip, $range_size, undef) = $self->unpack_range_size($range_name, $range_size, $used_data) ;
171 }
172 elsif('CODE' eq ref($range_size))
173 {
174 0 ($is_header, $is_comment, $is_bitfield, $is_skip, $range_size, undef) = $self->unpack_range_size($range_name, $range_size->(), $used_data) ;
175 }
176 else
177 {
178 0 $self->{INTERACTION}{DIE}("Error: size '$range_size' doesn't look like a number or a code reference in range '$range_name' at '$location'.\n")
179 }
180  
181 0 my $truncated_size ;
182 0 ▶ if($data_size - $used_data < $range_size)
Branch: 2 branches
BranchTrueFalseTotal
if ($data_size - $used_data < $range_size) { } 0 0 0
elsif ($size < $range_size) { } 0 0 0
183 {
184 0 $range_size = $truncated_size = max($data_size - $used_data, 0) ;
185 0 $skip_remaining_ranges++ ;
186 }
187 elsif($size < $range_size)
188 {
189 0 $range_size = $truncated_size = $size ;
190 0 $skip_remaining_ranges++ ;
191 }
192
193 # get the unpack format with the justified size
194 # note that we keep $is_comment and $is_bitfield from first run
195 # as the those are extracted from the size field and we have modified it
196 0 (undef,undef, undef, undef, $range_size, $unpack_format) = $self->unpack_range_size($range_name, $range_size, $used_data) ;
197
198 0 ▶ if($data_size == $used_data)
Branch: 1 branch
BranchTrueFalseTotal
if ($data_size == $used_data) 0 0 0
199 {
200 0 ▶ if($is_header || $is_comment || $is_bitfield)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_header or $is_comment or $is_bitfield) { } 0 0 0
Condition0%0/6
$is_header or $is_comment or $is_bitfield
l!l&&r!l&&!r
000
$is_header or $is_comment
l!l&&r!l&&!r
000
MC/DC0%0/3
$is_header or $is_comment or $is_bitfield
$is_header $is_comment $is_bitfield
Truth table0%0/4
A or B or C
A= $is_headerB= $is_commentC= $is_bitfield
ABCresult
1XX1
01X1
0011
0000
201 {
202 # display bitfields even for ranges that pass maximim_size (truncated ranges)
203 }
204 else
205 {
206 0 my $next_range = $range_provider->($self, $data, $used_data) ;
207
208 0 ▶ if(defined $next_range)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $next_range) 0 0 0
209 {
210 0 my ($next_range_name, $next_range_size_definition) = @{$next_range} ;
211 0 $self->{INTERACTION}{WARN}("Warning: More ranges to display but no more data.Next range name '$next_range_name'\n") ;
212 }
213
214 0 $skip_remaining_ranges++ ;
215 }
216 }
217
218 0 ▶ if(!$is_header && ! $is_comment && ! $is_bitfield)
Branch: 1 branch
BranchTrueFalseTotal
unless ($is_header or $is_comment or $is_bitfield) 0 0 0
Condition0%0/6
$is_header or $is_comment or $is_bitfield
l!l&&r!l&&!r
000
$is_header or $is_comment
l!l&&r!l&&!r
000
MC/DC0%0/3
$is_header or $is_comment or $is_bitfield
$is_header $is_comment $is_bitfield
Truth table0%0/4
A or B or C
A= $is_headerB= $is_commentC= $is_bitfield
ABCresult
1XX1
01X1
0011
0000
219 {
220 0 ▶ if($range_size == 0 && $self->{DISPLAY_ZERO_SIZE_RANGE_WARNING})
Branch: 1 branch
BranchTrueFalseTotal
if ($range_size == 0 and $self->{"DISPLAY_ZERO_SIZE_RANGE_WARNING"}) 0 0 0
Condition0%0/3
$range_size == 0 and $self->{"DISPLAY_ZERO_SIZE_RANGE_WARNING"}
!ll&&!rl&&r
000
MC/DC0%0/2
$range_size == 0 and $self->{"DISPLAY_ZERO_SIZE_RANGE_WARNING"}
$range_size == 0 $self->{"DISPLAY_ZERO_SIZE_RANGE_WARNING"}
Truth table0%0/3
A and B
A= $range_size == 0B= $self->{"DISPLAY_ZERO_SIZE_RANGE_WARNING"}
ABresult
0X0
100
111
221 {
222 0 $self->{INTERACTION}{WARN}("Warning: range '$range_name' requires zero bytes.\n") ;
223 }
224
225 0 ▶ if(defined $truncated_size)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $truncated_size) { } 0 0 0
226 {
227 0 $self->{INTERACTION}{WARN}("Warning: range '$range_name' size was reduced from $range_size_definition to $truncated_size due to size limit at '$location'.\n") ;
228 0 $range_name = "$range_size_definition->$truncated_size:$range_name" ;
229 }
230 else
231 {
232 0 ▶ if($self->{DISPLAY_RANGE_SIZE})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DISPLAY_RANGE_SIZE"}) 0 0 0
233 {
234 0 $range_name = "$range_size:$range_name" ;
235 }
236 }
237
238 0 $last_data = unpack($unpack_format, $data) # get out data from the previous range for bitfield
239 }
240
241 0 ▶ my $chunk =
Branch: 2 branches
BranchTrueFalseTotal
$is_comment ? : 0 0 0
$is_bitfield ? : 0 0 0
242 {
243 NAME => $range_name,
244 COLOR => $range_color,
245 OFFSET => $used_data,
246 DATA => $is_comment ? undef : $last_data,
247 IS_BITFIELD => $is_bitfield ? $range_size_definition : 0,
248 IS_HEADER => $is_header,
249 IS_SKIP => $is_skip,
250 IS_COMMENT => $is_comment,
251 USER_INFORMATION => $range_user_information,
252 } ;
253
254 0 ▶ if(defined $self->{GATHERED_CHUNK})
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'GATHERED_CHUNK'}) { } 0 0 0
255 {
256 0 my @chunks = $self->{GATHERED_CHUNK}($self, $chunk) ;
257 0 push @{$collected_data}, @chunks ;
258 }
259 else
260 {
261 0 push @{$collected_data}, $chunk ;
262 }
263
264 0 ▶ if($self->{DUMP_RANGE_DESCRIPTION})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"DUMP_RANGE_DESCRIPTION"}) 0 0 0
265 {
266 $self->{INTERACTION}{INFO}
267 (
268 DumpTree
269 {
270 0 ▶ %{$chunk},
Branch: 1 branch
BranchTrueFalseTotal
$is_bitfield ? : 0 0 0
271 'unpack format' => $is_bitfield ? $range_size_definition : $unpack_format,
272 },
273 $range_name,
274 QUOTE_VALUES => 1, DISPLAY_ADDRESS => 0,
275 ) ;
276 }
277  
278 0 $used_data += $range_size ;
279 0 $size -= $range_size ;
280  
281 0 ▶ last if $skip_remaining_ranges ;
Branch: 1 branch
BranchTrueFalseTotal
if $skip_remaining_ranges 0 0 0
282 }
283
284 0 return $collected_data, $used_data ;
285 }
286  
287 #-------------------------------------------------------------------------------
288  
289 sub create_range_provider
290 0 ▶{
Subroutine create_range_provider: not called CC=2 SCAR=17.9
Pod undocumented
291  
292 =head2 [P] create_range_provider($range_description)
293  
294 Transforms the user supplied ranges into an internal format
295  
296 I<Arguments> -
297  
298 =over 2
299  
300 =item * $range_description - An array reference or a subroutine reference
301  
302 =back
303  
304 I<Returns> - Array reference - ranges in internal format
305  
306 I<Exceptions> - None
307  
308 =cut
309  
310 0 my ($self, $range_description) = @_ ;
311  
312 0 my $range_provider ;
313  
314 0 ▶if('CODE' eq ref($range_description))
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $range_description) { } 0 0 0
315 {
316 0 my $ranges ;
317
318 $range_provider =
319 sub
320 0 ▶ {
Subroutine __ANON__: not called CC=7 SCAR=40.3
321 0 my ($dumper, $data, $offset) = @_ ;
322
323 0 ▶ if(! defined $ranges || ! @{$ranges})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ranges and @{$ranges;}) 0 0 0
Condition0%0/3
defined $ranges and @{$ranges;}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ranges and @{$ranges;}
defined $ranges @{$ranges;}
Truth table0%0/3
A and B
A= defined $rangesB= @{$ranges;}
ABresult
0X0
100
111
324 {
325 0 my $generated_range_description = $range_description->($dumper, $data, $offset) ;
326
327 0 ▶ return undef unless defined $generated_range_description ;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $generated_range_description 0 0 0
328
329 0 my $created_ranges = $self->create_ranges($generated_range_description) ;
330
331 0 push @{$ranges}, @{$created_ranges}, $range_description ;
332 }
333
334 RANGE:
335 0 my $local_description = shift@{$ranges} ;
336
337 0 ▶ if('CODE' eq ref $local_description)
Branch: 1 branch
BranchTrueFalseTotal
if ("CODE" eq ref $local_description) 0 0 0
338 {
339 0 my $sub_range_description = $local_description->($dumper, $data, $offset) ;
340
341 0 ▶ if(defined $sub_range_description)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sub_range_description) 0 0 0
342 {
343 0 unshift @{$ranges}, $local_description ;
344
345 0 ▶ if('CODE' eq ref $sub_range_description )
Branch: 1 branch
BranchTrueFalseTotal
if ('CODE' eq ref $sub_range_description) { } 0 0 0
346 {
347 0 unshift @{$ranges}, $sub_range_description ;
348 }
349 else
350 {
351 0 my $created_ranges = $self->create_ranges($sub_range_description) ;
352 0 unshift @{$ranges}, @{$created_ranges} ;
353 }
354 }
355 #else
356 # sub generating ranges is done
357
358 0 goto RANGE ;
359 }
360
361 0 return $local_description ;
362 }
363 0 }
364 else
365 {
366 0 my $ranges = $self->create_ranges($range_description) ;
367
368 $range_provider =
369 sub
370 0 ▶ {
Subroutine __ANON__: not called CC=1 SCAR=6.9
371 0 return shift @{$ranges} ;
372 }
373 0 }
374  
375 0 return $range_provider ;
376 }
377  
378 #-------------------------------------------------------------------------------
379  
380 sub unpack_range_size
381 0 ▶{
Subroutine unpack_range_size: not called CC=8 SCAR=42.8
Pod undocumented
382  
383 =head2 [P] unpack_range_size($self, $range_name, $size, $used_data)
384  
385 Verifies the size field from a range descritpion and generates unpack format
386  
387 I<Arguments> -
388  
389 =over 2
390  
391 =item * $self
392  
393 =item * $range_name
394  
395 =item * $size
396  
397 =item * $used_data
398  
399 =back
400  
401 I<Returns> - A list
402  
403 =over 2
404  
405 =item * $is_header - Boolean -
406  
407 =item * $is_comment - Boolean -
408  
409 =item * $is_bitfield - Boolean -
410  
411 =item * $range_size - Integer
412  
413 =item * $unpack_format - A String - formated according to I<pack>.
414  
415 =back
416  
417 I<Exceptions> - Croaks with an error messge if the input data is invalid
418  
419 =cut
420  
421 0 my ($self, $range_name, $size, $used_data) = @_ ;
422  
423 0 my ($is_header, $is_comment, $is_bitfield, $is_skip, $range_size, $unpack_format) = (0, 0, 0, 0, -1, '');
424  
425 0 my $digits_or_hex = '(?:(?:0x[0-9a-fA-F]+)|(?:\d+))' ;
426  
427 0 ▶if('#' eq $size)
Branch: 5 branches
BranchTrueFalseTotal
if ('#' eq $size) { } 0 0 0
elsif ('@' eq $size) { } 0 0 0
elsif ($size =~ /^\s*(X$digits_or_hex)?\s*(x$digits_or_hex)?\s*b$digits_or_hex\s*$/) { } 0 0 0
elsif ($size =~ /^\s*(x|X)($digits_or_hex)\s*$/) { } 0 0 0
elsif ($size =~ /^\s*($digits_or_hex)\s*$/) { } 0 0 0
428 {
429 0 $is_comment++ ;
430 0 $range_size = 0 ;
431 0 $unpack_format = '#' ;
432 }
433 elsif('@' eq $size)
434 {
435 0 $is_header++ ;
436 0 $range_size = 0 ;
437 0 $unpack_format = '#' ;
438 }
439 elsif($size =~ /^\s*(X$digits_or_hex)?\s*(x$digits_or_hex)?\s*b$digits_or_hex\s*$/)
440 {
441 0 $is_bitfield++ ;
442 0 $range_size = 0 ;
443 0 $unpack_format = '#' ;
444 }
445 elsif($size =~ /^\s*(x|X)($digits_or_hex)\s*$/)
446 {
447 0 $is_skip++ ;
448 0 $range_size = $2 ;
449
450 0 ▶ $range_size = hex($range_size) if $range_size =~ /^0x/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $range_size =~ /^0x/ 0 0 0
451 0 $unpack_format = '#' ;
452 }
453 elsif($size =~ /^\s*($digits_or_hex)\s*$/)
454 {
455 0 $range_size = $1 ;
456 0 ▶ $range_size = hex($range_size) if $range_size =~ /^0x/ ;
Branch: 1 branch
BranchTrueFalseTotal
if $range_size =~ /^0x/ 0 0 0
457
458 0 $unpack_format = "x$used_data a$range_size" ;
459 }
460 else
461 {
462 0 my $location = "$self->{FILE}:$self->{LINE}" ;
463  
464 0 $self->{INTERACTION}{DIE}("Error: size '$size' doesn't look valid in range '$range_name' at '$location'.\n")
465 }
466  
467 #~ print "$range_name => $is_header, $is_comment, $is_bitfield, $is_skip, $range_size, $unpack_format\n";
468  
469 0 return ($is_header, $is_comment, $is_bitfield, $is_skip, $range_size, $unpack_format) ;
470 }
471  
472 #-------------------------------------------------------------------------------
473  
474 sub create_ranges
475 0 ▶{
Subroutine create_ranges: not called CC=3 SCAR=24.8
Pod undocumented
476  
477 =head2 [P] create_ranges($range_description)
478  
479 Transforms the user supplied ranges into an internal format
480  
481 I<Arguments> -
482  
483 =over 2
484  
485 =item * $range_description - See L<gather>
486  
487 =back
488  
489 I<Returns> - Array ference - ranges in internal format
490  
491 I<Exceptions> - Croaks with an error messge if the input data is invalid
492  
493 =cut
494  
495 0 my ($self, $range_description) = @_ ;
496  
497 0 ▶return $self->create_ranges_from_array_ref($range_description) if 'ARRAY' eq ref($range_description) ;
Branch: 1 branch
BranchTrueFalseTotal
if "ARRAY" eq ref $range_description 0 0 0
498 0 ▶return $self->create_ranges_from_string($range_description) if '' eq ref($range_description) ;
Branch: 1 branch
BranchTrueFalseTotal
if "" eq ref $range_description 0 0 0
499  
500 }
501  
502 #-------------------------------------------------------------------------------
503  
504 sub create_ranges_from_string
505 0 ▶{
Subroutine create_ranges_from_string: not called CC=4 SCAR=25.6
Pod undocumented
506  
507 =head2 [P] create_ranges_from_string($range_description)
508  
509 Transforms the user supplied ranges into an internal format
510  
511 I<Arguments> -
512  
513 =over 2
514  
515 =item * $range_description - A string - See L<gather>
516  
517 =back
518  
519 I<Returns> - Array ference - ranges in internal format
520  
521 I<Exceptions> - Croaks with an error messge if the input data is invalid
522  
523 =cut
524  
525 0 my ($self, $range_description) = @_ ;
526  
527 # 'comment,#:name,size,color:name,size:name,size,color'
528  
529 my @ranges =
530 map
531 {
532 0 '' eq $_
533 ? []
534 0 ▶ : [ map {s/^\s+// ; s/\s+$//; $_} split /,/ ] ;
Branch: 1 branch
BranchTrueFalseTotal
'' eq $_ ? : 0 0 0
535 } split /:/, $range_description ;
536  
537 0 my @flattened ;
538  
539 eval
540 0 {
541 0 @flattened = $self->flatten(\@ranges) ;
542 } ;
543  
544 0 ▶if($EVAL_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::HexDump::Range::EVAL_ERROR) 0 0 0
545 {
546 0 my ($error_message, $range_index) = @{ $EVAL_ERROR } ;
547 0 chomp $error_message ;
548  
549 2 ▶ use Data::TreeDumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
550 2 ▶ use List::MoreUtils qw(pairwise) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
551
552 0 my @keys = ('name', 'size', 'color (optional)', 'user comment (optional)') ;
553  
554 $self->{INTERACTION}{DIE}->
555 (
556 DumpTree
557 0 ▶ { pairwise { ( $a, $b) } @keys, @{$ranges[$range_index]} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
558 "Range index $range_index: $error_message",
559 QUOTE_VALUES => 1,
560 0 ▶ TYPE_FILTERS => {'HASH' => sub {'HASH', undef, @keys }, }
Subroutine __ANON__: not called CC=1 SCAR=6.9
561 0 ) ;
562 }
563  
564 0 @ranges = () ;
565  
566 0 ▶while(@flattened)
Branch: 1 branch
BranchTrueFalseTotal
if (@flattened) 0 0 0
567 {
568 0 push @ranges, [splice(@flattened, 0, $RANGE_DEFINITON_FIELDS)] ;
569 }
570  
571 0 return \@ranges ;
572 }
573  
574  
575 sub create_ranges_from_array_ref
576 0 ▶{
Subroutine create_ranges_from_array_ref: not called CC=3 SCAR=19.8
Pod undocumented
577  
578 =head2 [P] create_ranges_from_array_ref($range_description)
579  
580 transforms the user supplied ranges into an internal format
581  
582 I<Arguments> -
583  
584 =over 2
585  
586 =item * $range_description - An array reference - See L<gather>
587  
588 =back
589  
590 I<Returns> - I<Returns> - Array ference - ranges in internal format
591  
592 I<Exceptions> - Croaks with an error messge if the input data is invalid
593  
594 =cut
595  
596 0 my ($self, $range_description) = @_ ;
597  
598 0 my @flattened ;
599  
600 eval
601 0 {
602 0 @flattened = $self->flatten($range_description) ;
603 } ;
604  
605 0 ▶if($EVAL_ERROR)
Branch: 1 branch
BranchTrueFalseTotal
if ($Data::HexDump::Range::EVAL_ERROR) 0 0 0
606 {
607 0 my ($error_message, $range_index) = @{ $EVAL_ERROR } ;
608 0 chomp $error_message ;
609  
610 2 ▶ use Data::TreeDumper ;
Subroutine BEGIN: called CC=1 SCAR=0.0
611 2 ▶ use List::MoreUtils qw(pairwise) ;
Subroutine BEGIN: called CC=1 SCAR=0.0
612
613 0 my @keys = ('name', 'size', 'color (optional)', 'user comment (optional)') ;
614  
615 $self->{INTERACTION}{DIE}->
616 (
617 DumpTree
618 0 ▶ { pairwise { ( $a, $b) } @keys, @{$range_description->[$range_index]} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
619 "Range index $range_index: $error_message",
620 QUOTE_VALUES => 1,
621 0 ▶ TYPE_FILTERS => {'HASH' => sub {'HASH', undef, @keys }, }
Subroutine __ANON__: not called CC=1 SCAR=6.9
622 0 ) ;
623 }
624  
625 0 my @ranges ;
626  
627 0 ▶while(@flattened)
Branch: 1 branch
BranchTrueFalseTotal
if (@flattened) 0 0 0
628 {
629 0 push @ranges, [splice(@flattened, 0, $RANGE_DEFINITON_FIELDS)] ;
630 }
631
632 0 return \@ranges ;
633 }
634  
635 #-------------------------------------------------------------------------------
636  
637 sub flatten
638 0 ▶{
Subroutine flatten: not called CC=17 SCAR=57.2
Pod undocumented
639
640 =head2 [P] flatten($range_description)
641  
642 transforms the user supplied ranges into an internal format
643  
644 I<Arguments> -
645  
646 =over 2
647  
648 =item * $range_description - See L<gather>
649  
650 =back
651  
652 I<Returns> - Nothing
653  
654 I<Exceptions> - Croaks with an error messge if the input data is invalid
655  
656 =cut
657  
658 0 my $self = shift ;
659 #my $location = "$self->{FILE}:$self->{LINE}" ;
660  
661 0 my $index = -1 ;
662  
663 map
664 {
665 0 my $description = $_ ;
666 0 $index++ ;
667  
668 0 ▶ if(ref($description) eq 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if (ref $description eq 'ARRAY') { } 0 0 0
669 {
670 0 ▶ if(@{$description} == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (@{$description;} == 0) 0 0 0
671 {
672 0 $self->{INTERACTION}{DIE}->(["Error: no elements in range description.", $index]) ;
673 }
674
675 0 ▶ if(all {'' eq ref($_) || 'CODE' eq ref($_) } @{$description} )
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 2 branches
BranchTrueFalseTotal
if (&all(sub { '' eq ref $_ or 'CODE' eq ref $_; } , @{$description;})) { } 0 0 0
"" eq ref $_ or "CODE" eq ref $_ 0 0 0
676 {
677 0 ▶ if(@{$description} == 0)
Branch: 6 branches
BranchTrueFalseTotal
if (@{$description;} == 0) { } 0 0 0
elsif (@{$description;} == 1) { } 0 0 0
elsif (@{$description;} == 2) { } 0 0 0
elsif (@{$description;} == 3) { } 0 0 0
elsif (@{$description;} == 4) { } 0 0 0
elsif (@{$description;} > $RANGE_DEFINITON_FIELDS) { } 0 0 0
678 {
679 0 $self->{INTERACTION}{DIE}->(["Error: no elements in range description.", $index]) ;
680 }
681 0 elsif(@{$description} == 1)
682 {
683 0 ▶ if('' eq ref($description->[0]))
Branch: 1 branch
BranchTrueFalseTotal
if ('' eq ref $description->[0]) { } 0 0 0
684 {
685 $self->{INTERACTION}{DIE}->
686 ([
687 "Error: too few elements in range description ["
688 0 ▶ . join(', ', map {defined $_ ? $_ : 'undef'} @{$description})
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
689 . "]." ,
690 $index
691 ]) ;
692 }
693 else
694 {
695 # OK, will be called at gather time
696 0 push @{$description}, undef, undef, undef ;
697 }
698 }
699 0 elsif(@{$description} == 2)
700 {
701 0 push @{$description}, undef, undef ;
702 }
703 0 elsif(@{$description} == 3)
704 {
705 0 push @{$description}, undef ;
706 # make sure we get a default color
707 0 ▶ $description->[2] = undef if defined $description->[2] && $description->[2] eq $EMPTY_STRING ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description->[2] and $description->[2] eq $EMPTY_STRING 0 0 0
Condition0%0/3
defined $description->[2] and $description->[2] eq $EMPTY_STRING
!ll&&!rl&&r
000
MC/DC0%0/2
defined $description->[2] and $description->[2] eq $EMPTY_STRING
defined $description->[2] $description->[2] eq $EMPTY_STRING
Truth table0%0/3
A and B
A= defined $description->[2]B= $description->[2] eq $EMPTY_STRING
ABresult
0X0
100
111
708 }
709 0 elsif(@{$description} == 4)
710 {
711 # make sure we get a default color
712 0 ▶ $description->[2] = undef if defined $description->[2] && $description->[2] eq $EMPTY_STRING ;
Branch: 1 branch
BranchTrueFalseTotal
if defined $description->[2] and $description->[2] eq $EMPTY_STRING 0 0 0
Condition0%0/3
defined $description->[2] and $description->[2] eq $EMPTY_STRING
!ll&&!rl&&r
000
MC/DC0%0/2
defined $description->[2] and $description->[2] eq $EMPTY_STRING
defined $description->[2] $description->[2] eq $EMPTY_STRING
Truth table0%0/3
A and B
A= defined $description->[2]B= $description->[2] eq $EMPTY_STRING
ABresult
0X0
100
111
713 }
714 0 elsif(@{$description} > $RANGE_DEFINITON_FIELDS)
715 {
716 $self->{INTERACTION}{DIE}->
717 ([
718 "Error: too many elements in range description ["
719 0 ▶ . join(', ', map {defined $_ ? $_ : 'undef'} @{$description})
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
720 . "].",
721 $index
722 ]) ;
723 }
724
725 0 @{$description} ;
726 }
727 else
728 {
729 0 $self->flatten(@{$description}) ;
730 }
731 }
732 else
733 {
734 0 $description
735 }
736 } @_
737 }
738  
739 #-------------------------------------------------------------------------------
740  
741 2 1 ;
742  
743 =head1 BUGS AND LIMITATIONS
744  
745 None so far.
746  
747 =head1 AUTHOR
748  
749 Nadim ibn hamouda el Khemir
750 CPAN ID: NKH
751 mailto: nadim@cpan.org
752  
753 =head1 COPYRIGHT AND LICENSE
754  
755 Copyright Nadim Khemir 2010.
756  
757 This program is free software; you can redistribute it and/or
758 modify it under the terms of either:
759  
760 =over 4
761  
762 =item * the GNU General Public License as published by the Free
763 Software Foundation; either version 1, or (at your option) any
764 later version, or
765  
766 =item * the Artistic License version 2.0.
767  
768 =back
769  
770 =head1 SUPPORT
771  
772 You can find documentation for this module with the perldoc command.
773  
774 perldoc Data::HexDump::Range
775  
776 You can also look for information at:
777  
778 =over 4
779  
780 =item * AnnoCPAN: Annotated CPAN documentation
781  
782 L<http://annocpan.org/dist/Data-HexDump-Range>
783  
784 =item * RT: CPAN's request tracker
785  
786 Please report any bugs or feature requests to L <bug-data-hexdump-range@rt.cpan.org>.
787  
788 We will be notified, and then you'll automatically be notified of progress on
789 your bug as we make changes.
790  
791 =item * Search CPAN
792  
793 L<http://search.cpan.org/dist/Data-HexDump-Range>
794  
795 =back
796  
797 =head1 SEE ALSO
798  
799 L<Data::HexDump::Range>
800  
801 =cut
↑ Summary Range/Split.pm »