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