1 package Encode::CN::HZ;
6 $VERSION = do { my @r = (q$Revision: 1.4 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
10 use base qw(Encode::Encoding);
11 __PACKAGE__->Define('hz');
13 # HZ is only escaped GB, so we implement it with the
14 # GB2312(raw) encoding here. Cf. RFCs 1842 & 1843.
21 return 0; # for the time being
26 my ($obj,$str,$chk) = @_;
27 my $gb = Encode::find_encoding('gb2312-raw');
29 $str =~ s{~ # starting tilde
31 (~) # another tilde - escaped (set $1)
33 \n # \n - output nothing
35 \{ # opening brace of GB data
36 ( # set $2 to any number of...
38 [^~] # non-tilde GB character
40 ~(?!\}) # tilde not followed by a closing brace
43 ~\} # closing brace of GB data
44 | # XXX: invalid escape - maybe die on $chk?
47 (defined $1) ? '~' # two tildes make one tilde
49 (defined $2) ? $gb->decode($2, $chk) # decode the characters
51 '' # ~\n and invalid escape = ''
59 my ($obj,$str,$chk) = @_;
61 my $gb = Encode::find_encoding('gb2312-raw');
65 # XXX: Since CHECK and partial decoding has not been implemented yet,
66 # we'll use a very crude way to test for GB2312ness.
68 for my $index (0 .. length($str) - 1) {
71 my $char = substr($str, $index, 1);
72 # try to encode this character
73 # with CHECK on so it stops at proper place.
74 # also note that the assignement was braced in eval
77 eval{ $try = $gb->encode($char, 1) };
79 if (defined($try)) { # is a GB character:
81 $out .= $try; # in GB mode - just append it
84 $in_gb = 1; # enter GB mode, then append it
87 } # not a GB character:
89 $in_gb = 0; # leave GB mode, then append it
93 $out .= $char; # not in GB mode - just append it
97 $out .= '~}' if $in_gb; # add closing brace if needed
108 Encode::CN::HZ -- internally used by Encode::CN