export win32_async_check or Perl_despatch_signals
[p5sagit/p5-mst-13.2.git] / lib / charnames.pm
index 5554ae0..1297a76 100644 (file)
@@ -55,7 +55,10 @@ sub charnames
   }
 
   ## If we don't have it by now, give up.
-  die "Unknown charname '$name'" unless @off;
+  unless (@off) {
+      carp "Unknown charname '$name'";
+      return "\x{FFFD}";
+  }
 
   ##
   ## Now know where in the string the name starts.
@@ -78,10 +81,11 @@ sub charnames
   if ($^H & $bytes::hint_bits) {       # "use bytes" in effect?
     use bytes;
     return chr $ord if $ord <= 255;
-    my $hex = sprintf '%X=0%o', $ord, $ord;
+    my $hex = sprintf "%04x", $ord;
     my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2;
-    die "Character 0x$hex with name '$fname' is above 0xFF";
+    croak "Character 0x$hex with name '$fname' is above 0xFF";
   }
+
   return pack "U", $ord;
 }
 
@@ -123,31 +127,47 @@ sub import
   }
 }
 
+require Unicode::UCD; # for Unicode::UCD::_getcode()
+
+my %viacode;
+
 sub viacode
 {
     if (@_ != 1) {
         carp "charnames::viacode() expects one numeric argument";
         return ()
     }
+
     my $arg = shift;
+    my $code = Unicode::UCD::_getcode($arg);
 
     my $hex;
-    if ($arg =~ m/^[0-9]+$/) {
+
+    if (defined $code) {
         $hex = sprintf "%04X", $arg;
     } else {
         carp("unexpected arg \"$arg\" to charnames::viacode()");
         return;
     }
 
+    if ($code > 0x10FFFF) {
+       carp "Unicode characters only allocated up to 0x10FFFF (you asked for $hex)";
+       return "\x{FFFD}";
+    }
+
+    return $viacode{$hex} if exists $viacode{$hex};
+
     $txt = do "unicore/Name.pl" unless $txt;
 
     if ($txt =~ m/^$hex\t\t(.+)/m) {
-        return $1;
+        return $viacode{$hex} = $1;
     } else {
         return;
     }
 }
 
+my %vianame;
+
 sub vianame
 {
     if (@_ != 1) {
@@ -157,10 +177,12 @@ sub vianame
 
     my $arg = shift;
 
+    return $vianame{$arg} if exists $vianame{$arg};
+
     $txt = do "unicore/Name.pl" unless $txt;
 
     if ($txt =~ m/^([0-9A-F]+)\t\t($arg)/m) {
-        return hex $1;
+        return $vianame{$arg} = hex $1;
     } else {
         return;
     }
@@ -213,7 +235,7 @@ is ignored.
 
 Note that C<\N{...}> is compile-time, it's a special form of string
 constant used inside double-quoted strings: in other words, you cannot
-used variables inside the C<\N{...}>.  If you want similar run-time
+use variables inside the C<\N{...}>.  If you want similar run-time
 functionality, use charnames::vianame().
 
 =head1 CUSTOM TRANSLATORS
@@ -274,6 +296,11 @@ Returns undef if no name is known for the name.
 This works only for the standard names, and does not yet aply 
 to custom translators.
 
+=head1 ILLEGAL CHARACTERS
+
+If you ask for a character that does not exist, a warning is given
+and the special Unicode I<replacement character> "\x{FFFD}" is returned.
+
 =head1 BUGS
 
 Since evaluation of the translation function happens in a middle of