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