File Coverage

blib/lib/Encode/JP/JIS7.pm
Criterion Covered Total %
statement 62 88 70.4
branch 25 38 65.7
condition 2 6 33.3
subroutine 12 13 92.3
pod 4 6 66.6
total 105 151 69.5


line stmt bran cond sub pod time code
1             package Encode::JP::JIS7;
2 14     14   110 use strict;
  14         40  
  14         442  
3 14     14   94 use warnings;
  14         39  
  14         1245  
4             our $VERSION = do { my @r = ( q$Revision: 2.7 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
5              
6 14     14   105 use Encode qw(:fallbacks);
  14         48  
  14         3715  
7              
8             for my $name ( '7bit-jis', 'iso-2022-jp', 'iso-2022-jp-1' ) {
9             my $h2z = ( $name eq '7bit-jis' ) ? 0 : 1;
10             my $jis0212 = ( $name eq 'iso-2022-jp' ) ? 0 : 1;
11              
12             my $obj = bless {
13             Name => $name,
14             h2z => $h2z,
15             jis0212 => $jis0212,
16             } => __PACKAGE__;
17             Encode::define_encoding($obj, $name);
18             }
19              
20 14     14   138 use parent qw(Encode::Encoding);
  14         37  
  14         174  
21              
22             # we override this to 1 so PerlIO works
23 12     12 1 206 sub needs_lines { 1 }
24              
25 14     14   6635 use Encode::CJKConstants qw(:all);
  14         56  
  14         6953  
26              
27             #
28             # decode is identical for all 2022 variants
29             #
30              
31             sub decode($$;$) {
32 47     47 1 2120 my ( $obj, $str, $chk ) = @_;
33 47 100       211 return undef unless defined $str;
34 44         130 my $residue = '';
35 44 100       146 if ($chk) {
36 26 100       1772 $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1;
37             }
38 44         208 $residue .= jis_euc( \$str );
39 44 100       263 $_[1] = $residue if $chk;
40 44         241 return Encode::decode( 'euc-jp', $str, FB_PERLQQ );
41             }
42              
43             #
44             # encode is different
45             #
46              
47             sub encode($$;$) {
48 1397     1397 1 18443 require Encode::JP::H2Z;
49 1397         4432 my ( $obj, $utf8, $chk ) = @_;
50 1397 100       4513 return undef unless defined $utf8;
51              
52             # empty the input string in the stack so perlio is ok
53 1394 100       4167 $_[1] = '' if $chk;
54 1394         4118 my ( $h2z, $jis0212 ) = @$obj{qw(h2z jis0212)};
55 1394         4857 my $octet = Encode::encode( 'euc-jp', $utf8, $chk );
56 1394 100       6801 $h2z and &Encode::JP::H2Z::h2z( \$octet );
57 1394         5056 euc_jis( \$octet, $jis0212 );
58 1394         25071 return $octet;
59             }
60              
61             #
62             # cat_decode
63             #
64             my $re_scan_jis_g = qr{
65             \G ( ($RE{JIS_0212}) | $RE{JIS_0208} |
66             ($RE{ISO_ASC}) | ($RE{JIS_KANA}) | )
67             ([^\e]*)
68             }x;
69              
70             sub cat_decode { # ($obj, $dst, $src, $pos, $trm, $chk)
71 0     0 1 0 my ( $obj, undef, undef, $pos, $trm ) = @_; # currently ignores $chk
72 0         0 my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ];
73 0         0 local ${^ENCODING};
74 14     14   122 use bytes;
  14         40  
  14         116  
75 0         0 my $opos = pos($$rsrc);
76 0         0 pos($$rsrc) = $pos;
77 0         0 while ( $$rsrc =~ /$re_scan_jis_g/gc ) {
78 0         0 my ( $esc, $esc_0212, $esc_asc, $esc_kana, $chunk ) =
79             ( $1, $2, $3, $4, $5 );
80              
81 0 0       0 unless ($chunk) { $esc or last; next; }
  0 0       0  
  0         0  
82              
83 0 0 0     0 if ( $esc && !$esc_asc ) {
    0          
84 0         0 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
85 0 0       0 if ($esc_kana) {
    0          
86 0         0 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
87             }
88             elsif ($esc_0212) {
89 0         0 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
90             }
91 0         0 $chunk = Encode::decode( 'euc-jp', $chunk, 0 );
92             }
93             elsif ( ( my $npos = index( $chunk, $trm ) ) >= 0 ) {
94 0         0 $$rdst .= substr( $chunk, 0, $npos + length($trm) );
95 0         0 $$rpos += length($esc) + $npos + length($trm);
96 0         0 pos($$rsrc) = $opos;
97 0         0 return 1;
98             }
99 0         0 $$rdst .= $chunk;
100 0         0 $$rpos = pos($$rsrc);
101             }
102 0         0 $$rpos = pos($$rsrc);
103 0         0 pos($$rsrc) = $opos;
104 0         0 return '';
105             }
106              
107             # JIS<->EUC
108             my $re_scan_jis = qr{
109             (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*)
110             }x;
111              
112             sub jis_euc {
113 44     44 0 203 local ${^ENCODING};
114 44         114 my $r_str = shift;
115 44         448 $$r_str =~ s($re_scan_jis)
116             {
117 5060         17235 my ($esc_0212, $esc_asc, $esc_kana, $chunk) =
118             ($1, $2, $3, $4);
119 5060 100       12651 if (!$esc_asc) {
120 2530         5852 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
121 2530 100       7501 if ($esc_kana) {
    100          
122 2         35 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
123             }
124             elsif ($esc_0212) {
125 426         6729 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
126             }
127             }
128 5060         21359 $chunk;
129             }geox;
130 44         511 my ($residue) = ( $$r_str =~ s/(\e.*)$//so );
131 44         214 return $residue;
132             }
133              
134             sub euc_jis {
135 14     14   7952 no warnings qw(uninitialized);
  14         43  
  14         4122  
136 1394     1394 0 5706 local ${^ENCODING};
137 1394         3051 my $r_str = shift;
138 1394         2762 my $jis0212 = shift;
139 1394         14529 $$r_str =~ s{
140             ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
141             }{
142 3467         9349 my $chunk = $1;
143             my $esc =
144             ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
145             ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
146 3467 100       12826 $ESC{JIS_0208};
    100          
147 3467 50 66     12161 if ($esc eq $ESC{JIS_0212} && !$jis0212){
148             # fallback to '?'
149 0         0 $chunk =~ tr/\xA1-\xFE/\x3F/;
150             }else{
151 3467         7009 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
152             }
153 3467         22827 $esc . $chunk . $ESC{ASC};
154             }geox;
155 1394         4943 $$r_str =~ s/\Q$ESC{ASC}\E
156             (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
157 1394         4462 $$r_str;
158             }
159              
160             1;
161             __END__