| 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
MC/DC100%1/1
$self->{"Output"} ||= \*STDOUT
$self->{"Output"}
Truth table100%2/2
A
A= $self->{"Output"}
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 446 | ▶ | while ( $text =~ /^(.*?)([\n\r\t])(.*)$/s ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 51 | 76 | $cur += length($1); | |||||||||||||||||||||||||||||||||||
| 52 | 76 | ▶ | if ( $2 eq "\t" ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 72 | 471 | my $word; | |||||||||||||||||||||||||||||||||||
| 73 | 471 | ▶ | if ( defined($break_before) | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
defined $break_before and $self->{"word"} =~ s/^(.+?)($break_before)/$2/s
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
| |||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||
| 94 | 399 | my $next_soft_space; | |||||||||||||||||||||||||||||||||||
| 95 | 399 | ▶ | if ( defined($swallow) && $word =~ s/($swallow)$//s ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $swallow and $word =~ s/($swallow)$//s
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
| |||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$new_pos > $self->{"Margin"} and $self->{"may_break"}
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"}
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||