File Coverage

blib/lib/Encode/EUCJPASCII.pm
Criterion Covered Total %
statement 48 82 58.5
branch 6 30 20.0
condition 0 9 0.0
subroutine 10 12 83.3
pod 4 4 100.0
total 68 137 49.6


line stmt bran cond sub pod time code
1             package Encode::EUCJPASCII;
2 1     1   14368 use strict;
  1         2  
  1         31  
3 1     1   6 use warnings;
  1         2  
  1         71  
4             our $VERSION = "0.03";
5            
6 1     1   111 use Encode qw(:fallbacks);
  1         2  
  1         202  
7 1     1   5 use XSLoader;
  1         1  
  1         139  
8             XSLoader::load(__PACKAGE__,$VERSION);
9              
10             Encode::define_alias(qr/\beuc-?jp(-?open)?(-?19970715)?-?ascii$/i
11             => '"eucJP-ascii"');
12             Encode::define_alias(qr/\b(x-)?iso-?2022-?jp-?ascii$/i
13             => '"x-iso2022jp-ascii"');
14              
15             my $name = 'x-iso2022jp-ascii';
16             $Encode::Encoding{$name} = bless { Name => $name } => __PACKAGE__;
17              
18 1     1   5 use base qw(Encode::Encoding);
  1         2  
  1         115  
19              
20             # we override this to 1 so PerlIO works
21 0     0 1 0 sub needs_lines { 1 }
22              
23 1     1   978 use Encode::CJKConstants qw(:all);
  1         562  
  1         133  
24 1     1   1018 use Encode::JP::JIS7;
  1         3291  
  1         839  
