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