Upgrade to Encode 1.56, from Dan Kogai.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / KR / 2022_KR.pm
1 package Encode::KR::2022_KR;
2 use Encode qw(:fallbacks);
3 use base 'Encode::Encoding';
4
5 use strict;
6
7 our $VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
8
9
10 my $canon = 'iso-2022-kr';
11 my $obj = bless {name => $canon}, __PACKAGE__;
12 $obj->Define($canon);
13
14 sub name { return $_[0]->{name}; }
15
16 sub needs_lines { 1 }
17
18 sub perlio_ok { 
19     #exists $INC{"PerlIO/encoding.pm"} or return 0;
20     #PerlIO::encoding->VERSION >= 0.03 and return 1;
21     return 0; # for the time being
22 }
23
24 sub decode
25 {
26     my ($obj, $str, $chk) = @_;
27     my $res = $str;
28     my $residue = iso_euc(\$res);
29     # This is for PerlIO
30     $_[1] = $residue if $chk;
31     return Encode::decode('euc-kr', $res, FB_PERLQQ);
32 }
33
34 sub encode
35 {
36     my ($obj, $utf8, $chk) = @_;
37     # empty the input string in the stack so perlio is ok
38     $_[1] = '' if $chk;
39     my $octet = Encode::encode('euc-jp', $utf8, FB_PERLQQ) ;
40     euc_iso(\$octet);
41     return $octet;
42 }
43
44 use Encode::CJKConstants qw(:all);
45
46 # ISO<->EUC
47
48 sub iso_euc{
49     my $r_str = shift;
50     $$r_str =~ s/$RE{'2022_KR'}//gox;  # remove the designator
51     $$r_str =~ s{                      # replace characters in GL
52      \x0e                              # between SO(\x0e) and SI(\x0f)
53      ([^\x0f]*)                        # with characters in GR
54      \x0f
55         }
56     {
57                         my $out= $1;
58       $out =~ tr/\x21-\x7e/\xa1-\xfe/;
59       $out;
60     }geox;
61     my ($residue) = ($$r_str =~ s/(\e.*)$//so);
62     return $residue;
63 }
64
65 sub euc_iso{
66     no warnings qw(uninitialized);
67     my $r_str = shift;
68     substr($$r_str,0,0)=$ESC{'2022_KR'};  # put the designator at the beg.
69     $$r_str =~ s{                         # move KS X 1001 characters in GR to GL
70         ($RE{EUC_C}+)                     # and enclose them with SO and SI
71         }{
72             my $str = $1;
73             $str =~ tr/\xA1-\xFE/\x21-\x7E/;
74             "\x0e" . $str . "\x0f";
75         }geox;
76     $$r_str;
77 }
78
79 1;
80 __END__
81
82 =head1 NAME
83
84 Encode::KR::2022_KR -- internally used by Encode::KR
85
86 =cut