Encode] 1.96 Released
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
CommitLineData
aae85ceb 1package Encode::JP::JIS7;
2use strict;
3
b5ab1f6f 4our $VERSION = do { my @r = (q$Revision: 1.11 $ =~ /\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){
b5ab1f6f 38 $str =~ s/([^\x00-\x7f].*)$//so and $residue = $1;
af1f55d9 39 }
40 $residue .= jis_euc(\$str);
85982a32 41 $_[1] = $residue if $chk;
6d1c0808 42 return Encode::decode('euc-jp', $str, FB_PERLQQ);
aae85ceb 43}
44
45#
46# encode is different
47#
48
6d1c0808 49sub encode($$;$)
aae85ceb 50{
51 require Encode::JP::H2Z;
85982a32 52 my ($obj, $utf8, $chk) = @_;
53 # empty the input string in the stack so perlio is ok
54 $_[1] = '' if $chk;
aae85ceb 55 my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
6d1c0808 56 my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
85982a32 57 $h2z and &Encode::JP::H2Z::h2z(\$octet);
58 euc_jis(\$octet, $jis0212);
59 return $octet;
aae85ceb 60}
61
220e2d4e 62#
63# cat_decode
64#
65my $re_scan_jis_g = qr{
66 \G ( ($RE{JIS_0212}) | $RE{JIS_0208} |
67 ($RE{ISO_ASC}) | ($RE{JIS_KANA}) | )
68 ([^\e]*)
69}x;
70sub 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}
aae85ceb 105
106# JIS<->EUC
220e2d4e 107my $re_scan_jis = qr{
b536bf57 108 (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*)
109}x;
aae85ceb 110
111sub jis_euc {
b536bf57 112 local ${^ENCODING};
aae85ceb 113 my $r_str = shift;
b536bf57 114 $$r_str =~ s($re_scan_jis)
aae85ceb 115 {
b536bf57 116 my ($esc_0212, $esc_asc, $esc_kana, $chunk) =
117 ($1, $2, $3, $4);
118 if (!$esc_asc) {
85982a32 119 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
b536bf57 120 if ($esc_kana) {
85982a32 121 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
aae85ceb 122 }
b536bf57 123 elsif ($esc_0212) {
85982a32 124 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
aae85ceb 125 }
126 }
85982a32 127 $chunk;
aae85ceb 128 }geox;
85982a32 129 my ($residue) = ($$r_str =~ s/(\e.*)$//so);
130 return $residue;
aae85ceb 131}
132
133sub euc_jis{
0ab8f81e 134 no warnings qw(uninitialized);
aae85ceb 135 my $r_str = shift;
136 my $jis0212 = shift;
137 $$r_str =~ s{
138 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
139 }{
85982a32 140 my $chunk = $1;
6d1c0808 141 my $esc =
85982a32 142 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
143 ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
aae85ceb 144 $ESC{JIS_0208};
145 if ($esc eq $ESC{JIS_0212} && !$jis0212){
146 # fallback to '?'
85982a32 147 $chunk =~ tr/\xA1-\xFE/\x3F/;
aae85ceb 148 }else{
85982a32 149 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
aae85ceb 150 }
85982a32 151 $esc . $chunk . $ESC{ASC};
aae85ceb 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
1591;
160__END__
161
162
163=head1 NAME
164
165Encode::JP::JIS7 -- internally used by Encode::JP
166
167=cut