Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
4.5 KiB

  1. package charnames;
  2. use bytes (); # for $bytes::hint_bits
  3. use warnings();
  4. $charnames::hint_bits = 0x20000;
  5. my $txt;
  6. # This is not optimized in any way yet
  7. sub charnames {
  8. $name = shift;
  9. $txt = do "unicode/Name.pl" unless $txt;
  10. my @off;
  11. if ($^H{charnames_full} and $txt =~ /\t\t$name$/m) {
  12. @off = ($-[0], $+[0]);
  13. }
  14. unless (@off) {
  15. if ($^H{charnames_short} and $name =~ /^(.*?):(.*)/s) {
  16. my ($script, $cname) = ($1,$2);
  17. my $case = ( $cname =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
  18. if ($txt =~ m/\t\t\U$script\E (?:$case )?LETTER \U$cname$/m) {
  19. @off = ($-[0], $+[0]);
  20. }
  21. }
  22. }
  23. unless (@off) {
  24. my $case = ( $name =~ /[[:upper:]]/ ? "CAPITAL" : "SMALL");
  25. for ( @{$^H{charnames_scripts}} ) {
  26. (@off = ($-[0], $+[0])), last
  27. if $txt =~ m/\t\t$_ (?:$case )?LETTER \U$name$/m;
  28. }
  29. }
  30. die "Unknown charname '$name'" unless @off;
  31. my $hexlen = 4; # Unicode guarantees 4-, 5-, or 6-digit format
  32. $hexlen++ while
  33. $hexlen < 6 && substr($txt, $off[0] - $hexlen - 1, 1) =~ /[0-9a-f]/;
  34. my $ord = hex substr $txt, $off[0] - $hexlen, $hexlen;
  35. if ($^H & $bytes::hint_bits) { # "use bytes" in effect?
  36. use bytes;
  37. return chr $ord if $ord <= 255;
  38. my $hex = sprintf '%X=0%o', $ord, $ord;
  39. my $fname = substr $txt, $off[0] + 2, $off[1] - $off[0] - 2;
  40. die "Character 0x$hex with name '$fname' is above 0xFF";
  41. }
  42. return chr $ord;
  43. }
  44. sub import {
  45. shift;
  46. die "`use charnames' needs explicit imports list" unless @_;
  47. $^H |= $charnames::hint_bits;
  48. $^H{charnames} = \&charnames ;
  49. my %h;
  50. @h{@_} = (1) x @_;
  51. $^H{charnames_full} = delete $h{':full'};
  52. $^H{charnames_short} = delete $h{':short'};
  53. $^H{charnames_scripts} = [map uc, keys %h];
  54. if (warnings::enabled('utf8') && @{$^H{charnames_scripts}}) {
  55. $txt = do "unicode/Name.pl" unless $txt;
  56. for (@{$^H{charnames_scripts}}) {
  57. warnings::warn('utf8', "No such script: '$_'") unless
  58. $txt =~ m/\t\t$_ (?:CAPITAL |SMALL )?LETTER /;
  59. }
  60. }
  61. }
  62. 1;
  63. __END__
  64. =head1 NAME
  65. charnames - define character names for C<\N{named}> string literal escape.
  66. =head1 SYNOPSIS
  67. use charnames ':full';
  68. print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n";
  69. use charnames ':short';
  70. print "\N{greek:Sigma} is an upper-case sigma.\n";
  71. use charnames qw(cyrillic greek);
  72. print "\N{sigma} is Greek sigma, and \N{be} is Cyrillic b.\n";
  73. =head1 DESCRIPTION
  74. Pragma C<use charnames> supports arguments C<:full>, C<:short> and
  75. script names. If C<:full> is present, for expansion of
  76. C<\N{CHARNAME}}> string C<CHARNAME> is first looked in the list of
  77. standard Unicode names of chars. If C<:short> is present, and
  78. C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
  79. as a letter in script C<SCRIPT>. If pragma C<use charnames> is used
  80. with script name arguments, then for C<\N{CHARNAME}}> the name
  81. C<CHARNAME> is looked up as a letter in the given scripts (in the
  82. specified order).
  83. For lookup of C<CHARNAME> inside a given script C<SCRIPTNAME>
  84. this pragma looks for the names
  85. SCRIPTNAME CAPITAL LETTER CHARNAME
  86. SCRIPTNAME SMALL LETTER CHARNAME
  87. SCRIPTNAME LETTER CHARNAME
  88. in the table of standard Unicode names. If C<CHARNAME> is lowercase,
  89. then the C<CAPITAL> variant is ignored, otherwise the C<SMALL> variant is
  90. ignored.
  91. =head1 CUSTOM TRANSLATORS
  92. The mechanism of translation of C<\N{...}> escapes is general and not
  93. hardwired into F<charnames.pm>. A module can install custom
  94. translations (inside the scope which C<use>s the module) with the
  95. following magic incantation:
  96. use charnames (); # for $charnames::hint_bits
  97. sub import {
  98. shift;
  99. $^H |= $charnames::hint_bits;
  100. $^H{charnames} = \&translator;
  101. }
  102. Here translator() is a subroutine which takes C<CHARNAME> as an
  103. argument, and returns text to insert into the string instead of the
  104. C<\N{CHARNAME}> escape. Since the text to insert should be different
  105. in C<bytes> mode and out of it, the function should check the current
  106. state of C<bytes>-flag as in:
  107. use bytes (); # for $bytes::hint_bits
  108. sub translator {
  109. if ($^H & $bytes::hint_bits) {
  110. return bytes_translator(@_);
  111. }
  112. else {
  113. return utf8_translator(@_);
  114. }
  115. }
  116. =head1 BUGS
  117. Since evaluation of the translation function happens in a middle of
  118. compilation (of a string literal), the translation function should not
  119. do any C<eval>s or C<require>s. This restriction should be lifted in
  120. a future version of Perl.
  121. =cut