This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perl datastructure conversion shortcuts for DDD
[perl5.git] / ext / Encode / lib / Encode / JP / JIS.pm
CommitLineData
0e567a6c
JH
1package Encode::JP::JIS;
2use Encode::JP;
3use base 'Encode::Encoding';
4
8f21750f
JH
5use strict;
6
0e567a6c
JH
7# Just for the time being, we implement jis-7bit
8# encoding via EUC
9
10my $canon = '7bit-jis';
11my $obj = bless {name => $canon}, __PACKAGE__;
12$obj->Define($canon);
13
14sub decode
15{
16 my ($obj,$str,$chk) = @_;
17 my $res = $str;
18 jis_euc(\$res);
8f21750f 19 return Encode::decode('euc-jp', $res, $chk);
0e567a6c
JH
20}
21
22sub encode
23{
24 my ($obj,$str,$chk) = @_;
25 my $res = Encode::encode('euc-jp', $str, $chk);
26 euc_jis(\$res);
27 return $res;
28}
29
30use Encode::JP::Constants qw(:all);
31
32# JIS<->EUC
33
34sub jis_euc {
35 my $r_str = shift;
36 $$r_str =~ s(
37 ($RE{JIS_0212}|$RE{JIS_0208}|$RE{JIS_ASC}|$RE{JIS_KANA})
38 ([^\e]*)
39 )
40 {
41 my ($esc, $str) = ($1, $2);
42 if ($esc !~ /$RE{JIS_ASC}/o) {
43 $str =~ tr/\x21-\x7e/\xa1-\xfe/;
44 if ($esc =~ /$RE{JIS_KANA}/o) {
45 $str =~ s/([\xa1-\xdf])/\x8e$1/og;
46 }
47 elsif ($esc =~ /$RE{JIS_0212}/o) {
48 $str =~ s/([\xa1-\xfe][\xa1-\xfe])/\x8f$1/og;
49 }
50 }
51 $str;
52 }geox;
53 $$r_str;
54}
55
56sub euc_jis{
57 my $r_str = shift;
58 $$r_str =~ s{
59 ((?:$RE{EUC_C})+|(?:$RE{EUC_KANA})+|(?:$RE{EUC_0212})+)
60 }{
61 my $str = $1;
62 my $esc =
63 ( $str =~ tr/\x8E//d ) ? $ESC{KANA} :
64 ( $str =~ tr/\x8F//d ) ? $ESC{JIS_0212} :
65 $ESC{JIS_0208};
66 $str =~ tr/\xA1-\xFE/\x21-\x7E/;
67 $esc . $str . $ESC{ASC};
68 }geox;
69 $$r_str =~
70 s/\Q$ESC{ASC}\E
71 (\Q$ESC{KANA}\E|\Q$ESC{JIS_0212}\E|\Q$ESC{JIS_0208}\E)/$1/gox;
72 $$r_str;
73}
74
751;
76__END__