| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Encode::JP::JIS7; | 
| 2 | 14 |  |  | 14 |  | 87 | use strict; | 
|  | 14 |  |  |  |  | 31 |  | 
|  | 14 |  |  |  |  | 322 |  | 
| 3 | 14 |  |  | 14 |  | 57 | use warnings; | 
|  | 14 |  |  |  |  | 29 |  | 
|  | 14 |  |  |  |  | 889 |  | 
| 4 |  |  |  |  |  |  | our $VERSION = do { my @r = ( q$Revision: 2.7 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r }; | 
| 5 |  |  |  |  |  |  |  | 
| 6 | 14 |  |  | 14 |  | 74 | use Encode qw(:fallbacks); | 
|  | 14 |  |  |  |  | 64 |  | 
|  | 14 |  |  |  |  | 2753 |  | 
| 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 |  | 92 | use parent qw(Encode::Encoding); | 
|  | 14 |  |  |  |  | 33 |  | 
|  | 14 |  |  |  |  | 90 |  | 
| 21 |  |  |  |  |  |  |  | 
| 22 |  |  |  |  |  |  | # we override this to 1 so PerlIO works | 
| 23 | 12 |  |  | 12 | 1 | 129 | sub needs_lines { 1 } | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 14 |  |  | 14 |  | 5154 | use Encode::CJKConstants qw(:all); | 
|  | 14 |  |  |  |  | 48 |  | 
|  | 14 |  |  |  |  | 5185 |  | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | # | 
| 28 |  |  |  |  |  |  | # decode is identical for all 2022 variants | 
| 29 |  |  |  |  |  |  | # | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub decode($$;$) { | 
| 32 | 47 |  |  | 47 | 1 | 1313 | my ( $obj, $str, $chk ) = @_; | 
| 33 | 47 | 100 |  |  |  | 154 | return undef unless defined $str; | 
| 34 | 44 |  |  |  |  | 92 | my $residue = ''; | 
| 35 | 44 | 100 |  |  |  | 124 | if ($chk) { | 
| 36 | 26 | 100 |  |  |  | 1237 | $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1; | 
| 37 |  |  |  |  |  |  | } | 
| 38 | 44 |  |  |  |  | 138 | $residue .= jis_euc( \$str ); | 
| 39 | 44 | 100 |  |  |  | 183 | $_[1] = $residue if $chk; | 
| 40 | 44 |  |  |  |  | 189 | return Encode::decode( 'euc-jp', $str, FB_PERLQQ ); | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # | 
| 44 |  |  |  |  |  |  | # encode is different | 
| 45 |  |  |  |  |  |  | # | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub encode($$;$) { | 
| 48 | 1397 |  |  | 1397 | 1 | 11632 | require Encode::JP::H2Z; | 
| 49 | 1397 |  |  |  |  | 2867 | my ( $obj, $utf8, $chk ) = @_; | 
| 50 | 1397 | 100 |  |  |  | 3068 | return undef unless defined $utf8; | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | # empty the input string in the stack so perlio is ok | 
| 53 | 1394 | 100 |  |  |  | 3047 | $_[1] = '' if $chk; | 
| 54 | 1394 |  |  |  |  | 2732 | my ( $h2z, $jis0212 ) = @$obj{qw(h2z jis0212)}; | 
| 55 | 1394 |  |  |  |  | 3188 | my $octet = Encode::encode( 'euc-jp', $utf8, $chk ); | 
| 56 | 1394 | 100 |  |  |  | 4194 | $h2z and &Encode::JP::H2Z::h2z( \$octet ); | 
| 57 | 1394 |  |  |  |  | 3215 | euc_jis( \$octet, $jis0212 ); | 
| 58 | 1394 |  |  |  |  | 15200 | 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 |  | 100 | use bytes; | 
|  | 14 |  |  |  |  | 28 |  | 
|  | 14 |  |  |  |  | 142 |  | 
| 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 | 144 | local ${^ENCODING}; | 
| 114 | 44 |  |  |  |  | 89 | my $r_str = shift; | 
| 115 | 44 |  |  |  |  | 349 | $$r_str =~ s($re_scan_jis) | 
| 116 |  |  |  |  |  |  | { | 
| 117 | 5060 |  |  |  |  | 12028 | my ($esc_0212, $esc_asc, $esc_kana, $chunk) = | 
| 118 |  |  |  |  |  |  | ($1, $2, $3, $4); | 
| 119 | 5060 | 100 |  |  |  | 9687 | if (!$esc_asc) { | 
| 120 | 2530 |  |  |  |  | 4159 | $chunk =~ tr/\x21-\x7e/\xa1-\xfe/; | 
| 121 | 2530 | 100 |  |  |  | 5861 | if ($esc_kana) { | 
|  |  | 100 |  |  |  |  |  | 
| 122 | 2 |  |  |  |  | 62 | $chunk =~ s/([\xa1-\xdf])/\x8e$1/og; | 
| 123 |  |  |  |  |  |  | } | 
| 124 |  |  |  |  |  |  | elsif ($esc_0212) { | 
| 125 | 426 |  |  |  |  | 6546 | $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og; | 
| 126 |  |  |  |  |  |  | } | 
| 127 |  |  |  |  |  |  | } | 
| 128 | 5060 |  |  |  |  | 14460 | $chunk; | 
| 129 |  |  |  |  |  |  | }geox; | 
| 130 | 44 |  |  |  |  | 389 | my ($residue) = ( $$r_str =~ s/(\e.*)$//so ); | 
| 131 | 44 |  |  |  |  | 148 | return $residue; | 
| 132 |  |  |  |  |  |  | } | 
| 133 |  |  |  |  |  |  |  | 
| 134 |  |  |  |  |  |  | sub euc_jis { | 
| 135 | 14 |  |  | 14 |  | 5638 | no warnings qw(uninitialized); | 
|  | 14 |  |  |  |  | 35 |  | 
|  | 14 |  |  |  |  | 3036 |  | 
| 136 | 1394 |  |  | 1394 | 0 | 3643 | local ${^ENCODING}; | 
| 137 | 1394 |  |  |  |  | 2119 | my $r_str   = shift; | 
| 138 | 1394 |  |  |  |  | 1857 | my $jis0212 = shift; | 
| 139 | 1394 |  |  |  |  | 9661 | $$r_str =~ s{ | 
| 140 |  |  |  |  |  |  | ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+) | 
| 141 |  |  |  |  |  |  | }{ | 
| 142 | 3467 |  |  |  |  | 6868 | my $chunk = $1; | 
| 143 |  |  |  |  |  |  | my $esc = | 
| 144 |  |  |  |  |  |  | ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} : | 
| 145 |  |  |  |  |  |  | ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} : | 
| 146 | 3467 | 100 |  |  |  | 9418 | $ESC{JIS_0208}; | 
|  |  | 100 |  |  |  |  |  | 
| 147 | 3467 | 50 | 66 |  |  | 9162 | if ($esc eq $ESC{JIS_0212} && !$jis0212){ | 
| 148 |  |  |  |  |  |  | # fallback to '?' | 
| 149 | 0 |  |  |  |  | 0 | $chunk =~ tr/\xA1-\xFE/\x3F/; | 
| 150 |  |  |  |  |  |  | }else{ | 
| 151 | 3467 |  |  |  |  | 5038 | $chunk =~ tr/\xA1-\xFE/\x21-\x7E/; | 
| 152 |  |  |  |  |  |  | } | 
| 153 | 3467 |  |  |  |  | 16359 | $esc . $chunk . $ESC{ASC}; | 
| 154 |  |  |  |  |  |  | }geox; | 
| 155 | 1394 |  |  |  |  | 3222 | $$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 |  |  |  |  | 2736 | $$r_str; | 
| 158 |  |  |  |  |  |  | } | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | 1; | 
| 161 |  |  |  |  |  |  | __END__ |