Upgrade to Encode 2.00.
[p5sagit/p5-mst-13.2.git] / ext / Encode / lib / Encode / CN / HZ.pm
index e4c2a8f..15440a4 100644 (file)
 package Encode::CN::HZ;
 
 use strict;
-no warnings 'redefine'; # to quell the "use Encode" below
 
-use Encode::CN;
-use Encode qw|encode decode|;
-use base 'Encode::Encoding';
+use vars qw($VERSION);
+$VERSION = do { my @r = (q$Revision: 2.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
 
-# HZ is but escaped GB, so we implement it with the
-# GB2312(raw) encoding here. Cf. RFC 1842 & 1843.
+use Encode qw(:fallbacks);
 
-my $canon = 'hz';
-my $obj = bless {name => $canon}, __PACKAGE__;
-$obj->Define($canon);
+use base qw(Encode::Encoding);
+__PACKAGE__->Define('hz');
 
-sub decode
+# 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 ($obj,$str,$chk) = @_;
-    my $gb = Encode::find_encoding('gb2312');
-
-    $str =~ s{~                        # starting tilde
-       (?:
-           (~)                 # another tilde - escaped (set $1)
-               |               #     or
-           \n                  # \n - output nothing
-               |               #     or
-           \{                  # opening brace of GB data
-               (               #  set $2 to any number of...
-                   (?: 
-                       [^~]    #  non-tilde GB character
-                           |   #     or
-                       ~(?!\}) #  tilde not followed by a closing brace
-                   )*
-               )
-           ~\}                 # closing brace of GB data
-               |               # XXX: invalid escape - maybe die on $chk?
-       )
-    }{
-       (defined $1)    ? '~'                   # two tildes make one tilde
-           :
-       (defined $2)    ? $gb->decode($2, $chk) # decode the characters
-           :
-       ''                                      # '' on ~\n and invalid escape
-    }egx;
-
-    return $str;
+
+    my $GB = Encode::find_encoding('gb2312-raw');
+    my $ret = '';
+    my $in_ascii = 1; # default mode is ASCII.
+
+    while (length $str) {
+       if ($in_ascii) { # ASCII mode
+           if ($str =~ s/^([\x00-\x7D\x7F]+)//) { # no '~' => ASCII
+               $ret .= $1;
+               # EBCDIC should need ascii2native, but not ported.
+           }
+           elsif ($str =~ s/^\x7E\x7E//) { # escaped tilde
+               $ret .= '~';
+           }
+           elsif ($str =~ s/^\x7E\cJ//) { # '\cJ' == LF in ASCII
+               1; # no-op
+           }
+           elsif ($str =~ s/^\x7E\x7B//) { # '~{'
+               $in_ascii = 0; # to GB
+           }
+           else { # encounters an invalid escape, \x80 or greater
+               last;
+           }
+       }
+       else { # GB mode; the byte ranges are as in RFC 1843.
+           if ($str =~ s/^((?:[\x21-\x77][\x21-\x7E])+)//) {
+               $ret .= $GB->decode($1, $chk);
+           }
+           elsif ($str =~ s/^\x7E\x7D//) { # '~}'
+               $in_ascii = 1;
+           }
+           else { # invalid
+               last;
+           }
+       }
+    }
+    $_[1] = '' if $chk; # needs_lines guarantees no partial character
+    return $ret;
 }
 
