blib/lib/Encode/MIME/Header/ISO_2022_JP.pm

filter: Statementstmt 95.8% 70 / 73 Branchbran 83.3% 20 / 24 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 1 / 1 totaltotal 91.0% 102 / 112 CC 15 SCAR 16.8
CC
15
Cov
92%
_mime_unstructured_header
9.5
encode
4.0
_add_encoded_word
4.0
CRAP 5.4
« blib/lib/Encode/JP/H2Z.pm ↑ Summary blib/lib/Encode/Encoder.pm »
1 package Encode::MIME::Header::ISO_2022_JP;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 4 ▶use parent qw(Encode::MIME::Header);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 my $obj =
9 bless { decode_b => '1', decode_q => '1', encode => 'B', bpl => 76, Name => 'MIME-Header-ISO_2022_JP' } =>
10 __PACKAGE__;
11 4 Encode::define_encoding($obj, 'MIME-Header-ISO_2022_JP');
12  
13 4 ▶use constant HEAD => '=?ISO-2022-JP?B?';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use constant TAIL => '?=';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶use Encode::CJKConstants qw(%RE);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 our $VERSION = '1.10';
19  
20 # I owe the below codes totally to
21 # Jcode by Dan Kogai & http://www.din.or.jp/~ohzaki/perl.htm#JP_Base64
22  
23 10 ▶sub encode {
Subroutine encode: called CC=4 SCAR=13.9
Pod documented
24 10 my $self = shift;
25 10 my $str = shift;
26 10 ▶ return undef unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 8 10
27  
28 8 ▶ utf8::encode($str) if ( Encode::is_utf8($str) );
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($str) 8 0 8
29 8 Encode::from_to( $str, 'utf8', 'euc-jp' );
30  
31 8 my ($trailing_crlf) = ( $str =~ /(\n|\r|\x0d\x0a)$/o );
32  
33 8 $str = _mime_unstructured_header( $str, $self->{bpl} );
34  
35 8 ▶ not $trailing_crlf and $str =~ s/(\n|\r|\x0d\x0a)$//o;
Branch: 1 branch
BranchTrueFalseTotal
$trailing_crlf or $str =~ s/(\n|\r|\x0d\x0a)$//o 3 5 8
36  
37 8 return $str;
38 }
39  
40 8 ▶sub _mime_unstructured_header {
Subroutine _mime_unstructured_header: called CC=9 SCAR=22.5
41 8 my ( $oldheader, $bpl ) = @_;
42 8 my $crlf = $oldheader =~ /\n$/;
43 8 my ( $header, @words, @wordstmp, $i ) = ('');
44  
45 8 $oldheader =~ s/\s+$//;
46  
47 8 @wordstmp = split /\s+/, $oldheader;
48  
49 8 ▶ for ( $i = 0 ; $i < $#wordstmp ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $#wordstmp) 4 8 12
50 4 ▶ if ( $wordstmp[$i] !~ /^[\x21-\x7E]+$/
Branch: 1 branch
BranchTrueFalseTotal
if (not $wordstmp[$i] =~ /^[\x21-\x7E]+$/ || $wordstmp[$i + 1] =~ /^[\x21-\x7E]+$/) { } 0 4 4
Condition66%2/3
$wordstmp[$i] =~ /^[\x21-\x7E]+$/ || $wordstmp[$i + 1] =~ /^[\x21-\x7E]+$/
l!l&&r!l&&!r
220
MC/DC0%0/2
$wordstmp[$i] =~ /^[\x21-\x7E]+$/ || $wordstmp[$i + 1] =~ /^[\x21-\x7E]+$/
$wordstmp[$i] =~ /^[\x21-\x7E]+$/ $wordstmp[$i + 1] =~ /^[\x21-\x7E]+$/
Truth table66%2/3
A || B
A= $wordstmp[$i] =~ /^[\x21-\x7E]+$/B= $wordstmp[$i + 1] =~ /^[\x21-\x7E]+$/
ABresult
1X1
011
000
51 and $wordstmp[ $i + 1 ] !~ /^[\x21-\x7E]+$/ )
52 {
53 0 $wordstmp[ $i + 1 ] = "$wordstmp[$i] $wordstmp[$i + 1]";
54 }
55 else {
56 4 push( @words, $wordstmp[$i] );
57 }
58 }
59  
60 8 push( @words, $wordstmp[-1] );
61  
62 8 for my $word (@words) {
63 12 ▶ if ( $word =~ /^[\x21-\x7E]+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($word =~ /^[\x21-\x7E]+$/) { } 6 6 12
64 6 $header =~ /(?:.*\n)*(.*)/;
65 6 ▶ if ( length($1) + length($word) > $bpl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($1) + length($word) > $bpl) { } 0 6 6
66 0 $header .= "\n $word";
67 }
68 else {
69 6 $header .= $word;
70 }
71 }
72 else {
73 6 $header = _add_encoded_word( $word, $header, $bpl );
74 }
75  
76 12 $header =~ /(?:.*\n)*(.*)/;
77  
78 12 ▶ if ( length($1) == $bpl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $1 == $bpl) { } 0 12 12
79 0 $header .= "\n ";
80 }
81 else {
82 12 $header .= ' ';
83 }
84 }
85  
86 8 $header =~ s/\n? $//mg;
87  
88 8 ▶ $crlf ? "$header\n" : $header;
Branch: 1 branch
BranchTrueFalseTotal
$crlf ? : 3 5 8
89 }
90  
91 6 ▶sub _add_encoded_word {
Subroutine _add_encoded_word: called CC=4 SCAR=13.9
92 6 my ( $str, $line, $bpl ) = @_;
93 6 my $result = '';
94  
95 6 ▶ while ( length($str) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $str) 8 6 14
96 8 my $target = $str;
97 8 $str = '';
98  
99 8 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (length($line) + 22 + $target =~ /^(?:$Encode::MIME::Header::ISO_2022_JP::RE{"EUC_0212"}|$Encode::MIME::Header::ISO_2022_JP::RE{"EUC_C"})/o * 8 > $bpl) 2 6 8
100 length($line) + 22 +
101 ( $target =~ /^(?:$RE{EUC_0212}|$RE{EUC_C})/o ) * 8 > $bpl )
102 {
103 2 $line =~ s/[ \t\n\r]*$/\n/;
104 2 $result .= $line;
105 2 $line = ' ';
106 }
107  
108 8 while (1) {
109 18 my $iso_2022_jp = $target;
110 18 Encode::from_to( $iso_2022_jp, 'euc-jp', 'iso-2022-jp' );
111  
112 18 my $encoded =
113 HEAD . MIME::Base64::encode_base64( $iso_2022_jp, '' ) . TAIL;
114  
115 18 ▶ if ( length($encoded) + length($line) > $bpl ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length($encoded) + length($line) > $bpl) { } 10 8 18
116 10 $target =~
117 s/($RE{EUC_0212}|$RE{EUC_KANA}|$RE{EUC_C}|$RE{ASCII})$//o;
118 10 $str = $1 . $str;
119 }
120 else {
121 8 $line .= $encoded;
122 8 last;
123 }
124 }
125  
126 }
127  
128 6 $result . $line;
129 }
130  
131 4 1;
132 __END__
« blib/lib/Encode/JP/H2Z.pm ↑ Summary blib/lib/Encode/Encoder.pm »