This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Duh. If the input is a stream of UTF-8 bytes, all that's
[perl5.git] / ext / Encode / Encode.pm
index 267df8e..3dd0ed3 100644 (file)
@@ -1,6 +1,6 @@
 package Encode;
 use strict;
-our $VERSION = do { my @r = (q$Revision: 1.0 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
+our $VERSION = do { my @r = (q$Revision: 1.20 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r };
 our $DEBUG = 0;
 
 require DynaLoader;
@@ -42,29 +42,53 @@ use Encode::Alias;
 
 # Make a %Encoding package variable to allow a certain amount of cheating
 our %Encoding;
-
-our %ExtModule =
-    (
-     viscii             => 'Encode/Byte.pm',
-     'koi8-r'           => 'Encode/Byte.pm',
-     cp1047             => 'Encode/EBCDIC.pm',
-     cp37               => 'Encode/EBCDIC.pm',
-     'posix-bc'         => 'Encode/EBCDIC.pm',
-     symbol             => 'Encode/Symbol.pm',
-     dingbats           => 'Encode/Symbol.pm',
-    );
+our %ExtModule;
+
+my @codepages = qw(
+                    37  424  437  500  737  775  850  852  855 
+                   856  857  860  861  862  863  864  865  866 
+                   869  874  875  932  936  949  950 1006 1026 
+                  1047 1250 1251 1252 1253 1254 1255 1256 1257
+                  1258
+                  );
+
+my @macintosh = qw(
+                  CentralEurRoman  Croatian  Cyrillic   Greek
+                  Iceland          Roman     Rumanian   Sami
+                  Thai             Turkish   Ukrainian
+                  );
 
 for my $k (2..11,13..16){
     $ExtModule{"iso-8859-$k"} = 'Encode/Byte.pm';
 }
 
-for my $k (1250..1258){
+for my $k (@codepages){
     $ExtModule{"cp$k"} = 'Encode/Byte.pm';
 }
 
+for my $k (@macintosh)
+{
+    $ExtModule{"mac$k"} = 'Encode/Byte.pm';
+}
+
+%ExtModule =
+    (%ExtModule,
+     'koi8-r'           => 'Encode/Byte.pm',
+     'posix-bc'         => 'Encode/EBCDIC.pm',
+     cp037              => 'Encode/EBCDIC.pm',
+     cp1026             => 'Encode/EBCDIC.pm',
+     cp1047             => 'Encode/EBCDIC.pm',
+     cp500              => 'Encode/EBCDIC.pm',
+     cp875              => 'Encode/EBCDIC.pm',
+     dingbats           => 'Encode/Symbol.pm',
+     macDingbats        => 'Encode/Symbol.pm',
+     macSymbol          => 'Encode/Symbol.pm',
+     symbol             => 'Encode/Symbol.pm',
+     viscii             => 'Encode/Byte.pm',
+);
+
 unless ($ON_EBCDIC) { # CJK added to autoload unless EBCDIC env
-%ExtModule =(
-            %ExtModule,
+%ExtModule =(%ExtModule,
             'euc-cn'           => 'Encode/CN.pm',
             gb2312             => 'Encode/CN.pm',
             gb12345            => 'Encode/CN.pm',
@@ -76,10 +100,11 @@ unless ($ON_EBCDIC) { # CJK added to autoload unless EBCDIC env
             'iso-2022-jp-1'    => 'Encode/JP.pm',
             '7bit-jis'         => 'Encode/JP.pm',
             shiftjis           => 'Encode/JP.pm',
-            macjapan           => 'Encode/JP.pm',
+            macJapanese        => 'Encode/JP.pm',
             cp932              => 'Encode/JP.pm',
             'euc-kr'           => 'Encode/KR.pm',
             ksc5601            => 'Encode/KR.pm',
+            macKorean          => 'Encode/KR.pm',
             cp949              => 'Encode/KR.pm',
             big5               => 'Encode/TW.pm',
             'big5-hkscs'       => 'Encode/TW.pm',
@@ -90,12 +115,7 @@ unless ($ON_EBCDIC) { # CJK added to autoload unless EBCDIC env
             );
 }
 
-for my $k (qw(centeuro croatian cyrillic dingbats greek
-             iceland roman rumanian sami 
-             thai turkish  ukraine))
-{
-    $ExtModule{"mac$k"} = 'Encode/Byte.pm';
-}
+
 
 
 sub encodings
@@ -197,9 +217,9 @@ sub from_to
     croak("Unknown encoding '$to'") unless defined $t;
     my $uni = $f->decode($string,$check);
     return undef if ($check && length($string));
-    $string = $t->encode($uni,$check);
+    $string =  $t->encode($uni,$check);
     return undef if ($check && length($uni));
-    return length($_[0] = $string);
+    return defined($_[0] = $string) ? length($string) : undef ;
 }
 
 sub encode_utf8
@@ -333,7 +353,7 @@ For example to convert ISO-8859-1 data to UTF-8:
 
   $utf8 = decode("iso-8859-1", $latin1);
 
-=item from_to($string, FROM_ENCODING, TO_ENCODING[, CHECK])
+=item [$length =] from_to($string, FROM_ENCODING, TO_ENCODING[, CHECK])
 
 Convert B<in-place> the data between two encodings.  How did the data
 in $string originally get to be in FROM_ENCODING?  Either using
@@ -352,6 +372,9 @@ and to convert it back:
 Note that because the conversion happens in place, the data to be
 converted cannot be a string constant, it must be a scalar variable.
 
+from_to() return the length of the converted string on success, undef
+otherwise.
+
 =back
 
 =head2 Listing available encodings
@@ -384,9 +407,9 @@ To add new alias to a given encoding,  Use;
   use Encode::Alias;
   define_alias(newName => ENCODING);
 
-After that, newName can be to be used as am alias for ENCODING.
-ENCODING may be either the name of an encoding or and I<encoding 
-object>
+After that, newName can be used as an alias for ENCODING.
+ENCODING may be either the name of an encoding or an I<encoding
+ object>
 
 See L<Encode::Alias> on details.