Commit | Line | Data |
aae85ceb |
1 | package Encode::JP::JIS7; |
2 | use strict; |
3 | |
b536bf57 |
4 | our $VERSION = do { my @r = (q$Revision: 1.9 $ =~ /\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 |
b536bf57 |
65 | our $re_scan_jis = qr{ |
66 | (?:($RE{JIS_0212})|$RE{JIS_0208}|($RE{ISO_ASC})|($RE{JIS_KANA}))([^\e]*) |
67 | }x; |
aae85ceb |
68 | |
69 | sub jis_euc { |
b536bf57 |
70 | local ${^ENCODING}; |
aae85ceb |
71 | my $r_str = shift; |
b536bf57 |
72 | $$r_str =~ s($re_scan_jis) |
aae85ceb |
73 | { |
b536bf57 |
74 | my ($esc_0212, $esc_asc, $esc_kana, $chunk) = |
75 | ($1, $2, $3, $4); |
76 | if (!$esc_asc) { |
85982a32 |
77 | $chunk =~ tr/\x21-\x7e/\xa1-\xfe/; |
b536bf57 |
78 | if ($esc_kana) { |
85982a32 |
79 | $chunk =~ s/([\xa1-\xdf])/\x8e$1/og; |
aae85ceb |
80 | } |
b536bf57 |
81 | elsif ($esc_0212) { |
85982a32 |
82 | $chunk =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og; |
aae85ceb |
83 | } |
84 | } |
85982a32 |
85 | $chunk; |
aae85ceb |
86 | }geox; |
85982a32 |
87 | my ($residue) = ($$r_str =~ s/(\e.*)$//so); |
88 | return $residue; |
aae85ceb |
89 | } |
90 | |
91 | sub euc_jis{ |
0ab8f81e |
92 | no warnings qw(uninitialized); |
aae85ceb |
93 | my $r_str = shift; |
94 | my $jis0212 = shift; |
95 | $$r_str =~ s{ |
96 | ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+) |
97 | }{ |
85982a32 |
98 | my $chunk = $1; |
6d1c0808 |
99 | my $esc = |
85982a32 |
100 | ( $chunk =~ tr/\x8E//d ) ? $ESC{KANA} : |
101 | ( $chunk =~ tr/\x8F//d ) ? $ESC{JIS_0212} : |
aae85ceb |
102 | $ESC{JIS_0208}; |
103 | if ($esc eq $ESC{JIS_0212} && !$jis0212){ |
104 | # fallback to '?' |
85982a32 |
105 | $chunk =~ tr/\xA1-\xFE/\x3F/; |
aae85ceb |
106 | }else{ |
85982a32 |
107 | $chunk =~ tr/\xA1-\xFE/\x21-\x7E/; |
aae85ceb |
108 | } |
85982a32 |
109 | $esc . $chunk . $ESC{ASC}; |
aae85ceb |
110 | }geox; |
111 | $$r_str =~ |
112 | s/\Q$ESC{ASC}\E |
113 | (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox; |
114 | $$r_str; |
115 | } |
116 | |
117 | 1; |
118 | __END__ |
119 | |
120 | |
121 | =head1 NAME |
122 | |
123 | Encode::JP::JIS7 -- internally used by Encode::JP |
124 | |
125 | =cut |