X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fcharnames.pm;h=00faa6902c0a2b3787de19e6dec04d84b5d14198;hb=cca8f13b7a786baee2df1fba298a1923907c3bad;hp=c9a8ea5bbd5b6d3ae33d594ca8fe9f6e5c2831c5;hpb=51cf30b653436b0bbc2a8b902381f06f5842ab04;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/charnames.pm b/lib/charnames.pm index c9a8ea5..00faa69 100644 --- a/lib/charnames.pm +++ b/lib/charnames.pm @@ -1,12 +1,10 @@ package charnames; use strict; use warnings; -use Carp; use File::Spec; -our $VERSION = '1.02'; +our $VERSION = '1.05'; use bytes (); # for $bytes::hint_bits -$charnames::hint_bits = 0x20000; my %alias1 = ( # Icky 3.2 names with parentheses. @@ -44,6 +42,16 @@ my %alias3 = ( ); my $txt; +sub croak +{ + require Carp; goto &Carp::croak; +} # croak + +sub carp +{ + require Carp; goto &Carp::carp; +} # carp + sub alias (@) { @_ or return %alias3; @@ -107,7 +115,7 @@ sub charnames ## @off will hold the index into the code/name string of the start and ## end of the name as we find it. - ## If :full, look for the the name exactly + ## If :full, look for the name exactly if ($^H{charnames_full} and $txt =~ /\t\t\Q$name\E$/m) { @off = ($-[0], $+[0]); } @@ -183,15 +191,14 @@ sub import if (not @_) { carp("`use charnames' needs explicit imports list"); } - $^H |= $charnames::hint_bits; $^H{charnames} = \&charnames ; ## ## fill %h keys with our @_ args. ## my ($promote, %h, @args) = (0); - while (@_ and $_ = shift) { - if ($_ eq ":alias") { + while (my $arg = shift) { + if ($arg eq ":alias") { @_ or croak ":alias needs an argument in charnames"; my $alias = shift; @@ -210,11 +217,11 @@ sub import alias_file ($alias); next; } - if (m/^:/ and ! ($_ eq ":full" || $_ eq ":short")) { - warn "unsupported special '$_' in charnames"; + if (substr($arg, 0, 1) eq ':' and ! ($arg eq ":full" || $arg eq ":short")) { + warn "unsupported special '$arg' in charnames"; next; } - push @args, $_; + push @args, $arg; } @args == 0 && $promote and @args = (":full"); @h{@args} = (1) x @args; @@ -238,30 +245,31 @@ sub import } } # import -require Unicode::UCD; # for Unicode::UCD::_getcode() - my %viacode; sub viacode { if (@_ != 1) { carp "charnames::viacode() expects one argument"; - return () + return; } my $arg = shift; - my $code = Unicode::UCD::_getcode($arg); + # this comes actually from Unicode::UCD, where it is the named + # function _getcode (), but it avoids the overhead of loading it my $hex; - - if (defined $code) { + if ($arg =~ /^[1-9]\d*$/) { $hex = sprintf "%04X", $arg; + } elsif ($arg =~ /^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/) { + $hex = $1; } else { carp("unexpected arg \"$arg\" to charnames::viacode()"); return; } - if ($code > 0x10FFFF) { + # checking the length first is slightly faster + if (length($hex) > 5 && hex($hex) > 0x10FFFF) { carp sprintf "Unicode characters only allocated up to U+10FFFF (you asked for U+%X)", $hex; return; } @@ -270,11 +278,9 @@ sub viacode $txt = do "unicore/Name.pl" unless $txt; - if ($txt =~ m/^$hex\t\t(.+)/m) { - return $viacode{$hex} = $1; - } else { - return; - } + return unless $txt =~ m/^$hex\t\t(.+)/m; + + $viacode{$hex} = $1; } # viacode my %vianame; @@ -333,9 +339,10 @@ charnames - define character names for C<\N{named}> string literal escapes use charnames ":full", ":alias" => { e_ACUTE => "LATIN SMALL LETTER E WITH ACUTE", - }; + }; print "\N{e_ACUTE} is a small letter e with an acute.\n"; + use charnames (); print charnames::viacode(0x1234); # prints "ETHIOPIC SYLLABLE SEE" printf "%04X", charnames::vianame("GOTHIC LETTER AHSA"); # prints "10330" @@ -343,8 +350,8 @@ charnames - define character names for C<\N{named}> string literal escapes Pragma C supports arguments C<:full>, C<:short>, script names and customized aliases. If C<:full> is present, for expansion 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<\N{CHARNAME}>, the string C is first looked up in the list of +standard Unicode character names. If C<:short> is present, and C has the form C, then C is looked up as a letter in script C