--- perl/lib/Encode/HZ.pm 2002/07/25 11:24:36 1.1 +++ perl/lib/Encode/HZ.pm 2002/12/12 08:17:16 1.5 @@ -2,12 +2,9 @@ use strict; use vars qw($VERSION); -$VERSION = do { my @r = (q$Revision: 1.1 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; - -use Encode (); -require Encode::CN; +$VERSION = do {my @r =(q$Revision: 1.5 $ =~ /\d+/g);sprintf "%d."."%02d" x $#r, @r}; use base qw(Encode::Encoding); -__PACKAGE__->Define(qw/hz hz-gb-2312/); +__PACKAGE__->Define(qw/hz chinese-hz hz-gb-2312 hz-gb2312 cp52936/); sub needs_lines { 1 } @@ -18,7 +15,7 @@ sub decode { my ($obj,$str,$chk) = @_; - my $gb = Encode::find_encoding('gb2312-raw'); + my $gb = Encode::find_encoding($obj->__hz_encoding_name); $str =~ s{~ # starting tilde (?: @@ -28,22 +25,24 @@ | # or \{ # opening brace of GB data ( # set $2 to any number of... - (?: - [^~] # non-tilde GB character - | # or - ~(?!\}) # tilde not followed by a closing brace - )* + (?:[\x21-\x7D][\x21-\x7E])* ) ~\} # closing brace of GB data - | # XXX: invalid escape - maybe die on $chk? + | + \{ + ((?:[\x21-\x7D][\x21-\x7E])+[\x0D\x0A]) + # | # XXX: invalid escape - maybe die on $chk? ) }{ - my ($t, $c) = ($1, $2); + my ($t, $c, $d) = ($1, $2, $3); if (defined $t) { # two tildes make one tilde '~'; } elsif (defined $c) { # decode the characters - $c =~ tr/\xA1-\xFE/\x21-\x7E/; + $c =~ tr/\x21-\x7E/\xA1-\xFE/; $gb->decode($c, $chk); + } elsif (defined $d) { # decode the characters + $d =~ tr/\x21-\x7E/\xA1-\xFE/; + $gb->decode($d, $chk); } else { # ~\n and invalid escape = '' ''; } @@ -55,7 +54,7 @@ sub encode ($$;$) { my ($obj,$str,$chk) = @_; $_[1] = ''; - my $gb = Encode::find_encoding('euc-cn'); + my $gb = Encode::find_encoding($obj->__hz_encoding_name); $str =~ s/~/~~/g; $str = $gb->encode ($str, 1); @@ -68,15 +67,16 @@ $str; } -package Encode::HZ::HZ8; +sub __hz_encoding_name { 'euc-cn' } +package Encode::HZ::HZ8; use base qw(Encode::HZ); -__PACKAGE__->Define(qw/hz8/); +__PACKAGE__->Define(qw/hz8 x-hz8/); sub encode ($$;$) { my ($obj,$str,$chk) = @_; $_[1] = ''; - my $gb = Encode::find_encoding('euc-cn'); + my $gb = Encode::find_encoding($obj->__hz_encoding_name); $str =~ s/~/~~/g; $str = $gb->encode ($str, 1); @@ -87,13 +87,19 @@ $str; } +package Encode::HZ::HZ165; +use base qw(Encode::HZ); +__PACKAGE__->Define(qw/hz-iso-ir-165 hz-isoir165 x-iso-ir-165-hz/); + +sub __hz_encoding_name { 'cn-gb-isoir165' } + 1; __END__ - =head1 NAME -Encode::HZ --- Encode module for HZ (HZ-GB-2312), HZ8 +Encode::HZ --- Encode module for HZ (HZ-GB-2312 and HZ for +ISO-IR 165) and HZ8 =head1 DESCRIPTION @@ -104,6 +110,46 @@ HZ (HZ-GB-2312 in IANA name), but other variants such as HZ8 can't be encoded/decode. +=head1 ENCODINGS + +=over 4 + +=item hz-gb-2312 + +HZ 7-bit encoding for Chinese with GB 2312-80, +defined by RFC 1842 and RFC 1843. +(Alias: hz, chinese-hz (emacsen), CP52936 (M$), +hz-gb2312) + +Note that hz8 is also decodable with this encoding. + +=item hz8 + +HZ 8-bit encoding for Chinese with GB 2312-80. +(Alias: x-hz8) + +Note that hz-gb-2312 is also decodable with this encoding. + +=item hz-iso-ir-165 + +HZ 7-bit encoding for Chinese with ISO-IR 165 +(syntax is same as hz-gb-2312, but coded character +set is differ) (Alias: hz-isoir165, x-iso-ir-165-hz) + +Note that you need load Encode module that support +'cn-gb-isoir165' encoding (defined by RFC 1922), +such as Encode::ISO2022::EightBit. + +Also note that since ISO-IR 165 is nealy superset of GB 2312-80, +hz-iso-ir-165 is also considerable as a superset of +hz-gb-2312. + +=back + +=head1 TODO + +Support of extended HZ such as EHZ. + =head1 ACKNOWLEDGEMENTS Most part of this module is taken from Encode::CN::HZ. @@ -116,3 +162,6 @@ and/or modify it under the same terms as Perl itself. =cut + +# $Date: 2002/12/12 08:17:16 $ +### HZ.pm ends here