Commit | Line | Data |
00a8df5c |
1 | ## !!!!!!!!!!!!!! IF YOU MODIFY THIS FILE !!!!!!!!!!!!!!!!!!!!!!!!! |
2 | ## Any files created or read by this program should be listed in 'mktables.lst' |
3 | |
d73e5302 |
4 | #!/usr/bin/perl -w |
4392c1cd |
5 | require 5.008; # Needs pack "U". Probably safest to run on 5.8.x |
d73e5302 |
6 | use strict; |
cf25bb62 |
7 | use Carp; |
d07a55ed |
8 | use File::Spec; |
5beb625e |
9 | |
cf25bb62 |
10 | ## |
11 | ## mktables -- create the runtime Perl Unicode files (lib/unicore/**/*.pl) |
12 | ## from the Unicode database files (lib/unicore/*.txt). |
13 | ## |
d2d499f5 |
14 | |
cb3d361a |
15 | ## "Fuzzy" means this section in Unicode TR18: |
16 | ## |
17 | ## The recommended names for UCD properties and property values are in |
18 | ## PropertyAliases.txt [Prop] and PropertyValueAliases.txt |
19 | ## [PropValue]. There are both abbreviated names and longer, more |
20 | ## descriptive names. It is strongly recommended that both names be |
21 | ## recognized, and that loose matching of property names be used, |
22 | ## whereby the case distinctions, whitespace, hyphens, and underbar |
23 | ## are ignored. |
24 | |
8e4b7420 |
25 | ## Base names already used in lib/gc_sc (for avoiding 8.3 conflicts) |
26 | my %BaseNames; |
27 | |
cf25bb62 |
28 | ## |
29 | ## Process any args. |
30 | ## |
5beb625e |
31 | my $Verbose = 0; |
32 | my $MakeTestScript = 0; |
00da7942 |
33 | my $AlwaysWrite = 0; |
02b1aeec |
34 | my $UseDir = ""; |
35 | my $FileList = "$0.lst"; |
cf25bb62 |
36 | |
37 | while (@ARGV) |
38 | { |
39 | my $arg = shift @ARGV; |
40 | if ($arg eq '-v') { |
41 | $Verbose = 1; |
42 | } elsif ($arg eq '-q') { |
43 | $Verbose = 0; |
00da7942 |
44 | } elsif ($arg eq '-w') { |
45 | $AlwaysWrite = 1; # update the files even if they havent changed |
02b1aeec |
46 | $FileList = ""; |
5beb625e |
47 | } elsif ($arg eq '-maketest') { |
48 | $MakeTestScript = 1; |
02b1aeec |
49 | } elsif ($arg eq '-C' && defined ($UseDir = shift)) { |
50 | -d $UseDir or die "Unknown directory '$UseDir'"; |
51 | } elsif ($arg eq '-L' && defined ($FileList = shift)) { |
52 | -e $FileList or die "Filelist '$FileList' doesn't appear to exist!"; |
d73e5302 |
53 | } else { |
02b1aeec |
54 | die "usage: $0 [-v|-q|-C dir|-L filelist] [-maketest]\n"; |
d73e5302 |
55 | } |
56 | } |
57 | |
02b1aeec |
58 | if ($FileList) { |
59 | print "Reading file list '$FileList'\n" |
00a8df5c |
60 | if $Verbose; |
02b1aeec |
61 | open my $fh,"<",$FileList or die "Failed to read '$FileList':$!"; |
62 | my @input; |
00a8df5c |
63 | my @output; |
64 | for my $list ( \@input, \@output ) { |
65 | while (<$fh>) { |
66 | s/^ \s+ | \s+ $//xg; |
67 | next if /^ \s* (?: \# .* )? $/x; |
68 | last if /^ =+ $/x; |
69 | my ( $file ) = split /\t/, $_; |
70 | push @$list, $file; |
71 | } |
72 | my %dupe; |
73 | @$list = grep !$dupe{ $_ }++, @$list; |
74 | } |
75 | close $fh; |
02b1aeec |
76 | die "No input or output files in '$FileList'!" |
00a8df5c |
77 | if !@input or !@output; |
78 | if ( $Verbose ) { |
79 | print "Expecting ".scalar( @input )." input files. ", |
80 | "Checking ".scalar( @output )." output files.\n"; |
81 | } |
02b1aeec |
82 | # we set maxtime to be the youngest input file, including $0 itself. |
83 | my $maxtime = -M $0; # do this before the chdir! |
84 | if ($UseDir) { |
85 | chdir $UseDir or die "Failed to chdir to '$UseDir':$!"; |
86 | } |
00a8df5c |
87 | foreach my $in (@input) { |
88 | my $time = -M $in; |
89 | die "Missing input file '$in'" unless defined $time; |
90 | $maxtime = $time if $maxtime < $time; |
91 | } |
92 | |
02b1aeec |
93 | # now we check to see if any output files are older than maxtime, if |
94 | # they are we need to continue on, otherwise we can presumably bail. |
00a8df5c |
95 | my $ok = 1; |
96 | foreach my $out (@output) { |
97 | if ( ! -e $out ) { |
98 | print "'$out' is missing.\n" |
99 | if $Verbose; |
100 | $ok = 0; |
101 | last; |
102 | } |
103 | if ( -M $out > $maxtime ) { |
104 | print "'$out' is too old.\n" |
105 | if $Verbose; |
106 | $ok = 0; |
107 | last; |
108 | } |
109 | } |
110 | if ($ok) { |
111 | print "Files seem to be ok, not bothering to rebuild.\n"; |
112 | exit(0); |
113 | } |
114 | print "Must rebuild tables.\n" |
115 | if $Verbose; |
02b1aeec |
116 | } else { |
117 | if ($Verbose) { |
118 | print "Not checking filelist.\n"; |
119 | } |
120 | if ($UseDir) { |
121 | chdir $UseDir or die "Failed to chdir to '$UseDir':$!"; |
122 | } |
00a8df5c |
123 | } |
124 | |
ca12659b |
125 | foreach my $lib ('To', 'lib', |
126 | map {File::Spec->catdir("lib",$_)} |
127 | qw(gc_sc dt bc hst ea jt lb nt ccc)) { |
128 | next if -d $lib; |
129 | mkdir $lib, 0755 or die "mkdir '$lib': $!"; |
130 | } |
131 | |
cf25bb62 |
132 | my $LastUnicodeCodepoint = 0x10FFFF; # As of Unicode 3.1.1. |
d73e5302 |
133 | |
cf25bb62 |
134 | my $HEADER=<<"EOF"; |
d73e5302 |
135 | # !!!!!!! DO NOT EDIT THIS FILE !!!!!!! |
551b6b6f |
136 | # This file is built by $0 from e.g. UnicodeData.txt. |
d73e5302 |
137 | # Any changes made here will be lost! |
cf25bb62 |
138 | |
139 | EOF |
140 | |
d07a55ed |
141 | sub force_unlink { |
142 | my $filename = shift; |
143 | return unless -e $filename; |
144 | return if CORE::unlink($filename); |
145 | # We might need write permission |
146 | chmod 0777, $filename; |
147 | CORE::unlink($filename) or die "Couldn't unlink $filename: $!\n"; |
148 | } |
5beb625e |
149 | |
150 | ## |
151 | ## Given a filename and a reference to an array of lines, |
152 | ## write the lines to the file only if the contents have not changed. |
d07a55ed |
153 | ## Filename can be given as an arrayref of directory names |
5beb625e |
154 | ## |
155 | sub WriteIfChanged($\@) |
156 | { |
157 | my $file = shift; |
158 | my $lines = shift; |
159 | |
d07a55ed |
160 | $file = File::Spec->catfile(@$file) if ref $file; |
161 | |
5beb625e |
162 | my $TextToWrite = join '', @$lines; |
163 | if (open IN, $file) { |
164 | local($/) = undef; |
165 | my $PreviousText = <IN>; |
166 | close IN; |
167 | if ($PreviousText eq $TextToWrite) { |
168 | print "$file unchanged.\n" if $Verbose; |
00da7942 |
169 | return unless $AlwaysWrite; |
5beb625e |
170 | } |
171 | } |
d07a55ed |
172 | force_unlink ($file); |
5beb625e |
173 | if (not open OUT, ">$file") { |
174 | die "$0: can't open $file for output: $!\n"; |
175 | } |
176 | print "$file written.\n" if $Verbose; |
177 | |
178 | print OUT $TextToWrite; |
179 | close OUT; |
180 | } |
181 | |
cf25bb62 |
182 | ## |
183 | ## The main datastructure (a "Table") represents a set of code points that |
184 | ## are part of a particular quality (that are part of \pL, \p{InGreek}, |
185 | ## etc.). They are kept as ranges of code points (starting and ending of |
186 | ## each range). |
187 | ## |
188 | ## For example, a range ASCII LETTERS would be represented as: |
189 | ## [ [ 0x41 => 0x5A, 'UPPER' ], |
190 | ## [ 0x61 => 0x7A, 'LOWER, ] ] |
191 | ## |
192 | sub RANGE_START() { 0 } ## index into range element |
193 | sub RANGE_END() { 1 } ## index into range element |
194 | sub RANGE_NAME() { 2 } ## index into range element |
195 | |
99598c8c |
196 | ## Conceptually, these should really be folded into the 'Table' objects |
cf25bb62 |
197 | my %TableInfo; |
99598c8c |
198 | my %TableDesc; |
cf25bb62 |
199 | my %FuzzyNames; |
200 | my %AliasInfo; |
5beb625e |
201 | my %CanonicalToOrig; |
cf25bb62 |
202 | |
203 | ## |
99598c8c |
204 | ## Turn something like |
205 | ## OLD-ITALIC |
5beb625e |
206 | ## into |
99598c8c |
207 | ## OldItalic |
208 | ## |
209 | sub CanonicalName($) |
210 | { |
5beb625e |
211 | my $orig = shift; |
212 | my $name = lc $orig; |
99598c8c |
213 | $name =~ s/(?<![a-z])(\w)/\u$1/g; |
5beb625e |
214 | $name =~ s/[-_\s]+//g; |
99598c8c |
215 | |
5beb625e |
216 | $CanonicalToOrig{$name} = $orig if not $CanonicalToOrig{$name}; |
99598c8c |
217 | return $name; |
218 | } |
219 | |
12ac2576 |
220 | |
221 | ## |
222 | ## Store the alias definitions for later use. |
223 | ## |
224 | my %PropertyAlias; |
225 | my %PropValueAlias; |
226 | |
227 | my %PA_reverse; |
228 | my %PVA_reverse; |
229 | |
230 | sub Build_Aliases() |
231 | { |
232 | ## |
233 | ## Most of the work with aliases doesn't occur here, |
7ebf06b3 |
234 | ## but rather in utf8_heavy.pl, which uses PVA.pl, |
235 | |
236 | # Placate the warnings about used only once. (They are used again, but |
237 | # via a typeglob lookup) |
238 | %utf8::PropertyAlias = (); |
239 | %utf8::PA_reverse = (); |
240 | %utf8::PropValueAlias = (); |
241 | %utf8::PVA_reverse = (); |
242 | %utf8::PVA_abbr_map = (); |
12ac2576 |
243 | |
244 | open PA, "< PropertyAliases.txt" |
245 | or confess "Can't open PropertyAliases.txt: $!"; |
246 | while (<PA>) { |
247 | s/#.*//; |
248 | s/\s+$//; |
249 | next if /^$/; |
250 | |
251 | my ($abbrev, $name) = split /\s*;\s*/; |
252 | next if $abbrev eq "n/a"; |
253 | $PropertyAlias{$abbrev} = $name; |
254 | $PA_reverse{$name} = $abbrev; |
7ebf06b3 |
255 | |
256 | # The %utf8::... versions use japhy's code originally from utf8_pva.pl |
257 | # However, it's moved here so that we build the tables at runtime. |
258 | tr/ _-//d for $abbrev, $name; |
259 | $utf8::PropertyAlias{lc $abbrev} = $name; |
260 | $utf8::PA_reverse{lc $name} = $abbrev; |
12ac2576 |
261 | } |
262 | close PA; |
263 | |
264 | open PVA, "< PropValueAliases.txt" |
265 | or confess "Can't open PropValueAliases.txt: $!"; |
266 | while (<PVA>) { |
267 | s/#.*//; |
268 | s/\s+$//; |
269 | next if /^$/; |
270 | |
271 | my ($prop, @data) = split /\s*;\s*/; |
272 | |
273 | if ($prop eq 'ccc') { |
274 | $PropValueAlias{$prop}{$data[1]} = [ @data[0,2] ]; |
275 | $PVA_reverse{$prop}{$data[2]} = [ @data[0,1] ]; |
276 | } |
277 | else { |
278 | next if $data[0] eq "n/a"; |
279 | $PropValueAlias{$prop}{$data[0]} = $data[1]; |
280 | $PVA_reverse{$prop}{$data[1]} = $data[0]; |
281 | } |
7ebf06b3 |
282 | |
283 | shift @data if $prop eq 'ccc'; |
284 | next if $data[0] eq "n/a"; |
285 | |
286 | $data[1] =~ tr/ _-//d; |
287 | $utf8::PropValueAlias{$prop}{lc $data[0]} = $data[1]; |
288 | $utf8::PVA_reverse{$prop}{lc $data[1]} = $data[0]; |
289 | |
290 | my $abbr_class = ($prop eq 'gc' or $prop eq 'sc') ? 'gc_sc' : $prop; |
291 | $utf8::PVA_abbr_map{$abbr_class}{lc $data[0]} = $data[0]; |
12ac2576 |
292 | } |
293 | close PVA; |
7ebf06b3 |
294 | |
295 | # backwards compatibility for L& -> LC |
296 | $utf8::PropValueAlias{gc}{'l&'} = $utf8::PropValueAlias{gc}{lc}; |
297 | $utf8::PVA_abbr_map{gc_sc}{'l&'} = $utf8::PVA_abbr_map{gc_sc}{lc}; |
298 | |
12ac2576 |
299 | } |
300 | |
301 | |
99598c8c |
302 | ## |
cf25bb62 |
303 | ## Associates a property ("Greek", "Lu", "Assigned",...) with a Table. |
304 | ## |
305 | ## Called like: |
99598c8c |
306 | ## New_Prop(In => 'Greek', $Table, Desc => 'Greek Block', Fuzzy => 1); |
cf25bb62 |
307 | ## |
308 | ## Normally, these parameters are set when the Table is created (when the |
309 | ## Table->New constructor is called), but there are times when it needs to |
310 | ## be done after-the-fact...) |
311 | ## |
312 | sub New_Prop($$$@) |
313 | { |
314 | my $Type = shift; ## "Is" or "In"; |
315 | my $Name = shift; |
316 | my $Table = shift; |
317 | |
318 | ## remaining args are optional key/val |
319 | my %Args = @_; |
320 | |
99598c8c |
321 | my $Fuzzy = delete $Args{Fuzzy}; |
322 | my $Desc = delete $Args{Desc}; # description |
323 | |
5beb625e |
324 | $Name = CanonicalName($Name) if $Fuzzy; |
cf25bb62 |
325 | |
326 | ## sanity check a few args |
327 | if (%Args or ($Type ne 'Is' and $Type ne 'In') or not ref $Table) { |
328 | confess "$0: bad args to New_Prop" |
329 | } |
330 | |
331 | if (not $TableInfo{$Type}->{$Name}) |
332 | { |
333 | $TableInfo{$Type}->{$Name} = $Table; |
99598c8c |
334 | $TableDesc{$Type}->{$Name} = $Desc; |
335 | if ($Fuzzy) { |
cf25bb62 |
336 | $FuzzyNames{$Type}->{$Name} = $Name; |
337 | } |
338 | } |
d73e5302 |
339 | } |
340 | |
d73e5302 |
341 | |
cf25bb62 |
342 | ## |
343 | ## Creates a new Table object. |
344 | ## |
345 | ## Args are key/value pairs: |
99598c8c |
346 | ## In => Name -- Name of "In" property to be associated with |
347 | ## Is => Name -- Name of "Is" property to be associated with |
348 | ## Fuzzy => Boolean -- True if name can be accessed "fuzzily" |
349 | ## Desc => String -- Description of the property |
cf25bb62 |
350 | ## |
351 | ## No args are required. |
352 | ## |
353 | sub Table::New |
354 | { |
355 | my $class = shift; |
356 | my %Args = @_; |
357 | |
358 | my $Table = bless [], $class; |
359 | |
99598c8c |
360 | my $Fuzzy = delete $Args{Fuzzy}; |
361 | my $Desc = delete $Args{Desc}; |
cf25bb62 |
362 | |
363 | for my $Type ('Is', 'In') |
364 | { |
365 | if (my $Name = delete $Args{$Type}) { |
99598c8c |
366 | New_Prop($Type => $Name, $Table, Desc => $Desc, Fuzzy => $Fuzzy); |
cf25bb62 |
367 | } |
368 | } |
369 | |
370 | ## shouldn't have any left over |
371 | if (%Args) { |
372 | confess "$0: bad args to Table->New" |
373 | } |
374 | |
375 | return $Table; |
d73e5302 |
376 | } |
377 | |
cf25bb62 |
378 | ## |
379 | ## Returns true if the Table has no code points |
380 | ## |
381 | sub Table::IsEmpty |
382 | { |
383 | my $Table = shift; #self |
384 | return not @$Table; |
d73e5302 |
385 | } |
386 | |
cf25bb62 |
387 | ## |
388 | ## Returns true if the Table has code points |
389 | ## |
390 | sub Table::NotEmpty |
391 | { |
392 | my $Table = shift; #self |
393 | return @$Table; |
d73e5302 |
394 | } |
395 | |
cf25bb62 |
396 | ## |
397 | ## Returns the maximum code point currently in the table. |
398 | ## |
399 | sub Table::Max |
400 | { |
401 | my $Table = shift; #self |
402 | confess "oops" if $Table->IsEmpty; ## must have code points to have a max |
403 | return $Table->[-1]->[RANGE_END]; |
404 | } |
d73e5302 |
405 | |
cf25bb62 |
406 | ## |
407 | ## Replaces the codepoints in the Table with those in the Table given |
408 | ## as an arg. (NOTE: this is not a "deep copy"). |
409 | ## |
410 | sub Table::Replace($$) |
411 | { |
412 | my $Table = shift; #self |
413 | my $New = shift; |
d73e5302 |
414 | |
cf25bb62 |
415 | @$Table = @$New; |
416 | } |
71d929cb |
417 | |
cf25bb62 |
418 | ## |
419 | ## Given a new code point, make the last range of the Table extend to |
420 | ## include the new (and all intervening) code points. |
421 | ## |
422 | sub Table::Extend |
423 | { |
424 | my $Table = shift; #self |
425 | my $codepoint = shift; |
d73e5302 |
426 | |
cf25bb62 |
427 | my $PrevMax = $Table->Max; |
e904f995 |
428 | |
cf25bb62 |
429 | confess "oops ($codepoint <= $PrevMax)" if $codepoint <= $PrevMax; |
e904f995 |
430 | |
cf25bb62 |
431 | $Table->[-1]->[RANGE_END] = $codepoint; |
432 | } |
c3a8a2b8 |
433 | |
cf25bb62 |
434 | ## |
435 | ## Given a code point range start and end (and optional name), blindly |
436 | ## append them to the list of ranges for the Table. |
437 | ## |
438 | ## NOTE: Code points must be added in strictly ascending numeric order. |
439 | ## |
440 | sub Table::RawAppendRange |
441 | { |
442 | my $Table = shift; #self |
443 | my $start = shift; |
444 | my $end = shift; |
445 | my $name = shift; |
446 | $name = "" if not defined $name; ## warning: $name can be "0" |
447 | |
448 | push @$Table, [ $start, # RANGE_START |
449 | $end, # RANGE_END |
450 | $name ]; # RANGE_NAME |
451 | } |
c3a8a2b8 |
452 | |
cf25bb62 |
453 | ## |
454 | ## Given a code point (and optional name), add it to the Table. |
455 | ## |
456 | ## NOTE: Code points must be added in strictly ascending numeric order. |
457 | ## |
458 | sub Table::Append |
459 | { |
460 | my $Table = shift; #self |
461 | my $codepoint = shift; |
462 | my $name = shift; |
463 | $name = "" if not defined $name; ## warning: $name can be "0" |
464 | |
465 | ## |
466 | ## If we've already got a range working, and this code point is the next |
467 | ## one in line, and if the name is the same, just extend the current range. |
468 | ## |
469 | if ($Table->NotEmpty |
470 | and |
471 | $Table->Max == $codepoint - 1 |
472 | and |
473 | $Table->[-1]->[RANGE_NAME] eq $name) |
474 | { |
475 | $Table->Extend($codepoint); |
c3a8a2b8 |
476 | } |
cf25bb62 |
477 | else |
478 | { |
479 | $Table->RawAppendRange($codepoint, $codepoint, $name); |
d73e5302 |
480 | } |
cf25bb62 |
481 | } |
d73e5302 |
482 | |
cf25bb62 |
483 | ## |
484 | ## Given a code point range starting value and ending value (and name), |
485 | ## Add the range to teh Table. |
486 | ## |
487 | ## NOTE: Code points must be added in strictly ascending numeric order. |
488 | ## |
489 | sub Table::AppendRange |
490 | { |
491 | my $Table = shift; #self |
492 | my $start = shift; |
493 | my $end = shift; |
494 | my $name = shift; |
495 | $name = "" if not defined $name; ## warning: $name can be "0" |
496 | |
497 | $Table->Append($start, $name); |
498 | $Table->Extend($end) if $end > $start; |
499 | } |
d73e5302 |
500 | |
cf25bb62 |
501 | ## |
502 | ## Return a new Table that represents all code points not in the Table. |
503 | ## |
504 | sub Table::Invert |
505 | { |
506 | my $Table = shift; #self |
507 | |
508 | my $New = Table->New(); |
509 | my $max = -1; |
510 | for my $range (@$Table) |
511 | { |
512 | my $start = $range->[RANGE_START]; |
513 | my $end = $range->[RANGE_END]; |
514 | if ($start-1 >= $max+1) { |
515 | $New->AppendRange($max+1, $start-1, ""); |
516 | } |
517 | $max = $end; |
d73e5302 |
518 | } |
cf25bb62 |
519 | if ($max+1 < $LastUnicodeCodepoint) { |
520 | $New->AppendRange($max+1, $LastUnicodeCodepoint); |
d73e5302 |
521 | } |
cf25bb62 |
522 | return $New; |
523 | } |
d73e5302 |
524 | |
cf25bb62 |
525 | ## |
526 | ## Merges any number of other tables with $self, returning the new table. |
527 | ## (existing tables are not modified) |
528 | ## |
a3a8c5f0 |
529 | ## |
530 | ## Args may be Tables, or individual code points (as integers). |
531 | ## |
cf25bb62 |
532 | ## Can be called as either a constructor or a method. |
533 | ## |
534 | sub Table::Merge |
535 | { |
536 | shift(@_) if not ref $_[0]; ## if called as a constructor, lose the class |
537 | my @Tables = @_; |
538 | |
539 | ## Accumulate all records from all tables |
540 | my @Records; |
a3a8c5f0 |
541 | for my $Arg (@Tables) |
542 | { |
543 | if (ref $Arg) { |
544 | ## arg is a table -- get its ranges |
545 | push @Records, @$Arg; |
546 | } else { |
547 | ## arg is a codepoint, make a range |
548 | push @Records, [ $Arg, $Arg ] |
549 | } |
d73e5302 |
550 | } |
551 | |
cf25bb62 |
552 | ## sort by range start, with longer ranges coming first. |
553 | my ($first, @Rest) = sort { |
554 | ($a->[RANGE_START] <=> $b->[RANGE_START]) |
555 | or |
556 | ($b->[RANGE_END] <=> $b->[RANGE_END]) |
557 | } @Records; |
558 | |
559 | my $New = Table->New(); |
560 | |
561 | ## Ensuring the first range is there makes the subsequent loop easier |
562 | $New->AppendRange($first->[RANGE_START], |
563 | $first->[RANGE_END]); |
564 | |
565 | ## Fold in records so long as they add new information. |
566 | for my $set (@Rest) |
567 | { |
568 | my $start = $set->[RANGE_START]; |
569 | my $end = $set->[RANGE_END]; |
570 | if ($start > $New->Max) { |
571 | $New->AppendRange($start, $end); |
572 | } elsif ($end > $New->Max) { |
573 | $New->Extend($end); |
574 | } |
d73e5302 |
575 | } |
d73e5302 |
576 | |
cf25bb62 |
577 | return $New; |
d73e5302 |
578 | } |
579 | |
cf25bb62 |
580 | ## |
581 | ## Given a filename, write a representation of the Table to a file. |
99598c8c |
582 | ## May have an optional comment as a 2nd arg. |
d07a55ed |
583 | ## Filename may actually be an arrayref of directories |
cf25bb62 |
584 | ## |
585 | sub Table::Write |
586 | { |
99598c8c |
587 | my $Table = shift; #self |
cf25bb62 |
588 | my $filename = shift; |
99598c8c |
589 | my $comment = shift; |
d73e5302 |
590 | |
5beb625e |
591 | my @OUT = $HEADER; |
99598c8c |
592 | if (defined $comment) { |
593 | $comment =~ s/\s+\Z//; |
594 | $comment =~ s/^/# /gm; |
5beb625e |
595 | push @OUT, "#\n$comment\n#\n"; |
99598c8c |
596 | } |
5beb625e |
597 | push @OUT, "return <<'END';\n"; |
d73e5302 |
598 | |
cf25bb62 |
599 | for my $set (@$Table) |
600 | { |
601 | my $start = $set->[RANGE_START]; |
602 | my $end = $set->[RANGE_END]; |
603 | my $name = $set->[RANGE_NAME]; |
d73e5302 |
604 | |
cf25bb62 |
605 | if ($start == $end) { |
5beb625e |
606 | push @OUT, sprintf "%04X\t\t%s\n", $start, $name; |
cf25bb62 |
607 | } else { |
5beb625e |
608 | push @OUT, sprintf "%04X\t%04X\t%s\n", $start, $end, $name; |
cf25bb62 |
609 | } |
610 | } |
d73e5302 |
611 | |
5beb625e |
612 | push @OUT, "END\n"; |
613 | |
614 | WriteIfChanged($filename, @OUT); |
615 | } |
616 | |
617 | ## This used only for making the test script. |
618 | ## helper function |
619 | sub IsUsable($) |
620 | { |
621 | my $code = shift; |
622 | return 0 if $code <= 0x0000; ## don't use null |
623 | return 0 if $code >= $LastUnicodeCodepoint; ## keep in range |
624 | return 0 if ($code >= 0xD800 and $code <= 0xDFFF); ## no surrogates |
625 | return 0 if ($code >= 0xFDD0 and $code <= 0xFDEF); ## utf8.c says no good |
626 | return 0 if (($code & 0xFFFF) == 0xFFFE); ## utf8.c says no good |
627 | return 0 if (($code & 0xFFFF) == 0xFFFF); ## utf8.c says no good |
628 | return 1; |
629 | } |
630 | |
631 | ## Return a code point that's part of the table. |
632 | ## Returns nothing if the table is empty (or covers only surrogates). |
633 | ## This used only for making the test script. |
634 | sub Table::ValidCode |
635 | { |
636 | my $Table = shift; #self |
637 | for my $set (@$Table) { |
638 | return $set->[RANGE_END] if IsUsable($set->[RANGE_END]); |
639 | } |
640 | return (); |
641 | } |
642 | |
643 | ## Return a code point that's not part of the table |
644 | ## Returns nothing if the table covers all code points. |
645 | ## This used only for making the test script. |
646 | sub Table::InvalidCode |
647 | { |
648 | my $Table = shift; #self |
649 | |
650 | return 0x1234 if $Table->IsEmpty(); |
651 | |
652 | for my $set (@$Table) |
653 | { |
654 | if (IsUsable($set->[RANGE_END] + 1)) |
655 | { |
656 | return $set->[RANGE_END] + 1; |
657 | } |
658 | |
659 | if (IsUsable($set->[RANGE_START] - 1)) |
660 | { |
661 | return $set->[RANGE_START] - 1; |
662 | } |
663 | } |
664 | return (); |
cf25bb62 |
665 | } |
666 | |
667 | ########################################################################### |
668 | ########################################################################### |
669 | ########################################################################### |
670 | |
671 | |
672 | ## |
673 | ## Called like: |
99598c8c |
674 | ## New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 1); |
cf25bb62 |
675 | ## |
99598c8c |
676 | ## The args must be in that order, although the Fuzzy pair may be omitted. |
cf25bb62 |
677 | ## |
678 | ## This creates 'IsAll' as an alias for 'IsAny' |
679 | ## |
680 | sub New_Alias($$$@) |
681 | { |
682 | my $Type = shift; ## "Is" or "In" |
683 | my $Alias = shift; |
99598c8c |
684 | my $SameAs = shift; # expecting "SameAs" -- just ignored |
cf25bb62 |
685 | my $Name = shift; |
686 | |
687 | ## remaining args are optional key/val |
688 | my %Args = @_; |
689 | |
99598c8c |
690 | my $Fuzzy = delete $Args{Fuzzy}; |
cf25bb62 |
691 | |
692 | ## sanity check a few args |
693 | if (%Args or ($Type ne 'Is' and $Type ne 'In') or $SameAs ne 'SameAs') { |
694 | confess "$0: bad args to New_Alias" |
d73e5302 |
695 | } |
696 | |
5beb625e |
697 | $Alias = CanonicalName($Alias) if $Fuzzy; |
698 | |
699 | if (not $TableInfo{$Type}->{$Name}) |
700 | { |
701 | my $CName = CanonicalName($Name); |
702 | if ($TableInfo{$Type}->{$CName}) { |
703 | confess "$0: Use canonical form '$CName' instead of '$Name' for alias."; |
704 | } else { |
12ac2576 |
705 | confess "$0: don't have original $Type => $Name to make alias\n"; |
5beb625e |
706 | } |
cf25bb62 |
707 | } |
708 | if ($TableInfo{$Alias}) { |
709 | confess "$0: already have original $Type => $Alias; can't make alias"; |
d73e5302 |
710 | } |
cf25bb62 |
711 | $AliasInfo{$Type}->{$Name} = $Alias; |
99598c8c |
712 | if ($Fuzzy) { |
cf25bb62 |
713 | $FuzzyNames{$Type}->{$Alias} = $Name; |
714 | } |
715 | |
d73e5302 |
716 | } |
717 | |
d73e5302 |
718 | |
cf25bb62 |
719 | ## All assigned code points |
99598c8c |
720 | my $Assigned = Table->New(Is => 'Assigned', |
721 | Desc => "All assigned code points", |
5beb625e |
722 | Fuzzy => 0); |
d2d499f5 |
723 | |
cf25bb62 |
724 | my $Name = Table->New(); ## all characters, individually by name |
725 | my $General = Table->New(); ## all characters, grouped by category |
726 | my %General; |
727 | my %Cat; |
d73e5302 |
728 | |
7ebf06b3 |
729 | ## Simple Data::Dumper alike. Good enough for our needs. We can't use the real |
730 | ## thing as we have to run under miniperl |
731 | sub simple_dumper { |
732 | my @lines; |
733 | my $item; |
734 | foreach $item (@_) { |
735 | if (ref $item) { |
736 | if (ref $item eq 'ARRAY') { |
737 | push @lines, "[\n", simple_dumper (@$item), "],\n"; |
738 | } elsif (ref $item eq 'HASH') { |
739 | push @lines, "{\n", simple_dumper (%$item), "},\n"; |
740 | } else { |
741 | die "Can't cope with $item"; |
742 | } |
743 | } else { |
744 | if (defined $item) { |
745 | my $copy = $item; |
746 | $copy =~ s/([\'\\])/\\$1/gs; |
747 | push @lines, "'$copy',\n"; |
748 | } else { |
749 | push @lines, "undef,\n"; |
750 | } |
751 | } |
752 | } |
753 | @lines; |
754 | } |
755 | |
cf25bb62 |
756 | ## |
551b6b6f |
757 | ## Process UnicodeData.txt (Categories, etc.) |
cf25bb62 |
758 | ## |
44da8cae |
759 | sub UnicodeData_Txt() |
cf25bb62 |
760 | { |
761 | my $Bidi = Table->New(); |
762 | my $Deco = Table->New(); |
763 | my $Comb = Table->New(); |
764 | my $Number = Table->New(); |
12ac2576 |
765 | my $Mirrored = Table->New();#Is => 'Mirrored', |
766 | #Desc => "Mirrored in bidirectional text", |
767 | #Fuzzy => 0); |
d73e5302 |
768 | |
cf25bb62 |
769 | my %DC; |
770 | my %Bidi; |
12ac2576 |
771 | my %Number; |
772 | $DC{can} = Table->New(); |
773 | $DC{com} = Table->New(); |
cf25bb62 |
774 | |
775 | ## Initialize Perl-generated categories |
551b6b6f |
776 | ## (Categories from UnicodeData.txt are auto-initialized in gencat) |
44da8cae |
777 | $Cat{Alnum} = |
778 | Table->New(Is => 'Alnum', Desc => "[[:Alnum:]]", Fuzzy => 0); |
779 | $Cat{Alpha} = |
780 | Table->New(Is => 'Alpha', Desc => "[[:Alpha:]]", Fuzzy => 0); |
781 | $Cat{ASCII} = |
782 | Table->New(Is => 'ASCII', Desc => "[[:ASCII:]]", Fuzzy => 0); |
783 | $Cat{Blank} = |
784 | Table->New(Is => 'Blank', Desc => "[[:Blank:]]", Fuzzy => 0); |
785 | $Cat{Cntrl} = |
786 | Table->New(Is => 'Cntrl', Desc => "[[:Cntrl:]]", Fuzzy => 0); |
787 | $Cat{Digit} = |
788 | Table->New(Is => 'Digit', Desc => "[[:Digit:]]", Fuzzy => 0); |
789 | $Cat{Graph} = |
790 | Table->New(Is => 'Graph', Desc => "[[:Graph:]]", Fuzzy => 0); |
791 | $Cat{Lower} = |
792 | Table->New(Is => 'Lower', Desc => "[[:Lower:]]", Fuzzy => 0); |
793 | $Cat{Print} = |
794 | Table->New(Is => 'Print', Desc => "[[:Print:]]", Fuzzy => 0); |
795 | $Cat{Punct} = |
796 | Table->New(Is => 'Punct', Desc => "[[:Punct:]]", Fuzzy => 0); |
797 | $Cat{Space} = |
798 | Table->New(Is => 'Space', Desc => "[[:Space:]]", Fuzzy => 0); |
799 | $Cat{Title} = |
800 | Table->New(Is => 'Title', Desc => "[[:Title:]]", Fuzzy => 0); |
801 | $Cat{Upper} = |
802 | Table->New(Is => 'Upper', Desc => "[[:Upper:]]", Fuzzy => 0); |
803 | $Cat{XDigit} = |
804 | Table->New(Is => 'XDigit', Desc => "[[:XDigit:]]", Fuzzy => 0); |
805 | $Cat{Word} = |
806 | Table->New(Is => 'Word', Desc => "[[:Word:]]", Fuzzy => 0); |
807 | $Cat{SpacePerl} = |
808 | Table->New(Is => 'SpacePerl', Desc => '\s', Fuzzy => 0); |
d73e5302 |
809 | |
cf25bb62 |
810 | my %To; |
811 | $To{Upper} = Table->New(); |
812 | $To{Lower} = Table->New(); |
813 | $To{Title} = Table->New(); |
814 | $To{Digit} = Table->New(); |
815 | |
816 | sub gencat($$$$) |
817 | { |
818 | my ($name, ## Name ("LATIN CAPITAL LETTER A") |
819 | $cat, ## Category ("Lu", "Zp", "Nd", etc.) |
820 | $code, ## Code point (as an integer) |
821 | $op) = @_; |
822 | |
823 | my $MajorCat = substr($cat, 0, 1); ## L, M, Z, S, etc |
824 | |
825 | $Assigned->$op($code); |
826 | $Name->$op($code, $name); |
827 | $General->$op($code, $cat); |
828 | |
829 | ## add to the sub category (e.g. "Lu", "Nd", "Cf", ..) |
99598c8c |
830 | $Cat{$cat} ||= Table->New(Is => $cat, |
831 | Desc => "General Category '$cat'", |
832 | Fuzzy => 0); |
cf25bb62 |
833 | $Cat{$cat}->$op($code); |
834 | |
835 | ## add to the major category (e.g. "L", "N", "C", ...) |
99598c8c |
836 | $Cat{$MajorCat} ||= Table->New(Is => $MajorCat, |
837 | Desc => "Major Category '$MajorCat'", |
838 | Fuzzy => 0); |
cf25bb62 |
839 | $Cat{$MajorCat}->$op($code); |
840 | |
841 | ($General{$name} ||= Table->New)->$op($code, $name); |
842 | |
843 | # 005F: SPACING UNDERSCORE |
d75d706f |
844 | $Cat{Word}->$op($code) if $cat =~ /^[LMN]|Pc/; |
c65e4d19 |
845 | $Cat{Alnum}->$op($code) if $cat =~ /^[LM]|Nd/; |
cf25bb62 |
846 | $Cat{Alpha}->$op($code) if $cat =~ /^[LM]/; |
847 | |
d75d706f |
848 | my $isspace = |
849 | ($cat =~ /Zs|Zl|Zp/ && |
850 | $code != 0x200B) # 200B is ZWSP which is for line break control |
851 | # and therefore it is not part of "space" even while it is "Zs". |
cf25bb62 |
852 | || $code == 0x0009 # 0009: HORIZONTAL TAB |
853 | || $code == 0x000A # 000A: LINE FEED |
854 | || $code == 0x000B # 000B: VERTICAL TAB |
855 | || $code == 0x000C # 000C: FORM FEED |
e6f58734 |
856 | || $code == 0x000D # 000D: CARRIAGE RETURN |
d75d706f |
857 | || $code == 0x0085 # 0085: NEL |
858 | |
859 | ; |
cf25bb62 |
860 | |
d75d706f |
861 | $Cat{Space}->$op($code) if $isspace; |
cf25bb62 |
862 | |
d75d706f |
863 | $Cat{SpacePerl}->$op($code) if $isspace |
864 | && $code != 0x000B; # Backward compat. |
cf25bb62 |
865 | |
d75d706f |
866 | $Cat{Blank}->$op($code) if $isspace |
867 | && !($code == 0x000A || |
868 | $code == 0x000B || |
869 | $code == 0x000C || |
870 | $code == 0x000D || |
871 | $code == 0x0085 || |
872 | $cat =~ /^Z[lp]/); |
cf25bb62 |
873 | |
874 | $Cat{Digit}->$op($code) if $cat eq "Nd"; |
875 | $Cat{Upper}->$op($code) if $cat eq "Lu"; |
876 | $Cat{Lower}->$op($code) if $cat eq "Ll"; |
877 | $Cat{Title}->$op($code) if $cat eq "Lt"; |
878 | $Cat{ASCII}->$op($code) if $code <= 0x007F; |
879 | $Cat{Cntrl}->$op($code) if $cat =~ /^C/; |
d75d706f |
880 | my $isgraph = !$isspace && $cat !~ /Cc|Cs|Cn/; |
881 | $Cat{Graph}->$op($code) if $isgraph; |
882 | $Cat{Print}->$op($code) if $isgraph || $isspace; |
cf25bb62 |
883 | $Cat{Punct}->$op($code) if $cat =~ /^P/; |
884 | |
885 | $Cat{XDigit}->$op($code) if ($code >= 0x30 && $code <= 0x39) ## 0..9 |
886 | || ($code >= 0x41 && $code <= 0x46) ## A..F |
887 | || ($code >= 0x61 && $code <= 0x66); ## a..f |
888 | } |
d73e5302 |
889 | |
cf25bb62 |
890 | ## open ane read file..... |
551b6b6f |
891 | if (not open IN, "UnicodeData.txt") { |
892 | die "$0: UnicodeData.txt: $!\n"; |
cf25bb62 |
893 | } |
d73e5302 |
894 | |
a3a8c5f0 |
895 | ## |
896 | ## For building \p{_CombAbove} and \p{_CanonDCIJ} |
897 | ## |
898 | my %_Above_HexCodes; ## Hexcodes for chars with $comb == 230 ("ABOVE") |
899 | |
900 | my %CodeToDeco; ## Maps code to decomp. list for chars with first |
901 | ## decomp. char an "i" or "j" (for \p{_CanonDCIJ}) |
902 | |
903 | ## This is filled in as we go.... |
99598c8c |
904 | my $CombAbove = Table->New(Is => '_CombAbove', |
905 | Desc => '(for internal casefolding use)', |
906 | Fuzzy => 0); |
a3a8c5f0 |
907 | |
cf25bb62 |
908 | while (<IN>) |
909 | { |
910 | next unless /^[0-9A-Fa-f]+;/; |
911 | s/\s+$//; |
912 | |
913 | my ($hexcode, ## code point in hex (e.g. "0041") |
914 | $name, ## character name (e.g. "LATIN CAPITAL LETTER A") |
915 | $cat, ## category (e.g. "Lu") |
916 | $comb, ## Canonical combining class (e.t. "230") |
917 | $bidi, ## directional category (e.g. "L") |
918 | $deco, ## decomposition mapping |
919 | $decimal, ## decimal digit value |
920 | $digit, ## digit value |
921 | $number, ## numeric value |
922 | $mirrored, ## mirrored |
923 | $unicode10, ## name in Unicode 1.0 |
924 | $comment, ## comment field |
925 | $upper, ## uppercase mapping |
926 | $lower, ## lowercase mapping |
927 | $title, ## titlecase mapping |
928 | ) = split(/\s*;\s*/); |
929 | |
2eb5892f |
930 | # Note that in Unicode 3.2 there will be names like |
931 | # LINE FEED (LF), which probably means that \N{} needs |
932 | # to cope also with LINE FEED and LF. |
933 | $name = $unicode10 if $name eq '<control>' && $unicode10 ne ''; |
934 | |
cf25bb62 |
935 | my $code = hex($hexcode); |
936 | |
a3a8c5f0 |
937 | if ($comb and $comb == 230) { |
938 | $CombAbove->Append($code); |
939 | $_Above_HexCodes{$hexcode} = 1; |
940 | } |
941 | |
942 | ## Used in building \p{_CanonDCIJ} |
943 | if ($deco and $deco =~ m/^006[9A]\b/) { |
944 | $CodeToDeco{$code} = $deco; |
945 | } |
946 | |
cf25bb62 |
947 | ## |
948 | ## There are a few pairs of lines like: |
949 | ## AC00;<Hangul Syllable, First>;Lo;0;L;;;;;N;;;;; |
950 | ## D7A3;<Hangul Syllable, Last>;Lo;0;L;;;;;N;;;;; |
951 | ## that define ranges. |
952 | ## |
953 | if ($name =~ /^<(.+), (First|Last)>$/) |
954 | { |
955 | $name = $1; |
956 | gencat($name, $cat, $code, $2 eq 'First' ? 'Append' : 'Extend'); |
99598c8c |
957 | #New_Prop(In => $name, $General{$name}, Fuzzy => 1); |
cf25bb62 |
958 | } |
959 | else |
960 | { |
961 | ## normal (single-character) lines |
962 | gencat($name, $cat, $code, 'Append'); |
963 | |
964 | # No Append() here since since several codes may map into one. |
965 | $To{Upper}->RawAppendRange($code, $code, $upper) if $upper; |
966 | $To{Lower}->RawAppendRange($code, $code, $lower) if $lower; |
967 | $To{Title}->RawAppendRange($code, $code, $title) if $title; |
968 | $To{Digit}->Append($code, $decimal) if length $decimal; |
969 | |
970 | $Bidi->Append($code, $bidi); |
971 | $Comb->Append($code, $comb) if $comb; |
972 | $Number->Append($code, $number) if length $number; |
973 | |
12ac2576 |
974 | length($decimal) and ($Number{De} ||= Table->New())->Append($code) |
975 | or |
976 | length($digit) and ($Number{Di} ||= Table->New())->Append($code) |
977 | or |
978 | length($number) and ($Number{Nu} ||= Table->New())->Append($code); |
979 | |
cf25bb62 |
980 | $Mirrored->Append($code) if $mirrored eq "Y"; |
981 | |
12ac2576 |
982 | $Bidi{$bidi} ||= Table->New();#Is => "bt/$bidi", |
983 | #Desc => "Bi-directional category '$bidi'", |
984 | #Fuzzy => 0); |
cf25bb62 |
985 | $Bidi{$bidi}->Append($code); |
986 | |
987 | if ($deco) |
988 | { |
989 | $Deco->Append($code, $deco); |
990 | if ($deco =~/^<(\w+)>/) |
991 | { |
12ac2576 |
992 | my $dshort = $PVA_reverse{dt}{ucfirst lc $1}; |
993 | $DC{com}->Append($code); |
cf25bb62 |
994 | |
12ac2576 |
995 | $DC{$dshort} ||= Table->New(); |
996 | $DC{$dshort}->Append($code); |
cf25bb62 |
997 | } |
998 | else |
999 | { |
12ac2576 |
1000 | $DC{can}->Append($code); |
cf25bb62 |
1001 | } |
1002 | } |
1003 | } |
1004 | } |
1005 | close IN; |
d2d499f5 |
1006 | |
cf25bb62 |
1007 | ## |
1008 | ## Tidy up a few special cases.... |
1009 | ## |
d73e5302 |
1010 | |
cf25bb62 |
1011 | $Cat{Cn} = $Assigned->Invert; ## Cn is everything that doesn't exist |
99598c8c |
1012 | New_Prop(Is => 'Cn', |
1013 | $Cat{Cn}, |
1014 | Desc => "General Category 'Cn' [not functional in Perl]", |
1015 | Fuzzy => 0); |
d73e5302 |
1016 | |
cf25bb62 |
1017 | ## Unassigned is the same as 'Cn' |
5beb625e |
1018 | New_Alias(Is => 'Unassigned', SameAs => 'Cn', Fuzzy => 0); |
d73e5302 |
1019 | |
cf25bb62 |
1020 | $Cat{C}->Replace($Cat{C}->Merge($Cat{Cn})); ## Now merge in Cn into C |
d73e5302 |
1021 | |
d73e5302 |
1022 | |
12ac2576 |
1023 | # LC is Ll, Lu, and Lt. |
1024 | # (used to be L& or L_, but PropValueAliases.txt defines it as LC) |
1025 | New_Prop(Is => 'LC', |
cf25bb62 |
1026 | Table->Merge(@Cat{qw[Ll Lu Lt]}), |
99598c8c |
1027 | Desc => '[\p{Ll}\p{Lu}\p{Lt}]', |
1028 | Fuzzy => 0); |
d73e5302 |
1029 | |
cf25bb62 |
1030 | ## Any and All are all code points. |
99598c8c |
1031 | my $Any = Table->New(Is => 'Any', |
1032 | Desc => sprintf("[\\x{0000}-\\x{%X}]", |
1033 | $LastUnicodeCodepoint), |
5beb625e |
1034 | Fuzzy => 0); |
cf25bb62 |
1035 | $Any->RawAppendRange(0, $LastUnicodeCodepoint); |
d73e5302 |
1036 | |
5beb625e |
1037 | New_Alias(Is => 'All', SameAs => 'Any', Fuzzy => 0); |
d73e5302 |
1038 | |
a3a8c5f0 |
1039 | ## |
1040 | ## Build special properties for Perl's internal case-folding needs: |
1041 | ## \p{_CaseIgnorable} |
1042 | ## \p{_CanonDCIJ} |
1043 | ## \p{_CombAbove} |
1044 | ## _CombAbove was built above. Others are built here.... |
1045 | ## |
1046 | |
1047 | ## \p{_CaseIgnorable} is [\p{Mn}\0x00AD\x2010] |
1048 | New_Prop(Is => '_CaseIgnorable', |
1049 | Table->Merge($Cat{Mn}, |
1050 | 0x00AD, #SOFT HYPHEN |
1051 | 0x2010), #HYPHEN |
99598c8c |
1052 | Desc => '(for internal casefolding use)', |
1053 | Fuzzy => 0); |
a3a8c5f0 |
1054 | |
1055 | |
1056 | ## \p{_CanonDCIJ} is fairly complex... |
99598c8c |
1057 | my $CanonCDIJ = Table->New(Is => '_CanonDCIJ', |
1058 | Desc => '(for internal casefolding use)', |
1059 | Fuzzy => 0); |
a3a8c5f0 |
1060 | ## It contains the ASCII 'i' and 'j'.... |
1061 | $CanonCDIJ->Append(0x0069); # ASCII ord("i") |
1062 | $CanonCDIJ->Append(0x006A); # ASCII ord("j") |
1063 | ## ...and any character with a decomposition that starts with either of |
1064 | ## those code points, but only if the decomposition does not have any |
1065 | ## combining character with the "ABOVE" canonical combining class. |
1066 | for my $code (sort { $a <=> $b} keys %CodeToDeco) |
1067 | { |
1068 | ## Need to ensure that all decomposition characters do not have |
1069 | ## a %HexCodeToComb in %AboveCombClasses. |
1070 | my $want = 1; |
1071 | for my $deco_hexcode (split / /, $CodeToDeco{$code}) |
1072 | { |
1073 | if (exists $_Above_HexCodes{$deco_hexcode}) { |
1074 | ## one of the decmposition chars has an ABOVE combination |
1075 | ## class, so we're not interested in this one |
1076 | $want = 0; |
1077 | last; |
1078 | } |
1079 | } |
1080 | if ($want) { |
1081 | $CanonCDIJ->Append($code); |
1082 | } |
1083 | } |
1084 | |
1085 | |
d73e5302 |
1086 | |
cf25bb62 |
1087 | ## |
1088 | ## Now dump the files. |
1089 | ## |
1090 | $Name->Write("Name.pl"); |
12ac2576 |
1091 | |
7ebf06b3 |
1092 | { |
1093 | my @PVA = $HEADER; |
1094 | foreach my $name (qw (PropertyAlias PA_reverse PropValueAlias |
1095 | PVA_reverse PVA_abbr_map)) { |
1096 | # Should I really jump through typeglob hoops just to avoid a |
1097 | # symbolic reference? (%{"utf8::$name}) |
1098 | push @PVA, "\n", "\%utf8::$name = (\n", |
1099 | simple_dumper (%{$utf8::{$name}}), ");\n"; |
1100 | } |
d403599d |
1101 | push @PVA, "1;\n"; |
7ebf06b3 |
1102 | WriteIfChanged("PVA.pl", @PVA); |
1103 | } |
1104 | |
12ac2576 |
1105 | # $Bidi->Write("Bidirectional.pl"); |
12ac2576 |
1106 | for (keys %Bidi) { |
1107 | $Bidi{$_}->Write( |
d07a55ed |
1108 | ["lib","bc","$_.pl"], |
12ac2576 |
1109 | "BidiClass category '$PropValueAlias{bc}{$_}'" |
1110 | ); |
1111 | } |
1112 | |
cf25bb62 |
1113 | $Comb->Write("CombiningClass.pl"); |
12ac2576 |
1114 | for (keys %{ $PropValueAlias{ccc} }) { |
1115 | my ($code, $name) = @{ $PropValueAlias{ccc}{$_} }; |
1116 | (my $c = Table->New())->Append($code); |
1117 | $c->Write( |
d07a55ed |
1118 | ["lib","ccc","$_.pl"], |
12ac2576 |
1119 | "CombiningClass category '$name'" |
1120 | ); |
1121 | } |
1122 | |
cf25bb62 |
1123 | $Deco->Write("Decomposition.pl"); |
12ac2576 |
1124 | for (keys %DC) { |
1125 | $DC{$_}->Write( |
d07a55ed |
1126 | ["lib","dt","$_.pl"], |
12ac2576 |
1127 | "DecompositionType category '$PropValueAlias{dt}{$_}'" |
1128 | ); |
1129 | } |
1130 | |
1131 | # $Number->Write("Number.pl"); |
12ac2576 |
1132 | for (keys %Number) { |
1133 | $Number{$_}->Write( |
d07a55ed |
1134 | ["lib","nt","$_.pl"], |
12ac2576 |
1135 | "NumericType category '$PropValueAlias{nt}{$_}'" |
1136 | ); |
1137 | } |
1138 | |
1139 | # $General->Write("Category.pl"); |
cf25bb62 |
1140 | |
1141 | for my $to (sort keys %To) { |
d07a55ed |
1142 | $To{$to}->Write(["To","$to.pl"]); |
d73e5302 |
1143 | } |
12ac2576 |
1144 | |
1145 | for (keys %{ $PropValueAlias{gc} }) { |
1146 | New_Alias(Is => $PropValueAlias{gc}{$_}, SameAs => $_, Fuzzy => 1); |
1147 | } |
d73e5302 |
1148 | } |
1149 | |
cf25bb62 |
1150 | ## |
551b6b6f |
1151 | ## Process LineBreak.txt |
cf25bb62 |
1152 | ## |
551b6b6f |
1153 | sub LineBreak_Txt() |
cf25bb62 |
1154 | { |
551b6b6f |
1155 | if (not open IN, "LineBreak.txt") { |
1156 | die "$0: LineBreak.txt: $!\n"; |
cf25bb62 |
1157 | } |
d73e5302 |
1158 | |
cf25bb62 |
1159 | my $Lbrk = Table->New(); |
1160 | my %Lbrk; |
d73e5302 |
1161 | |
cf25bb62 |
1162 | while (<IN>) |
1163 | { |
1164 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/; |
d73e5302 |
1165 | |
cf25bb62 |
1166 | my ($first, $last, $lbrk) = (hex($1), hex($2||""), $3); |
d73e5302 |
1167 | |
cf25bb62 |
1168 | $Lbrk->Append($first, $lbrk); |
d73e5302 |
1169 | |
12ac2576 |
1170 | $Lbrk{$lbrk} ||= Table->New(); |
cf25bb62 |
1171 | $Lbrk{$lbrk}->Append($first); |
d73e5302 |
1172 | |
cf25bb62 |
1173 | if ($last) { |
1174 | $Lbrk->Extend($last); |
1175 | $Lbrk{$lbrk}->Extend($last); |
d73e5302 |
1176 | } |
1177 | } |
cf25bb62 |
1178 | close IN; |
d73e5302 |
1179 | |
12ac2576 |
1180 | # $Lbrk->Write("Lbrk.pl"); |
1181 | |
12ac2576 |
1182 | |
1183 | for (keys %Lbrk) { |
1184 | $Lbrk{$_}->Write( |
d07a55ed |
1185 | ["lib","lb","$_.pl"], |
12ac2576 |
1186 | "Linebreak category '$PropValueAlias{lb}{$_}'" |
1187 | ); |
1188 | } |
cf25bb62 |
1189 | } |
d73e5302 |
1190 | |
cf25bb62 |
1191 | ## |
551b6b6f |
1192 | ## Process ArabicShaping.txt. |
cf25bb62 |
1193 | ## |
551b6b6f |
1194 | sub ArabicShaping_txt() |
cf25bb62 |
1195 | { |
551b6b6f |
1196 | if (not open IN, "ArabicShaping.txt") { |
1197 | die "$0: ArabicShaping.txt: $!\n"; |
cf25bb62 |
1198 | } |
d73e5302 |
1199 | |
cf25bb62 |
1200 | my $ArabLink = Table->New(); |
1201 | my $ArabLinkGroup = Table->New(); |
d73e5302 |
1202 | |
12ac2576 |
1203 | my %JoinType; |
1204 | |
cf25bb62 |
1205 | while (<IN>) |
1206 | { |
1207 | next unless /^[0-9A-Fa-f]+;/; |
1208 | s/\s+$//; |
d73e5302 |
1209 | |
cf25bb62 |
1210 | my ($hexcode, $name, $link, $linkgroup) = split(/\s*;\s*/); |
1211 | my $code = hex($hexcode); |
1212 | $ArabLink->Append($code, $link); |
1213 | $ArabLinkGroup->Append($code, $linkgroup); |
12ac2576 |
1214 | |
1215 | $JoinType{$link} ||= Table->New(Is => "JoinType$link"); |
1216 | $JoinType{$link}->Append($code); |
d73e5302 |
1217 | } |
cf25bb62 |
1218 | close IN; |
1219 | |
12ac2576 |
1220 | # $ArabLink->Write("ArabLink.pl"); |
1221 | # $ArabLinkGroup->Write("ArabLnkGrp.pl"); |
1222 | |
12ac2576 |
1223 | |
1224 | for (keys %JoinType) { |
1225 | $JoinType{$_}->Write( |
d07a55ed |
1226 | ["lib","jt","$_.pl"], |
12ac2576 |
1227 | "JoiningType category '$PropValueAlias{jt}{$_}'" |
1228 | ); |
1229 | } |
1230 | } |
1231 | |
1232 | ## |
1233 | ## Process EastAsianWidth.txt. |
1234 | ## |
1235 | sub EastAsianWidth_txt() |
1236 | { |
1237 | if (not open IN, "EastAsianWidth.txt") { |
1238 | die "$0: EastAsianWidth.txt: $!\n"; |
1239 | } |
1240 | |
1241 | my %EAW; |
1242 | |
1243 | while (<IN>) |
1244 | { |
1245 | next unless /^[0-9A-Fa-f]+;/; |
1246 | s/#.*//; |
1247 | s/\s+$//; |
1248 | |
1249 | my ($hexcode, $pv) = split(/\s*;\s*/); |
1250 | my $code = hex($hexcode); |
1251 | $EAW{$pv} ||= Table->New(Is => "EastAsianWidth$pv"); |
1252 | $EAW{$pv}->Append($code); |
1253 | } |
1254 | close IN; |
1255 | |
12ac2576 |
1256 | |
1257 | for (keys %EAW) { |
1258 | $EAW{$_}->Write( |
d07a55ed |
1259 | ["lib","ea","$_.pl"], |
12ac2576 |
1260 | "EastAsianWidth category '$PropValueAlias{ea}{$_}'" |
1261 | ); |
1262 | } |
1263 | } |
1264 | |
1265 | ## |
1266 | ## Process HangulSyllableType.txt. |
1267 | ## |
1268 | sub HangulSyllableType_txt() |
1269 | { |
1270 | if (not open IN, "HangulSyllableType.txt") { |
1271 | die "$0: HangulSyllableType.txt: $!\n"; |
1272 | } |
1273 | |
1274 | my %HST; |
1275 | |
1276 | while (<IN>) |
1277 | { |
1278 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(\w+)/; |
1279 | my ($first, $last, $pv) = (hex($1), hex($2||""), $3); |
1280 | |
1281 | $HST{$pv} ||= Table->New(Is => "HangulSyllableType$pv"); |
1282 | $HST{$pv}->Append($first); |
1283 | |
1284 | if ($last) { $HST{$pv}->Extend($last) } |
1285 | } |
1286 | close IN; |
1287 | |
12ac2576 |
1288 | for (keys %HST) { |
1289 | $HST{$_}->Write( |
d07a55ed |
1290 | ["lib","hst","$_.pl"], |
12ac2576 |
1291 | "HangulSyllableType category '$PropValueAlias{hst}{$_}'" |
1292 | ); |
1293 | } |
d73e5302 |
1294 | } |
1295 | |
cf25bb62 |
1296 | ## |
1297 | ## Process Jamo.txt. |
1298 | ## |
1299 | sub Jamo_txt() |
1300 | { |
1301 | if (not open IN, "Jamo.txt") { |
1302 | die "$0: Jamo.txt: $!\n"; |
1303 | } |
1304 | my $Short = Table->New(); |
d73e5302 |
1305 | |
cf25bb62 |
1306 | while (<IN>) |
1307 | { |
1308 | next unless /^([0-9A-Fa-f]+)\s*;\s*(\w*)/; |
1309 | my ($code, $short) = (hex($1), $2); |
d73e5302 |
1310 | |
cf25bb62 |
1311 | $Short->Append($code, $short); |
d73e5302 |
1312 | } |
cf25bb62 |
1313 | close IN; |
12ac2576 |
1314 | # $Short->Write("JamoShort.pl"); |
d73e5302 |
1315 | } |
1316 | |
cf25bb62 |
1317 | ## |
1318 | ## Process Scripts.txt. |
1319 | ## |
1320 | sub Scripts_txt() |
1321 | { |
1322 | my @ScriptInfo; |
d73e5302 |
1323 | |
cf25bb62 |
1324 | if (not open(IN, "Scripts.txt")) { |
1325 | die "$0: Scripts.txt: $!\n"; |
1326 | } |
1327 | while (<IN>) { |
1328 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/; |
d73e5302 |
1329 | |
cf25bb62 |
1330 | # Wait until all the scripts have been read since |
1331 | # they are not listed in numeric order. |
1332 | push @ScriptInfo, [ hex($1), hex($2||""), $3 ]; |
1333 | } |
1334 | close IN; |
d73e5302 |
1335 | |
cf25bb62 |
1336 | # Now append the scripts properties in their code point order. |
d73e5302 |
1337 | |
cf25bb62 |
1338 | my %Script; |
1339 | my $Scripts = Table->New(); |
d73e5302 |
1340 | |
cf25bb62 |
1341 | for my $script (sort { $a->[0] <=> $b->[0] } @ScriptInfo) |
1342 | { |
1343 | my ($first, $last, $name) = @$script; |
1344 | $Scripts->Append($first, $name); |
d73e5302 |
1345 | |
99598c8c |
1346 | $Script{$name} ||= Table->New(Is => $name, |
1347 | Desc => "Script '$name'", |
1348 | Fuzzy => 1); |
cf25bb62 |
1349 | $Script{$name}->Append($first, $name); |
d73e5302 |
1350 | |
cf25bb62 |
1351 | if ($last) { |
1352 | $Scripts->Extend($last); |
1353 | $Script{$name}->Extend($last); |
1354 | } |
1355 | } |
d73e5302 |
1356 | |
12ac2576 |
1357 | # $Scripts->Write("Scripts.pl"); |
d73e5302 |
1358 | |
cf25bb62 |
1359 | ## Common is everything not explicitly assigned to a Script |
1360 | ## |
1361 | ## ***shouldn't this be intersected with \p{Assigned}? ****** |
1362 | ## |
99598c8c |
1363 | New_Prop(Is => 'Common', |
1364 | $Scripts->Invert, |
1365 | Desc => 'Pseudo-Script of codepoints not in other Unicode scripts', |
1366 | Fuzzy => 1); |
cf25bb62 |
1367 | } |
d73e5302 |
1368 | |
cf25bb62 |
1369 | ## |
1370 | ## Given a name like "Close Punctuation", return a regex (that when applied |
1371 | ## with /i) matches any valid form of that name (e.g. "ClosePunctuation", |
1372 | ## "Close-Punctuation", etc.) |
1373 | ## |
1374 | ## Accept any space, dash, or underbar where in the official name there is |
1375 | ## space or a dash (or underbar, but there never is). |
1376 | ## |
1377 | ## |
1378 | sub NameToRegex($) |
1379 | { |
1380 | my $Name = shift; |
1381 | $Name =~ s/[- _]/(?:[-_]|\\s+)?/g; |
1382 | return $Name; |
1383 | } |
d73e5302 |
1384 | |
cf25bb62 |
1385 | ## |
1386 | ## Process Blocks.txt. |
1387 | ## |
1388 | sub Blocks_txt() |
1389 | { |
1390 | my $Blocks = Table->New(); |
1391 | my %Blocks; |
d73e5302 |
1392 | |
cf25bb62 |
1393 | if (not open IN, "Blocks.txt") { |
1394 | die "$0: Blocks.txt: $!\n"; |
1395 | } |
d73e5302 |
1396 | |
cf25bb62 |
1397 | while (<IN>) |
1398 | { |
1399 | #next if not /Private Use$/; |
1400 | next if not /^([0-9A-Fa-f]+)\.\.([0-9A-Fa-f]+)\s*;\s*(.+?)\s*$/; |
d73e5302 |
1401 | |
cf25bb62 |
1402 | my ($first, $last, $name) = (hex($1), hex($2), $3); |
d73e5302 |
1403 | |
cf25bb62 |
1404 | $Blocks->Append($first, $name); |
76ccdbe2 |
1405 | |
99598c8c |
1406 | $Blocks{$name} ||= Table->New(In => $name, |
1407 | Desc => "Block '$name'", |
1408 | Fuzzy => 1); |
cf25bb62 |
1409 | $Blocks{$name}->Append($first, $name); |
76ccdbe2 |
1410 | |
cf25bb62 |
1411 | if ($last and $last != $first) { |
1412 | $Blocks->Extend($last); |
1413 | $Blocks{$name}->Extend($last); |
d73e5302 |
1414 | } |
d73e5302 |
1415 | } |
cf25bb62 |
1416 | close IN; |
1417 | |
12ac2576 |
1418 | # $Blocks->Write("Blocks.pl"); |
d73e5302 |
1419 | } |
1420 | |
cf25bb62 |
1421 | ## |
1422 | ## Read in the PropList.txt. It contains extended properties not |
551b6b6f |
1423 | ## listed in the UnicodeData.txt, such as 'Other_Alphabetic': |
cf25bb62 |
1424 | ## alphabetic but not of the general category L; many modifiers |
1425 | ## belong to this extended property category: while they are not |
1426 | ## alphabets, they are alphabetic in nature. |
1427 | ## |
1428 | sub PropList_txt() |
1429 | { |
1430 | my @PropInfo; |
1431 | |
1432 | if (not open IN, "PropList.txt") { |
1433 | die "$0: PropList.txt: $!\n"; |
1434 | } |
d73e5302 |
1435 | |
cf25bb62 |
1436 | while (<IN>) |
1437 | { |
1438 | next unless /^([0-9A-Fa-f]+)(?:\.\.([0-9A-Fa-f]+))?\s*;\s*(.+?)\s*\#/; |
d73e5302 |
1439 | |
cf25bb62 |
1440 | # Wait until all the extended properties have been read since |
1441 | # they are not listed in numeric order. |
1442 | push @PropInfo, [ hex($1), hex($2||""), $3 ]; |
1443 | } |
1444 | close IN; |
71d929cb |
1445 | |
cf25bb62 |
1446 | # Now append the extended properties in their code point order. |
1447 | my $Props = Table->New(); |
1448 | my %Prop; |
71d929cb |
1449 | |
cf25bb62 |
1450 | for my $prop (sort { $a->[0] <=> $b->[0] } @PropInfo) |
1451 | { |
1452 | my ($first, $last, $name) = @$prop; |
1453 | $Props->Append($first, $name); |
71d929cb |
1454 | |
99598c8c |
1455 | $Prop{$name} ||= Table->New(Is => $name, |
1456 | Desc => "Extended property '$name'", |
1457 | Fuzzy => 1); |
cf25bb62 |
1458 | $Prop{$name}->Append($first, $name); |
71d929cb |
1459 | |
cf25bb62 |
1460 | if ($last) { |
1461 | $Props->Extend($last); |
1462 | $Prop{$name}->Extend($last); |
1463 | } |
71d929cb |
1464 | } |
d73e5302 |
1465 | |
12ac2576 |
1466 | for (keys %Prop) { |
1467 | (my $file = $PA_reverse{$_}) =~ tr/_//d; |
8e4b7420 |
1468 | # XXX I'm assuming that the names from %Prop don't suffer 8.3 clashes. |
1469 | $BaseNames{lc $file}++; |
12ac2576 |
1470 | $Prop{$_}->Write( |
d07a55ed |
1471 | ["lib","gc_sc","$file.pl"], |
12ac2576 |
1472 | "Binary property '$_'" |
1473 | ); |
1474 | } |
1475 | |
cf25bb62 |
1476 | # Alphabetic is L and Other_Alphabetic. |
99598c8c |
1477 | New_Prop(Is => 'Alphabetic', |
cf25bb62 |
1478 | Table->Merge($Cat{L}, $Prop{Other_Alphabetic}), |
99598c8c |
1479 | Desc => '[\p{L}\p{OtherAlphabetic}]', # use canonical names here |
1480 | Fuzzy => 1); |
cf25bb62 |
1481 | |
1482 | # Lowercase is Ll and Other_Lowercase. |
99598c8c |
1483 | New_Prop(Is => 'Lowercase', |
cf25bb62 |
1484 | Table->Merge($Cat{Ll}, $Prop{Other_Lowercase}), |
99598c8c |
1485 | Desc => '[\p{Ll}\p{OtherLowercase}]', # use canonical names here |
1486 | Fuzzy => 1); |
cf25bb62 |
1487 | |
1488 | # Uppercase is Lu and Other_Uppercase. |
1489 | New_Prop(Is => 'Uppercase', |
1490 | Table->Merge($Cat{Lu}, $Prop{Other_Uppercase}), |
99598c8c |
1491 | Desc => '[\p{Lu}\p{Other_Uppercase}]', # use canonical names here |
1492 | Fuzzy => 1); |
cf25bb62 |
1493 | |
1494 | # Math is Sm and Other_Math. |
1495 | New_Prop(Is => 'Math', |
1496 | Table->Merge($Cat{Sm}, $Prop{Other_Math}), |
99598c8c |
1497 | Desc => '[\p{Sm}\p{OtherMath}]', # use canonical names here |
1498 | Fuzzy => 1); |
cf25bb62 |
1499 | |
1500 | # ID_Start is Ll, Lu, Lt, Lm, Lo, and Nl. |
1501 | New_Prop(Is => 'ID_Start', |
1502 | Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl]}), |
99598c8c |
1503 | Desc => '[\p{Ll}\p{Lu}\p{Lt}\p{Lm}\p{Lo}\p{Nl}]', |
1504 | Fuzzy => 1); |
cf25bb62 |
1505 | |
1506 | # ID_Continue is ID_Start, Mn, Mc, Nd, and Pc. |
1507 | New_Prop(Is => 'ID_Continue', |
1508 | Table->Merge(@Cat{qw[Ll Lu Lt Lm Lo Nl Mn Mc Nd Pc ]}), |
99598c8c |
1509 | Desc => '[\p{ID_Start}\p{Mn}\p{Mc}\p{Nd}\p{Pc}]', |
1510 | Fuzzy => 1); |
d73e5302 |
1511 | } |
1512 | |
5beb625e |
1513 | |
1514 | ## |
1515 | ## These are used in: |
1516 | ## MakePropTestScript() |
1517 | ## WriteAllMappings() |
1518 | ## for making the test script. |
1519 | ## |
1520 | my %FuzzyNameToTest; |
1521 | my %ExactNameToTest; |
1522 | |
1523 | |
1524 | ## This used only for making the test script |
1525 | sub GenTests($$$$) |
1526 | { |
1527 | my $FH = shift; |
1528 | my $Prop = shift; |
1529 | my $MatchCode = shift; |
1530 | my $FailCode = shift; |
1531 | |
1532 | if (defined $MatchCode) { |
1533 | printf $FH qq/Expect(1, "\\x{%04X}", '\\p{$Prop}' );\n/, $MatchCode; |
1534 | printf $FH qq/Expect(0, "\\x{%04X}", '\\p{^$Prop}');\n/, $MatchCode; |
1535 | printf $FH qq/Expect(0, "\\x{%04X}", '\\P{$Prop}' );\n/, $MatchCode; |
1536 | printf $FH qq/Expect(1, "\\x{%04X}", '\\P{^$Prop}');\n/, $MatchCode; |
1537 | } |
1538 | if (defined $FailCode) { |
1539 | printf $FH qq/Expect(0, "\\x{%04X}", '\\p{$Prop}' );\n/, $FailCode; |
1540 | printf $FH qq/Expect(1, "\\x{%04X}", '\\p{^$Prop}');\n/, $FailCode; |
1541 | printf $FH qq/Expect(1, "\\x{%04X}", '\\P{$Prop}' );\n/, $FailCode; |
1542 | printf $FH qq/Expect(0, "\\x{%04X}", '\\P{^$Prop}');\n/, $FailCode; |
1543 | } |
1544 | } |
1545 | |
1546 | ## This used only for making the test script |
1547 | sub ExpectError($$) |
1548 | { |
1549 | my $FH = shift; |
1550 | my $prop = shift; |
1551 | |
1552 | print $FH qq/Error('\\p{$prop}');\n/; |
1553 | print $FH qq/Error('\\P{$prop}');\n/; |
1554 | } |
1555 | |
1556 | ## This used only for making the test script |
1557 | my @GoodSeps = ( |
1558 | " ", |
1559 | "-", |
1560 | " \t ", |
1561 | "", |
1562 | "", |
1563 | "_", |
1564 | ); |
1565 | my @BadSeps = ( |
1566 | "--", |
1567 | "__", |
1568 | " _", |
1569 | "/" |
1570 | ); |
1571 | |
1572 | ## This used only for making the test script |
1573 | sub RandomlyFuzzifyName($;$) |
1574 | { |
1575 | my $Name = shift; |
1576 | my $WantError = shift; ## if true, make an error |
1577 | |
1578 | my @parts; |
1579 | for my $part (split /[-\s_]+/, $Name) |
1580 | { |
1581 | if (@parts) { |
1582 | if ($WantError and rand() < 0.3) { |
1583 | push @parts, $BadSeps[rand(@BadSeps)]; |
1584 | $WantError = 0; |
1585 | } else { |
1586 | push @parts, $GoodSeps[rand(@GoodSeps)]; |
1587 | } |
1588 | } |
1589 | my $switch = int rand(4); |
1590 | if ($switch == 0) { |
1591 | push @parts, uc $part; |
1592 | } elsif ($switch == 1) { |
1593 | push @parts, lc $part; |
1594 | } elsif ($switch == 2) { |
1595 | push @parts, ucfirst $part; |
1596 | } else { |
1597 | push @parts, $part; |
1598 | } |
1599 | } |
1600 | my $new = join('', @parts); |
1601 | |
1602 | if ($WantError) { |
1603 | if (rand() >= 0.5) { |
1604 | $new .= $BadSeps[rand(@BadSeps)]; |
1605 | } else { |
1606 | $new = $BadSeps[rand(@BadSeps)] . $new; |
1607 | } |
1608 | } |
1609 | return $new; |
1610 | } |
1611 | |
1612 | ## This used only for making the test script |
1613 | sub MakePropTestScript() |
1614 | { |
1615 | ## this written directly -- it's huge. |
d07a55ed |
1616 | force_unlink ("TestProp.pl"); |
5beb625e |
1617 | if (not open OUT, ">TestProp.pl") { |
1618 | die "$0: TestProp.pl: $!\n"; |
1619 | } |
1620 | print OUT <DATA>; |
1621 | |
1622 | while (my ($Name, $Table) = each %ExactNameToTest) |
1623 | { |
1624 | GenTests(*OUT, $Name, $Table->ValidCode, $Table->InvalidCode); |
1625 | ExpectError(*OUT, uc $Name) if uc $Name ne $Name; |
1626 | ExpectError(*OUT, lc $Name) if lc $Name ne $Name; |
1627 | } |
1628 | |
1629 | |
1630 | while (my ($Name, $Table) = each %FuzzyNameToTest) |
1631 | { |
1632 | my $Orig = $CanonicalToOrig{$Name}; |
1633 | my %Names = ( |
1634 | $Name => 1, |
1635 | $Orig => 1, |
1636 | RandomlyFuzzifyName($Orig) => 1 |
1637 | ); |
1638 | |
1639 | for my $N (keys %Names) { |
1640 | GenTests(*OUT, $N, $Table->ValidCode, $Table->InvalidCode); |
1641 | } |
1642 | |
1643 | ExpectError(*OUT, RandomlyFuzzifyName($Orig, 'ERROR')); |
1644 | } |
1645 | |
1646 | print OUT "Finished();\n"; |
1647 | close OUT; |
1648 | } |
1649 | |
1650 | |
1651 | ## |
1652 | ## These are used only in: |
1653 | ## RegisterFileForName() |
1654 | ## WriteAllMappings() |
1655 | ## |
1656 | my %Exact; ## will become %utf8::Exact; |
1657 | my %Canonical; ## will become %utf8::Canonical; |
1658 | my %CaComment; ## Comment for %Canonical entry of same key |
1659 | |
1660 | ## |
1661 | ## Given info about a name and a datafile that it should be associated with, |
1662 | ## register that assocation in %Exact and %Canonical. |
1663 | sub RegisterFileForName($$$$) |
1664 | { |
1665 | my $Type = shift; |
1666 | my $Name = shift; |
1667 | my $IsFuzzy = shift; |
1668 | my $filename = shift; |
1669 | |
1670 | ## |
1671 | ## Now in details for the mapping. $Type eq 'Is' has the |
1672 | ## Is removed, as it will be removed in utf8_heavy when this |
1673 | ## data is being checked. In keeps its "In", but a second |
1674 | ## sans-In record is written if it doesn't conflict with |
1675 | ## anything already there. |
1676 | ## |
1677 | if (not $IsFuzzy) |
1678 | { |
1679 | if ($Type eq 'Is') { |
1680 | die "oops[$Name]" if $Exact{$Name}; |
1681 | $Exact{$Name} = $filename; |
1682 | } else { |
1683 | die "oops[$Type$Name]" if $Exact{"$Type$Name"}; |
1684 | $Exact{"$Type$Name"} = $filename; |
1685 | $Exact{$Name} = $filename if not $Exact{$Name}; |
1686 | } |
1687 | } |
1688 | else |
1689 | { |
1690 | my $CName = lc $Name; |
1691 | if ($Type eq 'Is') { |
1692 | die "oops[$CName]" if $Canonical{$CName}; |
1693 | $Canonical{$CName} = $filename; |
1694 | $CaComment{$CName} = $Name if $Name =~ tr/A-Z// >= 2; |
1695 | } else { |
1696 | die "oops[$Type$CName]" if $Canonical{lc "$Type$CName"}; |
1697 | $Canonical{lc "$Type$CName"} = $filename; |
1698 | $CaComment{lc "$Type$CName"} = "$Type$Name"; |
1699 | if (not $Canonical{$CName}) { |
1700 | $Canonical{$CName} = $filename; |
1701 | $CaComment{$CName} = "$Type$Name"; |
1702 | } |
1703 | } |
1704 | } |
1705 | } |
1706 | |
cf25bb62 |
1707 | ## |
1708 | ## Writes the info accumulated in |
1709 | ## |
1710 | ## %TableInfo; |
1711 | ## %FuzzyNames; |
1712 | ## %AliasInfo; |
1713 | ## |
1714 | ## |
1715 | sub WriteAllMappings() |
1716 | { |
99598c8c |
1717 | my @MAP; |
1718 | |
5beb625e |
1719 | ## 'Is' *MUST* come first, so its names have precidence over 'In's |
1720 | for my $Type ('Is', 'In') |
1721 | { |
1722 | my %RawNameToFile; ## a per-$Type cache |
cf25bb62 |
1723 | |
5beb625e |
1724 | for my $Name (sort {length $a <=> length $b} keys %{$TableInfo{$Type}}) |
cf25bb62 |
1725 | { |
5beb625e |
1726 | ## Note: $Name is already canonical |
99598c8c |
1727 | my $Table = $TableInfo{$Type}->{$Name}; |
5beb625e |
1728 | my $IsFuzzy = $FuzzyNames{$Type}->{$Name}; |
99598c8c |
1729 | |
1730 | ## Need an 8.3 safe filename (which means "an 8 safe" $filename) |
5beb625e |
1731 | my $filename; |
cf25bb62 |
1732 | { |
5beb625e |
1733 | ## 'Is' items lose 'Is' from the basename. |
12ac2576 |
1734 | $filename = $Type eq 'Is' ? |
1735 | ($PVA_reverse{sc}{$Name} || $Name) : |
1736 | "$Type$Name"; |
5beb625e |
1737 | |
1738 | $filename =~ s/[^\w_]+/_/g; # "L&" -> "L_" |
1739 | substr($filename, 8) = '' if length($filename) > 8; |
1740 | |
1741 | ## |
1742 | ## Make sure the basename doesn't conflict with something we |
1743 | ## might have already written. If we have, say, |
1744 | ## InGreekExtended1 |
1745 | ## InGreekExtended2 |
1746 | ## they become |
1747 | ## InGreekE |
1748 | ## InGreek2 |
1749 | ## |
1750 | while (my $num = $BaseNames{lc $filename}++) |
1751 | { |
1752 | $num++; ## so basenames with numbers start with '2', which |
1753 | ## just looks more natural. |
1754 | ## Want to append $num, but if it'll make the basename longer |
1755 | ## than 8 characters, pre-truncate $filename so that the result |
1756 | ## is acceptable. |
1757 | my $delta = length($filename) + length($num) - 8; |
1758 | if ($delta > 0) { |
1759 | substr($filename, -$delta) = $num; |
1760 | } else { |
1761 | $filename .= $num; |
1762 | } |
99598c8c |
1763 | } |
5beb625e |
1764 | }; |
99598c8c |
1765 | |
1766 | ## |
1767 | ## Construct a nice comment to add to the file, and build data |
1768 | ## for the "./Properties" file along the way. |
1769 | ## |
1770 | my $Comment; |
1771 | { |
1772 | my $Desc = $TableDesc{$Type}->{$Name} || ""; |
1773 | ## get list of names this table is reference by |
1774 | my @Supported = $Name; |
1775 | while (my ($Orig, $Alias) = each %{ $AliasInfo{$Type} }) |
1776 | { |
1777 | if ($Orig eq $Name) { |
1778 | push @Supported, $Alias; |
1779 | } |
1780 | } |
1781 | |
1782 | my $TypeToShow = $Type eq 'Is' ? "" : $Type; |
1783 | my $OrigProp; |
1784 | |
1785 | $Comment = "This file supports:\n"; |
1786 | for my $N (@Supported) |
1787 | { |
1788 | my $IsFuzzy = $FuzzyNames{$Type}->{$N}; |
5beb625e |
1789 | my $Prop = "\\p{$TypeToShow$Name}"; |
99598c8c |
1790 | $OrigProp = $Prop if not $OrigProp; #cache for aliases |
1791 | if ($IsFuzzy) { |
1792 | $Comment .= "\t$Prop (and fuzzy permutations)\n"; |
1793 | } else { |
1794 | $Comment .= "\t$Prop\n"; |
1795 | } |
1796 | my $MyDesc = ($N eq $Name) ? $Desc : "Alias for $OrigProp ($Desc)"; |
1797 | |
1798 | push @MAP, sprintf("%s %-42s %s\n", |
1799 | $IsFuzzy ? '*' : ' ', $Prop, $MyDesc); |
1800 | } |
1801 | if ($Desc) { |
1802 | $Comment .= "\nMeaning: $Desc\n"; |
1803 | } |
1804 | |
1805 | } |
cf25bb62 |
1806 | ## |
1807 | ## Okay, write the file... |
1808 | ## |
d07a55ed |
1809 | $Table->Write(["lib","gc_sc","$filename.pl"], $Comment); |
99598c8c |
1810 | |
5beb625e |
1811 | ## and register it |
1812 | $RawNameToFile{$Name} = $filename; |
1813 | RegisterFileForName($Type => $Name, $IsFuzzy, $filename); |
cf25bb62 |
1814 | |
5beb625e |
1815 | if ($IsFuzzy) |
1816 | { |
1817 | my $CName = CanonicalName($Type . '_'. $Name); |
1818 | $FuzzyNameToTest{$Name} = $Table if !$FuzzyNameToTest{$Name}; |
1819 | $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName}; |
1820 | } else { |
1821 | $ExactNameToTest{$Name} = $Table; |
cf25bb62 |
1822 | } |
1823 | |
cf25bb62 |
1824 | } |
1825 | |
5beb625e |
1826 | ## Register aliase info |
1827 | for my $Name (sort {length $a <=> length $b} keys %{$AliasInfo{$Type}}) |
cf25bb62 |
1828 | { |
5beb625e |
1829 | my $Alias = $AliasInfo{$Type}->{$Name}; |
1830 | my $IsFuzzy = $FuzzyNames{$Type}->{$Alias}; |
1831 | my $filename = $RawNameToFile{$Name}; |
1832 | die "oops [$Alias]->[$Name]" if not $filename; |
1833 | RegisterFileForName($Type => $Alias, $IsFuzzy, $filename); |
1834 | |
1835 | my $Table = $TableInfo{$Type}->{$Name}; |
1836 | die "oops" if not $Table; |
1837 | if ($IsFuzzy) |
1838 | { |
1839 | my $CName = CanonicalName($Type .'_'. $Alias); |
1840 | $FuzzyNameToTest{$Alias} = $Table if !$FuzzyNameToTest{$Alias}; |
1841 | $FuzzyNameToTest{$CName} = $Table if !$FuzzyNameToTest{$CName}; |
1842 | } else { |
1843 | $ExactNameToTest{$Alias} = $Table; |
1844 | } |
cf25bb62 |
1845 | } |
5beb625e |
1846 | } |
cf25bb62 |
1847 | |
5beb625e |
1848 | ## |
1849 | ## Write out the property list |
1850 | ## |
1851 | { |
1852 | my @OUT = ( |
1853 | "##\n", |
1854 | "## This file created by $0\n", |
1855 | "## List of built-in \\p{...}/\\P{...} properties.\n", |
1856 | "##\n", |
1857 | "## '*' means name may be 'fuzzy'\n", |
1858 | "##\n\n", |
1859 | sort { substr($a,2) cmp substr($b, 2) } @MAP, |
1860 | ); |
1861 | WriteIfChanged('Properties', @OUT); |
1862 | } |
cf25bb62 |
1863 | |
5beb625e |
1864 | use Text::Tabs (); ## using this makes the files about half the size |
1865 | |
1866 | ## Write Exact.pl |
1867 | { |
1868 | my @OUT = ( |
1869 | $HEADER, |
1870 | "##\n", |
1871 | "## Data in this file used by ../utf8_heavy.pl\n", |
1872 | "##\n\n", |
12ac2576 |
1873 | "## Mapping from name to filename in ./lib/gc_sc\n", |
5beb625e |
1874 | "%utf8::Exact = (\n", |
1875 | ); |
cf25bb62 |
1876 | |
12ac2576 |
1877 | $Exact{InGreek} = 'InGreekA'; # this is evil kludge |
cf25bb62 |
1878 | for my $Name (sort keys %Exact) |
1879 | { |
1880 | my $File = $Exact{$Name}; |
5beb625e |
1881 | $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name "; |
1882 | my $Text = sprintf("%-15s => %s,\n", $Name, qq/'$File'/); |
1883 | push @OUT, Text::Tabs::unexpand($Text); |
cf25bb62 |
1884 | } |
5beb625e |
1885 | push @OUT, ");\n1;\n"; |
1886 | |
1887 | WriteIfChanged('Exact.pl', @OUT); |
1888 | } |
cf25bb62 |
1889 | |
5beb625e |
1890 | ## Write Canonical.pl |
1891 | { |
1892 | my @OUT = ( |
1893 | $HEADER, |
1894 | "##\n", |
1895 | "## Data in this file used by ../utf8_heavy.pl\n", |
1896 | "##\n\n", |
1897 | "## Mapping from lc(canonical name) to filename in ./lib\n", |
1898 | "%utf8::Canonical = (\n", |
1899 | ); |
1900 | my $Trail = ""; ## used just to keep the spacing pretty |
1901 | for my $Name (sort keys %Canonical) |
cf25bb62 |
1902 | { |
5beb625e |
1903 | my $File = $Canonical{$Name}; |
1904 | if ($CaComment{$Name}) { |
1905 | push @OUT, "\n" if not $Trail; |
1906 | push @OUT, " # $CaComment{$Name}\n"; |
1907 | $Trail = "\n"; |
1908 | } else { |
1909 | $Trail = ""; |
cf25bb62 |
1910 | } |
5beb625e |
1911 | $Name = $Name =~ m/\W/ ? qq/'$Name'/ : " $Name "; |
1912 | my $Text = sprintf(" %-41s => %s,\n$Trail", $Name, qq/'$File'/); |
1913 | push @OUT, Text::Tabs::unexpand($Text); |
cf25bb62 |
1914 | } |
5beb625e |
1915 | push @OUT, ");\n1\n"; |
1916 | WriteIfChanged('Canonical.pl', @OUT); |
d2d499f5 |
1917 | } |
5beb625e |
1918 | |
1919 | MakePropTestScript() if $MakeTestScript; |
d2d499f5 |
1920 | } |
1921 | |
5beb625e |
1922 | |
551b6b6f |
1923 | sub SpecialCasing_txt() |
cf25bb62 |
1924 | { |
1925 | # |
1926 | # Read in the special cases. |
1927 | # |
983ffd37 |
1928 | |
cf25bb62 |
1929 | my %CaseInfo; |
1930 | |
551b6b6f |
1931 | if (not open IN, "SpecialCasing.txt") { |
1932 | die "$0: SpecialCasing.txt: $!\n"; |
cf25bb62 |
1933 | } |
1934 | while (<IN>) { |
1935 | next unless /^[0-9A-Fa-f]+;/; |
1936 | s/\#.*//; |
1937 | s/\s+$//; |
1938 | |
1939 | my ($code, $lower, $title, $upper, $condition) = split(/\s*;\s*/); |
1940 | |
1941 | if ($condition) { # not implemented yet |
1942 | print "# SKIPPING $_\n" if $Verbose; |
1943 | next; |
1944 | } |
1945 | |
1946 | # Wait until all the special cases have been read since |
1947 | # they are not listed in numeric order. |
1948 | my $ix = hex($code); |
a6da3000 |
1949 | push @{$CaseInfo{Lower}}, [ $ix, $code, $lower ] |
1950 | unless $code eq $lower; |
1951 | push @{$CaseInfo{Title}}, [ $ix, $code, $title ] |
1952 | unless $code eq $title; |
1953 | push @{$CaseInfo{Upper}}, [ $ix, $code, $upper ] |
1954 | unless $code eq $upper; |
cf25bb62 |
1955 | } |
1956 | close IN; |
1957 | |
1958 | # Now write out the special cases properties in their code point order. |
1959 | # Prepend them to the To/{Upper,Lower,Title}.pl. |
1960 | |
1961 | for my $case (qw(Lower Title Upper)) |
1962 | { |
1963 | my $NormalCase = do "To/$case.pl" || die "$0: $@\n"; |
cf25bb62 |
1964 | |
b08cf34e |
1965 | my @OUT = |
1966 | ( |
1967 | $HEADER, "\n", |
1968 | "# The key UTF-8 _bytes_, the value UTF-8 (speed hack)\n", |
1969 | "%utf8::ToSpec$case =\n(\n", |
1970 | ); |
cf25bb62 |
1971 | |
1972 | for my $prop (sort { $a->[0] <=> $b->[0] } @{$CaseInfo{$case}}) { |
1973 | my ($ix, $code, $to) = @$prop; |
1974 | my $tostr = |
1975 | join "", map { sprintf "\\x{%s}", $_ } split ' ', $to; |
b08cf34e |
1976 | push @OUT, sprintf qq["%s" => "$tostr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $ix))); |
5cb851a6 |
1977 | # Remove any single-character mappings for |
1978 | # the same character since we are going for |
1979 | # the special casing rules. |
1980 | $NormalCase =~ s/^$code\t\t\w+\n//m; |
cf25bb62 |
1981 | } |
5beb625e |
1982 | push @OUT, ( |
1983 | ");\n\n", |
1984 | "return <<'END';\n", |
1985 | $NormalCase, |
1986 | "END\n" |
1987 | ); |
d07a55ed |
1988 | WriteIfChanged(["To","$case.pl"], @OUT); |
d2d499f5 |
1989 | } |
d2d499f5 |
1990 | } |
1991 | |
c4051cc5 |
1992 | # |
1993 | # Read in the case foldings. |
1994 | # |
551b6b6f |
1995 | # We will do full case folding, C + F + I (see CaseFolding.txt). |
c4051cc5 |
1996 | # |
551b6b6f |
1997 | sub CaseFolding_txt() |
cf25bb62 |
1998 | { |
551b6b6f |
1999 | if (not open IN, "CaseFolding.txt") { |
2000 | die "$0: CaseFolding.txt: $!\n"; |
cf25bb62 |
2001 | } |
c4051cc5 |
2002 | |
cf25bb62 |
2003 | my $Fold = Table->New(); |
c4051cc5 |
2004 | my %Fold; |
2005 | |
cf25bb62 |
2006 | while (<IN>) { |
254ba52a |
2007 | # Skip status 'S', simple case folding |
c4051cc5 |
2008 | next unless /^([0-9A-Fa-f]+)\s*;\s*([CFI])\s*;\s*([0-9A-Fa-f]+(?: [0-9A-Fa-f]+)*)\s*;/; |
2009 | |
cf25bb62 |
2010 | my ($code, $status, $fold) = (hex($1), $2, $3); |
c4051cc5 |
2011 | |
2012 | if ($status eq 'C') { # Common: one-to-one folding |
254ba52a |
2013 | # No append() since several codes may fold into one. |
cf25bb62 |
2014 | $Fold->RawAppendRange($code, $code, $fold); |
c4051cc5 |
2015 | } else { # F: full, or I: dotted uppercase I -> dotless lowercase I |
cf25bb62 |
2016 | $Fold{$code} = $fold; |
c4051cc5 |
2017 | } |
2018 | } |
cf25bb62 |
2019 | close IN; |
c4051cc5 |
2020 | |
cf25bb62 |
2021 | $Fold->Write("To/Fold.pl"); |
c4051cc5 |
2022 | |
2023 | # |
2024 | # Prepend the special foldings to the common foldings. |
2025 | # |
c4051cc5 |
2026 | my $CommonFold = do "To/Fold.pl" || die "$0: To/Fold.pl: $!\n"; |
5beb625e |
2027 | |
b08cf34e |
2028 | my @OUT = |
2029 | ( |
2030 | $HEADER, "\n", |
2031 | "# The ke UTF-8 _bytes_, the value UTF-8 (speed hack)\n", |
2032 | "%utf8::ToSpecFold =\n(\n", |
2033 | ); |
cf25bb62 |
2034 | for my $code (sort { $a <=> $b } keys %Fold) { |
2035 | my $foldstr = |
2036 | join "", map { sprintf "\\x{%s}", $_ } split ' ', $Fold{$code}; |
b08cf34e |
2037 | push @OUT, sprintf qq["%s" => "$foldstr",\n], join("", map { sprintf "\\x%02X", $_ } unpack("U0C*", pack("U", $code))); |
c4051cc5 |
2038 | } |
5beb625e |
2039 | push @OUT, ( |
2040 | ");\n\n", |
2041 | "return <<'END';\n", |
2042 | $CommonFold, |
2043 | "END\n", |
2044 | ); |
2045 | |
d07a55ed |
2046 | WriteIfChanged(["To","Fold.pl"], @OUT); |
c4051cc5 |
2047 | } |
2048 | |
cf25bb62 |
2049 | ## Do it.... |
2050 | |
12ac2576 |
2051 | Build_Aliases(); |
44da8cae |
2052 | UnicodeData_Txt(); |
cf25bb62 |
2053 | PropList_txt(); |
2054 | |
2055 | Scripts_txt(); |
2056 | Blocks_txt(); |
2057 | |
5beb625e |
2058 | WriteAllMappings(); |
2059 | |
551b6b6f |
2060 | LineBreak_Txt(); |
2061 | ArabicShaping_txt(); |
12ac2576 |
2062 | EastAsianWidth_txt(); |
2063 | HangulSyllableType_txt(); |
cf25bb62 |
2064 | Jamo_txt(); |
551b6b6f |
2065 | SpecialCasing_txt(); |
2066 | CaseFolding_txt(); |
cf25bb62 |
2067 | |
5beb625e |
2068 | exit(0); |
cf25bb62 |
2069 | |
5beb625e |
2070 | ## TRAILING CODE IS USED BY MakePropTestScript() |
2071 | __DATA__ |
2072 | use strict; |
2073 | use warnings; |
2074 | |
2075 | my $Tests = 0; |
2076 | my $Fails = 0; |
cf25bb62 |
2077 | |
5beb625e |
2078 | sub Expect($$$) |
2079 | { |
2080 | my $Expect = shift; |
2081 | my $String = shift; |
2082 | my $Regex = shift; |
2083 | my $Line = (caller)[2]; |
2084 | |
2085 | $Tests++; |
2086 | my $RegObj; |
2087 | my $result = eval { |
2088 | $RegObj = qr/$Regex/; |
2089 | $String =~ $RegObj ? 1 : 0 |
2090 | }; |
2091 | |
2092 | if (not defined $result) { |
2093 | print "couldn't compile /$Regex/ on $0 line $Line: $@\n"; |
2094 | $Fails++; |
2095 | } elsif ($result ^ $Expect) { |
2096 | print "bad result (expected $Expect) on $0 line $Line: $@\n"; |
2097 | $Fails++; |
2098 | } |
2099 | } |
d73e5302 |
2100 | |
5beb625e |
2101 | sub Error($) |
2102 | { |
2103 | my $Regex = shift; |
2104 | $Tests++; |
2105 | if (eval { 'x' =~ qr/$Regex/; 1 }) { |
2106 | $Fails++; |
2107 | my $Line = (caller)[2]; |
2108 | print "expected error for /$Regex/ on $0 line $Line: $@\n"; |
2109 | } |
2110 | } |
2111 | |
2112 | sub Finished() |
2113 | { |
2114 | if ($Fails == 0) { |
2115 | print "All $Tests tests passed.\n"; |
2116 | exit(0); |
2117 | } else { |
2118 | print "$Tests tests, $Fails failed!\n"; |
2119 | exit(-1); |
2120 | } |
2121 | } |