Commit | Line | Data |
---|---|---|
e7a078a0 KW |
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; | |
36897d64 | 10 | our $VERSION = '1.41'; |
e7a078a0 KW |
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 = ( | |
e7a078a0 | 68 | |
22bd7dd2 KW |
69 | 'SINGLE-SHIFT 2' => pack("U", utf8::unicode_to_native(0x8E)), |
70 | 'SINGLE-SHIFT 3' => pack("U", utf8::unicode_to_native(0x8F)), | |
71 | 'PRIVATE USE 1' => pack("U", utf8::unicode_to_native(0x91)), | |
72 | 'PRIVATE USE 2' => pack("U", utf8::unicode_to_native(0x92)), | |
e7a078a0 KW |
73 | ); |
74 | ||
75 | # These are the aliases above that differ under :loose and :full matching | |
76 | # because the :full versions have blanks or hyphens in them. | |
7620cb10 KW |
77 | #my %loose_system_aliases = ( |
78 | #); | |
e7a078a0 | 79 | |
fe3193b5 | 80 | #my %deprecated_aliases; |
22bd7dd2 | 81 | #$deprecated_aliases{'BELL'} = pack("U", utf8::unicode_to_native(0x07)) if $^V lt v5.17.0; |
e7a078a0 | 82 | |
7620cb10 KW |
83 | #my %loose_deprecated_aliases = ( |
84 | #); | |
e7a078a0 KW |
85 | |
86 | # These are special cased in :loose matching, differing only in a medial | |
87 | # hyphen | |
88 | my $HANGUL_JUNGSEONG_O_E_utf8 = pack("U", 0x1180); | |
89 | my $HANGUL_JUNGSEONG_OE_utf8 = pack("U", 0x116C); | |
90 | ||
91 | ||
92 | my $txt; # The table of official character names | |
93 | ||
94 | my %full_names_cache; # Holds already-looked-up names, so don't have to | |
95 | # re-look them up again. The previous versions of charnames had scoping | |
96 | # bugs. For example if we use script A in one scope and find and cache | |
97 | # what Z resolves to, we can't use that cache in a different scope that | |
98 | # uses script B instead of A, as Z might be an entirely different letter | |
99 | # there; or there might be different aliases in effect in different | |
100 | # scopes, or :short may be in effect or not effect in different scopes, | |
101 | # or various combinations thereof. This was solved in this version | |
102 | # mostly by moving things to %^H. But some things couldn't be moved | |
103 | # there. One of them was the cache of runtime looked-up names, in part | |
104 | # because %^H is read-only at runtime. I (khw) don't know why the cache | |
105 | # was run-time only in the previous versions: perhaps oversight; perhaps | |
106 | # that compile time looking doesn't happen in a loop so didn't think it | |
107 | # was worthwhile; perhaps not wanting to make the cache too large. But | |
108 | # I decided to make it compile time as well; this could easily be | |
109 | # changed. | |
110 | # Anyway, this hash is not scoped, and is added to at runtime. It | |
111 | # doesn't have scoping problems because the data in it is restricted to | |
112 | # official names, which are always invariant, and we only set it and | |
113 | # look at it at during :full lookups, so is unaffected by any other | |
114 | # scoped options. I put this in to maintain parity with the older | |
115 | # version. If desired, a %short_names cache could also be made, as well | |
116 | # as one for each script, say in %script_names_cache, with each key | |
117 | # being a hash for a script named in a 'use charnames' statement. I | |
118 | # decided not to do that for now, just because it's added complication, | |
119 | # and because I'm just trying to maintain parity, not extend it. | |
120 | ||
121 | # Like %full_names_cache, but for use when :loose is in effect. There needs | |
122 | # to be two caches because :loose may not be in effect for a scope, and a | |
123 | # loose name could inappropriately be returned when only exact matching is | |
124 | # called for. | |
125 | my %loose_names_cache; | |
126 | ||
127 | # Designed so that test decimal first, and then hex. Leading zeros | |
128 | # imply non-decimal, as do non-[0-9] | |
129 | my $decimal_qr = qr/^[1-9]\d*$/; | |
130 | ||
131 | # Returns the hex number in $1. | |
132 | my $hex_qr = qr/^(?:[Uu]\+|0[xX])?([[:xdigit:]]+)$/; | |
133 | ||
134 | sub croak | |
135 | { | |
136 | require Carp; goto &Carp::croak; | |
137 | } # croak | |
138 | ||
139 | sub carp | |
140 | { | |
141 | require Carp; goto &Carp::carp; | |
142 | } # carp | |
143 | ||
144 | sub alias (@) # Set up a single alias | |
145 | { | |
225fb84f | 146 | my @errors; |
df758df2 | 147 | my $nbsp = chr utf8::unicode_to_native(0xA0); |
225fb84f | 148 | |
e7a078a0 | 149 | my $alias = ref $_[0] ? $_[0] : { @_ }; |
225fb84f KW |
150 | foreach my $name (sort keys %$alias) { # Sort only because it helps having |
151 | # deterministic output for | |
152 | # t/lib/charnames/alias | |
e7a078a0 KW |
153 | my $value = $alias->{$name}; |
154 | next unless defined $value; # Omit if screwed up. | |
155 | ||
156 | # Is slightly slower to just after this statement see if it is | |
157 | # decimal, since we already know it is after having converted from | |
158 | # hex, but makes the code easier to maintain, and is called | |
159 | # infrequently, only at compile-time | |
160 | if ($value !~ $decimal_qr && $value =~ $hex_qr) { | |
22bd7dd2 KW |
161 | my $temp = CORE::hex $1; |
162 | $temp = utf8::unicode_to_native($temp) if $value =~ /^[Uu]\+/; | |
163 | $value = $temp; | |
e7a078a0 KW |
164 | } |
165 | if ($value =~ $decimal_qr) { | |
013b94b6 | 166 | no warnings qw(non_unicode surrogate nonchar); # Allow any of these |
e7a078a0 KW |
167 | $^H{charnames_ord_aliases}{$name} = pack("U", $value); |
168 | ||
169 | # Use a canonical form. | |
170 | $^H{charnames_inverse_ords}{sprintf("%05X", $value)} = $name; | |
171 | } | |
172 | else { | |
36897d64 KW |
173 | my $ok_portion = ""; |
174 | $ok_portion = $1 if $name =~ / ^ ( | |
175 | \p{_Perl_Charname_Begin} | |
176 | \p{_Perl_Charname_Continue}* | |
177 | ) /x; | |
178 | ||
179 | # If the name was fully correct, the above should have matched all of | |
180 | # it. | |
181 | if (length $ok_portion < length $name) { | |
182 | my $first_bad = substr($name, length($ok_portion), 1); | |
183 | push @errors, "Invalid character in charnames alias definition; " | |
184 | . "marked by <-- HERE in '$ok_portion$first_bad<-- HERE " | |
185 | . substr($name, length($ok_portion) + 1) | |
186 | . "'"; | |
225fb84f KW |
187 | } |
188 | else { | |
bd299e29 | 189 | if ($name =~ / ( .* \s ) ( \s* ) $ /x) { |
2d8eb851 KW |
190 | push @errors, "charnames alias definitions may not contain " |
191 | . "trailing white-space; marked by <-- HERE in " | |
192 | . "'$1 <-- HERE " . $2 . "'"; | |
193 | next; | |
bd299e29 KW |
194 | } |
195 | ||
196 | # Use '+' instead of '*' in this regex, because any trailing | |
2d8eb851 | 197 | # blanks have already been found |
bd299e29 | 198 | if ($name =~ / ( .*? \s{2} ) ( .+ ) /x) { |
2d8eb851 KW |
199 | push @errors, "charnames alias definitions may not contain a " |
200 | . "sequence of multiple spaces; marked by <-- HERE " | |
201 | . "in '$1 <-- HERE " . $2 . "'"; | |
202 | next; | |
bd299e29 | 203 | } |
2d8eb851 KW |
204 | |
205 | $^H{charnames_name_aliases}{$name} = $value; | |
df758df2 KW |
206 | if (warnings::enabled('deprecated') |
207 | && $name =~ / ( .* $nbsp ) ( .* ) $ /x) | |
208 | { | |
209 | carp "NO-BREAK SPACE in a charnames alias definition is " | |
210 | . "deprecated; marked by <-- HERE in '$1 <-- HERE " | |
211 | . $2 . "'"; | |
212 | } | |
225fb84f | 213 | } |
e7a078a0 KW |
214 | } |
215 | } | |
225fb84f KW |
216 | |
217 | # We find and output all errors from this :alias definition, rather than | |
218 | # failing on the first one, so fewer runs are needed to get it to compile | |
219 | if (@errors) { | |
225fb84f KW |
220 | croak join "\n", @errors; |
221 | } | |
222 | ||
223 | return; | |
e7a078a0 KW |
224 | } # alias |
225 | ||
226 | sub not_legal_use_bytes_msg { | |
227 | my ($name, $utf8) = @_; | |
228 | my $return; | |
229 | ||
230 | if (length($utf8) == 1) { | |
231 | $return = sprintf("Character 0x%04x with name '%s' is", ord $utf8, $name); | |
232 | } else { | |
233 | $return = sprintf("String with name '%s' (and ordinals %s) contains character(s)", $name, join(" ", map { sprintf "0x%04X", ord $_ } split(//, $utf8))); | |
234 | } | |
235 | return $return . " above 0xFF with 'use bytes' in effect"; | |
236 | } | |
237 | ||
238 | sub alias_file ($) # Reads a file containing alias definitions | |
239 | { | |
240 | my ($arg, $file) = @_; | |
241 | if (-f $arg && File::Spec->file_name_is_absolute ($arg)) { | |
242 | $file = $arg; | |
243 | } | |
695481e2 | 244 | elsif ($arg =~ m/ ^ \p{_Perl_IDStart} \p{_Perl_IDCont}* $/x) { |
e7a078a0 KW |
245 | $file = "unicore/${arg}_alias.pl"; |
246 | } | |
247 | else { | |
013b94b6 | 248 | croak "Charnames alias file names can only have identifier characters"; |
e7a078a0 KW |
249 | } |
250 | if (my @alias = do $file) { | |
251 | @alias == 1 && !defined $alias[0] and | |
252 | croak "$file cannot be used as alias file for charnames"; | |
253 | @alias % 2 and | |
254 | croak "$file did not return a (valid) list of alias pairs"; | |
255 | alias (@alias); | |
256 | return (1); | |
257 | } | |
258 | 0; | |
259 | } # alias_file | |
260 | ||
261 | # For use when don't import anything. This structure must be kept in | |
262 | # sync with the one that import() fills up. | |
263 | my %dummy_H = ( | |
264 | charnames_stringified_names => "", | |
265 | charnames_stringified_ords => "", | |
266 | charnames_scripts => "", | |
267 | charnames_full => 1, | |
268 | charnames_loose => 0, | |
269 | charnames_short => 0, | |
270 | ); | |
271 | ||
272 | ||
273 | sub lookup_name ($$$) { | |
274 | my ($name, $wants_ord, $runtime) = @_; | |
275 | ||
276 | # Lookup the name or sequence $name in the tables. If $wants_ord is false, | |
277 | # returns the string equivalent of $name; if true, returns the ordinal value | |
278 | # instead, but in this case $name must not be a sequence; otherwise undef is | |
279 | # returned and a warning raised. $runtime is 0 if compiletime, otherwise | |
280 | # gives the number of stack frames to go back to get the application caller | |
281 | # info. | |
282 | # If $name is not found, returns undef in runtime with no warning; and in | |
283 | # compiletime, the Unicode replacement character, with a warning. | |
284 | ||
285 | # It looks first in the aliases, then in the large table of official Unicode | |
286 | # names. | |
287 | ||
288 | my $utf8; # The string result | |
289 | my $save_input; | |
290 | ||
291 | if ($runtime) { | |
292 | ||
293 | my $hints_ref = (caller($runtime))[10]; | |
294 | ||
295 | # If we didn't import anything (which happens with 'use charnames ()', | |
296 | # substitute a dummy structure. | |
297 | $hints_ref = \%dummy_H if ! defined $hints_ref | |
298 | || (! defined $hints_ref->{charnames_full} | |
299 | && ! defined $hints_ref->{charnames_loose}); | |
300 | ||
301 | # At runtime, but currently not at compile time, $^H gets | |
302 | # stringified, so un-stringify back to the original data structures. | |
303 | # These get thrown away by perl before the next invocation | |
304 | # Also fill in the hash with the non-stringified data. | |
305 | # N.B. New fields must be also added to %dummy_H | |
306 | ||
307 | %{$^H{charnames_name_aliases}} = split ',', | |
308 | $hints_ref->{charnames_stringified_names}; | |
309 | %{$^H{charnames_ord_aliases}} = split ',', | |
310 | $hints_ref->{charnames_stringified_ords}; | |
311 | $^H{charnames_scripts} = $hints_ref->{charnames_scripts}; | |
312 | $^H{charnames_full} = $hints_ref->{charnames_full}; | |
313 | $^H{charnames_loose} = $hints_ref->{charnames_loose}; | |
314 | $^H{charnames_short} = $hints_ref->{charnames_short}; | |
315 | } | |
316 | ||
317 | my $loose = $^H{charnames_loose}; | |
318 | my $lookup_name; # Input name suitably modified for grepping for in the | |
319 | # table | |
320 | ||
321 | # User alias should be checked first or else can't override ours, and if we | |
322 | # were to add any, could conflict with theirs. | |
323 | if (exists $^H{charnames_ord_aliases}{$name}) { | |
324 | $utf8 = $^H{charnames_ord_aliases}{$name}; | |
325 | } | |
326 | elsif (exists $^H{charnames_name_aliases}{$name}) { | |
327 | $name = $^H{charnames_name_aliases}{$name}; | |
328 | $save_input = $lookup_name = $name; # Cache the result for any error | |
329 | # message | |
330 | # The aliases are documented to not match loosely, so change loose match | |
331 | # into full. | |
332 | if ($loose) { | |
333 | $loose = 0; | |
334 | $^H{charnames_full} = 1; | |
335 | } | |
336 | } | |
337 | else { | |
338 | ||
339 | # Here, not a user alias. That means that loose matching may be in | |
340 | # effect; will have to modify the input name. | |
341 | $lookup_name = $name; | |
342 | if ($loose) { | |
343 | $lookup_name = uc $lookup_name; | |
344 | ||
345 | # Squeeze out all underscores | |
346 | $lookup_name =~ s/_//g; | |
347 | ||
348 | # Remove all medial hyphens | |
349 | $lookup_name =~ s/ (?<= \S ) - (?= \S )//gx; | |
350 | ||
351 | # Squeeze out all spaces | |
352 | $lookup_name =~ s/\s//g; | |
353 | } | |
354 | ||
355 | # Here, $lookup_name has been modified as necessary for looking in the | |
356 | # hashes. Check the system alias files next. Most of these aliases are | |
357 | # the same for both strict and loose matching. To save space, the ones | |
358 | # which differ are in their own separate hash, which is checked if loose | |
359 | # matching is selected and the regular match fails. To save time, the | |
360 | # loose hashes could be expanded to include all aliases, and there would | |
361 | # only have to be one check. But if someone specifies :loose, they are | |
362 | # interested in convenience over speed, and the time for this second check | |
363 | # is miniscule compared to the rest of the routine. | |
364 | if (exists $system_aliases{$lookup_name}) { | |
365 | $utf8 = $system_aliases{$lookup_name}; | |
366 | } | |
7620cb10 KW |
367 | # There are currently no entries in this hash, so don't waste time looking |
368 | # for them. But the code is retained for the unlikely possibility that | |
369 | # some will be added in the future. | |
370 | # elsif ($loose && exists $loose_system_aliases{$lookup_name}) { | |
371 | # $utf8 = $loose_system_aliases{$lookup_name}; | |
372 | # } | |
fe3193b5 KW |
373 | # if (exists $deprecated_aliases{$lookup_name}) { |
374 | # require warnings; | |
375 | # warnings::warnif('deprecated', | |
376 | # "Unicode character name \"$name\" is deprecated, use \"" | |
377 | # . viacode(ord $deprecated_aliases{$lookup_name}) | |
378 | # . "\" instead"); | |
379 | # $utf8 = $deprecated_aliases{$lookup_name}; | |
380 | # } | |
7620cb10 KW |
381 | # There are currently no entries in this hash, so don't waste time looking |
382 | # for them. But the code is retained for the unlikely possibility that | |
383 | # some will be added in the future. | |
384 | # elsif ($loose && exists $loose_deprecated_aliases{$lookup_name}) { | |
385 | # require warnings; | |
386 | # warnings::warnif('deprecated', | |
387 | # "Unicode character name \"$name\" is deprecated, use \"" | |
388 | # . viacode(ord $loose_deprecated_aliases{$lookup_name}) | |
389 | # . "\" instead"); | |
390 | # $utf8 = $loose_deprecated_aliases{$lookup_name}; | |
391 | # } | |
e7a078a0 KW |
392 | } |
393 | ||
394 | my @off; # Offsets into table of pattern match begin and end | |
395 | ||
396 | # If haven't found it yet... | |
397 | if (! defined $utf8) { | |
398 | ||
399 | # See if has looked this input up earlier. | |
400 | if (! $loose && $^H{charnames_full} && exists $full_names_cache{$name}) { | |
401 | $utf8 = $full_names_cache{$name}; | |
402 | } | |
403 | elsif ($loose && exists $loose_names_cache{$name}) { | |
404 | $utf8 = $loose_names_cache{$name}; | |
405 | } | |
406 | else { # Here, must do a look-up | |
407 | ||
408 | # If full or loose matching succeeded, points to where to cache the | |
409 | # result | |
410 | my $cache_ref; | |
411 | ||
412 | ## Suck in the code/name list as a big string. | |
413 | ## Lines look like: | |
414 | ## "00052\tLATIN CAPITAL LETTER R\n" | |
415 | # or | |
416 | # "0052 0303\tLATIN CAPITAL LETTER R WITH TILDE\n" | |
417 | $txt = do "unicore/Name.pl" unless $txt; | |
418 | ||
419 | ## @off will hold the index into the code/name string of the start and | |
420 | ## end of the name as we find it. | |
421 | ||
422 | ## If :loose, look for a loose match; if :full, look for the name | |
423 | ## exactly | |
424 | # First, see if the name is one which is algorithmically determinable. | |
425 | # The subroutine is included in Name.pl. The table contained in | |
426 | # $txt doesn't contain these. Experiments show that checking | |
427 | # for these before checking for the regular names has no | |
428 | # noticeable impact on performance for the regular names, but | |
429 | # the other way around slows down finding these immensely. | |
430 | # Algorithmically determinables are not placed in the cache because | |
431 | # that uses up memory, and finding these again is fast. | |
432 | if (($loose || $^H{charnames_full}) | |
433 | && (defined (my $ord = charnames::name_to_code_point_special($lookup_name, $loose)))) | |
434 | { | |
435 | $utf8 = pack("U", $ord); | |
436 | } | |
437 | else { | |
438 | ||
439 | # Not algorithmically determinable; look up in the table. The name | |
440 | # will be turned into a regex, so quote any meta characters. | |
441 | $lookup_name = quotemeta $lookup_name; | |
442 | ||
443 | if ($loose) { | |
444 | ||
445 | # For loose matches, $lookup_name has already squeezed out the | |
446 | # non-essential characters. We have to add in code to make the | |
447 | # squeezed version match the non-squeezed equivalent in the table. | |
448 | # The only remaining hyphens are ones that start or end a word in | |
449 | # the original. They have been quoted in $lookup_name so they look | |
450 | # like "\-". Change all other characters except the backslash | |
451 | # quotes for any metacharacters, and the final character, so that | |
452 | # e.g., COLON gets transformed into: /C[- ]?O[- ]?L[- ]?O[- ]?N/ | |
453 | $lookup_name =~ s/ (?! \\ -) # Don't do this to the \- sequence | |
454 | ( [^-\\] ) # Nor the "-" within that sequence, | |
455 | # nor the "\" that quotes metachars, | |
456 | # but otherwise put the char into $1 | |
457 | (?=.) # And don't do it for the final char | |
458 | /$1\[- \]?/gx; # And add an optional blank or | |
459 | # '-' after each $1 char | |
460 | ||
461 | # Those remaining hyphens were originally at the beginning or end of | |
462 | # a word, so they can match either a blank before or after, but not | |
463 | # both. (Keep in mind that they have been quoted, so are a '\-' | |
464 | # sequence) | |
465 | $lookup_name =~ s/\\ -/(?:- | -)/xg; | |
466 | } | |
467 | ||
468 | # Do the lookup in the full table if asked for, and if succeeds | |
469 | # save the offsets and set where to cache the result. | |
470 | if (($loose || $^H{charnames_full}) && $txt =~ /\t$lookup_name$/m) { | |
471 | @off = ($-[0] + 1, $+[0]); # The 1 is for the tab | |
472 | $cache_ref = ($loose) ? \%loose_names_cache : \%full_names_cache; | |
473 | } | |
474 | else { | |
475 | ||
476 | # Here, didn't look for, or didn't find the name. | |
477 | # If :short is allowed, see if input is like "greek:Sigma". | |
478 | # Keep in mind that $lookup_name has had the metas quoted. | |
479 | my $scripts_trie = ""; | |
480 | my $name_has_uppercase; | |
481 | if (($^H{charnames_short}) | |
482 | && $lookup_name =~ /^ (?: \\ \s)* # Quoted space | |
483 | (.+?) # $1 = the script | |
484 | (?: \\ \s)* | |
485 | \\ : # Quoted colon | |
486 | (?: \\ \s)* | |
487 | (.+?) # $2 = the name | |
488 | (?: \\ \s)* $ | |
489 | /xs) | |
490 | { | |
491 | # Even in non-loose matching, the script traditionally has been | |
203f930d | 492 | # case insensitive |
e7a078a0 KW |
493 | $scripts_trie = "\U$1"; |
494 | $lookup_name = $2; | |
495 | ||
496 | # Use original name to find its input casing, but ignore the | |
497 | # script part of that to make the determination. | |
498 | $save_input = $name if ! defined $save_input; | |
499 | $name =~ s/.*?://; | |
500 | $name_has_uppercase = $name =~ /[[:upper:]]/; | |
501 | } | |
502 | else { # Otherwise look in allowed scripts | |
503 | $scripts_trie = $^H{charnames_scripts}; | |
504 | ||
505 | # Use original name to find its input casing | |
506 | $name_has_uppercase = $name =~ /[[:upper:]]/; | |
507 | } | |
508 | ||
509 | my $case = $name_has_uppercase ? "CAPITAL" : "SMALL"; | |
90249f0a KW |
510 | return if (! $scripts_trie || $txt !~ |
511 | /\t (?: $scripts_trie ) \ (?:$case\ )? LETTER \ \U$lookup_name $/xm); | |
e7a078a0 KW |
512 | |
513 | # Here have found the input name in the table. | |
514 | @off = ($-[0] + 1, $+[0]); # The 1 is for the tab | |
515 | } | |
516 | ||
517 | # Here, the input name has been found; we haven't set up the output, | |
518 | # but we know where in the string | |
519 | # the name starts. The string is set up so that for single characters | |
520 | # (and not named sequences), the name is preceded immediately by a | |
521 | # tab and 5 hex digits for its code, with a \n before those. Named | |
522 | # sequences won't have the 7th preceding character be a \n. | |
523 | # (Actually, for the very first entry in the table this isn't strictly | |
524 | # true: subtracting 7 will yield -1, and the substr below will | |
525 | # therefore yield the very last character in the table, which should | |
526 | # also be a \n, so the statement works anyway.) | |
527 | if (substr($txt, $off[0] - 7, 1) eq "\n") { | |
528 | $utf8 = pack("U", CORE::hex substr($txt, $off[0] - 6, 5)); | |
529 | ||
530 | # Handle the single loose matching special case, in which two names | |
531 | # differ only by a single medial hyphen. If the original had a | |
532 | # hyphen (or more) in the right place, then it is that one. | |
533 | $utf8 = $HANGUL_JUNGSEONG_O_E_utf8 | |
534 | if $loose | |
535 | && $utf8 eq $HANGUL_JUNGSEONG_OE_utf8 | |
536 | && $name =~ m/O \s* - [-\s]* E/ix; | |
537 | # Note that this wouldn't work if there were a 2nd | |
538 | # OE in the name | |
539 | } | |
540 | else { | |
541 | ||
542 | # Here, is a named sequence. Need to go looking for the beginning, | |
543 | # which is just after the \n from the previous entry in the table. | |
544 | # The +1 skips past that newline, or, if the rindex() fails, to put | |
545 | # us to an offset of zero. | |
546 | my $charstart = rindex($txt, "\n", $off[0] - 7) + 1; | |
547 | $utf8 = pack("U*", map { CORE::hex } | |
548 | split " ", substr($txt, $charstart, $off[0] - $charstart - 1)); | |
549 | } | |
550 | } | |
551 | ||
552 | # Cache the input so as to not have to search the large table | |
553 | # again, but only if it came from the one search that we cache. | |
554 | # (Haven't bothered with the pain of sorting out scoping issues for the | |
555 | # scripts searches.) | |
556 | $cache_ref->{$name} = $utf8 if defined $cache_ref; | |
557 | } | |
558 | } | |
559 | ||
560 | ||
561 | # Here, have the utf8. If the return is to be an ord, must be any single | |
562 | # character. | |
563 | if ($wants_ord) { | |
564 | return ord($utf8) if length $utf8 == 1; | |
565 | } | |
566 | else { | |
567 | ||
568 | # Here, wants string output. If utf8 is acceptable, just return what | |
569 | # we've got; otherwise attempt to convert it to non-utf8 and return that. | |
570 | my $in_bytes = ($runtime) | |
571 | ? (caller $runtime)[8] & $bytes::hint_bits | |
572 | : $^H & $bytes::hint_bits; | |
573 | return $utf8 if (! $in_bytes || utf8::downgrade($utf8, 1)) # The 1 arg | |
574 | # means don't die on failure | |
575 | } | |
576 | ||
577 | # Here, there is an error: either there are too many characters, or the | |
578 | # result string needs to be non-utf8, and at least one character requires | |
579 | # utf8. Prefer any official name over the input one for the error message. | |
580 | if (@off) { | |
581 | $name = substr($txt, $off[0], $off[1] - $off[0]) if @off; | |
582 | } | |
583 | else { | |
584 | $name = (defined $save_input) ? $save_input : $_[0]; | |
585 | } | |
586 | ||
587 | if ($wants_ord) { | |
588 | # Only way to get here in this case is if result too long. Message | |
589 | # assumes that our only caller that requires single char result is | |
590 | # vianame. | |
591 | carp "charnames::vianame() doesn't handle named sequences ($name). Use charnames::string_vianame() instead"; | |
592 | return; | |
593 | } | |
594 | ||
595 | # Only other possible failure here is from use bytes. | |
596 | if ($runtime) { | |
597 | carp not_legal_use_bytes_msg($name, $utf8); | |
598 | return; | |
599 | } else { | |
600 | croak not_legal_use_bytes_msg($name, $utf8); | |
601 | } | |
602 | ||
603 | } # lookup_name | |
604 | ||
605 | sub charnames { | |
606 | ||
607 | # For \N{...}. Looks up the character name and returns the string | |
608 | # representation of it. | |
609 | ||
610 | # The first 0 arg means wants a string returned; the second that we are in | |
611 | # compile time | |
612 | return lookup_name($_[0], 0, 0); | |
613 | } | |
614 | ||
615 | sub import | |
616 | { | |
617 | shift; ## ignore class name | |
618 | ||
619 | if (not @_) { | |
620 | carp("'use charnames' needs explicit imports list"); | |
621 | } | |
622 | $^H{charnames} = \&charnames ; | |
623 | $^H{charnames_ord_aliases} = {}; | |
624 | $^H{charnames_name_aliases} = {}; | |
625 | $^H{charnames_inverse_ords} = {}; | |
626 | # New fields must be added to %dummy_H, and the code in lookup_name() | |
627 | # that copies fields from the runtime structure | |
628 | ||
629 | ## | |
630 | ## fill %h keys with our @_ args. | |
631 | ## | |
632 | my ($promote, %h, @args) = (0); | |
633 | while (my $arg = shift) { | |
634 | if ($arg eq ":alias") { | |
635 | @_ or | |
636 | croak ":alias needs an argument in charnames"; | |
637 | my $alias = shift; | |
638 | if (ref $alias) { | |
639 | ref $alias eq "HASH" or | |
640 | croak "Only HASH reference supported as argument to :alias"; | |
641 | alias ($alias); | |
5198c137 | 642 | $promote = 1; |
e7a078a0 KW |
643 | next; |
644 | } | |
645 | if ($alias =~ m{:(\w+)$}) { | |
646 | $1 eq "full" || $1 eq "loose" || $1 eq "short" and | |
647 | croak ":alias cannot use existing pragma :$1 (reversed order?)"; | |
648 | alias_file ($1) and $promote = 1; | |
649 | next; | |
650 | } | |
5198c137 | 651 | alias_file ($alias) and $promote = 1; |
e7a078a0 KW |
652 | next; |
653 | } | |
654 | if (substr($arg, 0, 1) eq ':' | |
655 | and ! ($arg eq ":full" || $arg eq ":short" || $arg eq ":loose")) | |
656 | { | |
657 | warn "unsupported special '$arg' in charnames"; | |
658 | next; | |
659 | } | |
660 | push @args, $arg; | |
661 | } | |
662 | ||
663 | @args == 0 && $promote and @args = (":full"); | |
664 | @h{@args} = (1) x @args; | |
665 | ||
666 | # Don't leave these undefined as are tested for in lookup_names | |
667 | $^H{charnames_full} = delete $h{':full'} || 0; | |
668 | $^H{charnames_loose} = delete $h{':loose'} || 0; | |
669 | $^H{charnames_short} = delete $h{':short'} || 0; | |
670 | my @scripts = map { uc quotemeta } keys %h; | |
671 | ||
672 | ## | |
673 | ## If utf8? warnings are enabled, and some scripts were given, | |
674 | ## see if at least we can find one letter from each script. | |
675 | ## | |
676 | if (warnings::enabled('utf8') && @scripts) { | |
677 | $txt = do "unicore/Name.pl" unless $txt; | |
678 | ||
679 | for my $script (@scripts) { | |
680 | if (not $txt =~ m/\t$script (?:CAPITAL |SMALL )?LETTER /) { | |
681 | warnings::warn('utf8', "No such script: '$script'"); | |
682 | $script = quotemeta $script; # Escape it, for use in the re. | |
683 | } | |
684 | } | |
685 | } | |
686 | ||
687 | # %^H gets stringified, so serialize it ourselves so can extract the | |
688 | # real data back later. | |
689 | $^H{charnames_stringified_ords} = join ",", %{$^H{charnames_ord_aliases}}; | |
690 | $^H{charnames_stringified_names} = join ",", %{$^H{charnames_name_aliases}}; | |
691 | $^H{charnames_stringified_inverse_ords} = join ",", %{$^H{charnames_inverse_ords}}; | |
692 | ||
693 | # Modify the input script names for loose name matching if that is also | |
694 | # specified, similar to the way the base character name is prepared. They | |
695 | # don't (currently, and hopefully never will) have dashes. These go into a | |
696 | # regex, and have already been uppercased and quotemeta'd. Squeeze out all | |
697 | # input underscores, blanks, and dashes. Then convert so will match a blank | |
698 | # between any characters. | |
699 | if ($^H{charnames_loose}) { | |
700 | for (my $i = 0; $i < @scripts; $i++) { | |
701 | $scripts[$i] =~ s/[_ -]//g; | |
702 | $scripts[$i] =~ s/ ( [^\\] ) (?= . ) /$1\\ ?/gx; | |
703 | } | |
704 | } | |
705 | ||
706 | $^H{charnames_scripts} = join "|", @scripts; # Stringifiy them as a trie | |
707 | } # import | |
708 | ||
709 | # Cache of already looked-up values. This is set to only contain | |
710 | # official values, and user aliases can't override them, so scoping is | |
711 | # not an issue. | |
712 | my %viacode; | |
713 | ||
22bd7dd2 KW |
714 | my $no_name_code_points_re = join "|", map { sprintf("%05X", |
715 | utf8::unicode_to_native($_)) } | |
716 | 0x80, 0x81, 0x84, 0x99; | |
717 | $no_name_code_points_re = qr/$no_name_code_points_re/; | |
718 | ||
e7a078a0 KW |
719 | sub viacode { |
720 | ||
721 | # Returns the name of the code point argument | |
722 | ||
723 | if (@_ != 1) { | |
724 | carp "charnames::viacode() expects one argument"; | |
725 | return; | |
726 | } | |
727 | ||
728 | my $arg = shift; | |
729 | ||
730 | # This is derived from Unicode::UCD, where it is nearly the same as the | |
731 | # function _getcode(), but here it makes sure that even a hex argument | |
732 | # has the proper number of leading zeros, which is critical in | |
733 | # matching against $txt below | |
734 | # Must check if decimal first; see comments at that definition | |
735 | my $hex; | |
736 | if ($arg =~ $decimal_qr) { | |
737 | $hex = sprintf "%05X", $arg; | |
738 | } elsif ($arg =~ $hex_qr) { | |
22bd7dd2 KW |
739 | $hex = CORE::hex $1; |
740 | $hex = utf8::unicode_to_native($hex) if $arg =~ /^[Uu]\+/; | |
e7a078a0 | 741 | # Below is the line that differs from the _getcode() source |
22bd7dd2 | 742 | $hex = sprintf "%05X", $hex; |
e7a078a0 KW |
743 | } else { |
744 | carp("unexpected arg \"$arg\" to charnames::viacode()"); | |
745 | return; | |
746 | } | |
747 | ||
748 | return $viacode{$hex} if exists $viacode{$hex}; | |
749 | ||
7620cb10 KW |
750 | my $return; |
751 | ||
e7a078a0 KW |
752 | # If the code point is above the max in the table, there's no point |
753 | # looking through it. Checking the length first is slightly faster | |
754 | if (length($hex) <= 5 || CORE::hex($hex) <= 0x10FFFF) { | |
755 | $txt = do "unicore/Name.pl" unless $txt; | |
756 | ||
757 | # See if the name is algorithmically determinable. | |
758 | my $algorithmic = charnames::code_point_to_name_special(CORE::hex $hex); | |
759 | if (defined $algorithmic) { | |
760 | $viacode{$hex} = $algorithmic; | |
761 | return $algorithmic; | |
762 | } | |
763 | ||
764 | # Return the official name, if exists. It's unclear to me (khw) at | |
765 | # this juncture if it is better to return a user-defined override, so | |
766 | # leaving it as is for now. | |
767 | if ($txt =~ m/^$hex\t/m) { | |
768 | ||
769 | # The name starts with the next character and goes up to the | |
770 | # next new-line. Using capturing parentheses above instead of | |
771 | # @+ more than doubles the execution time in Perl 5.13 | |
7620cb10 KW |
772 | $return = substr($txt, $+[0], index($txt, "\n", $+[0]) - $+[0]); |
773 | ||
774 | # If not one of these 4 code points, return what we've found. | |
22bd7dd2 | 775 | if ($hex !~ / ^ $no_name_code_points_re $ /x) { |
7620cb10 KW |
776 | $viacode{$hex} = $return; |
777 | return $return; | |
778 | } | |
779 | ||
780 | # For backwards compatibility, we don't return the official name of | |
781 | # the 4 code points if there are user-defined aliases for them -- so | |
782 | # continue looking. | |
e7a078a0 KW |
783 | } |
784 | } | |
785 | ||
786 | # See if there is a user name for it, before giving up completely. | |
787 | # First get the scoped aliases, give up if have none. | |
788 | my $H_ref = (caller(1))[10]; | |
7620cb10 KW |
789 | return if ! defined $return |
790 | && (! defined $H_ref | |
791 | || ! exists $H_ref->{charnames_stringified_inverse_ords}); | |
e7a078a0 | 792 | |
424313d4 KW |
793 | my %code_point_aliases; |
794 | if (defined $H_ref->{charnames_stringified_inverse_ords}) { | |
795 | %code_point_aliases = split ',', | |
e7a078a0 | 796 | $H_ref->{charnames_stringified_inverse_ords}; |
424313d4 KW |
797 | return $code_point_aliases{$hex} if exists $code_point_aliases{$hex}; |
798 | } | |
7620cb10 | 799 | |
de72f72f KW |
800 | # Here there is no user-defined alias, return any official one. |
801 | return $return if defined $return; | |
7620cb10 | 802 | |
83adc448 KW |
803 | if (CORE::hex($hex) > 0x10FFFF |
804 | && warnings::enabled('non_unicode')) | |
805 | { | |
de72f72f KW |
806 | carp "Unicode characters only allocated up to U+10FFFF (you asked for U+$hex)"; |
807 | } | |
808 | return; | |
e7a078a0 | 809 | |
a48a707d | 810 | } # viacode |
e7a078a0 KW |
811 | |
812 | 1; | |
813 | ||
814 | # ex: set ts=8 sts=2 sw=2 et: |