diff options
author | Jarkko Hietaniemi <jhi@iki.fi> | 2001-12-15 22:09:20 +0000 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-12-15 22:09:20 +0000 |
commit | f0175764fddadd774727075c0c2bf5e087033a07 (patch) | |
tree | 59fa54e0f03e429acac2b259fa342c3396b55498 /lib | |
parent | 758f564b3fb5c63b84ce2ab3dd6edc68d23923b7 (diff) | |
download | perl-f0175764fddadd774727075c0c2bf5e087033a07.tar.gz |
Make charnames more robust.
p4raw-id: //depot/perl@13704
Diffstat (limited to 'lib')
-rw-r--r-- | lib/charnames.pm | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/lib/charnames.pm b/lib/charnames.pm index ec200ec98d..1297a76796 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,6 +127,8 @@ sub import } } +require Unicode::UCD; # for Unicode::UCD::_getcode() + my %viacode; sub viacode @@ -131,16 +137,24 @@ sub viacode 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; @@ -282,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 |