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