25              
26             # 26 row-cell pairs swapped between JIS C 6226-1978 and JIS X 0208-1983.
27             # cf. JIS X 0208:1997 Annex 2 Table 1.
28             my @swap1978 = ("\x30\x33" => "\x72\x4D", "\x32\x29" => "\x72\x74",
29             "\x33\x42" => "\x69\x5a", "\x33\x49" => "\x59\x78",
30             "\x33\x76" => "\x63\x5e", "\x34\x43" => "\x5e\x75",
31             "\x34\x52" => "\x6b\x5d", "\x37\x5b" => "\x70\x74",
32             "\x39\x5c" => "\x62\x68", "\x3c\x49" => "\x69\x22",
33             "\x3F\x59" => "\x70\x57", "\x41\x28" => "\x6c\x4d",
34             "\x44\x5B" => "\x54\x64", "\x45\x57" => "\x62\x6a",
35             "\x45\x6e" => "\x5b\x6d", "\x45\x73" => "\x5e\x39",
36             "\x46\x76" => "\x6d\x6e", "\x47\x68" => "\x6a\x24",
37             "\x49\x30" => "\x5B\x58", "\x4b\x79" => "\x50\x56",
38             "\x4c\x79" => "\x69\x2e", "\x4F\x36" => "\x64\x46",
39             "\x36\x46" => "\x74\x21", "\x4B\x6A" => "\x74\x22",
40             "\x4D\x5A" => "\x74\x23", "\x60\x76" => "\x74\x24",
41             );
42             my %swap1978 = (@swap1978, reverse @swap1978);
43              
44             sub decode($$;$) {
45 8     8 1 178 my ( $obj, $str, $chk ) = @_;
46 8         16 my $residue = '';
47 8 50       17 if ($chk) {
48 0 0       0 $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1;
49             }
50             # Handle JIS X 0201 sequences.
51 8         17 $str =~ s{\e\(J ([^\e]*) (?:\e\(B)?}{
52 3         7 my $s = $1;
53 3         10 $s =~ s{([\x5C\x7E]+)}{
54 3         5 my $c = $1;
55 3         10 $c =~ s/\x5C/\x21\x6F/g;
56 3         9 $c =~ s/\x7E/\x21\x31/g;
57 3         10 "\e\$B".$c."\e(B";
58             }eg;
59 3 100       17 ($s =~ /^\e/? "\e(B": '').$s;
60             }egsx;
61             # Handle JIS C 6226-1978 sequences.
62 8         19 $str =~ s{\e\$\@ ([^\e]*) (?:\e\$B)?}{
63 1         3 my $s = $1;
64 1 50       5 $s =~ s{([\x21-\x7E]{2})}{$swap1978{$1} || $1}eg;
  52         186  
65 1         6 "\e\$B".$s;
66             }egsx;
67 8         25 $residue .= Encode::JP::JIS7::jis_euc( \$str );
68 8 50       355 $_[1] = $residue if $chk;
69 8         33 return Encode::decode( 'eucJP-ascii', $str, $chk );
70             }
71              
72             sub encode($$;$) {
73 8     8 1 11930 my ( $obj, $utf8, $chk ) = @_;
74              
75             # empty the input string in the stack so perlio is ok
76 8 50       26 $_[1] = '' if $chk;
77 8         24 my $octet = Encode::encode( 'eucJP-ascii', $utf8, $chk );
78 8         237 Encode::JP::JIS7::euc_jis( \$octet, 1 );
79 8         398 return $octet;
80             }
81              
82             #
83             # cat_decode
84             #
85             my $re_scan_jis_g = qr{
86             \G ( ($RE{JIS_0212}) | (\e\$\@) | $RE{JIS_0208} |
87             (\e\(J) | ($RE{ISO_ASC}) | ($RE{JIS_KANA}) | )
88             ([^\e]*)
89             }x;
90              
91             sub cat_decode { # ($obj, $dst, $src, $pos, $trm, $chk)
92 0     0 1   my ( $obj, undef, undef, $pos, $trm ) = @_; # currently ignores $chk
93 0           my ( $rdst, $rsrc, $rpos ) = \@_[ 1, 2, 3 ];
94 0           local ${^ENCODING};
95 1     1   7 use bytes;
  1         3  
  1         6  
96 0           my $opos = pos($$rsrc);
97 0           pos($$rsrc) = $pos;
98 0           while ( $$rsrc =~ /$re_scan_jis_g/gc ) {
99 0           my ( $esc, $esc_0212, $esc_0208_1978, $esc_0201, $esc_asc, $esc_kana, $chunk ) =
100             ( $1, $2, $3, $4, $5, $6, $7 );
101              
102 0 0         unless ($chunk) { $esc or last; next; }
  0 0          
  0            
103            
104 0 0 0       if ( $esc && !$esc_asc && !$esc_0208_1978 && !$esc_0201 ) {
    0 0        
    0 0        
    0          
105 0           $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
106 0 0         if ($esc_kana) {
    0          
107 0           $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
108             }
109             elsif ($esc_0212) {
110 0           $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
111             }
112 0           $chunk = Encode::decode( 'eucJP-ascii', $chunk, 0 );
113             }
114             elsif ( $esc_0208_1978 ) {
115 0 0         $chunk =~ s{([\x21-\x7E]{2})}{$swap1978{$1} || $1}eg;
  0            
116 0           $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
117 0           $chunk = Encode::decode( 'eucJP-ascii', $chunk, 0 );
118             }
119             elsif ( $esc_0201 ) {
120 0           $chunk =~ s/\x5C/\xA1\xEF/og;
121 0           $chunk =~ s/\x7E/\xA1\xB1/og;
122 0           $chunk = Encode::decode( 'eucJP-ascii', $chunk, 0 );
123             }
124             elsif ( ( my $npos = index( $chunk, $trm ) ) >= 0 ) {
125 0           $$rdst .= substr( $chunk, 0, $npos + length($trm) );
126 0           $$rpos += length($esc) + $npos + length($trm);
127 0           pos($$rsrc) = $opos;
128 0           return 1;
129             }
130 0           $$rdst .= $chunk;
131 0           $$rpos = pos($$rsrc);
132             }
133 0           $$rpos = pos($$rsrc);
134 0           pos($$rsrc) = $opos;
135 0           return '';
136             }
137              
138             1;
139             __END__