lib/Text/FixEOL.pm

filter: Statementstmt 100% 155 / 155 Branchbran 100% 64 / 64 Conditioncond 100% 15 / 15 MC/DCmcdc 100% 10 / 10 Subroutinesub 100% 18 / 18 Podpod 100% 13 / 13 totaltotal 100% 275 / 275 CC 39 SCAR 20.2
CC
39
Cov
100%
fix_eol
13.0
new
10.0
_platform_defaults
8.0
CRAP 7.5
↑ Summary
1 package Text::FixEOL;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 $Text::FixEOL::VERSION = '1.08';
7  
8 ##########################################################################################
9  
10 2 my %_Platform_Defaults = (
11 lf => {
12 'fixlast' => 'no',
13 'eof' => 'asis',
14 'eol' => "\012",
15 },
16 cr => {
17 'fixlast' => 'no',
18 'eof' => 'asis',
19 'eol' => "\015",
20 },
21 crlf => {
22 'fixlast' => 'no',
23 'eof' => 'asis',
24 'eol' => "\015\012",
25 },
26 asis => {
27 'fixlast' => 'no',
28 'eof' => 'asis',
29 'eol' => "asis",
30 },
31 network => {
32 'fixlast' => 'yes',
33 'eof' => 'remove',
34 'eol' => "\015\012",
35 },
36 mac => {
37 'fixlast' => 'yes',
38 'eof' => 'remove',
39 'eol' => "\015",
40 },
41 macos => {
42 'fixlast' => 'yes',
43 'eof' => 'remove',
44 'eol' => "\015",
45 },
46 windows => {
47 'fixlast' => 'yes',
48 'eof' => 'asis',
49 'eol' => "\015\012",
50 },
51 mswin32 => {
52 'fixlast' => 'yes',
53 'eof' => 'asis',
54 'eol' => "\015\012",
55 },
56 os2 => {
57 'fixlast' => 'yes',
58 'eof' => 'asis',
59 'eol' => "\015\012",
60 },
61 vms => {
62 'fixlast' => 'yes',
63 'eof' => 'remove',
64 'eol' => "\015\012",
65 },
66 netware => {
67 'fixlast' => 'yes',
68 'eof' => 'asis',
69 'eol' => "\015\012",
70 },
71 dos => {
72 'fixlast' => 'yes',
73 'eof' => 'asis',
74 'eol' => "\015\012",
75 },
76 cygwin => {
77 'fixlast' => 'yes',
78 'eof' => 'asis',
79 'eol' => "\015\012",
80 },
81 unix => {
82 'fixlast' => 'yes',
83 'eof' => 'remove',
84 'eol' => "\012",
85 },
86 'unknown' => {
87 'fixlast' => 'yes',
88 'eof' => 'remove',
89 'eol' => "\n",
90 },
91 );
92  
93 ##########################################################################################
94  
95 322 ▶sub new {
Subroutine new: called CC=10 SCAR=23.0
Pod documented
96 322 my $proto = shift;
97 322 my $proto_ref = ref($proto);
98 322 my $package = __PACKAGE__;
99 322 my $class;
100 322 ▶ if ($proto_ref) { $class = $proto_ref; }
Branch: 2 branches
BranchTrueFalseTotal
if ($proto_ref) { } 296 26 322
elsif ($proto) { } 25 1 26
101 25 elsif ($proto) { $class = $proto; }
102 1 else { $class = $package; }
103 322 my $self = bless {},$class;
104  
105 322 $self->eol_handling('platform');
106 322 $self->eof_handling('platform');
107 322 $self->fix_last_handling('platform');
108  
109 322 my %raw_properties = ();
110 322 ▶ if (1 < @_) { %raw_properties = @_; }
Branch: 2 branches
BranchTrueFalseTotal
if (1 < @_) { } 4 318 322
elsif (1 == @_) { } 301 17 318
111 elsif (1 == @_) {
112 301 my $parm = shift;
113 301 my $parm_type = ref($parm);
114 301 ▶ if ($parm_type eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($parm_type eq 'HASH') { } 300 1 301
115 300 %raw_properties = %$parm;
116 } else {
117 1 require Carp;
118 1 Carp::croak("${package}::new() - Unexpected parameter type passed to constructor: $parm_type");
119 }
120 } else {
121 17 return $self;
122 }
123  
124 304 my %properties = map { lc($_) => $raw_properties{$_} } keys %raw_properties;
125  
126 304 ▶ if ($properties{'eol'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"eol"}) 302 2 304
127 302 $self->eol_handling($properties{'eol'});
128 302 delete $properties{'eol'};
129 }
130 304 ▶ if ($properties{'eof'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"eof"}) 298 6 304
131 298 $self->eof_handling($properties{'eof'});
132 298 delete $properties{'eof'};
133 }
134 304 ▶ if ($properties{'fixlast'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($properties{"fixlast"}) 298 6 304
135 298 $self->fix_last_handling($properties{'fixlast'});
136 298 delete $properties{'fixlast'};
137 }
138 304 my @extra_properties = keys %properties;
139 304 ▶ if (0 < @extra_properties) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 < @extra_properties) 2 302 304
140 2 require Carp;
141 2 Carp::croak("${package}::new() - Unexpected attributes passed: " . join(', ',sort @extra_properties) . "\n");
142 }
143  
144 302 return $self;
145 }
146  
147 ##########################################################################################
148  
149 59 ▶sub eol_to_unix {
Subroutine eol_to_unix: called CC=1 SCAR=0.0
Pod documented
150 59 my $self = shift;
151  
152 59 my $to_unix = $self->new({
153 'EOL' => 'unix',
154 'EOF' => 'unix',
155 'FixLast' => 'unix',
156 })->fix_eol(@_);
157 59 return $to_unix;
158 }
159  
160 ##########################################################################################
161  
162 59 ▶sub eol_to_dos {
Subroutine eol_to_dos: called CC=1 SCAR=0.0
Pod documented
163 59 my $self = shift;
164  
165 59 my $to_dos = $self->new({
166 'EOL' => 'dos',
167 'EOF' => 'dos',
168 'FixLast' => 'dos',
169 })->fix_eol(@_);
170 59 return $to_dos;
171 }
172  
173 ##########################################################################################
174  
175 59 ▶sub eol_to_mac {
Subroutine eol_to_mac: called CC=1 SCAR=0.0
Pod documented
176 59 my $self = shift;
177  
178 59 my $to_mac = $self->new({
179 'EOL' => 'mac',
180 'EOF' => 'mac',
181 'FixLast' => 'mac',
182 })->fix_eol(@_);
183 59 return $to_mac;
184 }
185  
186 ##########################################################################################
187  
188 59 ▶sub eol_to_network {
Subroutine eol_to_network: called CC=1 SCAR=0.0
Pod documented
189 59 my $self = shift;
190  
191 59 my $to_network= $self->new({
192 'EOL' => 'network',
193 'EOF' => 'network',
194 'FixLast' => 'yes',
195 })->fix_eol(@_);
196 59 return $to_network;
197 }
198  
199 ##########################################################################################
200  
201 59 ▶sub eol_to_crlf {
Subroutine eol_to_crlf: called CC=1 SCAR=0.0
Pod documented
202 59 my $self = shift;
203  
204 59 my $to_crlf = $self->new({
205 'EOL' => 'crlf',
206 'EOF' => 'remove',
207 'FixLast' => 'yes',
208 })->fix_eol(@_);
209 59 return $to_crlf;
210 }
211  
212 ##########################################################################################
213  
214 424 ▶sub fix_eol {
Subroutine fix_eol: called CC=13 SCAR=25.6
Pod documented
215 424 my $self = shift;
216  
217 424 ▶ unless (1 == @_) {
Branch: 1 branch
BranchTrueFalseTotal
unless (1 == @_) 2 422 424
218 2 require Carp;
219 2 my $package = __PACKAGE__;
220 2 Carp::croak("${package}::fix_eol() - Incorrect number of parameters passed. One string (only) is required.");
221 }
222  
223 422 my ($string) = @_;
224 422 my $eol_mode = $self->eol_mode;
225 422 ▶ if ($eol_mode ne 'asis') {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_mode ne "asis") 363 59 422
226 363 $string = $self->_eol_to_base_lf($string);
227 }
228 422 my $fix_last = $self->fix_last_mode;
229 422 ▶ if ($fix_last eq 'yes') {
Branch: 1 branch
BranchTrueFalseTotal
if ($fix_last eq "yes") 421 1 422
230 421 my $old_eof = '';
231 421 ▶ if ($string =~ s/(\032+)$//s) { # \032 is Ctrl-Z
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/(\032+)$//s) 6 415 421
232 6 $old_eof = "\032";
233 }
234 421 ▶ if (($string ne '') and ($eol_mode ne 'asis')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string ne '' and $eol_mode ne 'asis') { } 358 63 421
Condition100%3/3
$string ne "" and $eol_mode ne "asis"
!ll&&!rl&&r
459358
MC/DC100%2/2
$string ne "" and $eol_mode ne "asis"
$string ne "" $eol_mode ne "asis"
Truth table100%3/3
A and B
A= $string ne ""B= $eol_mode ne "asis"
ABresult
0X0
100
111
235 358 ▶ if ($string !~ m/\012$/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /\012$/s) 4 354 358
236 4 $string .= "\012";
237 }
238  
239 } else {
240 63 ▶ if ($eol_mode ne 'asis') {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_mode ne "asis") 4 59 63
241 4 $string = "\012";
242 }
243 }
244 421 $string .= $old_eof;
245 }
246  
247 422 my $eof_handling = $self->eof_mode;
248 422 ▶ if ($eof_handling eq 'remove') {
Branch: 2 branches
BranchTrueFalseTotal
if ($eof_handling eq 'remove') { } 357 65 422
elsif ($eof_handling eq 'add' and not $string =~ /\032$/s) { } 2 124 126
249 357 $string =~ s/\032+$//s;
250  
251 ▶ } elsif (($eof_handling eq 'add') and ($string !~ m/\032$/s)) {
Condition100%3/3
$eof_handling eq "add" and not $string =~ /\032$/s
!ll&&!rl&&r
6122
MC/DC100%2/2
$eof_handling eq "add" and not $string =~ /\032$/s
$eof_handling eq "add" not $string =~ /\032$/s
Truth table100%3/3
A and not(B)
A= $eof_handling eq "add"B= not $string =~ /\032$/s
ABresult
0X0
100
111
252 2 $string .= "\032";
253 }
254  
255 422 ▶ if ($eol_mode ne 'asis') {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_mode ne "asis") 363 59 422
256 363 my $eol_replacement = $eol_mode;
257 363 $string =~ s/\012/$eol_replacement/gs;
258 }
259 422 return $string;
260 }
261  
262 ##########################################################################################
263  
264 426 ▶sub eol_mode {
Subroutine eol_mode: called CC=2 SCAR=6.9
Pod documented
265 426 my $self = shift;
266 426 my $eol_handling = $self->eol_handling;
267  
268 426 ▶ if ($eol_handling =~ m/^literal:(.+)$/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eol_handling =~ /^literal:(.+)$/s) { } 59 367 426
269 59 return $1;
270  
271 } else {
272 367 my $default_eol = $self->_platform_defaults($eol_handling, 'EOL');
273 367 return $default_eol;
274 }
275 }
276  
277 ##########################################################################################
278  
279 422 ▶sub eof_mode {
Subroutine eof_mode: called CC=1 SCAR=0.0
Pod documented
280 422 my $self = shift;
281  
282 422 my $eof_handling = $self->eof_handling;
283 422 my $default_eof = $self->_platform_defaults($eof_handling, 'EOF');
284 422 return $default_eof;
285 }
286  
287 ##########################################################################################
288  
289 422 ▶sub fix_last_mode {
Subroutine fix_last_mode: called CC=1 SCAR=0.0
Pod documented
290 422 my $self = shift;
291  
292 422 my $fix_last = $self->fix_last_handling;
293 422 my $fix_last_mode = $self->_platform_defaults($fix_last, 'FixLast');
294 422 return $fix_last_mode;
295 }
296  
297 ##########################################################################################
298  
299 1212 ▶sub _platform_defaults {
Subroutine _platform_defaults: called CC=8 SCAR=20.8
300 1212 my $self = shift;
301 1212 my $package = __PACKAGE__;
302  
303 1212 my ($platform_name, $property) = @_;
304  
305 1212 $platform_name = lc ($platform_name);
306 1212 $property = lc ($property);
307  
308 1212 ▶ return $platform_name if (($property eq 'fixlast') and ($platform_name =~ m/^(yes|no)$/));
Branch: 1 branch
BranchTrueFalseTotal
if $property eq "fixlast" and $platform_name =~ /^(yes|no)$/ 127 1085 1212
Condition100%3/3
$property eq "fixlast" and $platform_name =~ /^(yes|no)$/
!ll&&!rl&&r
790295127
MC/DC100%2/2
$property eq "fixlast" and $platform_name =~ /^(yes|no)$/
$property eq "fixlast" $platform_name =~ /^(yes|no)$/
Truth table100%3/3
A and B
A= $property eq "fixlast"B= $platform_name =~ /^(yes|no)$/
ABresult
0X0
100
111
309 1085 ▶ return $platform_name if (($property eq 'eof') and ($platform_name =~ m/^(asis|remove|add)$/));
Branch: 1 branch
BranchTrueFalseTotal
if $property eq "eof" and $platform_name =~ /^(asis|remove|add)$/ 68 1017 1085
Condition100%3/3
$property eq "eof" and $platform_name =~ /^(asis|remove|add)$/
!ll&&!rl&&r
66335468
MC/DC100%2/2
$property eq "eof" and $platform_name =~ /^(asis|remove|add)$/
$property eq "eof" $platform_name =~ /^(asis|remove|add)$/
Truth table100%3/3
A and B
A= $property eq "eof"B= $platform_name =~ /^(asis|remove|add)$/
ABresult
0X0
100
111
310  
311 1017 ▶ if ($platform_name eq 'platform') {
Branch: 1 branch
BranchTrueFalseTotal
if ($platform_name eq "platform") 236 781 1017
312 236 $platform_name = lc ($^O);
313 }
314  
315 1017 my $platform_defaults = $_Platform_Defaults{$platform_name};
316 1017 ▶ unless (defined ($platform_defaults)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $platform_defaults) 236 781 1017
317 236 $platform_defaults = $_Platform_Defaults{'unknown'};
318 }
319 1017 my $property_value = $platform_defaults->{$property};
320 1017 ▶ unless (defined ($property_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $property_value) 1 1016 1017
321 1 require Carp;
322 1 Carp::croak("${package}::_platform_defaults() - Unknown property of $property");
323 }
324 1016 return $property_value;
325 }
326  
327 ##########################################################################################
328  
329 363 ▶sub _eol_to_base_lf {
Subroutine _eol_to_base_lf: called CC=8 SCAR=20.8
330 363 my $self = shift;
331  
332 363 my ($string) = @_;
333  
334 # Undef converts to ''
335 363 ▶ return '' unless (defined $string);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 1 362 363
336  
337 # If there are not any DOS EOLs (\015 characters), return the original string
338 362 ▶ return $string unless ($string =~ m/\015/s);
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /\015/s 32 330 362
339  
340 # If there is nothing except DOS EOL, convert them to \012 directly
341 330 ▶ if ($string !~ m/\012/s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /\012/s) 24 306 330
342 24 $string =~ s/\015/\012/gs;
343 24 return $string;
344 }
345  
346 # If the EOLs are all 'singletons', do in-place cleanup of the DOS EOLs
347 306 ▶ if (($string !~ m/\015\012/s) and ($string !~ m/\012\015/s)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($string =~ /\015\012/s or $string =~ /\012\015/s) 36 270 306
Condition100%3/3
$string =~ /\015\012/s or $string =~ /\012\015/s
l!l&&r!l&&!r
1927836
MC/DC100%2/2
$string =~ /\015\012/s or $string =~ /\012\015/s
$string =~ /\015\012/s $string =~ /\012\015/s
Truth table100%3/3
A or B
A= $string =~ /\015\012/sB= $string =~ /\012\015/s
ABresult
1X1
011
000
348 36 $string =~ s/\015/\012/gs;
349 36 return $string;
350 }
351  
352 270 my @eols = $string =~ m/([\012\015]+)/sg;
353 270 my %replacement_map = ();
354 270 foreach my $eol_mode (@eols) {
355 762 ▶ next if (defined $replacement_map{$eol_mode});
Branch: 1 branch
BranchTrueFalseTotal
if defined $replacement_map{$eol_mode} 384 378 762
356 378 my $replace_with = $eol_mode;
357 378 $replace_with =~ s/(\015\012|\012\015)/\012/gs;
358 378 $replace_with =~ s/\015/\012/gs;
359 378 $replacement_map{$eol_mode} = $replace_with;
360 }
361 270 $string =~ s/([\012\015]+)/$replacement_map{$1}/gse;
362  
363 270 return $string;
364 }
365  
366 ##########################################################################################
367  
368 1064 ▶sub eol_handling { return shift->_property('eol_handling', @_); }
Subroutine eol_handling: called CC=1 SCAR=0.0
Pod documented
369 1055 ▶sub eof_handling { return shift->_property('eof_handling', @_); }
Subroutine eof_handling: called CC=1 SCAR=0.0
Pod documented
370 1056 ▶sub fix_last_handling { return shift->_property('fix_last_handling', @_); }
Subroutine fix_last_handling: called CC=1 SCAR=0.0
Pod documented
371  
372 ##########################################################################################
373 # _property('property_name' => $property_value)
374 #
375 # get/set base accessor for property values
376  
377 3176 ▶sub _property {
Subroutine _property: called CC=3 SCAR=11.0
378 3176 my $self = shift;
379  
380 3176 my $property = shift;
381  
382 3176 my $package = __PACKAGE__;
383 3176 ▶ if (0 == @_) {
Branch: 2 branches
BranchTrueFalseTotal
if (0 == @_) { } 1290 1886 3176
elsif (1 == @_) { } 1885 1 1886
384 1290 my $output = $self->{$package}->{$property};
385 1290 return $output;
386  
387 } elsif (1 == @_) {
388 1885 my $input = shift;
389 1885 $self->{$package}->{$property} = $input;
390 1885 return;
391 } else {
392 1 die ("Bad calling parameters to ${package}::${property}()\n");
393 }
394 }
395  
396 ##########################################################################################
397  
398 2 1;
↑ Summary