Encode] 1.96 Released
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
1 package Encode::JP::JIS7;
2 use strict;
3
4 our $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
5
6 use Encode qw(:fallbacks);
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     $Encode::Encoding{$name} =
13         bless {
14                Name      =>   $name,
15                h2z       =>   $h2z,
16                jis0212   =>   $jis0212,
17               } => __PACKAGE__;
18 }
19
20 use base qw(Encode::Encoding);
21
22 # we override this to 1 so PerlIO works
23 sub needs_lines { 1 }
24
25 use Encode::CJKConstants qw(:all);
26
27 our $DEBUG = 0;
28
29 #
30 # decode is identical for all 2022 variants
31 #
32
33 sub decode($$;$)
34 {
35     my ($obj, $str, $chk) = @_;
36     my $residue = '';
37     if ($chk){
38         $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1;
39     }
40     $residue .= jis_euc(\$str);
41     $_[1] = $residue if $chk;
42     return Encode::decode('euc-jp', $str, FB_PERLQQ);
43 }
44
45 #
46 # encode is different
47 #
48
49 sub encode($$;$)
50 {
51     require Encode::JP::H2Z;
52     my ($obj, $utf8, $chk) = @_;
53     # empty the input string in the stack so perlio is ok
54     $_[1] = '' if $chk;
55     my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
56     my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
57     $h2z and &Encode::JP::H2Z::h2z(\$octet);
58     euc_jis(\$octet, $jis0212);
59     return $octet;
60 }
61
62 #
63 # cat_decode
64 #
65 my $re_scan_jis_g = qr{
66    \G ( ($RE{JIS_0212}) |  $RE{JIS_0208}  |
67         ($RE{ISO_ASC})  | ($RE{JIS_KANA}) | )
68       ([^\e]*)
69 }x;
70 sub cat_decode { # ($obj, $dst, $src, $pos, $trm, $chk)
71     my ($obj, undef, undef, $pos, $trm) = @_; # currently ignores $chk
72     my ($rdst, $rsrc, $rpos) = \@_[1,2,3];
73     local ${^ENCODING};
74     use bytes;
75     my $opos = pos($$rsrc);
76     pos($$rsrc) = $pos;
77     while ($$rsrc =~ /$re_scan_jis_g/gc) {
78         my ($esc, $esc_0212, $esc_asc, $esc_kana, $chunk) =
79           ($1, $2, $3, $4, $5);
80
81         unless ($chunk) { $esc or last;  next; }
82
83         if ($esc && !$esc_asc) {
84             $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
85             if ($esc_kana) {
86                 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
87             } elsif ($esc_0212) {
88                 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
89             }
90             $chunk = Encode::decode('euc-jp', $chunk, 0);
91         }
92         elsif ((my $npos = index($chunk, $trm)) >= 0) {
93             $$rdst .= substr($chunk, 0, $npos + length($trm));
94             $$rpos += length($esc) + $npos + length($trm);
95             pos($$rsrc) = $opos;
96             return 1;
97         }
98         $$rdst .= $chunk;
99         $$rpos = pos($$rsrc);
100     }
101     $$rpos = pos($$rsrc);
102     pos($$rsrc) = $opos;
103     return '';
104 }
105
106 # JIS<->EUC
107 my $re_scan_jis = qr{
108    (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*)
109 }x;
110
111 sub jis_euc {
112     local ${^ENCODING};
113     my $r_str = shift;
114     $$r_str =~ s($re_scan_jis)
115     {
116         my ($esc_0212, $esc_asc, $esc_kana, $chunk) =
117            ($1, $2, $3, $4);
118         if (!$esc_asc) {
119             $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
120             if ($esc_kana) {
121                 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
122             }
123             elsif ($esc_0212) {
124                 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
125             }
126         }
127         $chunk;
128     }geox;
129     my ($residue) = ($$r_str =~ s/(\e.*)$//so);
130     return $residue;
131 }
132
133 sub euc_jis{
134     no warnings qw(uninitialized);
135     my $r_str = shift;
136     my $jis0212 = shift;
137     $$r_str =~ s{
138         ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
139         }{
140             my $chunk = $1;
141             my $esc =
142                 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
143                     ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
144                         $ESC{JIS_0208};
145             if ($esc eq $ESC{JIS_0212} && !$jis0212){
146                 # fallback to '?'
147                 $chunk =~ tr/\xA1-\xFE/\x3F/;
148             }else{
149                 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
150             }
151             $esc . $chunk . $ESC{ASC};
152         }geox;
153     $$r_str =~
154         s/\Q$ESC{ASC}\E
155             (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
156     $$r_str;
157 }
158
159 1;
160 __END__
161
162
163 =head1 NAME
164
165 Encode::JP::JIS7 -- internally used by Encode::JP
166
167 =cut