1 package Encode::CN::HZ;
6 $VERSION = do { my @r = (q$Revision: 1.3 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
10 use base 'Encode::Encoding';
12 # HZ is only escaped GB, so we implement it with the
13 # GB2312(raw) encoding here. Cf. RFCs 1842 & 1843.
16 my $obj = bless {name => $canon}, __PACKAGE__;
22 return 0; # for the time being
27 my ($obj,$str,$chk) = @_;
28 my $gb = Encode::find_encoding('gb2312-raw');
30 $str =~ s{~ # starting tilde
32 (~) # another tilde - escaped (set $1)
34 \n # \n - output nothing
36 \{ # opening brace of GB data
37 ( # set $2 to any number of...
39 [^~] # non-tilde GB character
41 ~(?!\}) # tilde not followed by a closing brace
44 ~\} # closing brace of GB data
45 | # XXX: invalid escape - maybe die on $chk?
48 (defined $1) ? '~' # two tildes make one tilde
50 (defined $2) ? $gb->decode($2, $chk) # decode the characters
52 '' # ~\n and invalid escape = ''
60 my ($obj,$str,$chk) = @_;
62 my $gb = Encode::find_encoding('gb2312-raw');
66 # XXX: Since CHECK and partial decoding has not been implemented yet,
67 # we'll use a very crude way to test for GB2312ness.
69 for my $index (0 .. length($str) - 1) {
72 my $char = substr($str, $index, 1);
73 # try to encode this character
74 # with CHECK on so it stops at proper place.
75 # also note that the assignement was braced in eval
78 eval{ $try = $gb->encode($char, 1) };
80 if (defined($try)) { # is a GB character:
82 $out .= $try; # in GB mode - just append it
85 $in_gb = 1; # enter GB mode, then append it
88 } # not a GB character:
90 $in_gb = 0; # leave GB mode, then append it
94 $out .= $char; # not in GB mode - just append it
98 $out .= '~}' if $in_gb; # add closing brace if needed
109 Encode::CN::HZ -- internally used by Encode::CN