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