This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
utf8.c: Modify doc comment; change whitespace
[perl5.git] / lib / charnames.pm
index cd7b08a..2bf36ac 100644 (file)
@@ -1,12 +1,10 @@
 package charnames;
 use strict;
 use warnings;
-use Carp;
 use File::Spec;
-our $VERSION = '1.02';
+our $VERSION = '1.08';
 
 use bytes ();          # for $bytes::hint_bits
-$charnames::hint_bits = 0x20000; # HINT_LOCALIZE_HH
 
 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;
@@ -159,7 +167,7 @@ sub charnames
 
     ## we know where it starts, so turn into number -
     ## the ordinal for the char.
-    $ord = hex substr($txt, $hexstart, $off[0] - $hexstart);
+    $ord = CORE::hex substr($txt, $hexstart, $off[0] - $hexstart);
   }
 
   if ($^H & $bytes::hint_bits) {       # "use bytes" in effect?
@@ -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,31 +245,35 @@ 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 is derived from Unicode::UCD, where it is nearly the same as the
+  # function _getcode(), but it makes sure that even a hex argument has the
+  # proper number of leading zeros, which is critical in matching against $txt
+  # below
   my $hex;
-
-  if (defined $code) {
+  if ($arg =~ /^[1-9]\d*$/) {
     $hex = sprintf "%04X", $arg;
+  } elsif ($arg =~ /^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/) {
+    # Below is the line that differs from the _getcode() source
+    $hex = sprintf "%04X", hex $arg;
   } else {
     carp("unexpected arg \"$arg\" to charnames::viacode()");
     return;
   }
 
-  if ($code > 0x10FFFF) {
-    carp sprintf "Unicode characters only allocated up to U+10FFFF (you asked for U+%X)", $hex;
+  # checking the length first is slightly faster
+  if (length($hex) > 5 && hex($hex) > 0x10FFFF) {
+    carp "Unicode characters only allocated up to U+10FFFF (you asked for U+$hex)";
     return;
   }
 
@@ -270,11 +281,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;
@@ -288,20 +297,20 @@ sub vianame
 
   my $arg = shift;
 
-  return chr hex $1 if $arg =~ /^U\+([0-9a-fA-F]+)$/;
+  return chr CORE::hex $1 if $arg =~ /^U\+([0-9a-fA-F]+)$/;
 
   return $vianame{$arg} if exists $vianame{$arg};
 
   $txt = do "unicore/Name.pl" unless $txt;
 
   my $pos = index $txt, "\t\t$arg\n";
-  if ($[ <= $pos) {
+  if (0 <= $pos) {
     my $posLF = rindex $txt, "\n", $pos;
     (my $code = substr $txt, $posLF + 1, 6) =~ tr/\t//d;
-    return $vianame{$arg} = hex $code;
+    return $vianame{$arg} = CORE::hex $code;
 
-    # If $pos is at the 1st line, $posLF must be $[ - 1 (not found);
-    # then $posLF + 1 equals to $[ (at the beginning of $txt).
+    # If $pos is at the 1st line, $posLF must be -1 (not found);
+    # then $posLF + 1 equals to 0 (at the beginning of $txt).
     # Otherwise $posLF is the position of "\n";
     # then $posLF + 1 must be the position of the next to "\n"
     # (the beginning of the line).
@@ -333,9 +342,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 +353,8 @@ charnames - define character names for C<\N{named}> string literal escapes
 
 Pragma C<use charnames> supports arguments C<:full>, C<:short>, script
 names and customized aliases.  If C<:full> is present, for expansion of
-C<\N{CHARNAME}> string C<CHARNAME> is first looked in the list of
-standard Unicode names of chars.  If C<:short> is present, and
+C<\N{CHARNAME}>, the string C<CHARNAME> is first looked up in the list of
+standard Unicode character names.  If C<:short> is present, and
 C<CHARNAME> has the form C<SCRIPT:CNAME>, then C<CNAME> is looked up
 as a letter in script C<SCRIPT>.  If pragma C<use charnames> is used
 with script name arguments, then for C<\N{CHARNAME}> the name
@@ -377,40 +387,68 @@ U+0084, and U+0099 do not have names even in ISO 6429.
 Since the Unicode standard uses "U+HHHH", so can you: "\N{U+263a}"
 is the Unicode smiley face, or "\N{WHITE SMILING FACE}".
 
-=head1 CUSTOM TRANSLATORS
+=head1 ALIASES
 
-The mechanism of translation of C<\N{...}> escapes is general and not
-hardwired into F<charnames.pm>.  A module can install custom
-translations (inside the scope which C<use>s the module) with the
-following magic incantation:
+A few aliases have been defined for convenience: instead of having
+to use the official names
 
-    use charnames ();          # for $charnames::hint_bits
-    sub import {
-       shift;
-       $^H |= $charnames::hint_bits;
-       $^H{charnames} = \&translator;
-    }
+    LINE FEED (LF)
+    FORM FEED (FF)
+    CARRIAGE RETURN (CR)
+    NEXT LINE (NEL)
 
-Here translator() is a subroutine which takes C<CHARNAME> 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<bytes> mode and out of it, the function should check the current
-state of C<bytes>-flag as in:
+(yes, with parentheses) one can use
 
-    use bytes ();                      # for $bytes::hint_bits
-    sub translator {
-       if ($^H & $bytes::hint_bits) {
-           return bytes_translator(@_);
-       }
-       else {
-           return utf8_translator(@_);
-       }
-    }
+    LINE FEED
+    FORM FEED
+    CARRIAGE RETURN
+    NEXT LINE
+    LF
+    FF
+    CR
+    NEL
+
+One can also use
+
+    BYTE ORDER MARK
+    BOM
+
+and
+
+    ZWNJ
+    ZWJ
+
+for ZERO WIDTH NON-JOINER and ZERO WIDTH JOINER.
+
+For backward compatibility one can use the old names for
+certain C0 and C1 controls
+
+    old                         new
+
+    HORIZONTAL TABULATION       CHARACTER TABULATION
+    VERTICAL TABULATION         LINE TABULATION
+    FILE SEPARATOR              INFORMATION SEPARATOR FOUR
+    GROUP SEPARATOR             INFORMATION SEPARATOR THREE
+    RECORD SEPARATOR            INFORMATION SEPARATOR TWO
+    UNIT SEPARATOR              INFORMATION SEPARATOR ONE
+    PARTIAL LINE DOWN           PARTIAL LINE FORWARD
+    PARTIAL LINE UP             PARTIAL LINE BACKWARD
+
+but the old names in addition to giving the character
+will also give a warning about being deprecated.
 
 =head1 CUSTOM ALIASES
 
 This version of charnames supports three mechanisms of adding local
-or customized aliases to standard Unicode naming conventions (:full)
+or customized aliases to standard Unicode naming conventions (:full).
+
+Note that an alias should not be something that is a legal curly
+brace-enclosed quantifier (see L<perlreref/QUANTIFIERS>).  For example
+C<\N{123}> means to match 123 non-newline characters, and is not treated as an
+alias.  Aliases are discouraged from beginning with anything other than an
+alphabetic character and from containing anything other than alphanumerics,
+spaces, dashes, colons, parentheses, and underscores.  Currently they must be
+ASCII.
 
 =head2 Anonymous hashes
 
@@ -475,68 +513,66 @@ Returns undef if the name is unknown.
 This works only for the standard names, and does not yet apply
 to custom translators.
 
-=head1 ALIASES
-
-A few aliases have been defined for convenience: instead of having
-to use the official names
-
-    LINE FEED (LF)
-    FORM FEED (FF)
-    CARRIAGE RETURN (CR)
-    NEXT LINE (NEL)
+=head1 CUSTOM TRANSLATORS
 
-(yes, with parentheses) one can use
+The mechanism of translation of C<\N{...}> escapes is general and not
+hardwired into F<charnames.pm>.  A module can install custom
+translations (inside the scope which C<use>s the module) with the
+following magic incantation:
 
-    LINE FEED
-    FORM FEED
-    CARRIAGE RETURN
-    NEXT LINE
-    LF
-    FF
-    CR
-    NEL
+    sub import {
+       shift;
+       $^H{charnames} = \&translator;
+    }
 
-One can also use
+Here translator() is a subroutine which takes C<CHARNAME> 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<bytes> mode and out of it, the function should check the current
+state of C<bytes>-flag as in:
 
-    BYTE ORDER MARK
-    BOM
+    use bytes ();                      # for $bytes::hint_bits
+    sub translator {
+       if ($^H & $bytes::hint_bits) {
+           return bytes_translator(@_);
+       }
+       else {
+           return utf8_translator(@_);
+       }
+    }
 
-and
+See L</CUSTOM ALIASES> above for restrictions on C<CHARNAME>.
 
-    ZWNJ
-    ZWJ
+=head1 ILLEGAL CHARACTERS
 
-for ZERO WIDTH NON-JOINER and ZERO WIDTH JOINER.
+If you ask by name for a character that does not exist, a warning is given and
+the Unicode I<replacement character> "\x{FFFD}" is returned.
 
-For backward compatibility one can use the old names for
-certain C0 and C1 controls
+If you ask by code for a character that is unassigned, no warning is
+given and C<undef> is returned.  (Though if you ask for a code point
+past U+10FFFF you do get a warning.)  See L</BUGS> below.
 
-    old                         new
+=head1 BUGS
 
-    HORIZONTAL TABULATION       CHARACTER TABULATION
-    VERTICAL TABULATION         LINE TABULATION
-    FILE SEPARATOR              INFORMATION SEPARATOR FOUR
-    GROUP SEPARATOR             INFORMATION SEPARATOR THREE
-    RECORD SEPARATOR            INFORMATION SEPARATOR TWO
-    UNIT SEPARATOR              INFORMATION SEPARATOR ONE
-    PARTIAL LINE DOWN           PARTIAL LINE FORWARD
-    PARTIAL LINE UP             PARTIAL LINE BACKWARD
+viacode should return an empty string for unassigned in-range Unicode code
+points, as that is their correct current name.
 
-but the old names in addition to giving the character
-will also give a warning about being deprecated.
+viacode(0) doesn't return C<NULL>, but C<undef>
 
-=head1 ILLEGAL CHARACTERS
+vianame returns a chr if the input name is of the form C<U+...>, and an ord
+otherwise.  It is planned to change this to always return an ord.
 
-If you ask by name for a character that does not exist, a warning is
-given and the Unicode I<replacement character> "\x{FFFD}" is returned.
+None of the functions work on almost all the Hangul syllable and CJK Unicode
+characters that have their code points as part of their names.
 
-If you ask by code for a character that does not exist, no warning is
-given and C<undef> is returned.  (Though if you ask for a code point
-past U+10FFFF you do get a warning.)
+Names must be ASCII characters only.
 
-=head1 BUGS
+Unicode standard named sequences are not recognized, such as
+C<LATIN CAPITAL LETTER A WITH MACRON AND GRAVE>
+(which should mean C<LATIN CAPITAL LETTER A WITH MACRON> with an additional
+C<COMBINING GRAVE ACCENT>).
 
-Since evaluation of the translation function happens in a middle of
+Since evaluation of the translation function happens in the middle of
 compilation (of a string literal), the translation function should not
 do any C<eval>s or C<require>s.  This restriction should be lifted in
 a future version of Perl.