[Patch] parsing under encoding (Re: [Encode] HEADS-UP; $Encode::VERSION++ to enhance...
[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.9 $ =~ /\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;
39         $1 and $residue = $1;
40     }
41     $residue .= jis_euc(\$str);
42     $_[1] = $residue if $chk;
43     return Encode::decode('euc-jp', $str, FB_PERLQQ);
44 }
45
46 #
47 # encode is different
48 #
49
50 sub encode($$;$)
51 {
52     require Encode::JP::H2Z;
53     my ($obj, $utf8, $chk) = @_;
54     # empty the input string in the stack so perlio is ok
55     $_[1] = '' if $chk;
56     my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
57     my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
58     $h2z and &Encode::JP::H2Z::h2z(\$octet);
59     euc_jis(\$octet, $jis0212);
60     return $octet;
61 }
62
63 #
64 # cat_decode
65 #
66 my $re_scan_jis_g = qr{
67    \G ( ($RE{JIS_0212}) |  $RE{JIS_0208}  |
68         ($RE{ISO_ASC})  | ($RE{JIS_KANA}) | )
69       ([^\e]*)
70 }x;
71 sub cat_decode { # ($obj, $dst, $src, $pos, $trm, $chk)
72     my ($obj, undef, undef, $pos, $trm) = @_; # currently ignores $chk
73     my ($rdst, $rsrc, $rpos) = \@_[1,2,3];
74     local ${^ENCODING};
75     use bytes;
76     my $opos = pos($$rsrc);
77     pos($$rsrc) = $pos;
78     while ($$rsrc =~ /$re_scan_jis_g/gc) {
79         my ($esc, $esc_0212, $esc_asc, $esc_kana, $chunk) =
80           ($1, $2, $3, $4, $5);
81
82         unless ($chunk) { $esc or last;  next; }
83
84         if ($esc && !$esc_asc) {
85             $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
86             if ($esc_kana) {
87                 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
88             } elsif ($esc_0212) {
89                 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
90             }
91             $chunk = Encode::decode('euc-jp', $chunk, 0);
92         }
93         elsif ((my $npos = index($chunk, $trm)) >= 0) {
94             $$rdst .= substr($chunk, 0, $npos + length($trm));
95             $$rpos += length($esc) + $npos + length($trm);
96             pos($$rsrc) = $opos;
97             return 1;
98         }
99         $$rdst .= $chunk;
100         $$rpos = pos($$rsrc);
101     }
102     $$rpos = pos($$rsrc);
103     pos($$rsrc) = $opos;
104     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     local ${^ENCODING};
114     my $r_str = shift;
115     $$r_str =~ s($re_scan_jis)
116     {
117         my ($esc_0212, $esc_asc, $esc_kana, $chunk) =
118            ($1, $2, $3, $4);
119         if (!$esc_asc) {
120             $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
121             if ($esc_kana) {
122                 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
123             }
124             elsif ($esc_0212) {
125                 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
126             }
127         }
128         $chunk;
129     }geox;
130     my ($residue) = ($$r_str =~ s/(\e.*)$//so);
131     return $residue;
132 }
133
134 sub euc_jis{
135     no warnings qw(uninitialized);
136     my $r_str = shift;
137     my $jis0212 = shift;
138     $$r_str =~ s{
139         ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
140         }{
141             my $chunk = $1;
142             my $esc =
143                 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
144                     ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
145                         $ESC{JIS_0208};
146             if ($esc eq $ESC{JIS_0212} && !$jis0212){
147                 # fallback to '?'
148                 $chunk =~ tr/\xA1-\xFE/\x3F/;
149             }else{
150                 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
151             }
152             $esc . $chunk . $ESC{ASC};
153         }geox;
154     $$r_str =~
155         s/\Q$ESC{ASC}\E
156             (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
157     $$r_str;
158 }
159
160 1;
161 __END__
162
163
164 =head1 NAME
165
166 Encode::JP::JIS7 -- internally used by Encode::JP
167
168 =cut