another case of enabling binmode() where it should not be: if the
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / JP / JIS7.pm
CommitLineData
aae85ceb 1package Encode::JP::JIS7;
2use strict;
3
011b2d2f 4our $VERSION = do { my @r = (q$Revision: 1.6 $ =~ /\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{
45 my ($obj,$str,$chk) = @_;
85982a32 46 my $residue = jis_euc(\$str);
47 # This is for PerlIO
48 $_[1] = $residue if $chk;
6d1c0808 49 return Encode::decode('euc-jp', $str, FB_PERLQQ);
aae85ceb 50}
51
52#
53# encode is different
54#
55
6d1c0808 56sub encode($$;$)
aae85ceb 57{
58 require Encode::JP::H2Z;
85982a32 59 my ($obj, $utf8, $chk) = @_;
60 # empty the input string in the stack so perlio is ok
61 $_[1] = '' if $chk;
aae85ceb 62 my ($h2z, $jis0212) = @$obj{qw(h2z jis0212)};
6d1c0808 63 my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
85982a32 64 $h2z and &Encode::JP::H2Z::h2z(\$octet);
65 euc_jis(\$octet, $jis0212);
66 return $octet;
aae85ceb 67}
68
69
70# JIS<->EUC
71
72sub jis_euc {
73 my $r_str = shift;
74 $$r_str =~ s(
75 ($RE{JIS_0212}|$RE{JIS_0208}|$RE{ISO_ASC}|$RE{JIS_KANA})
76 ([^\e]*)
77 )
78 {
85982a32 79 my ($esc, $chunk) = ($1, $2);
aae85ceb 80 if ($esc !~ /$RE{ISO_ASC}/o) {
85982a32 81 $chunk =~ tr/\x21-\x7e/\xa1-\xfe/;
aae85ceb 82 if ($esc =~ /$RE{JIS_KANA}/o) {
85982a32 83 $chunk =~ s/([\xa1-\xdf])/\x8e$1/og;
aae85ceb 84 }
85 elsif ($esc =~ /$RE{JIS_0212}/o) {
85982a32 86 $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
aae85ceb 87 }
88 }
85982a32 89 $chunk;
aae85ceb 90 }geox;
85982a32 91 my ($residue) = ($$r_str =~ s/(\e.*)$//so);
92 return $residue;
aae85ceb 93}
94
95sub euc_jis{
0ab8f81e 96 no warnings qw(uninitialized);
aae85ceb 97 my $r_str = shift;
98 my $jis0212 = shift;
99 $$r_str =~ s{
100 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
101 }{
85982a32 102 my $chunk = $1;
6d1c0808 103 my $esc =
85982a32 104 ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} :
105 ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
aae85ceb 106 $ESC{JIS_0208};
107 if ($esc eq $ESC{JIS_0212} && !$jis0212){
108 # fallback to '?'
85982a32 109 $chunk =~ tr/\xA1-\xFE/\x3F/;
aae85ceb 110 }else{
85982a32 111 $chunk =~ tr/\xA1-\xFE/\x21-\x7E/;
aae85ceb 112 }
85982a32 113 $esc . $chunk . $ESC{ASC};
aae85ceb 114 }geox;
115 $$r_str =~
116 s/\Q$ESC{ASC}\E
117 (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
118 $$r_str;
119}
120
1211;
122__END__
123
124
125=head1 NAME
126
127Encode::JP::JIS7 -- internally used by Encode::JP
128
129=cut