This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perldiag.pod: Document overload’s invalid arg warning
[perl5.git] / lib / _charnames.pm
1 # !!!!!!!   INTERNAL PERL USE ONLY   !!!!!!!
2 # This helper module is for internal use by core Perl only.  This module is
3 # subject to change or removal at any time without notice.  Don't use it
4 # directly.  Use the public <charnames> module instead.
5
6 package _charnames;
7 use strict;
8 use warnings;
9 use File::Spec;
10 our $VERSION = '1.28';
11 use unicore::Name;    # mktables-generated algorithmically-defined names
12
13 use bytes ();          # for $bytes::hint_bits
14 use re "/aa";          # Everything in here should be ASCII
15
16 $Carp::Internal{ (__PACKAGE__) } = 1;
17
18 # Translate between Unicode character names and their code points.  This is a
19 # submodule of package <charnames>, used to allow \N{...} to be autoloaded,
20 # but it was decided not to autoload the various functions in charnames; the
21 # splitting allows this behavior.
22 #
23 # The official names with their code points are stored in a table in
24 # lib/unicore/Name.pl which is read in as a large string (almost 3/4 Mb in
25 # Unicode 6.0).  Each code point/name combination is separated by a \n in the
26 # string.  (Some of the CJK and the Hangul syllable names are determined
27 # instead algorithmically via subroutines stored instead in
28 # lib/unicore/Name.pm).  Because of the large size of this table, it isn't
29 # converted into hashes for faster lookup.
30 #
31 # But, user defined aliases are stored in their own hashes, as are Perl
32 # extensions to the official names.  These are checked first before looking at
33 # the official table.
34 #
35 # Basically, the table is grepped for the input code point (viacode()) or
36 # name (the other functions), and the corresponding value on the same line is
37 # returned.  The grepping is done by turning the input into a regular
38 # expression.  Thus, the same table does double duty, used by both name and
39 # code point lookup.  (If we were to have hashes, we would need two, one for
40 # each lookup direction.)
41 #
42 # For loose name matching, the logical thing would be to have a table
43 # with all the ignorable characters squeezed out, and then grep it with the
44 # similiarly-squeezed input name.  (And this is in fact how the lookups are
45 # done with the small Perl extension hashes.)  But since we need to be able to
46 # go from code point to official name, the original table would still need to
47 # exist.  Due to the large size of the table, it was decided to not read
48 # another very large string into memory for a second table.  Instead, the
49 # regular expression of the input name is modified to have optional spaces and
50 # dashes between characters.  For example, in strict matching, the regular
51 # expression would be:
52 #   qr/\tDIGIT ONE$/m
53 # Under loose matching, the blank would be squeezed out, and the re would be:
54 #   qr/\tD[- ]?I[- ]?G[- ]?I[- ]?T[- ]?O[- ]?N[- ]?E$/m
55 # which matches a blank or dash between any characters in the official table.
56 #
57 # This is also how script lookup is done.  Basically the re looks like
58 #   qr/ (?:LATIN|GREEK|CYRILLIC) (?:SMALL )?LETTER $name/
59 # where $name is the loose or strict regex for the remainder of the name.
60
61 # The hashes are stored as utf8 strings.  This makes it easier to deal with
62 # sequences.  I (khw) also tried making Name.pl utf8, but it slowed things
63 # down by a factor of 7.  I then tried making Name.pl store the ut8
64 # equivalents but not calling them utf8.  That led to similar speed as leaving
65 # it alone, but since that is harder for a human to parse, I left it as-is.
66
67 my %system_aliases = (
68     # Synonyms for the icky 3.2 names that have parentheses.
69     'LINE FEED'             => pack("U", 0x0A), # LINE FEED (LF)
70     'FORM FEED'             => pack("U", 0x0C), # FORM FEED (FF)
71     'CARRIAGE RETURN'       => pack("U", 0x0D), # CARRIAGE RETURN (CR)
72     'NEXT LINE'             => pack("U", 0x85), # NEXT LINE (NEL)
73
74     # Some variant names from Wikipedia
75     'SINGLE-SHIFT 2'                => pack("U", 0x8E),
76     'SINGLE-SHIFT 3'                => pack("U", 0x8F),
77     'PRIVATE USE 1'                 => pack("U", 0x91),
78     'PRIVATE USE 2'                 => pack("U", 0x92),
79     'START OF PROTECTED AREA'       => pack("U", 0x96),
80     'END OF PROTECTED AREA'         => pack("U", 0x97),
81
82     # Convenience.  Standard abbreviations for the controls
83     'NUL'           => pack("U", 0x00), # NULL
84     'SOH'           => pack("U", 0x01), # START OF HEADING
85     'STX'           => pack("U", 0x02), # START OF TEXT
86     'ETX'           => pack("U", 0x03), # END OF TEXT
87     'EOT'           => pack("U", 0x04), # END OF TRANSMISSION
88     'ENQ'           => pack("U", 0x05), # ENQUIRY
89     'ACK'           => pack("U", 0x06), # ACKNOWLEDGE
90     'BEL'           => pack("U", 0x07), # ALERT; formerly BELL
91     'BS'            => pack("U", 0x08), # BACKSPACE
92     'HT'            => pack("U", 0x09), # HORIZONTAL TABULATION
93     'LF'            => pack("U", 0x0A), # LINE FEED (LF)
94     'VT'            => pack("U", 0x0B), # VERTICAL TABULATION
95     'FF'            => pack("U", 0x0C), # FORM FEED (FF)
96     'CR'            => pack("U", 0x0D), # CARRIAGE RETURN (CR)
97     'SO'            => pack("U", 0x0E), # SHIFT OUT
98     'SI'            => pack("U", 0x0F), # SHIFT IN
99     'DLE'           => pack("U", 0x10), # DATA LINK ESCAPE
100     'DC1'           => pack("U", 0x11), # DEVICE CONTROL ONE
101     'DC2'           => pack("U", 0x12), # DEVICE CONTROL TWO
102     'DC3'           => pack("U", 0x13), # DEVICE CONTROL THREE
103     'DC4'           => pack("U", 0x14), # DEVICE CONTROL FOUR
104     'NAK'           => pack("U", 0x15), # NEGATIVE ACKNOWLEDGE
105     'SYN'           => pack("U", 0x16), # SYNCHRONOUS IDLE
106     'ETB'           => pack("U", 0x17), # END OF TRANSMISSION BLOCK
107     'CAN'           => pack("U", 0x18), # CANCEL
108     'EOM'           => pack("U", 0x19), # END OF MEDIUM
109     'SUB'           => pack("U", 0x1A), # SUBSTITUTE
110     'ESC'           => pack("U", 0x1B), # ESCAPE
111     'FS'            => pack("U", 0x1C), # FILE SEPARATOR
112     'GS'            => pack("U", 0x1D), # GROUP SEPARATOR
113     'RS'            => pack("U", 0x1E), # RECORD SEPARATOR
114     'US'            => pack("U", 0x1F), # UNIT SEPARATOR
115     'DEL'           => pack("U", 0x7F), # DELETE
116     'BPH'           => pack("U", 0x82), # BREAK PERMITTED HERE
117     'NBH'           => pack("U", 0x83), # NO BREAK HERE
118     'NEL'           => pack("U", 0x85), # NEXT LINE (NEL)
119     'SSA'           => pack("U", 0x86), # START OF SELECTED AREA
120     'ESA'           => pack("U", 0x87), # END OF SELECTED AREA
121     'HTS'           => pack("U", 0x88), # CHARACTER TABULATION SET
122     'HTJ'           => pack("U", 0x89), # CHARACTER TABULATION WITH JUSTIFICATION
123     'VTS'           => pack("U", 0x8A), # LINE TABULATION SET
124     'PLD'           => pack("U", 0x8B), # PARTIAL LINE FORWARD
125     'PLU'           => pack("U", 0x8C), # PARTIAL LINE BACKWARD
126     'RI'            => pack("U", 0x8D), # REVERSE LINE FEED
127     'SS2'           => pack("U", 0x8E), # SINGLE SHIFT TWO
128     'SS3'           => pack("U", 0x8F), # SINGLE SHIFT THREE
129     'DCS'           => pack("U", 0x90), # DEVICE CONTROL STRING
130     'PU1'           => pack("U", 0x91), # PRIVATE USE ONE
131     'PU2'           => pack("U", 0x92), # PRIVATE USE TWO
132     'STS'           => pack("U", 0x93), # SET TRANSMIT STATE
133     'CCH'           => pack("U", 0x94), # CANCEL CHARACTER
134     'MW'            => pack("U", 0x95), # MESSAGE WAITING
135     'SPA'           => pack("U", 0x96), # START OF GUARDED AREA
136     'EPA'           => pack("U", 0x97), # END OF GUARDED AREA
137     'SOS'           => pack("U", 0x98), # START OF STRING
138     'SCI'           => pack("U", 0x9A), # SINGLE CHARACTER INTRODUCER
139     'CSI'           => pack("U", 0x9B), # CONTROL SEQUENCE INTRODUCER
140     'ST'            => pack("U", 0x9C), # STRING TERMINATOR
141     'OSC'           => pack("U", 0x9D), # OPERATING SYSTEM COMMAND
142     'PM'            => pack("U", 0x9E), # PRIVACY MESSAGE
143     'APC'           => pack("U", 0x9F), # APPLICATION PROGRAM COMMAND
144
145     # There are no names for these in the Unicode standard; perhaps should be
146     # deprecated, but then again there are no alternative names, so am not
147     # deprecating.  And if did, the code would have to change to not recommend
148     # an alternative for these.
149     'PADDING CHARACTER'                     => pack("U", 0x80),
150     'PAD'                                   => pack("U", 0x80),
151     'HIGH OCTET PRESET'                     => pack("U", 0x81),
152     'HOP'                                   => pack("U", 0x81),
153     'INDEX'                                 => pack("U", 0x84),
154     'IND'                                   => pack("U", 0x84),
155     'SINGLE GRAPHIC CHARACTER INTRODUCER'   => pack("U", 0x99),
156     'SGC'                                   => pack("U", 0x99),
157
158     # More convenience.  For further convenience, it is suggested some way of
159     # using the NamesList aliases be implemented, but there are ambiguities in
160     # NamesList.txt
161     'BOM'   => pack("U", 0xFEFF), # BYTE ORDER MARK
162     'BYTE ORDER MARK'=> pack("U", 0xFEFF),
163     'CGJ'   => pack("U", 0x034F), # COMBINING GRAPHEME JOINER
164     'FVS1'  => pack("U", 0x180B), # MONGOLIAN FREE VARIATION SELECTOR ONE
165     'FVS2'  => pack("U", 0x180C), # MONGOLIAN FREE VARIATION SELECTOR TWO
166     'FVS3'  => pack("U", 0x180D), # MONGOLIAN FREE VARIATION SELECTOR THREE
167     'LRE'   => pack("U", 0x202A), # LEFT-TO-RIGHT EMBEDDING
168     'LRM'   => pack("U", 0x200E), # LEFT-TO-RIGHT MARK
169     'LRO'   => pack("U", 0x202D), # LEFT-TO-RIGHT OVERRIDE
170     'MMSP'  => pack("U", 0x205F), # MEDIUM MATHEMATICAL SPACE
171     'MVS'   => pack("U", 0x180E), # MONGOLIAN VOWEL SEPARATOR
172     'NBSP'  => pack("U", 0x00A0), # NO-BREAK SPACE
173     'NNBSP' => pack("U", 0x202F), # NARROW NO-BREAK SPACE
174     'PDF'   => pack("U", 0x202C), # POP DIRECTIONAL FORMATTING
175     'RLE'   => pack("U", 0x202B), # RIGHT-TO-LEFT EMBEDDING
176     'RLM'   => pack("U", 0x200F), # RIGHT-TO-LEFT MARK
177     'RLO'   => pack("U", 0x202E), # RIGHT-TO-LEFT OVERRIDE
178     'SHY'   => pack("U", 0x00AD), # SOFT HYPHEN
179     'VS1'   => pack("U", 0xFE00), # VARIATION SELECTOR-1
180     'VS2'   => pack("U", 0xFE01), # VARIATION SELECTOR-2
181     'VS3'   => pack("U", 0xFE02), # VARIATION SELECTOR-3
182     'VS4'   => pack("U", 0xFE03), # VARIATION SELECTOR-4
183     'VS5'   => pack("U", 0xFE04), # VARIATION SELECTOR-5
184     'VS6'   => pack("U", 0xFE05), # VARIATION SELECTOR-6
185     'VS7'   => pack("U", 0xFE06), # VARIATION SELECTOR-7
186     'VS8'   => pack("U", 0xFE07), # VARIATION SELECTOR-8
187     'VS9'   => pack("U", 0xFE08), # VARIATION SELECTOR-9
188     'VS10'  => pack("U", 0xFE09), # VARIATION SELECTOR-10
189     'VS11'  => pack("U", 0xFE0A), # VARIATION SELECTOR-11
190     'VS12'  => pack("U", 0xFE0B), # VARIATION SELECTOR-12
191     'VS13'  => pack("U", 0xFE0C), # VARIATION SELECTOR-13
192     'VS14'  => pack("U", 0xFE0D), # VARIATION SELECTOR-14
193     'VS15'  => pack("U", 0xFE0E), # VARIATION SELECTOR-15
194     'VS16'  => pack("U", 0xFE0F), # VARIATION SELECTOR-16
195     'VS17'  => pack("U", 0xE0100), # VARIATION SELECTOR-17
196     'VS18'  => pack("U", 0xE0101), # VARIATION SELECTOR-18
197     'VS19'  => pack("U", 0xE0102), # VARIATION SELECTOR-19
198     'VS20'  => pack("U", 0xE0103), # VARIATION SELECTOR-20
199     'VS21'  => pack("U", 0xE0104), # VARIATION SELECTOR-21
200     'VS22'  => pack("U", 0xE0105), # VARIATION SELECTOR-22
201     'VS23'  => pack("U", 0xE0106), # VARIATION SELECTOR-23
202     'VS24'  => pack("U", 0xE0107), # VARIATION SELECTOR-24
203     'VS25'  => pack("U", 0xE0108), # VARIATION SELECTOR-25
204     'VS26'  => pack("U", 0xE0109), # VARIATION SELECTOR-26
205     'VS27'  => pack("U", 0xE010A), # VARIATION SELECTOR-27
206     'VS28'  => pack("U", 0xE010B), # VARIATION SELECTOR-28
207     'VS29'  => pack("U", 0xE010C), # VARIATION SELECTOR-29
208     'VS30'  => pack("U", 0xE010D), # VARIATION SELECTOR-30
209     'VS31'  => pack("U", 0xE010E), # VARIATION SELECTOR-31
210     'VS32'  => pack("U", 0xE010F), # VARIATION SELECTOR-32
211     'VS33'  => pack("U", 0xE0110), # VARIATION SELECTOR-33
212     'VS34'  => pack("U", 0xE0111), # VARIATION SELECTOR-34
213     'VS35'  => pack("U", 0xE0112), # VARIATION SELECTOR-35
214     'VS36'  => pack("U", 0xE0113), # VARIATION SELECTOR-36
215     'VS37'  => pack("U", 0xE0114), # VARIATION SELECTOR-37
216     'VS38'  => pack("U", 0xE0115), # VARIATION SELECTOR-38
217     'VS39'  => pack("U", 0xE0116), # VARIATION SELECTOR-39
218     'VS40'  => pack("U", 0xE0117), # VARIATION SELECTOR-40
219     'VS41'  => pack("U", 0xE0118), # VARIATION SELECTOR-41
220     'VS42'  => pack("U", 0xE0119), # VARIATION SELECTOR-42
221     'VS43'  => pack("U", 0xE011A), # VARIATION SELECTOR-43
222     'VS44'  => pack("U", 0xE011B), # VARIATION SELECTOR-44
223     'VS45'  => pack("U", 0xE011C), # VARIATION SELECTOR-45
224     'VS46'  => pack("U", 0xE011D), # VARIATION SELECTOR-46
225     'VS47'  => pack("U", 0xE011E), # VARIATION SELECTOR-47
226     'VS48'  => pack("U", 0xE011F), # VARIATION SELECTOR-48
227     'VS49'  => pack("U", 0xE0120), # VARIATION SELECTOR-49
228     'VS50'  => pack("U", 0xE0121), # VARIATION SELECTOR-50
229     'VS51'  => pack("U", 0xE0122), # VARIATION SELECTOR-51
230     'VS52'  => pack("U", 0xE0123), # VARIATION SELECTOR-52
231     'VS53'  => pack("U", 0xE0124), # VARIATION SELECTOR-53
232     'VS54'  => pack("U", 0xE0125), # VARIATION SELECTOR-54
233     'VS55'  => pack("U", 0xE0126), # VARIATION SELECTOR-55
234     'VS56'  => pack("U", 0xE0127), # VARIATION SELECTOR-56
235     'VS57'  => pack("U", 0xE0128), # VARIATION SELECTOR-57
236     'VS58'  => pack("U", 0xE0129), # VARIATION SELECTOR-58
237     'VS59'  => pack("U", 0xE012A), # VARIATION SELECTOR-59
238     'VS60'  => pack("U", 0xE012B), # VARIATION SELECTOR-60
239     'VS61'  => pack("U", 0xE012C), # VARIATION SELECTOR-61
240     'VS62'  => pack("U", 0xE012D), # VARIATION SELECTOR-62
241     'VS63'  => pack("U", 0xE012E), # VARIATION SELECTOR-63
242     'VS64'  => pack("U", 0xE012F), # VARIATION SELECTOR-64
243     'VS65'  => pack("U", 0xE0130), # VARIATION SELECTOR-65
244     'VS66'  => pack("U", 0xE0131), # VARIATION SELECTOR-66
245     'VS67'  => pack("U", 0xE0132), # VARIATION SELECTOR-67
246     'VS68'  => pack("U", 0xE0133), # VARIATION SELECTOR-68
247     'VS69'  => pack("U", 0xE0134), # VARIATION SELECTOR-69
248     'VS70'  => pack("U", 0xE0135), # VARIATION SELECTOR-70
249     'VS71'  => pack("U", 0xE0136), # VARIATION SELECTOR-71
250     'VS72'  => pack("U", 0xE0137), # VARIATION SELECTOR-72
251     'VS73'  => pack("U", 0xE0138), # VARIATION SELECTOR-73
252     'VS74'  => pack("U", 0xE0139), # VARIATION SELECTOR-74
253     'VS75'  => pack("U", 0xE013A), # VARIATION SELECTOR-75
254     'VS76'  => pack("U", 0xE013B), # VARIATION SELECTOR-76
255     'VS77'  => pack("U", 0xE013C), # VARIATION SELECTOR-77
256     'VS78'  => pack("U", 0xE013D), # VARIATION SELECTOR-78
257     'VS79'  => pack("U", 0xE013E), # VARIATION SELECTOR-79
258     'VS80'  => pack("U", 0xE013F), # VARIATION SELECTOR-80
259     'VS81'  => pack("U", 0xE0140), # VARIATION SELECTOR-81
260     'VS82'  => pack("U", 0xE0141), # VARIATION SELECTOR-82
261     'VS83'  => pack("U", 0xE0142), # VARIATION SELECTOR-83
262     'VS84'  => pack("U", 0xE0143), # VARIATION SELECTOR-84
263     'VS85'  => pack("U", 0xE0144), # VARIATION SELECTOR-85
264     'VS86'  => pack("U", 0xE0145), # VARIATION SELECTOR-86
265     'VS87'  => pack("U", 0xE0146), # VARIATION SELECTOR-87
266     'VS88'  => pack("U", 0xE0147), # VARIATION SELECTOR-88
267     'VS89'  => pack("U", 0xE0148), # VARIATION SELECTOR-89
268     'VS90'  => pack("U", 0xE0149), # VARIATION SELECTOR-90
269     'VS91'  => pack("U", 0xE014A), # VARIATION SELECTOR-91
270     'VS92'  => pack("U", 0xE014B), # VARIATION SELECTOR-92
271     'VS93'  => pack("U", 0xE014C), # VARIATION SELECTOR-93
272     'VS94'  => pack("U", 0xE014D), # VARIATION SELECTOR-94
273     'VS95'  => pack("U", 0xE014E), # VARIATION SELECTOR-95
274     'VS96'  => pack("U", 0xE014F), # VARIATION SELECTOR-96
275     'VS97'  => pack("U", 0xE0150), # VARIATION SELECTOR-97
276     'VS98'  => pack("U", 0xE0151), # VARIATION SELECTOR-98
277     'VS99'  => pack("U", 0xE0152), # VARIATION SELECTOR-99
278     'VS100' => pack("U", 0xE0153), # VARIATION SELECTOR-100
279     'VS101' => pack("U", 0xE0154), # VARIATION SELECTOR-101
280     'VS102' => pack("U", 0xE0155), # VARIATION SELECTOR-102
281     'VS103' => pack("U", 0xE0156), # VARIATION SELECTOR-103
282     'VS104' => pack("U", 0xE0157), # VARIATION SELECTOR-104
283     'VS105' => pack("U", 0xE0158), # VARIATION SELECTOR-105
284     'VS106' => pack("U", 0xE0159), # VARIATION SELECTOR-106
285     'VS107' => pack("U", 0xE015A), # VARIATION SELECTOR-107
286     'VS108' => pack("U", 0xE015B), # VARIATION SELECTOR-108
287     'VS109' => pack("U", 0xE015C), # VARIATION SELECTOR-109
288     'VS110' => pack("U", 0xE015D), # VARIATION SELECTOR-110
289     'VS111' => pack("U", 0xE015E), # VARIATION SELECTOR-111
290     'VS112' => pack("U", 0xE015F), # VARIATION SELECTOR-112
291     'VS113' => pack("U", 0xE0160), # VARIATION SELECTOR-113
292     'VS114' => pack("U", 0xE0161), # VARIATION SELECTOR-114
293     'VS115' => pack("U", 0xE0162), # VARIATION SELECTOR-115
294     'VS116' => pack("U", 0xE0163), # VARIATION SELECTOR-116
295     'VS117' => pack("U", 0xE0164), # VARIATION SELECTOR-117
296     'VS118' => pack("U", 0xE0165), # VARIATION SELECTOR-118
297     'VS119' => pack("U", 0xE0166), # VARIATION SELECTOR-119
298     'VS120' => pack("U", 0xE0167), # VARIATION SELECTOR-120
299     'VS121' => pack("U", 0xE0168), # VARIATION SELECTOR-121
300     'VS122' => pack("U", 0xE0169), # VARIATION SELECTOR-122
301     'VS123' => pack("U", 0xE016A), # VARIATION SELECTOR-123
302     'VS124' => pack("U", 0xE016B), # VARIATION SELECTOR-124
303     'VS125' => pack("U", 0xE016C), # VARIATION SELECTOR-125
304     'VS126' => pack("U", 0xE016D), # VARIATION SELECTOR-126
305     'VS127' => pack("U", 0xE016E), # VARIATION SELECTOR-127
306     'VS128' => pack("U", 0xE016F), # VARIATION SELECTOR-128
307     'VS129' => pack("U", 0xE0170), # VARIATION SELECTOR-129
308     'VS130' => pack("U", 0xE0171), # VARIATION SELECTOR-130
309     'VS131' => pack("U", 0xE0172), # VARIATION SELECTOR-131
310     'VS132' => pack("U", 0xE0173), # VARIATION SELECTOR-132
311     'VS133' => pack("U", 0xE0174), # VARIATION SELECTOR-133
312     'VS134' => pack("U", 0xE0175), # VARIATION SELECTOR-134
313     'VS135' => pack("U", 0xE0176), # VARIATION SELECTOR-135
314     'VS136' => pack("U", 0xE0177), # VARIATION SELECTOR-136
315     'VS137' => pack("U", 0xE0178), # VARIATION SELECTOR-137
316     'VS138' => pack("U", 0xE0179), # VARIATION SELECTOR-138
317     'VS139' => pack("U", 0xE017A), # VARIATION SELECTOR-139
318     'VS140' => pack("U", 0xE017B), # VARIATION SELECTOR-140
319     'VS141' => pack("U", 0xE017C), # VARIATION SELECTOR-141
320     'VS142' => pack("U", 0xE017D), # VARIATION SELECTOR-142
321     'VS143' => pack("U", 0xE017E), # VARIATION SELECTOR-143
322     'VS144' => pack("U", 0xE017F), # VARIATION SELECTOR-144
323     'VS145' => pack("U", 0xE0180), # VARIATION SELECTOR-145
324     'VS146' => pack("U", 0xE0181), # VARIATION SELECTOR-146
325     'VS147' => pack("U", 0xE0182), # VARIATION SELECTOR-147
326     'VS148' => pack("U", 0xE0183), # VARIATION SELECTOR-148
327     'VS149' => pack("U", 0xE0184), # VARIATION SELECTOR-149
328     'VS150' => pack("U", 0xE0185), # VARIATION SELECTOR-150
329     'VS151' => pack("U", 0xE0186), # VARIATION SELECTOR-151
330     'VS152' => pack("U", 0xE0187), # VARIATION SELECTOR-152
331     'VS153' => pack("U", 0xE0188), # VARIATION SELECTOR-153
332     'VS154' => pack("U", 0xE0189), # VARIATION SELECTOR-154
333     'VS155' => pack("U", 0xE018A), # VARIATION SELECTOR-155
334     'VS156' => pack("U", 0xE018B), # VARIATION SELECTOR-156
335     'VS157' => pack("U", 0xE018C), # VARIATION SELECTOR-157
336     'VS158' => pack("U", 0xE018D), # VARIATION SELECTOR-158
337     'VS159' => pack("U", 0xE018E), # VARIATION SELECTOR-159
338     'VS160' => pack("U", 0xE018F), # VARIATION SELECTOR-160
339     'VS161' => pack("U", 0xE0190), # VARIATION SELECTOR-161
340     'VS162' => pack("U", 0xE0191), # VARIATION SELECTOR-162
341     'VS163' => pack("U", 0xE0192), # VARIATION SELECTOR-163
342     'VS164' => pack("U", 0xE0193), # VARIATION SELECTOR-164
343     'VS165' => pack("U", 0xE0194), # VARIATION SELECTOR-165
344     'VS166' => pack("U", 0xE0195), # VARIATION SELECTOR-166
345     'VS167' => pack("U", 0xE0196), # VARIATION SELECTOR-167
346     'VS168' => pack("U", 0xE0197), # VARIATION SELECTOR-168
347     'VS169' => pack("U", 0xE0198), # VARIATION SELECTOR-169
348     'VS170' => pack("U", 0xE0199), # VARIATION SELECTOR-170
349     'VS171' => pack("U", 0xE019A), # VARIATION SELECTOR-171
350     'VS172' => pack("U", 0xE019B), # VARIATION SELECTOR-172
351     'VS173' => pack("U", 0xE019C), # VARIATION SELECTOR-173
352     'VS174' => pack("U", 0xE019D), # VARIATION SELECTOR-174
353     'VS175' => pack("U", 0xE019E), # VARIATION SELECTOR-175
354     'VS176' => pack("U", 0xE019F), # VARIATION SELECTOR-176
355     'VS177' => pack("U", 0xE01A0), # VARIATION SELECTOR-177
356     'VS178' => pack("U", 0xE01A1), # VARIATION SELECTOR-178
357     'VS179' => pack("U", 0xE01A2), # VARIATION SELECTOR-179
358     'VS180' => pack("U", 0xE01A3), # VARIATION SELECTOR-180
359     'VS181' => pack("U", 0xE01A4), # VARIATION SELECTOR-181
360     'VS182' => pack("U", 0xE01A5), # VARIATION SELECTOR-182
361     'VS183' => pack("U", 0xE01A6), # VARIATION SELECTOR-183
362     'VS184' => pack("U", 0xE01A7), # VARIATION SELECTOR-184
363     'VS185' => pack("U", 0xE01A8), # VARIATION SELECTOR-185
364     'VS186' => pack("U", 0xE01A9), # VARIATION SELECTOR-186
365     'VS187' => pack("U", 0xE01AA), # VARIATION SELECTOR-187
366     'VS188' => pack("U", 0xE01AB), # VARIATION SELECTOR-188
367     'VS189' => pack("U", 0xE01AC), # VARIATION SELECTOR-189
368     'VS190' => pack("U", 0xE01AD), # VARIATION SELECTOR-190
369     'VS191' => pack("U", 0xE01AE), # VARIATION SELECTOR-191
370     'VS192' => pack("U", 0xE01AF), # VARIATION SELECTOR-192
371     'VS193' => pack("U", 0xE01B0), # VARIATION SELECTOR-193
372     'VS194' => pack("U", 0xE01B1), # VARIATION SELECTOR-194
373     'VS195' => pack("U", 0xE01B2), # VARIATION SELECTOR-195
374     'VS196' => pack("U", 0xE01B3), # VARIATION SELECTOR-196
375     'VS197' => pack("U", 0xE01B4), # VARIATION SELECTOR-197
376     'VS198' => pack("U", 0xE01B5), # VARIATION SELECTOR-198
377     'VS199' => pack("U", 0xE01B6), # VARIATION SELECTOR-199
378     'VS200' => pack("U", 0xE01B7), # VARIATION SELECTOR-200
379     'VS201' => pack("U", 0xE01B8), # VARIATION SELECTOR-201
380     'VS202' => pack("U", 0xE01B9), # VARIATION SELECTOR-202
381     'VS203' => pack("U", 0xE01BA), # VARIATION SELECTOR-203
382     'VS204' => pack("U", 0xE01BB), # VARIATION SELECTOR-204
383     'VS205' => pack("U", 0xE01BC), # VARIATION SELECTOR-205
384     'VS206' => pack("U", 0xE01BD), # VARIATION SELECTOR-206
385     'VS207' => pack("U", 0xE01BE), # VARIATION SELECTOR-207
386     'VS208' => pack("U", 0xE01BF), # VARIATION SELECTOR-208
387     'VS209' => pack("U", 0xE01C0), # VARIATION SELECTOR-209
388     'VS210' => pack("U", 0xE01C1), # VARIATION SELECTOR-210
389     'VS211' => pack("U", 0xE01C2), # VARIATION SELECTOR-211
390     'VS212' => pack("U", 0xE01C3), # VARIATION SELECTOR-212
391     'VS213' => pack("U", 0xE01C4), # VARIATION SELECTOR-213
392     'VS214' => pack("U", 0xE01C5), # VARIATION SELECTOR-214
393     'VS215' => pack("U", 0xE01C6), # VARIATION SELECTOR-215
394     'VS216' => pack("U", 0xE01C7), # VARIATION SELECTOR-216
395     'VS217' => pack("U", 0xE01C8), # VARIATION SELECTOR-217
396     'VS218' => pack("U", 0xE01C9), # VARIATION SELECTOR-218
397     'VS219' => pack("U", 0xE01CA), # VARIATION SELECTOR-219
398     'VS220' => pack("U", 0xE01CB), # VARIATION SELECTOR-220
399     'VS221' => pack("U", 0xE01CC), # VARIATION SELECTOR-221
400     'VS222' => pack("U", 0xE01CD), # VARIATION SELECTOR-222
401     'VS223' => pack("U", 0xE01CE), # VARIATION SELECTOR-223
402     'VS224' => pack("U", 0xE01CF), # VARIATION SELECTOR-224
403     'VS225' => pack("U", 0xE01D0), # VARIATION SELECTOR-225
404     'VS226' => pack("U", 0xE01D1), # VARIATION SELECTOR-226
405     'VS227' => pack("U", 0xE01D2), # VARIATION SELECTOR-227
406     'VS228' => pack("U", 0xE01D3), # VARIATION SELECTOR-228
407     'VS229' => pack("U", 0xE01D4), # VARIATION SELECTOR-229
408     'VS230' => pack("U", 0xE01D5), # VARIATION SELECTOR-230
409     'VS231' => pack("U", 0xE01D6), # VARIATION SELECTOR-231
410     'VS232' => pack("U", 0xE01D7), # VARIATION SELECTOR-232
411     'VS233' => pack("U", 0xE01D8), # VARIATION SELECTOR-233
412     'VS234' => pack("U", 0xE01D9), # VARIATION SELECTOR-234
413     'VS235' => pack("U", 0xE01DA), # VARIATION SELECTOR-235
414     'VS236' => pack("U", 0xE01DB), # VARIATION SELECTOR-236
415     'VS237' => pack("U", 0xE01DC), # VARIATION SELECTOR-237
416     'VS238' => pack("U", 0xE01DD), # VARIATION SELECTOR-238
417     'VS239' => pack("U", 0xE01DE), # VARIATION SELECTOR-239
418     'VS240' => pack("U", 0xE01DF), # VARIATION SELECTOR-240
419     'VS241' => pack("U", 0xE01E0), # VARIATION SELECTOR-241
420     'VS242' => pack("U", 0xE01E1), # VARIATION SELECTOR-242
421     'VS243' => pack("U", 0xE01E2), # VARIATION SELECTOR-243
422     'VS244' => pack("U", 0xE01E3), # VARIATION SELECTOR-244
423     'VS245' => pack("U", 0xE01E4), # VARIATION SELECTOR-245
424     'VS246' => pack("U", 0xE01E5), # VARIATION SELECTOR-246
425     'VS247' => pack("U", 0xE01E6), # VARIATION SELECTOR-247
426     'VS248' => pack("U", 0xE01E7), # VARIATION SELECTOR-248
427     'VS249' => pack("U", 0xE01E8), # VARIATION SELECTOR-249
428     'VS250' => pack("U", 0xE01E9), # VARIATION SELECTOR-250
429     'VS251' => pack("U", 0xE01EA), # VARIATION SELECTOR-251
430     'VS252' => pack("U", 0xE01EB), # VARIATION SELECTOR-252
431     'VS253' => pack("U", 0xE01EC), # VARIATION SELECTOR-253
432     'VS254' => pack("U", 0xE01ED), # VARIATION SELECTOR-254
433     'VS255' => pack("U", 0xE01EE), # VARIATION SELECTOR-255
434     'VS256' => pack("U", 0xE01EF), # VARIATION SELECTOR-256
435     'WJ'    => pack("U", 0x2060), # WORD JOINER
436     'ZWJ'   => pack("U", 0x200D), # ZERO WIDTH JOINER
437     'ZWNJ'  => pack("U", 0x200C), # ZERO WIDTH NON-JOINER
438     'ZWSP'  => pack("U", 0x200B), # ZERO WIDTH SPACE
439 );
440
441 # These are the aliases above that differ under :loose and :full matching
442 # because the :full versions have blanks or hyphens in them.
443 my %loose_system_aliases = (
444     'LINEFEED'                         => pack("U", 0x0A),
445     'FORMFEED'                         => pack("U", 0x0C),
446     'CARRIAGERETURN'                   => pack("U", 0x0D),
447     'NEXTLINE'                         => pack("U", 0x85),
448     'SINGLESHIFT2'                     => pack("U", 0x8E),
449     'SINGLESHIFT3'                     => pack("U", 0x8F),
450     'PRIVATEUSE1'                      => pack("U", 0x91),
451     'PRIVATEUSE2'                      => pack("U", 0x92),
452     'STARTOFPROTECTEDAREA'             => pack("U", 0x96),
453     'ENDOFPROTECTEDAREA'               => pack("U", 0x97),
454     'PADDINGCHARACTER'                 => pack("U", 0x80),
455     'HIGHOCTETPRESET'                  => pack("U", 0x81),
456     'SINGLEGRAPHICCHARACTERINTRODUCER' => pack("U", 0x99),
457     'BYTEORDERMARK'                    => pack("U", 0xFEFF),
458 );
459
460 my %deprecated_aliases = (
461     # Pre-3.2 compatibility (only for the first 256 characters).
462     # Use of these gives deprecated message.
463     'HORIZONTAL TABULATION' => pack("U", 0x09), # CHARACTER TABULATION
464     'VERTICAL TABULATION'   => pack("U", 0x0B), # LINE TABULATION
465     'FILE SEPARATOR'        => pack("U", 0x1C), # INFORMATION SEPARATOR FOUR
466     'GROUP SEPARATOR'       => pack("U", 0x1D), # INFORMATION SEPARATOR THREE
467     'RECORD SEPARATOR'      => pack("U", 0x1E), # INFORMATION SEPARATOR TWO
468     'UNIT SEPARATOR'        => pack("U", 0x1F), # INFORMATION SEPARATOR ONE
469     'HORIZONTAL TABULATION SET' => pack("U", 0x88), # CHARACTER TABULATION SET
470     'HORIZONTAL TABULATION WITH JUSTIFICATION' => pack("U", 0x89), # CHARACTER TABULATION WITH JUSTIFICATION
471     'PARTIAL LINE DOWN'       => pack("U", 0x8B), # PARTIAL LINE FORWARD
472     'PARTIAL LINE UP'         => pack("U", 0x8C), # PARTIAL LINE BACKWARD
473     'VERTICAL TABULATION SET' => pack("U", 0x8A), # LINE TABULATION SET
474     'REVERSE INDEX'           => pack("U", 0x8D), # REVERSE LINE FEED
475
476     # Unicode 6.0 co-opted this for U+1F514, so deprecate it for now.
477     'BELL'                    => pack("U", 0x07),
478 );
479
480 my %loose_deprecated_aliases = (
481     'HORIZONTALTABULATION'                  => pack("U", 0x09),
482     'VERTICALTABULATION'                    => pack("U", 0x0B),
483     'FILESEPARATOR'                         => pack("U", 0x1C),
484     'GROUPSEPARATOR'                        => pack("U", 0x1D),
485     'RECORDSEPARATOR'                       => pack("U", 0x1E),
486     'UNITSEPARATOR'                         => pack("U", 0x1F),
487     'HORIZONTALTABULATIONSET'               => pack("U", 0x88),
488     'HORIZONTALTABULATIONWITHJUSTIFICATION' => pack("U", 0x89),
489     'PARTIALLINEDOWN'                       => pack("U", 0x8B),
490     'PARTIALLINEUP'                         => pack("U", 0x8C),
491     'VERTICALTABULATIONSET'                 => pack("U", 0x8A),
492     'REVERSEINDEX'                          => pack("U", 0x8D),
493 );
494
495 # These are special cased in :loose matching, differing only in a medial
496 # hyphen
497 my $HANGUL_JUNGSEONG_O_E_utf8 = pack("U", 0x1180);
498 my $HANGUL_JUNGSEONG_OE_utf8 = pack("U", 0x116C);
499
500
501 my $txt;  # The table of official character names
502
503 my %full_names_cache; # Holds already-looked-up names, so don't have to
504 # re-look them up again.  The previous versions of charnames had scoping
505 # bugs.  For example if we use script A in one scope and find and cache
506 # what Z resolves to, we can't use that cache in a different scope that
507 # uses script B instead of A, as Z might be an entirely different letter
508 # there; or there might be different aliases in effect in different
509 # scopes, or :short may be in effect or not effect in different scopes,
510 # or various combinations thereof.  This was solved in this version
511 # mostly by moving things to %^H.  But some things couldn't be moved
512 # there.  One of them was the cache of runtime looked-up names, in part
513 # because %^H is read-only at runtime.  I (khw) don't know why the cache
514 # was run-time only in the previous versions: perhaps oversight; perhaps
515 # that compile time looking doesn't happen in a loop so didn't think it
516 # was worthwhile; perhaps not wanting to make the cache too large.  But
517 # I decided to make it compile time as well; this could easily be
518 # changed.
519 # Anyway, this hash is not scoped, and is added to at runtime.  It
520 # doesn't have scoping problems because the data in it is restricted to
521 # official names, which are always invariant, and we only set it and
522 # look at it at during :full lookups, so is unaffected by any other
523 # scoped options.  I put this in to maintain parity with the older
524 # version.  If desired, a %short_names cache could also be made, as well
525 # as one for each script, say in %script_names_cache, with each key
526 # being a hash for a script named in a 'use charnames' statement.  I
527 # decided not to do that for now, just because it's added complication,
528 # and because I'm just trying to maintain parity, not extend it.
529
530 # Like %full_names_cache, but for use when :loose is in effect.  There needs
531 # to be two caches because :loose may not be in effect for a scope, and a
532 # loose name could inappropriately be returned when only exact matching is
533 # called for.
534 my %loose_names_cache;
535
536 # Designed so that test decimal first, and then hex.  Leading zeros
537 # imply non-decimal, as do non-[0-9]
538 my $decimal_qr = qr/^[1-9]\d*$/;
539
540 # Returns the hex number in $1.
541 my $hex_qr = qr/^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/;
542
543 sub croak
544 {
545   require Carp; goto &Carp::croak;
546 } # croak
547
548 sub carp
549 {
550   require Carp; goto &Carp::carp;
551 } # carp
552
553 sub alias (@) # Set up a single alias
554 {
555   my $alias = ref $_[0] ? $_[0] : { @_ };
556   foreach my $name (keys %$alias) {
557     my $value = $alias->{$name};
558     next unless defined $value;          # Omit if screwed up.
559
560     # Is slightly slower to just after this statement see if it is
561     # decimal, since we already know it is after having converted from
562     # hex, but makes the code easier to maintain, and is called
563     # infrequently, only at compile-time
564     if ($value !~ $decimal_qr && $value =~ $hex_qr) {
565       $value = CORE::hex $1;
566     }
567     if ($value =~ $decimal_qr) {
568         no warnings qw(non_unicode surrogate nonchar); # Allow any non-malformed
569         $^H{charnames_ord_aliases}{$name} = pack("U", $value);
570
571         # Use a canonical form.
572         $^H{charnames_inverse_ords}{sprintf("%05X", $value)} = $name;
573     }
574     else {
575         # XXX validate syntax when deprecation cycle complete. ie. start
576         # with an alpha only, etc.
577         $^H{charnames_name_aliases}{$name} = $value;
578     }
579   }
580 } # alias
581
582 sub not_legal_use_bytes_msg {
583   my ($name, $utf8) = @_;
584   my $return;
585
586   if (length($utf8) == 1) {
587     $return = sprintf("Character 0x%04x with name '%s' is", ord $utf8, $name);
588   } else {
589     $return = sprintf("String with name '%s' (and ordinals %s) contains character(s)", $name, join(" ", map { sprintf "0x%04X", ord $_ } split(//, $utf8)));
590   }
591   return $return . " above 0xFF with 'use bytes' in effect";
592 }
593
594 sub alias_file ($)  # Reads a file containing alias definitions
595 {
596   my ($arg, $file) = @_;
597   if (-f $arg && File::Spec->file_name_is_absolute ($arg)) {
598     $file = $arg;
599   }
600   elsif ($arg =~ m/^\w+$/) {
601     $file = "unicore/${arg}_alias.pl";
602   }
603   else {
604     croak "Charnames alias files can only have identifier characters";
605   }
606   if (my @alias = do $file) {
607     @alias == 1 && !defined $alias[0] and
608       croak "$file cannot be used as alias file for charnames";
609     @alias % 2 and
610       croak "$file did not return a (valid) list of alias pairs";
611     alias (@alias);
612     return (1);
613   }
614   0;
615 } # alias_file
616
617 # For use when don't import anything.  This structure must be kept in
618 # sync with the one that import() fills up.
619 my %dummy_H = (
620                 charnames_stringified_names => "",
621                 charnames_stringified_ords => "",
622                 charnames_scripts => "",
623                 charnames_full => 1,
624                 charnames_loose => 0,
625                 charnames_short => 0,
626               );
627
628
629 sub lookup_name ($$$) {
630   my ($name, $wants_ord, $runtime) = @_;
631
632   # Lookup the name or sequence $name in the tables.  If $wants_ord is false,
633   # returns the string equivalent of $name; if true, returns the ordinal value
634   # instead, but in this case $name must not be a sequence; otherwise undef is
635   # returned and a warning raised.  $runtime is 0 if compiletime, otherwise
636   # gives the number of stack frames to go back to get the application caller
637   # info.
638   # If $name is not found, returns undef in runtime with no warning; and in
639   # compiletime, the Unicode replacement character, with a warning.
640
641   # It looks first in the aliases, then in the large table of official Unicode
642   # names.
643
644   my $utf8;       # The string result
645   my $save_input;
646
647   if ($runtime) {
648
649     my $hints_ref = (caller($runtime))[10];
650
651     # If we didn't import anything (which happens with 'use charnames ()',
652     # substitute a dummy structure.
653     $hints_ref = \%dummy_H if ! defined $hints_ref
654                               || (! defined $hints_ref->{charnames_full}
655                                   && ! defined $hints_ref->{charnames_loose});
656
657     # At runtime, but currently not at compile time, $^H gets
658     # stringified, so un-stringify back to the original data structures.
659     # These get thrown away by perl before the next invocation
660     # Also fill in the hash with the non-stringified data.
661     # N.B.  New fields must be also added to %dummy_H
662
663     %{$^H{charnames_name_aliases}} = split ',',
664                                       $hints_ref->{charnames_stringified_names};
665     %{$^H{charnames_ord_aliases}} = split ',',
666                                       $hints_ref->{charnames_stringified_ords};
667     $^H{charnames_scripts} = $hints_ref->{charnames_scripts};
668     $^H{charnames_full} = $hints_ref->{charnames_full};
669     $^H{charnames_loose} = $hints_ref->{charnames_loose};
670     $^H{charnames_short} = $hints_ref->{charnames_short};
671   }
672
673   my $loose = $^H{charnames_loose};
674   my $lookup_name;  # Input name suitably modified for grepping for in the
675                     # table
676
677   # User alias should be checked first or else can't override ours, and if we
678   # were to add any, could conflict with theirs.
679   if (exists $^H{charnames_ord_aliases}{$name}) {
680     $utf8 = $^H{charnames_ord_aliases}{$name};
681   }
682   elsif (exists $^H{charnames_name_aliases}{$name}) {
683     $name = $^H{charnames_name_aliases}{$name};
684     $save_input = $lookup_name = $name;  # Cache the result for any error
685                                          # message
686     # The aliases are documented to not match loosely, so change loose match
687     # into full.
688     if ($loose) {
689       $loose = 0;
690       $^H{charnames_full} = 1;
691     }
692   }
693   else {
694
695     # Here, not a user alias.  That means that loose matching may be in
696     # effect; will have to modify the input name.
697     $lookup_name = $name;
698     if ($loose) {
699       $lookup_name = uc $lookup_name;
700
701       # Squeeze out all underscores
702       $lookup_name =~ s/_//g;
703
704       # Remove all medial hyphens
705       $lookup_name =~ s/ (?<= \S  ) - (?= \S  )//gx;
706
707       # Squeeze out all spaces
708       $lookup_name =~ s/\s//g;
709     }
710
711     # Here, $lookup_name has been modified as necessary for looking in the
712     # hashes.  Check the system alias files next.  Most of these aliases are
713     # the same for both strict and loose matching.  To save space, the ones
714     # which differ are in their own separate hash, which is checked if loose
715     # matching is selected and the regular match fails.  To save time, the
716     # loose hashes could be expanded to include all aliases, and there would
717     # only have to be one check.  But if someone specifies :loose, they are
718     # interested in convenience over speed, and the time for this second check
719     # is miniscule compared to the rest of the routine.
720     if (exists $system_aliases{$lookup_name}) {
721       $utf8 = $system_aliases{$lookup_name};
722     }
723     elsif ($loose && exists $loose_system_aliases{$lookup_name}) {
724       $utf8 = $loose_system_aliases{$lookup_name};
725     }
726     elsif (exists $deprecated_aliases{$lookup_name}) {
727       require warnings;
728       warnings::warnif('deprecated',
729                        "Unicode character name \"$name\" is deprecated, use \""
730                        . viacode(ord $deprecated_aliases{$lookup_name})
731                        . "\" instead");
732       $utf8 = $deprecated_aliases{$lookup_name};
733     }
734     elsif ($loose && exists $loose_deprecated_aliases{$lookup_name}) {
735       require warnings;
736       warnings::warnif('deprecated',
737                        "Unicode character name \"$name\" is deprecated, use \""
738                        . viacode(ord $loose_deprecated_aliases{$lookup_name})
739                        . "\" instead");
740       $utf8 = $loose_deprecated_aliases{$lookup_name};
741     }
742   }
743
744   my @off;  # Offsets into table of pattern match begin and end
745
746   # If haven't found it yet...
747   if (! defined $utf8) {
748
749     # See if has looked this input up earlier.
750     if (! $loose && $^H{charnames_full} && exists $full_names_cache{$name}) {
751       $utf8 = $full_names_cache{$name};
752     }
753     elsif ($loose && exists $loose_names_cache{$name}) {
754       $utf8 = $loose_names_cache{$name};
755     }
756     else { # Here, must do a look-up
757
758       # If full or loose matching succeeded, points to where to cache the
759       # result
760       my $cache_ref;
761
762       ## Suck in the code/name list as a big string.
763       ## Lines look like:
764       ##     "00052\tLATIN CAPITAL LETTER R\n"
765       # or
766       #      "0052 0303\tLATIN CAPITAL LETTER R WITH TILDE\n"
767       $txt = do "unicore/Name.pl" unless $txt;
768
769       ## @off will hold the index into the code/name string of the start and
770       ## end of the name as we find it.
771
772       ## If :loose, look for a loose match; if :full, look for the name
773       ## exactly
774       # First, see if the name is one which is algorithmically determinable.
775       # The subroutine is included in Name.pl.  The table contained in
776       # $txt doesn't contain these.  Experiments show that checking
777       # for these before checking for the regular names has no
778       # noticeable impact on performance for the regular names, but
779       # the other way around slows down finding these immensely.
780       # Algorithmically determinables are not placed in the cache because
781       # that uses up memory, and finding these again is fast.
782       if (($loose || $^H{charnames_full})
783           && (defined (my $ord = charnames::name_to_code_point_special($lookup_name, $loose))))
784       {
785         $utf8 = pack("U", $ord);
786       }
787       else {
788
789         # Not algorithmically determinable; look up in the table.  The name
790         # will be turned into a regex, so quote any meta characters.
791         $lookup_name = quotemeta $lookup_name;
792
793         if ($loose) {
794
795           # For loose matches, $lookup_name has already squeezed out the
796           # non-essential characters.  We have to add in code to make the
797           # squeezed version match the non-squeezed equivalent in the table.
798           # The only remaining hyphens are ones that start or end a word in
799           # the original.  They have been quoted in $lookup_name so they look
800           # like "\-".  Change all other characters except the backslash
801           # quotes for any metacharacters, and the final character, so that
802           # e.g., COLON gets transformed into: /C[- ]?O[- ]?L[- ]?O[- ]?N/
803           $lookup_name =~ s/ (?! \\ -)    # Don't do this to the \- sequence
804                              ( [^-\\] )   # Nor the "-" within that sequence,
805                                           # nor the "\" that quotes metachars,
806                                           # but otherwise put the char into $1
807                              (?=.)        # And don't do it for the final char
808                            /$1\[- \]?/gx; # And add an optional blank or
809                                           # '-' after each $1 char
810
811           # Those remaining hyphens were originally at the beginning or end of
812           # a word, so they can match either a blank before or after, but not
813           # both.  (Keep in mind that they have been quoted, so are a '\-'
814           # sequence)
815           $lookup_name =~ s/\\ -/(?:- | -)/xg;
816         }
817
818         # Do the lookup in the full table if asked for, and if succeeds
819         # save the offsets and set where to cache the result.
820         if (($loose || $^H{charnames_full}) && $txt =~ /\t$lookup_name$/m) {
821           @off = ($-[0] + 1, $+[0]);    # The 1 is for the tab
822           $cache_ref = ($loose) ? \%loose_names_cache : \%full_names_cache;
823         }
824         else {
825
826           # Here, didn't look for, or didn't find the name.
827           # If :short is allowed, see if input is like "greek:Sigma".
828           # Keep in mind that $lookup_name has had the metas quoted.
829           my $scripts_trie = "";
830           my $name_has_uppercase;
831           if (($^H{charnames_short})
832               && $lookup_name =~ /^ (?: \\ \s)*   # Quoted space
833                                     (.+?)         # $1 = the script
834                                     (?: \\ \s)*
835                                     \\ :          # Quoted colon
836                                     (?: \\ \s)*
837                                     (.+?)         # $2 = the name
838                                     (?: \\ \s)* $
839                                   /xs)
840           {
841               # Even in non-loose matching, the script traditionally has been
842               # case insensitve
843               $scripts_trie = "\U$1";
844               $lookup_name = $2;
845
846               # Use original name to find its input casing, but ignore the
847               # script part of that to make the determination.
848               $save_input = $name if ! defined $save_input;
849               $name =~ s/.*?://;
850               $name_has_uppercase = $name =~ /[[:upper:]]/;
851           }
852           else { # Otherwise look in allowed scripts
853               $scripts_trie = $^H{charnames_scripts};
854
855               # Use original name to find its input casing
856               $name_has_uppercase = $name =~ /[[:upper:]]/;
857           }
858
859           my $case = $name_has_uppercase ? "CAPITAL" : "SMALL";
860           if (! $scripts_trie
861               || $txt !~
862               /\t (?: $scripts_trie ) \ (?:$case\ )? LETTER \ \U$lookup_name $/xm)
863           {
864             # Here we still don't have it, give up.
865             return if $runtime;
866
867             # May have zapped input name, get it again.
868             $name = (defined $save_input) ? $save_input : $_[0];
869             carp "Unknown charname '$name'";
870             return ($wants_ord) ? 0xFFFD : pack("U", 0xFFFD);
871           }
872
873           # Here have found the input name in the table.
874           @off = ($-[0] + 1, $+[0]);  # The 1 is for the tab
875         }
876
877         # Here, the input name has been found; we haven't set up the output,
878         # but we know where in the string
879         # the name starts.  The string is set up so that for single characters
880         # (and not named sequences), the name is preceded immediately by a
881         # tab and 5 hex digits for its code, with a \n before those.  Named
882         # sequences won't have the 7th preceding character be a \n.
883         # (Actually, for the very first entry in the table this isn't strictly
884         # true: subtracting 7 will yield -1, and the substr below will
885         # therefore yield the very last character in the table, which should
886         # also be a \n, so the statement works anyway.)
887         if (substr($txt, $off[0] - 7, 1) eq "\n") {
888           $utf8 = pack("U", CORE::hex substr($txt, $off[0] - 6, 5));
889
890           # Handle the single loose matching special case, in which two names
891           # differ only by a single medial hyphen.  If the original had a
892           # hyphen (or more) in the right place, then it is that one.
893           $utf8 = $HANGUL_JUNGSEONG_O_E_utf8
894                   if $loose
895                      && $utf8 eq $HANGUL_JUNGSEONG_OE_utf8
896                      && $name =~ m/O \s* - [-\s]* E/ix;
897                      # Note that this wouldn't work if there were a 2nd
898                      # OE in the name
899         }
900         else {
901
902           # Here, is a named sequence.  Need to go looking for the beginning,
903           # which is just after the \n from the previous entry in the table.
904           # The +1 skips past that newline, or, if the rindex() fails, to put
905           # us to an offset of zero.
906           my $charstart = rindex($txt, "\n", $off[0] - 7) + 1;
907           $utf8 = pack("U*", map { CORE::hex }
908               split " ", substr($txt, $charstart, $off[0] - $charstart - 1));
909         }
910       }
911
912       # Cache the input so as to not have to search the large table
913       # again, but only if it came from the one search that we cache.
914       # (Haven't bothered with the pain of sorting out scoping issues for the
915       # scripts searches.)
916       $cache_ref->{$name} = $utf8 if defined $cache_ref;
917     }
918   }
919
920
921   # Here, have the utf8.  If the return is to be an ord, must be any single
922   # character.
923   if ($wants_ord) {
924     return ord($utf8) if length $utf8 == 1;
925   }
926   else {
927
928     # Here, wants string output.  If utf8 is acceptable, just return what
929     # we've got; otherwise attempt to convert it to non-utf8 and return that.
930     my $in_bytes = ($runtime)
931                    ? (caller $runtime)[8] & $bytes::hint_bits
932                    : $^H & $bytes::hint_bits;
933     return $utf8 if (! $in_bytes || utf8::downgrade($utf8, 1)) # The 1 arg
934                                                   # means don't die on failure
935   }
936
937   # Here, there is an error:  either there are too many characters, or the
938   # result string needs to be non-utf8, and at least one character requires
939   # utf8.  Prefer any official name over the input one for the error message.
940   if (@off) {
941     $name = substr($txt, $off[0], $off[1] - $off[0]) if @off;
942   }
943   else {
944     $name = (defined $save_input) ? $save_input : $_[0];
945   }
946
947   if ($wants_ord) {
948     # Only way to get here in this case is if result too long.  Message
949     # assumes that our only caller that requires single char result is
950     # vianame.
951     carp "charnames::vianame() doesn't handle named sequences ($name).  Use charnames::string_vianame() instead";
952     return;
953   }
954
955   # Only other possible failure here is from use bytes.
956   if ($runtime) {
957     carp not_legal_use_bytes_msg($name, $utf8);
958     return;
959   } else {
960     croak not_legal_use_bytes_msg($name, $utf8);
961   }
962
963 } # lookup_name
964
965 sub charnames {
966
967   # For \N{...}.  Looks up the character name and returns the string
968   # representation of it.
969
970   # The first 0 arg means wants a string returned; the second that we are in
971   # compile time
972   return lookup_name($_[0], 0, 0);
973 }
974
975 sub import
976 {
977   shift; ## ignore class name
978
979   if (not @_) {
980     carp("'use charnames' needs explicit imports list");
981   }
982   $^H{charnames} = \&charnames ;
983   $^H{charnames_ord_aliases} = {};
984   $^H{charnames_name_aliases} = {};
985   $^H{charnames_inverse_ords} = {};
986   # New fields must be added to %dummy_H, and the code in lookup_name()
987   # that copies fields from the runtime structure
988
989   ##
990   ## fill %h keys with our @_ args.
991   ##
992   my ($promote, %h, @args) = (0);
993   while (my $arg = shift) {
994     if ($arg eq ":alias") {
995       @_ or
996         croak ":alias needs an argument in charnames";
997       my $alias = shift;
998       if (ref $alias) {
999         ref $alias eq "HASH" or
1000           croak "Only HASH reference supported as argument to :alias";
1001         alias ($alias);
1002         next;
1003       }
1004       if ($alias =~ m{:(\w+)$}) {
1005         $1 eq "full" || $1 eq "loose" || $1 eq "short" and
1006           croak ":alias cannot use existing pragma :$1 (reversed order?)";
1007         alias_file ($1) and $promote = 1;
1008         next;
1009       }
1010       alias_file ($alias);
1011       next;
1012     }
1013     if (substr($arg, 0, 1) eq ':'
1014       and ! ($arg eq ":full" || $arg eq ":short" || $arg eq ":loose"))
1015     {
1016       warn "unsupported special '$arg' in charnames";
1017       next;
1018     }
1019     push @args, $arg;
1020   }
1021
1022   @args == 0 && $promote and @args = (":full");
1023   @h{@args} = (1) x @args;
1024
1025   # Don't leave these undefined as are tested for in lookup_names
1026   $^H{charnames_full} = delete $h{':full'} || 0;
1027   $^H{charnames_loose} = delete $h{':loose'} || 0;
1028   $^H{charnames_short} = delete $h{':short'} || 0;
1029   my @scripts = map { uc quotemeta } keys %h;
1030
1031   ##
1032   ## If utf8? warnings are enabled, and some scripts were given,
1033   ## see if at least we can find one letter from each script.
1034   ##
1035   if (warnings::enabled('utf8') && @scripts) {
1036     $txt = do "unicore/Name.pl" unless $txt;
1037
1038     for my $script (@scripts) {
1039       if (not $txt =~ m/\t$script (?:CAPITAL |SMALL )?LETTER /) {
1040         warnings::warn('utf8',  "No such script: '$script'");
1041         $script = quotemeta $script;  # Escape it, for use in the re.
1042       }
1043     }
1044   }
1045
1046   # %^H gets stringified, so serialize it ourselves so can extract the
1047   # real data back later.
1048   $^H{charnames_stringified_ords} = join ",", %{$^H{charnames_ord_aliases}};
1049   $^H{charnames_stringified_names} = join ",", %{$^H{charnames_name_aliases}};
1050   $^H{charnames_stringified_inverse_ords} = join ",", %{$^H{charnames_inverse_ords}};
1051
1052   # Modify the input script names for loose name matching if that is also
1053   # specified, similar to the way the base character name is prepared.  They
1054   # don't (currently, and hopefully never will) have dashes.  These go into a
1055   # regex, and have already been uppercased and quotemeta'd.  Squeeze out all
1056   # input underscores, blanks, and dashes.  Then convert so will match a blank
1057   # between any characters.
1058   if ($^H{charnames_loose}) {
1059     for (my $i = 0; $i < @scripts; $i++) {
1060       $scripts[$i] =~ s/[_ -]//g;
1061       $scripts[$i] =~ s/ ( [^\\] ) (?= . ) /$1\\ ?/gx;
1062     }
1063   }
1064
1065   $^H{charnames_scripts} = join "|", @scripts;  # Stringifiy them as a trie
1066 } # import
1067
1068 # Cache of already looked-up values.  This is set to only contain
1069 # official values, and user aliases can't override them, so scoping is
1070 # not an issue.
1071 my %viacode;
1072
1073 sub viacode {
1074
1075   # Returns the name of the code point argument
1076
1077   if (@_ != 1) {
1078     carp "charnames::viacode() expects one argument";
1079     return;
1080   }
1081
1082   my $arg = shift;
1083
1084   # This is derived from Unicode::UCD, where it is nearly the same as the
1085   # function _getcode(), but here it makes sure that even a hex argument
1086   # has the proper number of leading zeros, which is critical in
1087   # matching against $txt below
1088   # Must check if decimal first; see comments at that definition
1089   my $hex;
1090   if ($arg =~ $decimal_qr) {
1091     $hex = sprintf "%05X", $arg;
1092   } elsif ($arg =~ $hex_qr) {
1093     # Below is the line that differs from the _getcode() source
1094     $hex = sprintf "%05X", hex $1;
1095   } else {
1096     carp("unexpected arg \"$arg\" to charnames::viacode()");
1097     return;
1098   }
1099
1100   return $viacode{$hex} if exists $viacode{$hex};
1101
1102   # If the code point is above the max in the table, there's no point
1103   # looking through it.  Checking the length first is slightly faster
1104   if (length($hex) <= 5 || CORE::hex($hex) <= 0x10FFFF) {
1105     $txt = do "unicore/Name.pl" unless $txt;
1106
1107     # See if the name is algorithmically determinable.
1108     my $algorithmic = charnames::code_point_to_name_special(CORE::hex $hex);
1109     if (defined $algorithmic) {
1110       $viacode{$hex} = $algorithmic;
1111       return $algorithmic;
1112     }
1113
1114     # Return the official name, if exists.  It's unclear to me (khw) at
1115     # this juncture if it is better to return a user-defined override, so
1116     # leaving it as is for now.
1117     if ($txt =~ m/^$hex\t/m) {
1118
1119         # The name starts with the next character and goes up to the
1120         # next new-line.  Using capturing parentheses above instead of
1121         # @+ more than doubles the execution time in Perl 5.13
1122         $viacode{$hex} = substr($txt, $+[0], index($txt, "\n", $+[0]) - $+[0]);
1123         return $viacode{$hex};
1124     }
1125   }
1126
1127   # See if there is a user name for it, before giving up completely.
1128   # First get the scoped aliases, give up if have none.
1129   my $H_ref = (caller(1))[10];
1130   return if ! defined $H_ref
1131             || ! exists $H_ref->{charnames_stringified_inverse_ords};
1132
1133   my %code_point_aliases = split ',',
1134                           $H_ref->{charnames_stringified_inverse_ords};
1135   if (! exists $code_point_aliases{$hex}) {
1136     if (CORE::hex($hex) > 0x10FFFF) {
1137         carp "Unicode characters only allocated up to U+10FFFF (you asked for U+$hex)";
1138     }
1139     return;
1140   }
1141
1142   return $code_point_aliases{$hex};
1143 } # _viacode
1144
1145 1;
1146
1147 # ex: set ts=8 sts=2 sw=2 et: