File Coverage

blib/lib/Encode/KR/2022_KR.pm
Criterion Covered Total %
statement 45 47 95.7
branch 6 8 75.0
condition n/a
subroutine 10 12 83.3
pod 4 6 66.6
total 65 73 89.0


line stmt bran cond sub pod time code
1             package Encode::KR::2022_KR;
2 8     8   50 use strict;
  8         16  
  8         211  
3 8     8   43 use warnings;
  8         17  
  8         602  
4             our $VERSION = do { my @r = ( q$Revision: 2.4 $ =~ /\d+/g ); sprintf "%d." . "%02d" x $#r, @r };
5              
6 8     8   49 use Encode qw(:fallbacks);
  8         18  
  8         1062  
7              
8 8     8   51 use parent qw(Encode::Encoding);
  8         17  
  8         55  
9             __PACKAGE__->Define('iso-2022-kr');
10              
11 0     0 1 0 sub needs_lines { 1 }
12              
13             sub perlio_ok {
14 0     0 1 0 return 0; # for the time being
15             }
16              
17             sub decode {
18 4     4 1 25 my ( $obj, $str, $chk ) = @_;
19 4 100       24 return undef unless defined $str;
20 3         10 my $res = $str;
21 3         16 my $residue = iso_euc( \$res );
22              
23             # This is for PerlIO
24 3 50       16 $_[1] = $residue if $chk;
25 3         21 return Encode::decode( 'euc-kr', $res, FB_PERLQQ );
26             }
27              
28             sub encode {
29 4     4 1 51 my ( $obj, $utf8, $chk ) = @_;
30 4 100       22 return undef unless defined $utf8;
31              
32             # empty the input string in the stack so perlio is ok
33 3 50       13 $_[1] = '' if $chk;
34 3         11 my $octet = Encode::encode( 'euc-kr', $utf8, FB_PERLQQ );
35 3         19 euc_iso( \$octet );
36 3         24 return $octet;
37             }
38              
39 8     8   1957 use Encode::CJKConstants qw(:all);
  8         24  
  8         1950  
40              
41             # ISO<->EUC
42              
43             sub iso_euc {
44 3     3 0 8 my $r_str = shift;
45 3         66 $$r_str =~ s/$RE{'2022_KR'}//gox; # remove the designator
46 3         21 $$r_str =~ s{ # replace characters in GL
47             \x0e # between SO(\x0e) and SI(\x0f)
48             ([^\x0f]*) # with characters in GR
49             \x0f
50             }
51 272         649 {
52 272         599 my $out= $1;
53 272         1385 $out =~ tr/\x21-\x7e/\xa1-\xfe/;
54             $out;
55 3         44 }geox;
56 3         15 my ($residue) = ( $$r_str =~ s/(\e.*)$//so );
57             return $residue;
58             }
59              
60 8     8   58 sub euc_iso {
  8         19  
  8         1064  
61 3     3 0 8 no warnings qw(uninitialized);
62             my $r_str = shift;
63 3         56 substr( $$r_str, 0, 0 ) =
64 3         85 $ESC{'2022_KR'}; # put the designator at the beg.
65             $$r_str =~
66             s{ # move KS X 1001 characters in GR to GL
67             ($RE{EUC_C}+) # and enclose them with SO and SI
68 272         636 }{
69 272         492 my $str = $1;
70 272         1133 $str =~ tr/\xA1-\xFE/\x21-\x7E/;
71             "\x0e" . $str . "\x0f";
72 3         14 }geox;
73             $$r_str;
74             }
75              
76             1;
77             __END__