Upgrade to Encode 1.50, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
CommitLineData
aae85ceb 1package Encode::JP::JIS7;
2use strict;
3
85982a32 4our $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
aae85ceb 5
6require Encode;
7for my $name ('7bit-jis', 'iso-2022-jp', 'iso-2022-jp-1'){
8 my $h2z = ($name eq '7bit-jis') ? 0 : 1;
9 my $jis0212 = ($name eq 'iso-2022-jp') ? 0 : 1;
10
11 $Encode::Encoding{$name} =
12 bless {
13 Name => $name,
14 h2z => $h2z,
15 jis0212 => $jis0212,
16 } => __PACKAGE__;
17}
18
19sub name { shift->{'Name'} }
20sub new_sequence { $_[0] };
21
22use Encode::CJKConstants qw(:all);
23
85982a32 24our $DEBUG = 0;
25
aae85ceb 26#
27# decode is identical for all 2022 variants
28#
29
30sub decode
31{
32 my ($obj,$str,$chk) = @_;
85982a32 33 my $residue = jis_euc(\$str);
34 # This is for PerlIO
35 $_[1] = $residue if $chk;
36 # use perlqq fallback for euc-jp -> utf8
37 return Encode::decode('euc-jp', $str, 0);
aae85ceb 38}
39
40#
41# encode is different
42#
43
44sub encode
45{
46 require Encode::JP::H2Z;
85982a32 47 my ($obj, $utf8, $chk) = @_;
48 # empty the input string in the stack so perlio is ok
49 $_[1] = '' if $chk;
aae85ceb 50 my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
85982a32 51 my $octet = Encode::encode('euc-jp', $utf8, 0) ;
52 $h2z and &Encode::JP::H2Z::h2z(\$octet);
53 euc_jis(\$octet, $jis0212);
54 return $octet;
aae85ceb 55}
56
57
58# JIS<->EUC
59
60sub jis_euc {
61 my $r_str = shift;
62 $$r_str =~ s(
63 ($RE{JIS_0212}|$RE{JIS_0208}|$RE{ISO_ASC}|$RE{JIS_KANA})
64 ([^\e]*)
65 )
66 {
85982a32 67 my ($esc, $chunk) = ($1, $2);
aae85ceb 68 if ($esc !~ /$RE{ISO_ASC}/o) {
85982a32 69 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
aae85ceb 70 if ($esc =~ /$RE{JIS_KANA}/o) {
85982a32 71 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
aae85ceb 72 }
73 elsif ($esc =~ /$RE{JIS_0212}/o) {
85982a32 74 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
aae85ceb 75 }
76 }
85982a32 77 $chunk;
aae85ceb 78 }geox;
85982a32 79 my ($residue) = ($$r_str =~ s/(\e.*)$//so);
80 return $residue;
aae85ceb 81}
82
83sub euc_jis{
84 my $r_str = shift;
85 my $jis0212 = shift;
86 $$r_str =~ s{
87 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
88 }{
85982a32 89 my $chunk = $1;
aae85ceb 90 my $esc =
85982a32 91 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
92 ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
aae85ceb 93 $ESC{JIS_0208};
94 if ($esc eq $ESC{JIS_0212} && !$jis0212){
95 # fallback to '?'
85982a32 96 $chunk =~ tr/\xA1-\xFE/\x3F/;
aae85ceb 97 }else{
85982a32 98 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
aae85ceb 99 }
85982a32 100 $esc . $chunk . $ESC{ASC};
aae85ceb 101 }geox;
102 $$r_str =~
103 s/\Q$ESC{ASC}\E
104 (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
105 $$r_str;
106}
107
1081;
109__END__
110
111
112=head1 NAME
113
114Encode::JP::JIS7 -- internally used by Encode::JP
115
116=cut