Oops in change #19809.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
CommitLineData
aae85ceb 1package Encode::JP::JIS7;
2use strict;
3
151b5d36 4our $VERSION = do { my @r = (q$Revision: 1.10 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
6d1c0808 5
6use Encode qw(:fallbacks);
aae85ceb 7
aae85ceb 8for 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;
6d1c0808 11
12 $Encode::Encoding{$name} =
aae85ceb 13 bless {
14 Name => $name,
15 h2z => $h2z,
16 jis0212 => $jis0212,
17 } => __PACKAGE__;
18}
19
10c5ecbb 20use base qw(Encode::Encoding);
6d1c0808 21
10c5ecbb 22# we override this to 1 so PerlIO works
6d1c0808 23sub needs_lines { 1 }
aae85ceb 24
25use Encode::CJKConstants qw(:all);
26
85982a32 27our $DEBUG = 0;
28
aae85ceb 29#
30# decode is identical for all 2022 variants
31#
32
6d1c0808 33sub decode($$;$)
aae85ceb 34{
af1f55d9 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);
85982a32 42 $_[1] = $residue if $chk;
6d1c0808 43 return Encode::decode('euc-jp', $str, FB_PERLQQ);
aae85ceb 44}
45
46#
47# encode is different
48#
49
6d1c0808 50sub encode($$;$)
aae85ceb 51{
52 require Encode::JP::H2Z;
85982a32 53 my ($obj, $utf8, $chk) = @_;
54 # empty the input string in the stack so perlio is ok
55 $_[1] = '' if $chk;
aae85ceb 56 my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
6d1c0808 57 my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
85982a32 58 $h2z and &Encode::JP::H2Z::h2z(\$octet);
59 euc_jis(\$octet, $jis0212);
60 return $octet;
aae85ceb 61}
62
220e2d4e 63#
64# cat_decode
65#
66my $re_scan_jis_g = qr{
67 \G ( ($RE{JIS_0212}) | $RE{JIS_0208} |
68 ($RE{ISO_ASC}) | ($RE{JIS_KANA}) | )
69 ([^\e]*)
70}x;
71sub 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}
aae85ceb 106
107# JIS<->EUC
220e2d4e 108my $re_scan_jis = qr{
b536bf57 109 (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*)
110}x;
aae85ceb 111
112sub jis_euc {
b536bf57 113 local ${^ENCODING};
aae85ceb 114 my $r_str = shift;
b536bf57 115 $$r_str =~ s($re_scan_jis)
aae85ceb 116 {
b536bf57 117 my ($esc_0212, $esc_asc, $esc_kana, $chunk) =
118 ($1, $2, $3, $4);
119 if (!$esc_asc) {
85982a32 120 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
b536bf57 121 if ($esc_kana) {
85982a32 122 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
aae85ceb 123 }
b536bf57 124 elsif ($esc_0212) {
85982a32 125 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
aae85ceb 126 }
127 }
85982a32 128 $chunk;
aae85ceb 129 }geox;
85982a32 130 my ($residue) = ($$r_str =~ s/(\e.*)$//so);
131 return $residue;
aae85ceb 132}
133
134sub euc_jis{
0ab8f81e 135 no warnings qw(uninitialized);
aae85ceb 136 my $r_str = shift;
137 my $jis0212 = shift;
138 $$r_str =~ s{
139 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
140 }{
85982a32 141 my $chunk = $1;
6d1c0808 142 my $esc =
85982a32 143 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
144 ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
aae85ceb 145 $ESC{JIS_0208};
146 if ($esc eq $ESC{JIS_0212} && !$jis0212){
147 # fallback to '?'
85982a32 148 $chunk =~ tr/\xA1-\xFE/\x3F/;
aae85ceb 149 }else{
85982a32 150 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
aae85ceb 151 }
85982a32 152 $esc . $chunk . $ESC{ASC};
aae85ceb 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
1601;
161__END__
162
163
164=head1 NAME
165
166Encode::JP::JIS7 -- internally used by Encode::JP
167
168=cut