X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fcharnames.pm;h=1297a767964578b4d8552920cde5a70caeec5abf;hb=5290524f8b52822096b01140005d681d126b507d;hp=5554ae0368b74c44377d0ef41973801d9e27dec8;hpb=daf0d4939ae7ec91853159e8e5e62c8cae1e0a9f;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/charnames.pm b/lib/charnames.pm index 5554ae0..1297a76 100644 --- a/lib/charnames.pm +++ b/lib/charnames.pm @@ -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 "\x{FFFD}" is returned. + =head1 BUGS Since evaluation of the translation function happens in a middle of