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