X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fcharnames.pm;h=934fafddd5ff8f5e0e95edaa6d3702d3376c51cd;hb=92dffb52e8549d6a194db47a2e5b989b8338a19a;hp=e407ff7c8af8eaeb59ba1e621f7ac9c958c02ec7;hpb=423cee853811c26846bd1948939b85f9866dfb4a;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/charnames.pm b/lib/charnames.pm index e407ff7..934fafd 100644 --- a/lib/charnames.pm +++ b/lib/charnames.pm @@ -1,6 +1,11 @@ package charnames; -my $fname = 'unicode/UnicodeData-Latest.txt'; +our $VERSION = '1.00'; + +use bytes (); # for $bytes::hint_bits +use warnings(); +$charnames::hint_bits = 0x20000; + my $txt; # This is not optimized in any way yet @@ -29,29 +34,34 @@ sub charnames { } die "Unknown charname '$name'" unless @off; - # use caller 'encoding'; # Does not work at compile time? - my $ord = hex substr $txt, $off[0] - 4, 4; - if ($^H & 0x8) { - use utf8; - return chr $ord; + 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 $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2; + die "Character 0x$hex with name '$fname' is above 0xFF"; } - return chr $ord if $ord <= 255; - my $hex = sprintf '%X=0%o', $ord, $ord; - my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2; - die "Character 0x$hex with name '$fname' is above 0xFF"; + return chr $ord; } sub import { shift; - die "No scripts for `use charnames'" unless @_; - $^H |= 0x20000; + die "`use charnames' needs explicit imports list" unless @_; + $^H |= $charnames::hint_bits; $^H{charnames} = \&charnames ; my %h; @h{@_} = (1) x @_; $^H{charnames_full} = delete $h{':full'}; $^H{charnames_short} = delete $h{':short'}; $^H{charnames_scripts} = [map uc, keys %h]; + if (warnings::enabled('utf8') && @{$^H{charnames_scripts}}) { + $txt = do "unicode/Name.pl" unless $txt; + for (@{$^H{charnames_scripts}}) { + warnings::warn('utf8', "No such script: '$_'") unless + $txt =~ m/\t\t$_ (?:CAPITAL |SMALL )?LETTER /; + } + } } @@ -60,68 +70,71 @@ __END__ =head1 NAME -charnames - define character names for C<\C{named}> string literal escape. +charnames - define character names for C<\N{named}> string literal escape. =head1 SYNOPSIS use charnames ':full'; - print "\C{GREEK SMALL LETTER SIGMA} is called sigma.\n"; + print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n"; use charnames ':short'; - print "\C{greek:Sigma} is an upper-case sigma.\n"; + print "\N{greek:Sigma} is an upper-case sigma.\n"; use charnames qw(cyrillic greek); - print "\C{sigma} is Greek sigma, and \C{be} is Cyrillic b.\n"; + print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n"; =head1 DESCRIPTION Pragma C supports arguments C<:full>, C<:short> and script names. If C<:full> is present, for expansion of -C<\C{CHARNAME}}> string C is first looked in the list of +C<\N{CHARNAME}}> string C is first looked in the list of standard Unicode names of chars. If C<:short> is present, and C has the form C, then C is looked up as a letter in script C