1 package Encode::CN::HZ;
4 no warnings 'redefine'; # to quell the "use Encode" below
7 use Encode qw|encode decode|;
8 use base 'Encode::Encoding';
10 # HZ is but escaped GB, so we implement it with the
11 # GB2312(raw) encoding here. Cf. RFC 1842 & 1843.
14 my $obj = bless {name => $canon}, __PACKAGE__;
19 my ($obj,$str,$chk) = @_;
20 my $gb = Encode::find_encoding('gb2312');
22 $str =~ s{~ # starting tilde
24 (~) # another tilde - escaped (set $1)
26 \n # \n - output nothing
28 \{ # opening brace of GB data
29 ( # set $2 to any number of...
31 [^~] # non-tilde GB character
33 ~(?!\}) # tilde not followed by a closing brace
36 ~\} # closing brace of GB data
37 | # XXX: invalid escape - maybe die on $chk?
40 (defined $1) ? '~' # two tildes make one tilde
42 (defined $2) ? $gb->decode($2, $chk) # decode the characters
44 '' # '' on ~\n and invalid escape
52 my ($obj,$str,$chk) = @_;
54 my $gb = Encode::find_encoding('gb2312');
58 # XXX: Since CHECK and partial decoding has not been implemented yet,
59 # we'll use a very crude way to test for GB2312ness.
61 for my $index (0 .. length($str) - 1) {
64 my $char = substr($str, $index, 1);
65 my $try = $gb->encode($char); # try encode this char
67 if (defined($try)) { # is a GB character
69 $out .= $try; # in GB mode - just append it
72 $out .= "~{$try"; # enter GB mode, then append it
77 $out .= "~}$char"; # leave GB mode, then append it
81 $out .= $char; # not in GB mode - just append it
85 $out .= '~}' if $in_gb; # add closing brace as needed