summaryrefslogtreecommitdiff
path: root/ext/Encode/lib/Encode/KR/2022_KR.pm
blob: 4a3b1d086a807b22c38f0cf974eebcc3c4751fa1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package Encode::KR::2022_KR;
use Encode::KR;
use base 'Encode::Encoding';

use strict;

our $VERSION = do { my @r = (q$Revision: 1.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };


my $canon = 'iso-2022-kr';
my $obj = bless {name => $canon}, __PACKAGE__;
$obj->Define($canon);

sub name { return $_[0]->{name}; }

sub decode
{
    my ($obj,$str,$chk) = @_;
    my $res = $str;
    iso_euc(\$res);
    return Encode::decode('euc-kr', $res, $chk);
}

sub encode
{
    my ($obj,$str,$chk) = @_;
    my $res = Encode::encode('euc-kr', $str, $chk);
    euc_iso(\$res);
    return $res;
}

use Encode::CJKConstants qw(:all);

# ISO<->EUC

sub iso_euc{
    my $r_str = shift;
    $$r_str =~ s/$RE{'2022_KR'}//gox;  # remove the designator 
    $$r_str =~ s{                    # replace chars. in GL
     \x0e                            # between SO(\x0e) and SI(\x0f)
     ([^\x0f]*)                      # with chars. in GR
     \x0f
	}
    {
			my $out= $1; 
      $out =~ tr/\x21-\x7e/\xa1-\xfe/;
      $out;
    }geox;
    $$r_str;
}

sub euc_iso{
    my $r_str = shift;
    substr($$r_str,0,0)=$ESC{'2022_KR'};  # put the designator at the beg. 
    $$r_str =~ s{                     # move KS X 1001 chars. in GR to GL
	($RE{EUC_C}+)                       # and enclose them with SO and SI
	}{
	    my $str = $1;
	    $str =~ tr/\xA1-\xFE/\x21-\x7E/;
	    "\x0e" . $str . "\x0f";
	}geox;
    $$r_str;
}

1;
__END__