From: Jarkko Hietaniemi Date: Wed, 4 Aug 1999 07:59:05 +0000 (+0000) Subject: Introduce the charnames pragma. X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=423cee853811c26846bd1948939b85f9866dfb4a;p=p5sagit%2Fp5-mst-13.2.git Introduce the charnames pragma. Subject: [PATCH 5.005_58] Free \C (for named chars), move to \O From: Ilya Zakharevich <[9]ilya@math.ohio-state.edu> To: Chip Salzenberg <[11]chip@perlsupport.com> Cc: Mailing list Perl5 <[12]perl5-porters@perl.org> Date: Sat, 31 Jul 1999 05:44:05 -0400 Message-Id: <[13]199907311407.IAA25042@localhost.frii.com> From: Ilya Zakharevich To: Mailing list Perl5 Subject: [PATCH 5.005_58] Named characters in Perl Date: Mon, 2 Aug 1999 19:25:40 -0400 Message-ID: <19990802192540.B24407@monk.mps.ohio-state.edu> p4raw-id: //depot/cfgperl@3916 --- diff --git a/MAINTAIN b/MAINTAIN index 12f987d..4507ca9 100644 --- a/MAINTAIN +++ b/MAINTAIN @@ -477,6 +477,7 @@ lib/bigint.pl lib/bigrat.pl lib/blib.pm lib/cacheout.pl +lib/charnames.pm ilya lib/chat2.pl lib/complete.pl lib/constant.pm @@ -683,6 +684,7 @@ t/lib/cgi-form.t t/lib/cgi-function.t t/lib/cgi-html.t t/lib/cgi-request.t +t/lib/charnames.t ilya t/lib/checktree.t t/lib/complex.t complex t/lib/db-btree.t pmarquess diff --git a/MANIFEST b/MANIFEST index 0db9a3f..0c0c077 100644 --- a/MANIFEST +++ b/MANIFEST @@ -648,6 +648,7 @@ lib/bigrat.pl An arbitrary precision rational arithmetic package lib/blib.pm For "use blib" lib/cacheout.pl Manages output filehandles when you need too many lib/caller.pm Inherit pragmatic attributes from caller's context +lib/charnames.pm Character names lib/chat2.pl Obsolete ipc library (use Comm.pm etc instead) lib/complete.pl A command completion subroutine lib/constant.pm For "use constant" @@ -1112,6 +1113,7 @@ t/lib/cgi-form.t See if CGI.pm works t/lib/cgi-function.t See if CGI.pm works t/lib/cgi-html.t See if CGI.pm works t/lib/cgi-request.t See if CGI.pm works +t/lib/charnames.t See if character names work t/lib/checktree.t See if File::CheckTree works t/lib/complex.t See if Math::Complex works t/lib/db-btree.t See if DB_File works diff --git a/lib/charnames.pm b/lib/charnames.pm new file mode 100644 index 0000000..e407ff7 --- /dev/null +++ b/lib/charnames.pm @@ -0,0 +1,134 @@ +package charnames; + +my $fname = 'unicode/UnicodeData-Latest.txt'; +my $txt; + +# This is not optimized in any way yet +sub charnames { + $name = shift; + $txt = do "unicode/Name.pl" unless $txt; + my @off; + if ($^H{charnames_full} and $txt =~ /\t\t$name$/m) { + @off = ($-[0], $+[0]); + } + unless (@off) { + if ($^H{charnames_short} and $name =~ /^(.*?):(.*)/s) { + my ($script, $cname) = ($1,$2); + my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL"); + if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U$cname$/m) { + @off = ($-[0], $+[0]); + } + } + } + unless (@off) { + my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL"); + for ( @{$^H{charnames_scripts}} ) { + (@off = ($-[0], $+[0])), last + if $txt =~ m/\t\t$_ (?:$case )?LETTER \U$name$/m; + } + } + 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; + } + 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"; +} + +sub import { + shift; + die "No scripts for `use charnames'" unless @_; + $^H |= 0x20000; + $^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]; +} + + +1; +__END__ + +=head1 NAME + +charnames - define character names for C<\C{named}> string literal escape. + +=head1 SYNOPSIS + + use charnames ':full'; + print "\C{GREEK SMALL LETTER SIGMA} is called sigma.\n"; + + use charnames ':short'; + print "\C{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"; + +=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 +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