1 package Encode::CN::HZ;
4 no warnings 'redefine'; # to quell the "use Encode" below
7 $VERSION = do { my @r = (q$Revision: 0.92 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
10 use Encode qw|encode decode|;
11 use base 'Encode::Encoding';
13 # HZ is but escaped GB, so we implement it with the
14 # GB2312(raw) encoding here. Cf. RFC 1842 & 1843.
17 my $obj = bless {name => $canon}, __PACKAGE__;
22 my ($obj,$str,$chk) = @_;
23 my $gb = Encode::find_encoding('gb2312');
25 $str =~ s{~ # starting tilde
27 (~) # another tilde - escaped (set $1)
29 \n # \n - output nothing
31 \{ # opening brace of GB data
32 ( # set $2 to any number of...
34 [^~] # non-tilde GB character
36 ~(?!\}) # tilde not followed by a closing brace
39 ~\} # closing brace of GB data
40 | # XXX: invalid escape - maybe die on $chk?
43 (defined $1) ? '~' # two tildes make one tilde
45 (defined $2) ? $gb->decode($2, $chk) # decode the characters
47 '' # '' on ~\n and invalid escape
55 my ($obj,$str,$chk) = @_;
57 my $gb = Encode::find_encoding('gb2312');
61 # XXX: Since CHECK and partial decoding has not been implemented yet,
62 # we'll use a very crude way to test for GB2312ness.
64 for my $index (0 .. length($str) - 1) {
67 my $char = substr($str, $index, 1);
68 my $try = $gb->encode($char); # try encode this char
70 if (defined($try)) { # is a GB character
72 $out .= $try; # in GB mode - just append it
75 $out .= "~{$try"; # enter GB mode, then append it
80 $out .= "~}$char"; # leave GB mode, then append it
84 $out .= $char; # not in GB mode - just append it
88 $out .= '~}' if $in_gb; # add closing brace as needed