-sub encode
-{
-    my ($obj,$str,$chk) = @_;
-    my ($out, $in_gb);
-    my $gb = Encode::find_encoding('gb2312');
+sub cat_decode {
+    my ($obj, undef, $src, $pos, $trm, $chk) = @_;
+    my ($rdst, $rsrc, $rpos) = \@_[1..3];
 
-    $str =~ s/~/~~/g;
+    my $GB = Encode::find_encoding('gb2312-raw');
+    my $ret = '';
+    my $in_ascii = 1; # default mode is ASCII.
 
-    # XXX: Since CHECK and partial decoding  has not been implemented yet,
-    #      we'll use a very crude way to test for GB2312ness.
+    my $ini_pos = pos($$rsrc);
 
-    for my $index (0 .. length($str) - 1) {
-       no warnings 'utf8';
+    substr($src, 0, $pos) = '';
 
-       my $char = substr($str, $index, 1);
-       my $try  = $gb->encode($char);  # try encode this char
+    my $ini_len = bytes::length($src);
 
-       if (defined($try)) {            # is a GB character
-           if ($in_gb) {
-               $out .= $try;           # in GB mode - just append it
+    # $trm is the first of the pair '~~', then 2nd tilde is to be removed.
+    # XXX: Is better C<$src =~ s/^\x7E// or die if ...>?
+    $src =~ s/^\x7E// if $trm eq "\x7E";
+
+    while (length $src) {
+       my $now;
+       if ($in_ascii) { # ASCII mode
+           if ($src =~ s/^([\x00-\x7D\x7F])//) { # no '~' => ASCII
+               $now = $1;
+           }
+           elsif ($src =~ s/^\x7E\x7E//) { # escaped tilde
+               $now = '~';
+           }
+           elsif ($src =~ s/^\x7E\cJ//) { # '\cJ' == LF in ASCII
+               next;
+           }
+           elsif ($src =~ s/^\x7E\x7B//) { # '~{'
+               $in_ascii = 0; # to GB
+               next;
+           }
+           else { # encounters an invalid escape, \x80 or greater
+               last;
+           }
+       }
+       else { # GB mode; the byte ranges are as in RFC 1843.
+           if ($src =~ s/^((?:[\x21-\x77][\x21-\x7F])+)//) {
+               $now = $GB->decode($1, $chk);
+           }
+           elsif ($src =~ s/^\x7E\x7D//) { # '~}'
+               $in_ascii = 1;
+               next;
+           }
+           else { # invalid
+               last;
            }
-           else {
-               $out .= "~{$try";       # enter GB mode, then append it
-               $in_gb = 1;
+       }
+
+       next if ! defined $now;
+
+       $ret .= $now;
+
+       if ($now eq $trm) {
+           $$rdst .= $ret;
+           $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
+           pos($$rsrc) = $ini_pos;
+           return 1;
+       }
+    }
+
+    $$rdst .= $ret;
+    $$rpos = $ini_pos + $pos + $ini_len - bytes::length($src);
+    pos($$rsrc) = $ini_pos;
+    return ''; # terminator not found
+}
+
+
+sub encode($$;$)
+{
+    my ($obj,$str,$chk) = @_;
+
+    my $GB = Encode::find_encoding('gb2312-raw');
+    my $ret = '';
+    my $in_ascii = 1; # default mode is ASCII.
+
+    no warnings 'utf8'; # $str may be malformed UTF8 at the end of a chunk.
+
+    while (length $str) {
+       if ($str =~ s/^([[:ascii:]]+)//) {
+           my $tmp = $1;
+           $tmp =~ s/~/~~/g; # escapes tildes
+           if (! $in_ascii) {
+               $ret .= "\x7E\x7D"; # '~}'
+               $in_ascii = 1;
            }
+           $ret .= pack 'a*', $tmp; # remove UTF8 flag.
        }
-       elsif ($in_gb) {
-           $out .= "~}$char";          # leave GB mode, then append it
-           $in_gb = 0;
+       elsif ($str =~ s/(.)//) {
+           my $tmp = $GB->encode($1, $chk);
+           last if !defined $tmp;
+           if (length $tmp == 2) { # maybe a valid GB char (XXX)
+               if ($in_ascii) {
+                   $ret .= "\x7E\x7B"; # '~{'
+                   $in_ascii = 0;
+               }
+               $ret .= $tmp;
+           }
+           elsif (length $tmp) { # maybe FALLBACK in ASCII (XXX)
+               if (!$in_ascii) {
+                   $ret .= "\x7E\x7D"; # '~}'
+                   $in_ascii = 1;
+               }
+               $ret .= $tmp;
+           }
        }
-       else {
-           $out .= $char;              # not in GB mode - just append it
+       else { # if $str is malformed UTF8 *and* if length $str != 0.
+           last;
        }
     }
+    $_[1] = $str if $chk;
 
-    $out .= '~}' if $in_gb;            # add closing brace as needed
+  # 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...
 
-    return $out;
+    if (! $in_ascii) {
+       $ret .= "\x7E\x7D"; # '~}'
+       $in_ascii = 1;
+    }
+    return $ret;
 }
 
 1;
 __END__
+
+=head1 NAME
+
+Encode::CN::HZ -- internally used by Encode::CN
+
+=cut