HZ.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
use strict;
use vars qw($VERSION);
#$VERSION = do { my @r = (q$Revision: 1.5 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
# HZ is a combination of ASCII and escaped GB, so we implement it
# with the GB2312(raw) encoding here. Cf. RFCs 1842 & 1843.
# not ported for EBCDIC. Which should be used, "~" or "\x7E"?
sub needs_lines { 1 }
sub decode ($$;$)
{
my $ret = '';
while (length $str) {
if ($in_ascii) { # ASCII mode
$ret .= $1;
# EBCDIC should need ascii2native, but not ported.
}
$ret .= '~';
}
1; # no-op
}
}
else { # encounters an invalid escape, \x80 or greater
last;
}
}
else { # GB mode; the byte ranges are as in RFC 1843.
}
$in_ascii = 1;
}
else { # invalid
last;
}
}
}
return $ret;
}
sub cat_decode {
my $ret = '';
# $trm is the first of the pair '~~', then 2nd tilde is to be removed.
# XXX: Is better C<$src =~ s/^\x7E// or die if ...>?
while (length $src) {
my $now;
if ($in_ascii) { # ASCII mode
$now = $1;
}
$now = '~';
}
next;
}
next;
}
else { # encounters an invalid escape, \x80 or greater
last;
}
}
else { # GB mode; the byte ranges are as in RFC 1843.
}
$in_ascii = 1;
next;
}
else { # invalid
last;
}
}
next if ! defined $now;
return 1;
}
}
return ''; # terminator not found
}
sub encode($$;$)
{
my $ret = '';
no warnings 'utf8'; # $str may be malformed UTF8 at the end of a chunk.
while (length $str) {
my $tmp = $1;
$tmp =~ s/~/~~/g; # escapes tildes
if (! $in_ascii) {
$in_ascii = 1;
}
}
elsif ($str =~ s/(.)//) {
last if !defined $tmp;
if ($in_ascii) {
$in_ascii = 0;
}
}
elsif (length $tmp) { # maybe FALLBACK in ASCII (XXX)
if (!$in_ascii) {
$in_ascii = 1;
}
}
}
else { # if $str is malformed UTF8 *and* if length $str != 0.
last;
}
}
# The state at the end of the chunk is discarded, even if in GB mode.
# That results in the combination of GB-OUT and GB-IN, i.e. "~}~{".
# Parhaps it is harmless, but further investigations may be required...
if (! $in_ascii) {
$in_ascii = 1;
}
return $ret;
}
1;
=head1 NAME
Encode::CN::HZ -- internally used by Encode::CN
=cut