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