1 package Encode::CN::HZ;
6 $VERSION = do { my @r = (q$Revision: 1.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
10 use base 'Encode::Encoding';
12 # HZ is but escaped GB, so we implement it with the
13 # GB2312(raw) encoding here. Cf. RFC 1842 & 1843.
16 my $obj = bless {name => $canon}, __PACKAGE__;
21 my ($obj,$str,$chk) = @_;
22 my $gb = Encode::find_encoding('gb2312-raw');
24 $str =~ s{~ # starting tilde
26 (~) # another tilde - escaped (set $1)
28 \n # \n - output nothing
30 \{ # opening brace of GB data
31 ( # set $2 to any number of...
33 [^~] # non-tilde GB character
35 ~(?!\}) # tilde not followed by a closing brace
38 ~\} # closing brace of GB data
39 | # XXX: invalid escape - maybe die on $chk?
42 (defined $1) ? '~' # two tildes make one tilde
44 (defined $2) ? $gb->decode($2, $chk) # decode the characters
46 '' # ~\n and invalid escape = ''
54 my ($obj,$str,$chk) = @_;
56 my $gb = Encode::find_encoding('gb2312-raw');
60 # XXX: Since CHECK and partial decoding has not been implemented yet,
61 # we'll use a very crude way to test for GB2312ness.
63 for my $index (0 .. length($str) - 1) {
66 my $char = substr($str, $index, 1);
67 my $try = $gb->encode($char); # try to encode this character
69 if (defined($try)) { # is a GB character:
71 $out .= $try; # in GB mode - just append it
74 $in_gb = 1; # enter GB mode, then append it
77 } # not a GB character:
79 $in_gb = 0; # leave GB mode, then append it
83 $out .= $char; # not in GB mode - just append it
87 $out .= '~}' if $in_gb; # add closing brace if needed
98 Encode::CN::HZ -- internally used by Encode::CN