Encode/MIME/EncWords.pm

filter: Statementstmt 65.5% 78 / 119 Branchbran 37.5% 30 / 80 Conditioncond 76.9% 10 / 13 MC/DCmcdc 62.5% 5 / 8 Subroutinesub 80.0% 8 / 10 Podpod 100% 5 / 5 totaltotal 57.8% 136 / 235 CC 49 SCAR 45.5
CC
49
Cov
56%
decode
167.0
encode
47.8
config
4.2
CRAP 94.6
↑ Summary MIME/EncWords.pm »
1 # -*- perl -*-
2  
3 package Encode::MIME::EncWords;
4 2 require 5.007003;
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use MIME::EncWords;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = '0.03';
12  
13 # Default of options
14 my $Config = {
15 Charset => 'UTF-8',
16 # Encoding => specified by each subclass.
17 # Folding => fixes to "\n".
18 # Replacement => given by encode()/decode().
19 # others => derived from MIME::EncWords:
20 2 map { ($_ => $MIME::EncWords::Config->{$_}) }
21 qw(Detect7bit Field Mapping MaxLineLen Minimal)
22 };
23  
24 2 $Encode::Encoding{'MIME-EncWords'} = bless {
25 Encoding => 'A',
26 Name => 'MIME-EncWords',
27 } => __PACKAGE__;
28  
29 2 $Encode::Encoding{'MIME-EncWords-B'} = bless {
30 Encoding => 'B',
31 Name => 'MIME-EncWords-B',
32 } => __PACKAGE__;
33  
34 2 $Encode::Encoding{'MIME-EncWords-Q'} = bless {
35 Encoding => 'Q',
36 Name => 'MIME-EncWords-Q',
37 } => __PACKAGE__;
38  
39 2 $Encode::Encoding{'MIME-EncWords-ISO_2022_JP'} = bless {
40 Charset => 'ISO-2022-JP',
41 Encoding => 'B',
42 Name => 'MIME-EncWords-ISO_2022_JP',
43 } => __PACKAGE__;
44  
45 2 ▶use base qw(Encode::Encoding);
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47 0 ▶sub needs_lines { 1 }
Subroutine needs_lines: not called CC=1 SCAR=6.9
Pod documented
48 0 ▶sub perlio_ok { 0 }
Subroutine perlio_ok: not called CC=1 SCAR=6.9
Pod documented
49  
50 11 ▶sub decode($$;$) {
Subroutine decode: called CC=27 SCAR=51.2
Pod documented
51 11 my ($obj, $str, $chk) = @_;
52  
53 11 ▶ my %opts = map { ($_ => ($obj->{$_} || $Config->{$_})) }
Condition33%1/3
$obj->{$_} || $Config->{$_}
l!l&&r!l&&!r
0220
MC/DC0%0/2
$obj->{$_} || $Config->{$_}
$obj->{$_} $Config->{$_}
Truth table33%1/3
A || B
A= $obj->{$_}B= $Config->{$_}
ABresult
1X1
011
000
54 qw(Detect7bit Mapping);
55 11 ▶ $chk = 0 if ref $chk; # coderef not supported.
Branch: 1 branch
BranchTrueFalseTotal
if ref $chk 0 11 11
56 11 ▶ my $repl = ($chk & 4) ? ($chk & ~4 | 1) : $chk;
Branch: 1 branch
BranchTrueFalseTotal
$chk & 4 ? : 0 11 11
57  
58 11 local $@;
59 11 my $skip = 0; # for RETURN_ON_ERR
60 11 my $ret = undef;
61 11 pos($str) = 0;
62 11 foreach my $line (
63 $str =~ m{ \G (.*?) (?:\r\n|[\r\n]) (?![ \t]) }cgsx,
64 substr($str, pos($str))
65 ) {
66 32 ▶ if (defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) { } 21 11 32
67 21 ▶ $ret .= "\n" unless $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 21 0 21
68 } else {
69 11 $ret = '';
70 }
71 32 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 32 32
72 0 $_[1] .= "\n";
73 0 $_[1] .= $line;
74 0 next;
75 }
76 32 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 8 24 32
77  
78 24 my @words = MIME::EncWords::decode_mimewords($line, %opts);
79 24 ▶ if ($@) { # broken MIME encoding.
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 24 24
80 0 ▶ croak $@ if $chk & 1; # DIE_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 1 0 0 0
81 0 ▶ carp $@ if $chk & 2; # WARN_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 2 0 0 0
82 0 ▶ if ($chk & 4) { # RETURN_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if ($chk & 4) 0 0 0
83 0 $_[1] = $line;
84 0 $skip = 1;
85 0 next;
86 }
87 }
88 24 ▶ for (my $i = 0; $i <= $#words; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#words) 58 24 82
89 58 my $word = $words[$i];
90 my $cset = MIME::Charset->new(($word->[1] || 'US-ASCII'),
91 58 ▶ Mapping => $opts{Mapping});
Condition100%2/2
$word->[1] || 'US-ASCII'
l!l
2533
MC/DC100%1/1
$word->[1] || 'US-ASCII'
$word->[1]
Truth table100%2/2
A
A= $word->[1]
Aresult
11
00
92 58 ▶ if (! $cset->decoder) { # unknown charset or ``8BIT''.
Branch: 1 branch
BranchTrueFalseTotal
unless ($cset->decoder) 0 58 58
93 0 $@ = 'Unknown charset "'.$cset->as_string.'"';
94 0 ▶ croak $@ if $chk & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 1 0 0 0
95 0 ▶ carp $@ if $chk & 2;
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 2 0 0 0
96 0 ▶ if ($chk & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chk & 4) 0 0 0
97 # already decoded... re-encoding
98 0 $_[1] =
99 MIME::EncWords::encode_mimewords([splice @words, $i],
100 Encoding => 'B',
101 Folding => '',
102 MaxLineLen => -1);
103 0 $skip = 1;
104 0 last;
105 }
106 0 $ret .= Encode::decode("ISO-8859-1", $word->[0], 0); #FIXME
107  
108 0 next;
109 }
110 58 eval {
111 58 $ret .= $cset->decode($word->[0], $repl);
112 };
113 58 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 58 58
114 0 $@ =~ s/ at .+? line \d+[.\n]*$//;
115 0 ▶ croak $@ if $chk & 1;
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 1 0 0 0
116 0 ▶ carp $@ if $chk & 2;
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 2 0 0 0
117 0 ▶ if ($chk & 4) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chk & 4) 0 0 0
118 # already decoded... re-encoding
119 0 $_[1] =
120 MIME::EncWords::encode_mimewords([splice @words, $i],
121 Encoding => 'B',
122 Folding => '',
123 MaxLineLen => -1);
124 0 $skip = 1;
125 0 last;
126 }
127 }
128 }
129 }
130  
131 11 ▶ if ($chk & 4) { # RETURN_ON_ERR
Branch: 2 branches
BranchTrueFalseTotal
if ($chk & 4) { } 0 11 11
elsif ($chk) { } 0 11 11
132 0 ▶ $_[1] = '' unless $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 0 0 0
133 } elsif ($chk) { # ! LEAVE_SRC
134 0 ▶ $_[1] = $ret unless $chk & 8;
Branch: 1 branch
BranchTrueFalseTotal
unless $chk & 8 0 0 0
135 }
136 11 return $ret;
137 }
138  
139 18 ▶sub encode($$;$) {
Subroutine encode: called CC=20 SCAR=38.7
Pod documented
140 18 my ($obj, $str, $chk) = @_;
141  
142 18 ▶ my %opts = map { ($_ => ($obj->{$_} || $Config->{$_})) }
Condition100%3/3
$obj->{$_} || $Config->{$_}
l!l&&r!l&&!r
248418
MC/DC100%2/2
$obj->{$_} || $Config->{$_}
$obj->{$_} $Config->{$_}
Truth table100%3/3
A || B
A= $obj->{$_}B= $Config->{$_}
ABresult
1X1
011
000
143 qw(Charset Detect7bit Encoding Field Mapping MaxLineLen Minimal);
144 18 ▶ $opts{Charset} ||= 'UTF-8';
Condition50%1/2
$opts{"Charset"} ||= "UTF-8"
l!l
180
MC/DC0%0/1
$opts{"Charset"} ||= "UTF-8"
$opts{"Charset"}
Truth table50%1/2
A
A= $opts{"Charset"}
Aresult
11
00
145 18 $opts{Folding} = "\n";
146 18 ▶ $chk = 0 if ref $chk; # coderef not supported.
Branch: 1 branch
BranchTrueFalseTotal
if ref $chk 0 18 18
147 18 ▶ my $repl = ($chk & 4) ? ($chk & ~4 | 1) : $chk;
Branch: 1 branch
BranchTrueFalseTotal
$chk & 4 ? : 0 18 18
148  
149 18 ▶ $str = Encode::decode('ISO-8859-1', $str)
Branch: 1 branch
BranchTrueFalseTotal
if not &Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/ 1 17 18
Condition100%3/3
not &Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/
!ll&&!rl&&r
1521
MC/DC100%2/2
not &Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/
not &Encode::is_utf8($str) $str =~ /[^\x00-\x7F]/
Truth table100%3/3
not(A) and B
A= not &Encode::is_utf8($str)B= $str =~ /[^\x00-\x7F]/
ABresult
0X0
100
111
150 if ! Encode::is_utf8($str) and $str =~ /[^\x00-\x7F]/;
151  
152 18 local $@;
153 18 my $skip = 0; # for RETURN_ON_ERR
154 18 my $ret = undef;
155 18 pos($str) = 0;
156 18 foreach my $line (
157 $str =~ m{ \G (.*?) (?:\r\n|[\r\n]) (?![ \t]) }cgsx,
158 substr($str, pos($str))
159 ) {
160 27 ▶ if (defined $ret) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ret) { } 9 18 27
161 9 ▶ $ret .= "\n" unless $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 9 0 9
162 } else {
163 18 $ret = '';
164 }
165 27 ▶ if ($skip) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip) 0 27 27
166 0 $_[1] .= "\n";
167 0 $_[1] .= $line;
168 0 next;
169 }
170 27 ▶ next unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 5 22 27
171  
172 22 eval {
173 22 $ret .= MIME::EncWords::encode_mimewords($line, %opts,
174 Replacement => $repl);
175 };
176 22 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 22 22
177 0 $@ =~ s/ at .+? line \d+[.\n]*$//;
178 0 ▶ croak $@ if $chk & 1; # DIE_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 1 0 0 0
179 0 ▶ carp $@ if $chk & 2; # WARN_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if $chk & 2 0 0 0
180 0 ▶ if ($chk & 4) { # RETURN_ON_ERR
Branch: 1 branch
BranchTrueFalseTotal
if ($chk & 4) 0 0 0
181 0 $_[1] = $line;
182 0 $skip = 1;
183 0 next;
184 }
185 }
186 }
187  
188 18 ▶ if ($chk & 4) { # RETURN_ON_ERR
Branch: 2 branches
BranchTrueFalseTotal
if ($chk & 4) { } 0 18 18
elsif ($chk) { } 0 18 18
189 0 ▶ $_[1] = '' unless $skip;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 0 0 0
190 } elsif ($chk) { # ! LEAVE_SRC
191 0 ▶ $_[1] = '' unless $chk & 8; # FIXME:spec?
Branch: 1 branch
BranchTrueFalseTotal
unless $chk & 8 0 0 0
192 }
193 18 return $ret;
194 }
195  
196 2 ▶sub config {
Subroutine config: called CC=4 SCAR=14.3
Pod documented
197 2 ▶ my $klass = shift if scalar @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ % 2 2 0 2
198 2 my %opts = @_;
199 2 foreach my $key (keys %opts) {
200 2 ▶ croak "Unknown config option: $key" unless exists $Config->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $Config->{$key} 0 2 2
201 2 $Config->{$key} = $opts{$key};
202 }
203 }
204  
205 2 1;
206 __END__
↑ Summary MIME/EncWords.pm »