DKIM/TextWrap.pm

filter: Statementstmt 98.6% 74 / 75 Branchbran 84.6% 22 / 26 Conditioncond 100% 14 / 14 MC/DCmcdc 100% 9 / 9 Subroutinesub 100% 10 / 10 Podpod 83.3% 5 / 6 totaltotal 95.7% 134 / 140 CC 19 SCAR 19.9
CC
19
Cov
96%
add
13.0
_calculate_new_column
4.0
output
3.2
CRAP 7.3
« DKIM/Canonicalization/DkCommon.pm ↑ Summary DKIM/Canonicalization/seal.pm »
1 package Mail::DKIM::TextWrap;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 our $VERSION = '1.20261116'; # VERSION
5 # ABSTRACT: text wrapping module written for use with DKIM
6  
7 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 9 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
11 9 my $class = shift;
12 9 my %args = @_;
13 9 my $self = {
14 Margin => 72,
15 Break => qr/\s/,
16 BreakBefore => undef,
17 Swallow => qr/\s/,
18 Separator => "\n",
19 cur => 0,
20 may_break => 0,
21 soft_space => "",
22 word => "",
23 %args,
24 };
25 9 ▶ $self->{Output} ||= \*STDOUT;
Condition100%2/2
$self->{"Output"} ||= \*STDOUT
l!l
81
MC/DC100%1/1
$self->{"Output"} ||= \*STDOUT
$self->{"Output"}
Truth table100%2/2
A
A= $self->{"Output"}
Aresult
11
00
26 9 return bless $self, $class;
27 }
28  
29 # Internal properties:
30 #
31 # cur - the last known column position
32 #
33 # may_break - nonzero if the current location allows a linebreak
34 #
35 # soft_space - contains added text that will not be printed if a linebreak
36 # occurs
37 #
38 # word - contains the current word
39  
40 # Internal methods:
41 #
42 # _calculate_new_column() - determine where cur would be after adding some text
43 #
44 # my $new_cur = _calculate_new_column($cur, "some additional\ntext");
45 #
46 446 ▶sub _calculate_new_column {
Subroutine _calculate_new_column: called CC=4 SCAR=13.9
47 446 my ( $cur, $text ) = @_;
48 446 ▶ confess "invalid argument" unless defined($text);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 446 446
49  
50 446 ▶ while ( $text =~ /^(.*?)([\n\r\t])(.*)$/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^(.*?)([\n\r\t])(.*)$/s) 76 446 522
51 76 $cur += length($1);
52 76 ▶ if ( $2 eq "\t" ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($2 eq "\t") { } 18 58 76
53 18 $cur = ( int( $cur / 8 ) + 1 ) * 8;
54 }
55 else {
56 58 $cur = 0;
57 }
58 76 $text = $3;
59 }
60 446 $cur += length($text);
61 446 return $cur;
62 }
63  
64  
65 122 ▶sub add {
Subroutine add: called CC=13 SCAR=25.7
Pod documented
66 122 my ( $self, $text ) = @_;
67 122 my $break_after = $self->{Break};
68 122 my $break_before = $self->{BreakBefore};
69 122 my $swallow = $self->{Swallow};
70 122 $self->{word} .= $text;
71 122 ▶ while ( length $self->{word} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"word"}) 471 50 521
72 471 my $word;
73 471 ▶ if ( defined($break_before)
Branch: 3 branches
BranchTrueFalseTotal
if (defined $break_before and $self->{'word'} =~ s/^(.+?)($break_before)/$2/s) { } 16 455 471
elsif (defined $break_after and $self->{'word'} =~ s/^(.*?)($break_after)//s) { } 361 94 455
elsif ($self->{'NoBuffering'}) { } 22 72 94
Condition100%3/3
defined $break_before and $self->{"word"} =~ s/^(.+?)($break_before)/$2/s
!ll&&!rl&&r
452316
MC/DC100%2/2
defined $break_before and $self->{"word"} =~ s/^(.+?)($break_before)/$2/s
defined $break_before $self->{"word"} =~ s/^(.+?)($break_before)/$2/s
Truth table100%3/3
A and B
A= defined $break_beforeB= $self->{"word"} =~ s/^(.+?)($break_before)/$2/s
ABresult
0X0
100
111
74 and $self->{word} =~ s/^(.+?)($break_before)/$2/s )
75 {
76 # note- $1 should have at least one character
77 16 $word = $1;
78 }
79 ▶ elsif ( defined($break_after)
Condition100%3/3
defined $break_after and $self->{"word"} =~ s/^(.*?)($break_after)//s
!ll&&!rl&&r
6331361
MC/DC100%2/2
defined $break_after and $self->{"word"} =~ s/^(.*?)($break_after)//s
defined $break_after $self->{"word"} =~ s/^(.*?)($break_after)//s
Truth table100%3/3
A and B
A= defined $break_afterB= $self->{"word"} =~ s/^(.*?)($break_after)//s
ABresult
0X0
100
111
80 and $self->{word} =~ s/^(.*?)($break_after)//s )
81 {
82 361 $word = $1 . $2;
83 }
84 elsif ( $self->{NoBuffering} ) {
85 22 $word = $self->{word};
86 22 $self->{word} = "";
87 }
88 else {
89 72 last;
90 }
91  
92 399 ▶ die "assertion failed" unless length($word) >= 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $word >= 1 0 399 399
93  
94 399 my $next_soft_space;
95 399 ▶ if ( defined($swallow) && $word =~ s/($swallow)$//s ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $swallow and $word =~ s/($swallow)$//s) { } 84 315 399
Condition100%3/3
defined $swallow and $word =~ s/($swallow)$//s
!ll&&!rl&&r
2229384
MC/DC100%2/2
defined $swallow and $word =~ s/($swallow)$//s
defined $swallow $word =~ s/($swallow)$//s
Truth table100%3/3
A and B
A= defined $swallowB= $word =~ s/($swallow)$//s
ABresult
0X0
100
111
96 84 $next_soft_space = $1;
97 }
98 else {
99 315 $next_soft_space = "";
100 }
101  
102 399 my $to_print = $self->{soft_space} . $word;
103 399 my $new_pos = _calculate_new_column( $self->{cur}, $to_print );
104  
105 399 ▶ if ( $new_pos > $self->{Margin} && $self->{may_break} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_pos > $self->{"Margin"} and $self->{"may_break"}) 47 352 399
Condition100%3/3
$new_pos > $self->{"Margin"} and $self->{"may_break"}
!ll&&!rl&&r
350247
MC/DC100%2/2
$new_pos > $self->{"Margin"} and $self->{"may_break"}
$new_pos > $self->{"Margin"} $self->{"may_break"}
Truth table100%3/3
A and B
A= $new_pos > $self->{"Margin"}B= $self->{"may_break"}
ABresult
0X0
100
111
106  
107 # what would happen if we put the separator in?
108 my $w_sep =
109 47 _calculate_new_column( $self->{cur}, $self->{Separator} );
110 47 ▶ if ( $w_sep < $self->{cur} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($w_sep < $self->{"cur"}) 46 1 47
111  
112 # inserting the separator gives us more room,
113 # so do it
114 46 $self->output( $self->{Separator} );
115 46 $self->{soft_space} = "";
116 46 $self->{cur} = $w_sep;
117 46 $self->{word} = $word . $next_soft_space . $self->{word};
118 46 next;
119 }
120 }
121  
122 353 $self->output($to_print);
123 353 $self->{soft_space} = $next_soft_space;
124 353 $self->{cur} = $new_pos;
125 353 $self->{may_break} = 1;
126 }
127 }
128  
129  
130 12 ▶sub finish {
Subroutine finish: called CC=1 SCAR=0.0
Pod documented
131 12 my $self = shift;
132 12 $self->flush;
133 12 $self->reset;
134 }
135  
136  
137 30 ▶sub flush {
Subroutine flush: called CC=1 SCAR=0.0
Pod documented
138 30 my $self = shift;
139  
140 30 local $self->{NoBuffering} = 1;
141 30 local $self->{Swallow} = undef;
142 30 $self->add("");
143 }
144  
145 399 ▶sub output {
Subroutine output: called CC=3 SCAR=11.8
Pod documented
146 399 my $self = shift;
147 399 my $to_print = shift;
148  
149 399 my $out = $self->{Output};
150 399 ▶ if ( UNIVERSAL::isa( $out, "GLOB" ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($out, 'GLOB')) { } 0 399 399
elsif (UNIVERSAL::isa($out, 'SCALAR')) { } 399 0 399
151 0 print $out $to_print;
152 }
153 elsif ( UNIVERSAL::isa( $out, "SCALAR" ) ) {
154 399 $$out .= $to_print;
155 }
156 }
157  
158 12 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
159 12 my $self = shift;
160 12 $self->{cur} = 0;
161 12 $self->{soft_space} = "";
162 12 $self->{word} = "";
163 }
164  
165 2 1;
166  
167 __END__
« DKIM/Canonicalization/DkCommon.pm ↑ Summary DKIM/Canonicalization/seal.pm »