X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fcharnames.pm;h=934fafddd5ff8f5e0e95edaa6d3702d3376c51cd;hb=92dffb52e8549d6a194db47a2e5b989b8338a19a;hp=817b4c559ebf7154ab75dea9c32d668b90051827;hpb=9c6664052fe5df4f48aca49de15d21ae45b49e89;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/charnames.pm b/lib/charnames.pm index 817b4c5..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 @@ -30,8 +35,8 @@ sub charnames { die "Unknown charname '$name'" unless @off; my $ord = hex substr $txt, $off[0] - 4, 4; - if ($^H & 0x8) { # "use byte" in effect? - use byte; + 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; @@ -42,14 +47,21 @@ sub charnames { 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 /; + } + } } @@ -84,42 +96,45 @@ C is looked up as a letter in the given scripts (in the specified order). For lookup of C inside a given script C -F looks for the names +this pragma looks for the names SCRIPTNAME CAPITAL LETTER CHARNAME SCRIPTNAME SMALL LETTER CHARNAME SCRIPTNAME LETTER CHARNAME in the table of standard Unicode names. If C is lowercase, -then the C variant is ignored, otherwise C variant is +then the C variant is ignored, otherwise the C variant is ignored. =head1 CUSTOM TRANSLATORS -The mechanism of translation is C<\N{...}> escapes is general and not +The mechanism of translation of C<\N{...}> escapes is general and not hardwired into F. A module can install custom -translations (inside the scope which Cs the module) by the +translations (inside the scope which Cs the module) with the following magic incantation: - sub import { - shift; - $^H |= 0x20000; - $^H{charnames} = \&translator; - } + use charnames (); # for $charnames::hint_bits + sub import { + shift; + $^H |= $charnames::hint_bits; + $^H{charnames} = \&translator; + } Here translator() is a subroutine which takes C as an argument, and returns text to insert into the string instead of the C<\N{CHARNAME}> escape. Since the text to insert should be different -in C mode and out of it, the function should check the current -state of C-flag as in - - sub translator { - if ($^H & 0x8) { - return utf_translator(@_); - } else { - return no_utf_translator(@_); +in C mode and out of it, the function should check the current +state of C-flag as in: + + use bytes (); # for $bytes::hint_bits + sub translator { + if ($^H & $bytes::hint_bits) { + return bytes_translator(@_); + } + else { + return utf8_translator(@_); + } } - } =head1 BUGS @@ -129,4 +144,3 @@ do any Cs or Cs. This restriction should be lifted in a future version of Perl. =cut -