Update Changes.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
CommitLineData
aae85ceb 1package Encode::JP::JIS7;
2use strict;
3
af1f55d9 4our $VERSION = do { my @r = (q$Revision: 1.7 $ =~ /\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
20sub name { shift->{'Name'} }
6d1c0808 21
22sub new_sequence { $_[0] }
23
24sub needs_lines { 1 }
aae85ceb 25
0ab8f81e 26sub perlio_ok {
011b2d2f 27 eval{ require PerlIO::encoding };
28 if ($@){
29 return 0;
30 }else{
31 return (PerlIO::encoding->VERSION >= 0.03);
32 }
0ab8f81e 33}
34
aae85ceb 35use Encode::CJKConstants qw(:all);
36
85982a32 37our $DEBUG = 0;
38
aae85ceb 39#
40# decode is identical for all 2022 variants
41#
42
6d1c0808 43sub decode($$;$)
aae85ceb 44{
af1f55d9 45 my ($obj, $str, $chk) = @_;
46 my $residue = '';
47 if ($chk){
48 $str =~ s/([^\x00-\x7f].*)$//so;
49 $1 and $residue = $1;
50 }
51 $residue .= jis_euc(\$str);
85982a32 52 $_[1] = $residue if $chk;
6d1c0808 53 return Encode::decode('euc-jp', $str, FB_PERLQQ);
aae85ceb 54}
55
56#
57# encode is different
58#
59
6d1c0808 60sub encode($$;$)
aae85ceb 61{
62 require Encode::JP::H2Z;
85982a32 63 my ($obj, $utf8, $chk) = @_;
64 # empty the input string in the stack so perlio is ok
65 $_[1] = '' if $chk;
aae85ceb 66 my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
6d1c0808 67 my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
85982a32 68 $h2z and &Encode::JP::H2Z::h2z(\$octet);
69 euc_jis(\$octet, $jis0212);
70 return $octet;
aae85ceb 71}
72
73
74# JIS<->EUC
75
76sub jis_euc {
77 my $r_str = shift;
78 $$r_str =~ s(
79 ($RE{JIS_0212}|$RE{JIS_0208}|$RE{ISO_ASC}|$RE{JIS_KANA})
80 ([^\e]*)
81 )
82 {
85982a32 83 my ($esc, $chunk) = ($1, $2);
aae85ceb 84 if ($esc !~ /$RE{ISO_ASC}/o) {
85982a32 85 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
aae85ceb 86 if ($esc =~ /$RE{JIS_KANA}/o) {
85982a32 87 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
aae85ceb 88 }
89 elsif ($esc =~ /$RE{JIS_0212}/o) {
85982a32 90 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
aae85ceb 91 }
92 }
85982a32 93 $chunk;
aae85ceb 94 }geox;
85982a32 95 my ($residue) = ($$r_str =~ s/(\e.*)$//so);
96 return $residue;
aae85ceb 97}
98
99sub euc_jis{
0ab8f81e 100 no warnings qw(uninitialized);
aae85ceb 101 my $r_str = shift;
102 my $jis0212 = shift;
103 $$r_str =~ s{
104 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
105 }{
85982a32 106 my $chunk = $1;
6d1c0808 107 my $esc =
85982a32 108 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
109 ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
aae85ceb 110 $ESC{JIS_0208};
111 if ($esc eq $ESC{JIS_0212} && !$jis0212){
112 # fallback to '?'
85982a32 113 $chunk =~ tr/\xA1-\xFE/\x3F/;
aae85ceb 114 }else{
85982a32 115 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
aae85ceb 116 }
85982a32 117 $esc . $chunk . $ESC{ASC};
aae85ceb 118 }geox;
119 $$r_str =~
120 s/\Q$ESC{ASC}\E
121 (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
122 $$r_str;
123}
124
1251;
126__END__
127
128
129=head1 NAME
130
131Encode::JP::JIS7 -- internally used by Encode::JP
132
133=cut