Commit | Line | Data |
---|---|---|
a0ed51b3 | 1 | package utf8; |
cf25bb62 JH |
2 | use strict; |
3 | use warnings; | |
2783468b KW |
4 | use re "/aa"; # So we won't even try to look at above Latin1, potentially |
5 | # resulting in a recursive call | |
a0ed51b3 | 6 | |
15732964 | 7 | sub DEBUG () { 0 } |
4b17cb47 | 8 | $|=1 if DEBUG; |
a0ed51b3 LW |
9 | |
10 | sub DESTROY {} | |
11 | ||
5beb625e JH |
12 | my %Cache; |
13 | ||
86916d66 JH |
14 | sub croak { require Carp; Carp::croak(@_) } |
15 | ||
c4ab2516 | 16 | sub _loose_name ($) { |
45376db6 KW |
17 | # Given a lowercase property or property-value name, return its |
18 | # standardized version that is expected for look-up in the 'loose' hashes | |
19 | # in Heavy.pl (hence, this depends on what mktables does). This squeezes | |
20 | # out blanks, underscores and dashes. The complication stems from the | |
21 | # grandfathered-in 'L_', which retains a single trailing underscore. | |
22 | ||
23 | my $loose = $_[0] =~ s/[-\s_]//rg; | |
24 | ||
cf2cd801 | 25 | return $loose if $loose !~ / ^ (?: is | to )? l $/x; |
45376db6 KW |
26 | return 'l_' if $_[0] =~ / l .* _ /x; # If original had a trailing '_' |
27 | return $loose; | |
28 | } | |
29 | ||
5beb625e | 30 | ## |
3a2263fe RGS |
31 | ## "SWASH" == "SWATCH HASH". A "swatch" is a swatch of the Unicode landscape. |
32 | ## It's a data structure that encodes a set of Unicode characters. | |
5beb625e JH |
33 | ## |
34 | ||
99870f4d KW |
35 | { |
36 | # If a floating point number is within this distance from the value of a | |
37 | # fraction, it is considered to be that fraction, even if many more digits | |
38 | # are specified that don't exactly match. | |
39 | my $min_floating_slop; | |
40 | ||
3dd10fe8 KW |
41 | # To guard against this program calling something that in turn ends up |
42 | # calling this program with the same inputs, and hence infinitely | |
43 | # recursing, we keep a stack of the properties that are currently in | |
44 | # progress, pushed upon entry, popped upon return. | |
45 | my @recursed; | |
46 | ||
99870f4d KW |
47 | sub SWASHNEW { |
48 | my ($class, $type, $list, $minbits, $none) = @_; | |
0fada5e1 | 49 | my $user_defined = 0; |
99870f4d KW |
50 | local $^D = 0 if $^D; |
51 | ||
3dd10fe8 | 52 | $class = "" unless defined $class; |
5bcf08c4 KW |
53 | print STDERR __LINE__, ": class=$class, type=$type, list=", |
54 | (defined $list) ? $list : ':undef:', | |
55 | ", minbits=$minbits, none=$none\n" if DEBUG; | |
99870f4d KW |
56 | |
57 | ## | |
58 | ## Get the list of codepoints for the type. | |
59 | ## Called from swash_init (see utf8.c) or SWASHNEW itself. | |
60 | ## | |
61 | ## Callers of swash_init: | |
62 | ## op.c:pmtrans -- for tr/// and y/// | |
63 | ## regexec.c:regclass_swash -- for /[]/, \p, and \P | |
64 | ## utf8.c:is_utf8_common -- for common Unicode properties | |
65 | ## utf8.c:to_utf8_case -- for lc, uc, ucfirst, etc. and //i | |
681d705c | 66 | ## Unicode::UCD::prop_invlist |
62b3b855 | 67 | ## Unicode::UCD::prop_invmap |
99870f4d KW |
68 | ## |
69 | ## Given a $type, our goal is to fill $list with the set of codepoint | |
70 | ## ranges. If $type is false, $list passed is used. | |
71 | ## | |
72 | ## $minbits: | |
73 | ## For binary properties, $minbits must be 1. | |
74 | ## For character mappings (case and transliteration), $minbits must | |
75 | ## be a number except 1. | |
76 | ## | |
77 | ## $list (or that filled according to $type): | |
78 | ## Refer to perlunicode.pod, "User-Defined Character Properties." | |
79 | ## | |
80 | ## For binary properties, only characters with the property value | |
81 | ## of True should be listed. The 3rd column, if any, will be ignored | |
82 | ## | |
83 | ## $none is undocumented, so I'm (khw) trying to do some documentation | |
84 | ## of it now. It appears to be if there is a mapping in an input file | |
85 | ## that maps to 'XXXX', then that is replaced by $none+1, expressed in | |
83fe8199 | 86 | ## hexadecimal. It is used somehow in tr///. |
99870f4d KW |
87 | ## |
88 | ## To make the parsing of $type clear, this code takes the a rather | |
89 | ## unorthodox approach of last'ing out of the block once we have the | |
90 | ## info we need. Were this to be a subroutine, the 'last' would just | |
91 | ## be a 'return'. | |
92 | ## | |
fbe6f3cf KW |
93 | # If a problem is found $type is returned; |
94 | # Upon success, a new (or cached) blessed object is returned with | |
95 | # keys TYPE, BITS, EXTRAS, LIST, and NONE with values having the | |
96 | # same meanings as the input parameters. | |
a6517ebf KW |
97 | # SPECIALS contains a reference to any special-treatment hash in the |
98 | # INVERT_IT is non-zero if the result should be inverted before use | |
0fada5e1 KW |
99 | # USER_DEFINED is non-zero if the result came from a user-defined |
100 | # property. | |
99870f4d | 101 | my $file; ## file to load data from, and also part of the %Cache key. |
99870f4d KW |
102 | |
103 | # Change this to get a different set of Unicode tables | |
104 | my $unicore_dir = 'unicore'; | |
a6517ebf | 105 | my $invert_it = 0; |
7b953548 KW |
106 | my $list_is_from_mktables = 0; # Is $list returned from a mktables |
107 | # generated file? If so, we know it's | |
108 | # well behaved. | |
99870f4d KW |
109 | |
110 | if ($type) | |
cf25bb62 | 111 | { |
3dd10fe8 | 112 | # Verify that this isn't a recursive call for this property. |
dbe7a391 | 113 | # Can't use croak, as it may try to recurse to here itself. |
3dd10fe8 KW |
114 | my $class_type = $class . "::$type"; |
115 | if (grep { $_ eq $class_type } @recursed) { | |
116 | CORE::die "panic: Infinite recursion in SWASHNEW for '$type'\n"; | |
117 | } | |
118 | push @recursed, $class_type; | |
119 | ||
99870f4d KW |
120 | $type =~ s/^\s+//; |
121 | $type =~ s/\s+$//; | |
122 | ||
4ff124d1 KW |
123 | # regcomp.c surrounds the property name with '__" and '_i' if this |
124 | # is to be caseless matching. | |
20b1a891 | 125 | my $caseless = $type =~ s/^(.*)__(.*)_i$/$1$2/; |
4ff124d1 KW |
126 | |
127 | print STDERR __LINE__, ": type=$type, caseless=$caseless\n" if DEBUG; | |
99870f4d KW |
128 | |
129 | GETFILE: | |
130 | { | |
131 | ## | |
a44f4b74 KW |
132 | ## It could be a user-defined property. Look in current |
133 | ## package if no package given | |
99870f4d KW |
134 | ## |
135 | ||
20b1a891 KW |
136 | |
137 | my $caller0 = caller(0); | |
345c18b8 KW |
138 | my $caller1 = $type =~ s/(.+)::// |
139 | ? $1 | |
140 | : $caller0 eq 'main' | |
141 | ? 'main' | |
142 | : caller(1); | |
99870f4d | 143 | |
d658a8a8 | 144 | if (defined $caller1 && $type =~ /^I[ns]\w+$/) { |
99870f4d KW |
145 | my $prop = "${caller1}::$type"; |
146 | if (exists &{$prop}) { | |
0e9be77f DM |
147 | # stolen from Scalar::Util::PP::tainted() |
148 | my $tainted; | |
149 | { | |
150 | local($@, $SIG{__DIE__}, $SIG{__WARN__}); | |
151 | local $^W = 0; | |
152 | no warnings; | |
153 | eval { kill 0 * $prop }; | |
154 | $tainted = 1 if $@ =~ /^Insecure/; | |
155 | } | |
156 | die "Insecure user-defined property \\p{$prop}\n" | |
157 | if $tainted; | |
99870f4d | 158 | no strict 'refs'; |
4ff124d1 | 159 | $list = &{$prop}($caseless); |
0fada5e1 | 160 | $user_defined = 1; |
99870f4d KW |
161 | last GETFILE; |
162 | } | |
163 | } | |
164 | ||
a44f4b74 KW |
165 | # During Perl's compilation, this routine may be called before |
166 | # the tables are constructed. If so, we have a chicken/egg | |
167 | # problem. If we die, the tables never get constructed, so | |
168 | # keep going, but return an empty table so only what the code | |
169 | # has compiled in internally (currently ASCII/Latin1 range | |
170 | # matching) will work. | |
1363cbd0 FC |
171 | BEGIN { |
172 | # Poor man's constant, to avoid a run-time check. | |
173 | $utf8::{miniperl} | |
174 | = \! defined &DynaLoader::boot_DynaLoader; | |
175 | } | |
176 | if (miniperl) { | |
a44f4b74 KW |
177 | eval "require '$unicore_dir/Heavy.pl'"; |
178 | last GETFILE if $@; | |
179 | } | |
180 | else { | |
181 | require "$unicore_dir/Heavy.pl"; | |
182 | } | |
1363cbd0 | 183 | BEGIN { delete $utf8::{miniperl} } |
99870f4d | 184 | |
4ff124d1 | 185 | # All property names are matched caselessly |
49801ec2 | 186 | my $property_and_table = CORE::lc $type; |
99870f4d KW |
187 | print STDERR __LINE__, ": $property_and_table\n" if DEBUG; |
188 | ||
189 | # See if is of the compound form 'property=value', where the | |
190 | # value indicates the table we should use. | |
191 | my ($property, $table, @remainder) = | |
192 | split /\s*[:=]\s*/, $property_and_table, -1; | |
3dd10fe8 KW |
193 | if (@remainder) { |
194 | pop @recursed if @recursed; | |
195 | return $type; | |
196 | } | |
99870f4d KW |
197 | |
198 | my $prefix; | |
199 | if (! defined $table) { | |
200 | ||
201 | # Here, is the single form. The property becomes empty, and | |
202 | # the whole value is the table. | |
203 | $table = $property; | |
204 | $prefix = $property = ""; | |
205 | } else { | |
206 | print STDERR __LINE__, ": $property\n" if DEBUG; | |
207 | ||
208 | # Here it is the compound property=table form. The property | |
807807b7 KW |
209 | # name is always loosely matched, and always can have an |
210 | # optional 'is' prefix (which isn't true in the single | |
211 | # form). | |
c4ab2516 | 212 | $property = _loose_name($property) =~ s/^is//r; |
99870f4d KW |
213 | |
214 | # And convert to canonical form. Quit if not valid. | |
215 | $property = $utf8::loose_property_name_of{$property}; | |
3dd10fe8 KW |
216 | if (! defined $property) { |
217 | pop @recursed if @recursed; | |
218 | return $type; | |
219 | } | |
99870f4d KW |
220 | |
221 | $prefix = "$property="; | |
222 | ||
223 | # If the rhs looks like it is a number... | |
224 | print STDERR __LINE__, ": table=$table\n" if DEBUG; | |
225 | if ($table =~ qr{ ^ [ \s 0-9 _ + / . -]+ $ }x) { | |
226 | print STDERR __LINE__, ": table=$table\n" if DEBUG; | |
227 | ||
228 | # Don't allow leading nor trailing slashes | |
3dd10fe8 KW |
229 | if ($table =~ / ^ \/ | \/ $ /x) { |
230 | pop @recursed if @recursed; | |
231 | return $type; | |
232 | } | |
99870f4d KW |
233 | |
234 | # Split on slash, in case it is a rational, like \p{1/5} | |
235 | my @parts = split qr{ \s* / \s* }x, $table, -1; | |
236 | print __LINE__, ": $type\n" if @parts > 2 && DEBUG; | |
237 | ||
238 | # Can have maximum of one slash | |
3dd10fe8 KW |
239 | if (@parts > 2) { |
240 | pop @recursed if @recursed; | |
241 | return $type; | |
242 | } | |
99870f4d KW |
243 | |
244 | foreach my $part (@parts) { | |
245 | print __LINE__, ": part=$part\n" if DEBUG; | |
246 | ||
247 | $part =~ s/^\+\s*//; # Remove leading plus | |
248 | $part =~ s/^-\s*/-/; # Remove blanks after unary | |
249 | # minus | |
250 | ||
251 | # Remove underscores between digits. | |
eb568297 | 252 | $part =~ s/(?<= [0-9] ) _ (?= [0-9] ) //xg; |
99870f4d KW |
253 | |
254 | # No leading zeros (but don't make a single '0' | |
255 | # into a null string) | |
256 | $part =~ s/ ^ ( -? ) 0+ /$1/x; | |
257 | $part .= '0' if $part eq '-' || $part eq ""; | |
258 | ||
259 | # No trailing zeros after a decimal point | |
260 | $part =~ s/ ( \. .*? ) 0+ $ /$1/x; | |
261 | ||
262 | # Begin with a 0 if a leading decimal point | |
263 | $part =~ s/ ^ ( -? ) \. /${1}0./x; | |
264 | ||
265 | # Ensure not a trailing decimal point: turn into an | |
266 | # integer | |
267 | $part =~ s/ \. $ //x; | |
268 | ||
269 | print STDERR __LINE__, ": part=$part\n" if DEBUG; | |
270 | #return $type if $part eq ""; | |
271 | ||
272 | # Result better look like a number. (This test is | |
273 | # needed because, for example could have a plus in | |
274 | # the middle.) | |
3dd10fe8 KW |
275 | if ($part !~ / ^ -? [0-9]+ ( \. [0-9]+)? $ /x) { |
276 | pop @recursed if @recursed; | |
277 | return $type; | |
278 | } | |
99870f4d KW |
279 | } |
280 | ||
281 | # If a rational... | |
282 | if (@parts == 2) { | |
283 | ||
284 | # If denominator is negative, get rid of it, and ... | |
285 | if ($parts[1] =~ s/^-//) { | |
286 | ||
287 | # If numerator is also negative, convert the | |
288 | # whole thing to positive, or move the minus to | |
289 | # the numerator | |
290 | if ($parts[0] !~ s/^-//) { | |
291 | $parts[0] = '-' . $parts[0]; | |
292 | } | |
293 | } | |
294 | $table = join '/', @parts; | |
295 | } | |
296 | elsif ($property ne 'nv' || $parts[0] !~ /\./) { | |
297 | ||
298 | # Here is not numeric value, or doesn't have a | |
299 | # decimal point. No further manipulation is | |
300 | # necessary. (Note the hard-coded property name. | |
301 | # This could fail if other properties eventually | |
302 | # had fractions as well; perhaps the cjk ones | |
303 | # could evolve to do that. This hard-coding could | |
304 | # be fixed by mktables generating a list of | |
305 | # properties that could have fractions.) | |
306 | $table = $parts[0]; | |
307 | } else { | |
308 | ||
309 | # Here is a floating point numeric_value. Try to | |
310 | # convert to rational. First see if is in the list | |
311 | # of known ones. | |
312 | if (exists $utf8::nv_floating_to_rational{$parts[0]}) { | |
313 | $table = $utf8::nv_floating_to_rational{$parts[0]}; | |
314 | } else { | |
315 | ||
316 | # Here not in the list. See if is close | |
317 | # enough to something in the list. First | |
318 | # determine what 'close enough' means. It has | |
319 | # to be as tight as what mktables says is the | |
320 | # maximum slop, and as tight as how many | |
321 | # digits we were passed. That is, if the user | |
322 | # said .667, .6667, .66667, etc. we match as | |
323 | # many digits as they passed until get to | |
324 | # where it doesn't matter any more due to the | |
325 | # machine's precision. If they said .6666668, | |
326 | # we fail. | |
327 | (my $fraction = $parts[0]) =~ s/^.*\.//; | |
328 | my $epsilon = 10 ** - (length($fraction)); | |
329 | if ($epsilon > $utf8::max_floating_slop) { | |
330 | $epsilon = $utf8::max_floating_slop; | |
331 | } | |
332 | ||
333 | # But it can't be tighter than the minimum | |
334 | # precision for this machine. If haven't | |
335 | # already calculated that minimum, do so now. | |
336 | if (! defined $min_floating_slop) { | |
337 | ||
338 | # Keep going down an order of magnitude | |
339 | # until find that adding this quantity to | |
340 | # 1 remains 1; but put an upper limit on | |
341 | # this so in case this algorithm doesn't | |
342 | # work properly on some platform, that we | |
343 | # won't loop forever. | |
344 | my $count = 0; | |
345 | $min_floating_slop = 1; | |
346 | while (1+ $min_floating_slop != 1 | |
347 | && $count++ < 50) | |
348 | { | |
349 | my $next = $min_floating_slop / 10; | |
350 | last if $next == 0; # If underflows, | |
351 | # use previous one | |
352 | $min_floating_slop = $next; | |
353 | print STDERR __LINE__, ": min_float_slop=$min_floating_slop\n" if DEBUG; | |
354 | } | |
355 | ||
356 | # Back off a couple orders of magnitude, | |
357 | # just to be safe. | |
358 | $min_floating_slop *= 100; | |
359 | } | |
360 | ||
361 | if ($epsilon < $min_floating_slop) { | |
362 | $epsilon = $min_floating_slop; | |
363 | } | |
364 | print STDERR __LINE__, ": fraction=.$fraction; epsilon=$epsilon\n" if DEBUG; | |
365 | ||
366 | undef $table; | |
367 | ||
368 | # And for each possible rational in the table, | |
369 | # see if it is within epsilon of the input. | |
370 | foreach my $official | |
371 | (keys %utf8::nv_floating_to_rational) | |
372 | { | |
373 | print STDERR __LINE__, ": epsilon=$epsilon, official=$official, diff=", abs($parts[0] - $official), "\n" if DEBUG; | |
374 | if (abs($parts[0] - $official) < $epsilon) { | |
375 | $table = | |
376 | $utf8::nv_floating_to_rational{$official}; | |
377 | last; | |
378 | } | |
379 | } | |
380 | ||
381 | # Quit if didn't find one. | |
3dd10fe8 KW |
382 | if (! defined $table) { |
383 | pop @recursed if @recursed; | |
384 | return $type; | |
385 | } | |
99870f4d KW |
386 | } |
387 | } | |
388 | print STDERR __LINE__, ": $property=$table\n" if DEBUG; | |
389 | } | |
390 | } | |
391 | ||
392 | # Combine lhs (if any) and rhs to get something that matches | |
393 | # the syntax of the lookups. | |
394 | $property_and_table = "$prefix$table"; | |
395 | print STDERR __LINE__, ": $property_and_table\n" if DEBUG; | |
396 | ||
397 | # First try stricter matching. | |
398 | $file = $utf8::stricter_to_file_of{$property_and_table}; | |
399 | ||
400 | # If didn't find it, try again with looser matching by editing | |
401 | # out the applicable characters on the rhs and looking up | |
402 | # again. | |
403 | if (! defined $file) { | |
c4ab2516 | 404 | $table = _loose_name($table); |
99870f4d KW |
405 | $property_and_table = "$prefix$table"; |
406 | print STDERR __LINE__, ": $property_and_table\n" if DEBUG; | |
407 | $file = $utf8::loose_to_file_of{$property_and_table}; | |
408 | } | |
409 | ||
410 | # Add the constant and go fetch it in. | |
411 | if (defined $file) { | |
a6517ebf | 412 | |
eb092534 KW |
413 | # If the file name contains a !, it means to invert. The |
414 | # 0+ makes sure result is numeric | |
415 | $invert_it = 0 + $file =~ s/!//; | |
a6517ebf | 416 | |
99870f4d KW |
417 | if ($utf8::why_deprecated{$file}) { |
418 | warnings::warnif('deprecated', "Use of '$type' in \\p{} or \\P{} is deprecated because: $utf8::why_deprecated{$file};"); | |
419 | } | |
4ff124d1 KW |
420 | |
421 | if ($caseless | |
422 | && exists $utf8::caseless_equivalent{$property_and_table}) | |
423 | { | |
424 | $file = $utf8::caseless_equivalent{$property_and_table}; | |
425 | } | |
eb092534 KW |
426 | |
427 | # The pseudo-directory '#' means that there really isn't a | |
428 | # file to read, the data is in-line as part of the string; | |
429 | # we extract it below. | |
430 | $file = "$unicore_dir/lib/$file.pl" unless $file =~ m!^#/!; | |
99870f4d KW |
431 | last GETFILE; |
432 | } | |
433 | print STDERR __LINE__, ": didn't find $property_and_table\n" if DEBUG; | |
434 | ||
435 | ## | |
99870f4d KW |
436 | ## Last attempt -- see if it's a standard "To" name |
437 | ## (e.g. "ToLower") ToTitle is used by ucfirst(). | |
438 | ## The user-level way to access ToDigit() and ToFold() | |
439 | ## is to use Unicode::UCD. | |
440 | ## | |
cf2cd801 | 441 | # Only check if caller wants non-binary |
6aec79bd KW |
442 | my $retried = 0; |
443 | if ($minbits != 1 && $property_and_table =~ s/^to//) {{ | |
cf2cd801 KW |
444 | # Look input up in list of properties for which we have |
445 | # mapping files. | |
446 | if (defined ($file = | |
447 | $utf8::loose_property_to_file_of{$property_and_table})) | |
448 | { | |
449 | $type = $utf8::file_to_swash_name{$file}; | |
450 | print STDERR __LINE__, ": type set to $type\n" if DEBUG; | |
451 | $file = "$unicore_dir/$file.pl"; | |
452 | last GETFILE; | |
6aec79bd KW |
453 | } # If that fails see if there is a corresponding binary |
454 | # property file | |
455 | elsif (defined ($file = | |
456 | $utf8::loose_to_file_of{$property_and_table})) | |
457 | { | |
458 | ||
459 | # Here, there is no map file for the property we are | |
460 | # trying to get the map of, but this is a binary | |
461 | # property, and there is a file for it that can easily | |
462 | # be translated to a mapping. | |
463 | ||
464 | # In the case of properties that are forced to binary, | |
465 | # they are a combination. We return the actual | |
466 | # mapping instead of the binary. If the input is | |
467 | # something like 'Tocjkkiicore', it will be found in | |
468 | # %loose_property_to_file_of above as => 'To/kIICore'. | |
469 | # But the form like ToIskiicore won't be. To fix | |
470 | # this, it was easiest to do it here. These | |
471 | # properties are the complements of the default | |
472 | # property, so there is an entry in %loose_to_file_of | |
473 | # that is 'iskiicore' => '!kIICore/N', If we find such | |
474 | # an entry, strip off things and try again, which | |
475 | # should find the entry in %loose_property_to_file_of. | |
476 | # Actual binary properties that are of this form, such | |
477 | # as this entry: 'ishrkt' => '!Perl/Any' will also be | |
478 | # retried, but won't be in %loose_property_to_file_of, | |
479 | # and instead the next time through, it will find | |
480 | # 'hrkt' => '!Perl/Any' and proceed. | |
481 | redo if ! $retried | |
482 | && $file =~ /^!/ | |
483 | && $property_and_table =~ s/^is//; | |
484 | ||
485 | # This is a binary property. Setting this here causes | |
486 | # it to be stored as such in the cache, so if someone | |
487 | # comes along later looking for just a binary, they | |
488 | # get it. | |
489 | $minbits = 1; | |
490 | ||
4a7e937e | 491 | # The 0+ makes sure is numeric |
eb092534 KW |
492 | $invert_it = 0 + $file =~ s/!//; |
493 | $file = "$unicore_dir/lib/$file.pl" unless $file =~ m!^#/!; | |
6aec79bd | 494 | last GETFILE; |
cf2cd801 | 495 | } |
6aec79bd | 496 | } } |
99870f4d KW |
497 | |
498 | ## | |
499 | ## If we reach this line, it's because we couldn't figure | |
500 | ## out what to do with $type. Ouch. | |
501 | ## | |
502 | ||
3dd10fe8 | 503 | pop @recursed if @recursed; |
99870f4d | 504 | return $type; |
c9db9632 | 505 | } # end of GETFILE block |
cf25bb62 | 506 | |
99870f4d KW |
507 | if (defined $file) { |
508 | print STDERR __LINE__, ": found it (file='$file')\n" if DEBUG; | |
509 | ||
510 | ## | |
511 | ## If we reach here, it was due to a 'last GETFILE' above | |
512 | ## (exception: user-defined properties and mappings), so we | |
513 | ## have a filename, so now we load it if we haven't already. | |
eb092534 KW |
514 | |
515 | # The pseudo-directory '#' means the result isn't really a | |
516 | # file, but is in-line, with semi-colons to be turned into | |
517 | # new-lines. Since it is in-line there is no advantage to | |
518 | # caching the result | |
519 | if ($file =~ s!^#/!!) { | |
3854b4b8 | 520 | $list = $utf8::inline_definitions[$file]; |
eb092534 KW |
521 | } |
522 | else { | |
523 | # Here, we have an actual file to read in and load, but it | |
524 | # may already have been read-in and cached. The cache key | |
525 | # is the class and file to load, and whether the results | |
526 | # need to be inverted. | |
1f9f7d4c KW |
527 | my $found = $Cache{$class, $file, $invert_it}; |
528 | if ($found and ref($found) eq $class) { | |
529 | print STDERR __LINE__, ": Returning cached swash for '$class,$file,$invert_it' for \\p{$type}\n" if DEBUG; | |
530 | pop @recursed if @recursed; | |
531 | return $found; | |
532 | } | |
99870f4d | 533 | |
1f9f7d4c KW |
534 | local $@; |
535 | local $!; | |
536 | $list = do $file; die $@ if $@; | |
eb092534 KW |
537 | } |
538 | ||
7b953548 | 539 | $list_is_from_mktables = 1; |
0a0ffbce | 540 | } |
c9db9632 KW |
541 | } # End of $type is non-null |
542 | ||
543 | # Here, either $type was null, or we found the requested property and | |
544 | # read it into $list | |
cf25bb62 | 545 | |
7b953548 | 546 | my $extras = ""; |
c9db9632 | 547 | |
99870f4d KW |
548 | my $bits = $minbits; |
549 | ||
7b953548 KW |
550 | # mktables lists don't have extras, like '&utf8::prop', so don't need |
551 | # to separate them; also lists are already sorted, so don't need to do | |
552 | # that. | |
553 | if ($list && ! $list_is_from_mktables) { | |
0e9be77f | 554 | my $taint = substr($list,0,0); # maintain taint |
9b225781 | 555 | |
cb6d3474 KW |
556 | # Separate the extras from the code point list, and make sure |
557 | # user-defined properties and tr/// are well-behaved for | |
558 | # downstream code. | |
559 | if ($user_defined || $none) { | |
9b225781 KW |
560 | my @tmp = split(/^/m, $list); |
561 | my %seen; | |
562 | no warnings; | |
563 | ||
564 | # The extras are anything that doesn't begin with a hex digit. | |
565 | $extras = join '', $taint, grep /^[^0-9a-fA-F]/, @tmp; | |
566 | ||
567 | # Remove the extras, and sort the remaining entries by the | |
568 | # numeric value of their beginning hex digits, removing any | |
569 | # duplicates. | |
570 | $list = join '', $taint, | |
571 | map { $_->[1] } | |
572 | sort { $a->[0] <=> $b->[0] } | |
573 | map { /^([0-9a-fA-F]+)/; [ CORE::hex($1), $_ ] } | |
574 | grep { /^([0-9a-fA-F]+)/ and not $seen{$1}++ } @tmp; # XXX doesn't do ranges right | |
4de6d205 KW |
575 | } |
576 | else { | |
577 | # mktables has gone to some trouble to make non-user defined | |
578 | # properties well-behaved, so we can skip the effort we do for | |
579 | # user-defined ones. Any extras are at the very beginning of | |
580 | # the string. | |
581 | ||
582 | # This regex splits out the first lines of $list into $1 and | |
583 | # strips them off from $list, until we get one that begins | |
584 | # with a hex number, alone on the line, or followed by a tab. | |
585 | # Either portion may be empty. | |
586 | $list =~ s/ \A ( .*? ) | |
587 | (?: \z | (?= ^ [0-9a-fA-F]+ (?: \t | $) ) ) | |
588 | //msx; | |
589 | ||
590 | $extras = "$taint$1"; | |
591 | } | |
99870f4d | 592 | } |
a0ed51b3 | 593 | |
99870f4d KW |
594 | if ($none) { |
595 | my $hextra = sprintf "%04x", $none + 1; | |
596 | $list =~ s/\tXXXX$/\t$hextra/mg; | |
597 | } | |
a0ed51b3 | 598 | |
99870f4d KW |
599 | if ($minbits != 1 && $minbits < 32) { # not binary property |
600 | my $top = 0; | |
601 | while ($list =~ /^([0-9a-fA-F]+)(?:[\t]([0-9a-fA-F]+)?)(?:[ \t]([0-9a-fA-F]+))?/mg) { | |
602 | my $min = CORE::hex $1; | |
603 | my $max = defined $2 ? CORE::hex $2 : $min; | |
604 | my $val = defined $3 ? CORE::hex $3 : 0; | |
605 | $val += $max - $min if defined $3; | |
606 | $top = $val if $val > $top; | |
607 | } | |
608 | my $topbits = | |
609 | $top > 0xffff ? 32 : | |
610 | $top > 0xff ? 16 : 8; | |
611 | $bits = $topbits if $bits < $topbits; | |
612 | } | |
a0ed51b3 | 613 | |
99870f4d KW |
614 | my @extras; |
615 | if ($extras) { | |
616 | for my $x ($extras) { | |
0e9be77f | 617 | my $taint = substr($x,0,0); # maintain taint |
99870f4d KW |
618 | pos $x = 0; |
619 | while ($x =~ /^([^0-9a-fA-F\n])(.*)/mg) { | |
0e9be77f DM |
620 | my $char = "$1$taint"; |
621 | my $name = "$2$taint"; | |
622 | print STDERR __LINE__, ": char [$char] => name [$name]\n" | |
623 | if DEBUG; | |
99870f4d KW |
624 | if ($char =~ /[-+!&]/) { |
625 | my ($c,$t) = split(/::/, $name, 2); # bogus use of ::, really | |
626 | my $subobj; | |
627 | if ($c eq 'utf8') { | |
628 | $subobj = utf8->SWASHNEW($t, "", $minbits, 0); | |
629 | } | |
630 | elsif (exists &$name) { | |
631 | $subobj = utf8->SWASHNEW($name, "", $minbits, 0); | |
632 | } | |
633 | elsif ($c =~ /^([0-9a-fA-F]+)/) { | |
634 | $subobj = utf8->SWASHNEW("", $c, $minbits, 0); | |
635 | } | |
25df5e40 | 636 | print STDERR __LINE__, ": returned from getting sub object for $name\n" if DEBUG; |
3dd10fe8 KW |
637 | if (! ref $subobj) { |
638 | pop @recursed if @recursed && $type; | |
639 | return $subobj; | |
640 | } | |
99870f4d KW |
641 | push @extras, $name => $subobj; |
642 | $bits = $subobj->{BITS} if $bits < $subobj->{BITS}; | |
04e9cbbb KW |
643 | $user_defined = $subobj->{USER_DEFINED} |
644 | if $subobj->{USER_DEFINED}; | |
99870f4d KW |
645 | } |
646 | } | |
647 | } | |
648 | } | |
a0ed51b3 | 649 | |
99870f4d | 650 | if (DEBUG) { |
0fada5e1 | 651 | print STDERR __LINE__, ": CLASS = $class, TYPE => $type, BITS => $bits, NONE => $none, INVERT_IT => $invert_it, USER_DEFINED => $user_defined"; |
99870f4d KW |
652 | print STDERR "\nLIST =>\n$list" if defined $list; |
653 | print STDERR "\nEXTRAS =>\n$extras" if defined $extras; | |
654 | print STDERR "\n"; | |
655 | } | |
a0ed51b3 | 656 | |
99870f4d KW |
657 | my $SWASH = bless { |
658 | TYPE => $type, | |
659 | BITS => $bits, | |
660 | EXTRAS => $extras, | |
661 | LIST => $list, | |
662 | NONE => $none, | |
0fada5e1 | 663 | USER_DEFINED => $user_defined, |
99870f4d KW |
664 | @extras, |
665 | } => $class; | |
666 | ||
667 | if ($file) { | |
4a7e937e | 668 | $Cache{$class, $file, $invert_it} = $SWASH; |
64870e05 KW |
669 | if ($type |
670 | && exists $utf8::SwashInfo{$type} | |
671 | && exists $utf8::SwashInfo{$type}{'specials_name'}) | |
672 | { | |
673 | my $specials_name = $utf8::SwashInfo{$type}{'specials_name'}; | |
674 | no strict "refs"; | |
69b36ee0 | 675 | print STDERR "\nspecials_name => $specials_name\n" if DEBUG; |
64870e05 KW |
676 | $SWASH->{'SPECIALS'} = \%$specials_name; |
677 | } | |
a6517ebf | 678 | $SWASH->{'INVERT_IT'} = $invert_it; |
99870f4d | 679 | } |
5beb625e | 680 | |
3dd10fe8 KW |
681 | pop @recursed if @recursed && $type; |
682 | ||
99870f4d | 683 | return $SWASH; |
5beb625e | 684 | } |
a0ed51b3 LW |
685 | } |
686 | ||
b0e3252e | 687 | # Now SWASHGET is recasted into a C function S_swatch_get (see utf8.c). |
a0ed51b3 LW |
688 | |
689 | 1; |