aboutsummaryrefslogtreecommitdiffstats
path: root/gcc-4.8.3/libjava/scripts/unicode-to-chartables.pl
blob: 49095352b3ce2426a7d5c62c56e6be8ab3ba8b35 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
#!/usr/bin/perl -w
# unicode-to-chartables.pl -- generate Unicode database for java.lang.Character
# Copyright (C) 1998, 2002, 2004, 2006  Free Software Foundation, Inc.
#
# This file is part of GNU Classpath.
#
# GNU Classpath is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# GNU Classpath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Classpath; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301 USA.
#
# Linking this library statically or dynamically with other modules is
# making a combined work based on this library.  Thus, the terms and
# conditions of the GNU General Public License cover the whole
# combination.
#
# As a special exception, the copyright holders of this library give you
# permission to link this library with independent modules to produce an
# executable, regardless of the license terms of these independent
# modules, and to copy and distribute the resulting executable under
# terms of your choice, provided that you also meet, for each linked
# independent module, the terms and conditions of the license of that
# module.  An independent module is a module which is not derived from
# or based on this library.  If you modify this library, you may extend
# this exception to your version of the library, but you are not
# obligated to do so.  If you do not wish to do so, delete this
# exception statement from your version.

# Code for reading UnicodeData-4.0.0.txt and SpecialCasing-4.0.0.txt to generate
# the code for java-chartables.h. The relevant files can be found here:
#
#   http://www.unicode.org/Public/4.0-Update/UnicodeData-4.0.0.txt
#   http://www.unicode.org/Public/4.0-Update/SpecialCasing-4.0.0.txt
#
# Inspired by code from Jochen Hoenicke.
# author Eric Blake <ebb9@email.byu.edu>
# Unicode 4.0.0 support by Anthony Balkissoon <abalkiss@redhat.com>
#
# Usage: ./unicode-to-chartables.pl <UnicodeData> <SpecialCasing> <tables>
#   where <UnicodeData.txt> is obtained from www.unicode.org (named
#   UnicodeData-4.0.0.txt for Unicode version 4.0.0), <SpecialCasing>
#   is obtained from www.unicode too (named SpecialCasing-4.0.0.txt for Unicode
#   version 4.0.0), and <tables> is the final location for the header file
#   java-chartables.h. As of JDK 1.5, use Unicode version 4.0.0
#   for best results.


##
## Return the given variable interpreted as a 16 bit signed number.
##
sub cShort($) {
    my ($char) = @_;
    return unpack "s", pack "I", $char;
}

##
## Convert the text UnicodeData file from www.unicode.org into a header file
## interface with arrays holding the compressed information.
##
my @TYPECODES = qw(Cn Lu Ll Lt Lm Lo Mn Me Mc Nd Nl No Zs Zl Zp Cc Cf
                   SKIPPED Co Cs Pd Ps Pe Pc Po Sm Sc Sk So Pi Pf);
my @DIRCODES = qw(L R AL EN ES ET AN CS NSM BN B S WS ON LRE LRO RLE RLO PDF);

my $NOBREAK_FLAG  = 32;
my $MIRRORED_FLAG = 64;

my %special = ();

# infoArray is an array where each element is a list of character information
# for characters in a plane.  The index of each list is equal to the plane 
# that it corresponds to even though most of these lists will currently be
# empty.  This is done so that that this script can be easily modified to 
# accomodate future versions of Unicode.
my @infoArray = \((), (), (), (), (), (), (), (), 
    (), (), (), (), (), (), (), (), ());

# info is a reference to one of the lists in infoArray, depending on which 
# plane we're currently parsing.
my $info;

# largeNums is an array of numerical values that are too large to fit 
# into the 16 bit char where most numerical values are stored.  
# What is stored in the char then is a number N such that (-N - 3) is 
# the index into largeNums where the numerical value can be found.
my @largeNums = ();

my $titlecase = "";
my $count = 0;
my $range = 0;

die "Usage: $0 <UnicodeData.txt> <SpecialCasing.txt> <java-chartables.h>"
    unless @ARGV == 3;
$| = 1;
print "GNU Classpath Unicode Attribute Database Generator 2.1\n";
print "Copyright (C) 1998, 2002 Free Software Foundation, Inc.\n";


################################################################################
################################################################################
# Stage 0: Parse the special casing file
print "Parsing special casing file\n";
open (SPECIAL, "< $ARGV[1]") || die "Can't open special casing file: $!\n";
while (<SPECIAL>) {
    next if /^\#/;
    my ($ch, undef, undef, $upper) = split / *; */;

    # This grabs only the special casing for multi-char uppercase. Note that
    # there are no multi-char lowercase, and that Sun ignores multi-char
    # titlecase rules. This script omits 3 special cases in Unicode 3.0.0,
    # which must be hardcoded in java.lang.String:
    #  \u03a3 (Sun ignores this special case)
    #  \u0049 - lowercases to \u0131, but only in Turkish locale
    #  \u0069 - uppercases to \u0130, but only in Turkish locale
    next unless defined $upper and $upper =~ / /;
    $special{hex $ch} = [map {hex} split ' ', $upper];
}

close SPECIAL;


################################################################################
################################################################################
## Stage 1: Parse the attribute file
print "Parsing attributes file";
open (UNICODE, "< $ARGV[0]") || die "Can't open Unicode attribute file: $!\n";
while (<UNICODE>) {
    print "." unless $count++ % 1000;
    chomp;
    s/\r//g;
    my ($ch, $name, $category, undef, $bidir, $decomp, undef, undef, $numeric,
        $mirrored, undef, undef, $upcase, $lowcase, $title) = split ';';
    $ch = hex($ch);

    # plane tells us which Unicode code plane we're currently in and is an
    # index into infoArray.
    my $plane = int($ch / 0x10000);
    my $planeBase = $plane * 0x10000;
    $info = \@{$infoArray[$plane]};

    my ($type, $numValue, $upperchar, $lowerchar, $direction);

    $type = 0;
    while ($category !~ /^$TYPECODES[$type]$/) {
        if (++$type == @TYPECODES) {
            die "$ch: Unknown type: $category";
        }
    }
    $type |= $NOBREAK_FLAG if ($decomp =~ /noBreak/);
    $type |= $MIRRORED_FLAG if ($mirrored =~ /Y/);

    if ($numeric =~ /^[0-9]+$/) {
        $numValue = $numeric;
        # If numeric takes more than 16 bits to store we want to store that 
	# number in a separate array and store a number N in numValue such 
	# that (-N - 3) is the offset into the separate array containing the
	# large numerical value.
	if ($numValue >= 0x7fff) {
	    $numValue = -3 - @largeNums;
	    push @largeNums, $numeric;	    
	}
    } elsif ($numeric eq "") {
        # Special case sequences of 'a'-'z'
        if ($ch >= 0x0041 && $ch <= 0x005a) {
            $numValue = $ch - 0x0037;
        } elsif ($ch >= 0x0061 && $ch <= 0x007a) {
            $numValue = $ch - 0x0057;
        } elsif ($ch >= 0xff21 && $ch <= 0xff3a) {
            $numValue = $ch - 0xff17;
        } elsif ($ch >= 0xff41 && $ch <= 0xff5a) {
            $numValue = $ch - 0xff37;
        } else {
            $numValue = -1;
        }
    } else {
        $numValue = -2;
    }

    $upperchar = $upcase ? hex($upcase) - $ch : 0;
    $lowerchar = $lowcase ? hex($lowcase) - $ch : 0;
    if ($title ne $upcase) {
        my $titlechar = $title ? hex($title) : $ch;
        $titlecase .= pack("n2", $ch, $titlechar);
    }

    $direction = 0;
    while ($bidir !~ /^$DIRCODES[$direction]$/) {
        if (++$direction == @DIRCODES) {
            $direction = -1;
            last;
        }
    }
    $direction <<= 2;
    $direction += $#{$special{$ch}} if defined $special{$ch};

    if ($range) {
        die "Expecting end of range at $ch\n" unless $name =~ /Last>$/;
        for ($range + 1 .. $ch - 1) {
            $info->[$_ - $planeBase] = pack("n5", $type, $numValue, $upperchar,
                             $lowerchar, $direction);
        }
        $range = 0;
    } elsif ($name =~ /First>$/) {
        $range = $ch;
    }
    # Store all this parsed information into the element in infoArray that info
    # points to.
    $info->[$ch - $planeBase] = pack("n5", $type, $numValue, $upperchar, $lowerchar,
                      $direction);
}
close UNICODE;


################################################################################
################################################################################
## Stage 2: Compress the data structures
printf "\nCompressing data structures";
$count = 0;

# data is a String that will be used to create the DATA String containing 
# character information and offsets into the attribute tables.
my @data = ();

# charhashArray is an array of hashtables used so that we can reuse character
# attributes when characters share the same attributes ... this makes our
# attribute tables smaller.  charhash is a pointer into this array.
my @charhashArray = ({}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {});
my $charhash = ();

# charinfoArray is an array of arrays, one per plane, for storing character 
# information.  charinfo is a pointer into this array.
my @charinfoArray = \((), (), (), (), (), (), (), (), 
    (), (), (), (), (), (), (), (), ());
my $charinfo;

# charlen is an array, one element per plane, that tells us how many unique
# character attributes there are for that plane.
my @charlen = ();

for my $plane (0 .. 0x10) {
    $info = \@{$infoArray[$plane]};
    my $planeBase = $plane * 0x10000;
    $charhash = \%{$charhashArray[$plane]};
    $charinfo = \@{$charinfoArray[$plane]};

    for my $ch ($planeBase .. $planeBase + 0xffff) {
	my $index = $ch - $planeBase;
	print "." unless $count++ % 0x1000;
	$info->[$index] = pack("n5", 0, -1, 0, 0, -4) unless defined $info->[$index];
	
	my ($type, $numVal, $upper, $lower, $direction) = unpack("n5", $info->[$index]);
	if (! exists $charhash->{$info->[$index]}) {
	    # If we entered this loop that means the character we're looking at 
	    # now has attributes that are unique from those that we've looked
	    # at so far for this plane.  So we push its attributes into charinfo
	    # and store in charhash the offset into charinfo where these
	    # attributes can later be found.
	    push @{$charinfo}, [ $numVal, $upper, $lower, $direction ];
	    $charhash->{$info->[$index]} = @{$charinfo} - 1;
	    # When the file is generaged, the number we just stored in charhas
	    # will be the upper 9 bits in the DATA String that are an offset
	    # into the attribute tables.
	}
	$data[$plane] .= pack("n", ($charhash->{$info->[$index]} << 7) | $type);
    }
    $charlen[$plane] = scalar(@{$charinfoArray[$plane]});
}

# the shift that results in the best compression of the table.  This is an array
# because different shifts are better for the different tables for each plane.
my @bestshift;

# an initial guess.
my $bestest = 1000000;
my @bestblkstr;
my @blksize = ();

for my $plane (0 .. 0x10) {
    print "\n\nplane: $plane\n";
    print "Unique character entries: $charlen[$plane]\n";
    $bestest = 1000000;
    for my $i (3 .. 8) {
        my $blksize = 1 << $i;
        my %blocks = ();
        my @blkarray = ();
        my ($j, $k);
        print "shift: $i";

        for ($j = 0; $j < 0x10000; $j += $blksize) {
            my $blkkey = substr $data[$plane], 2 * $j, 2 * $blksize;
            if (! exists $blocks{$blkkey}) {
	        push @blkarray, $blkkey;
                $blocks{$blkkey} = $#blkarray;
            }
        }

        my $blknum = @blkarray;
        my $blocklen = $blknum * $blksize;
        printf " before %5d", $blocklen;

        # Now we try to pack the blkarray as tight as possible by finding matching
        # heads and tails.
        for ($j = $blksize - 1; $j > 0; $j--) {
            my %tails = ();
            for $k (0 .. $#blkarray) {
                next unless defined $blkarray[$k];
                my $len = length $blkarray[$k];
                my $tail = substr $blkarray[$k], $len - $j * 2;
                if (exists $tails{$tail}) {
                    push @{$tails{$tail}}, $k;
                } else {
                    $tails{$tail} = [ $k ];
                }
            }

            # tails are calculated, now calculate the heads and merge.
          BLOCK:
            for $k (0 .. $#blkarray) {
                next unless defined $blkarray[$k];
                my $tomerge = $k;
                while (1) {
                    my $head = substr($blkarray[$tomerge], 0, $j * 2);
                    my $entry = $tails{$head};
                    next BLOCK unless defined $entry;

                    my $other = shift @{$entry};
                    if ($other == $tomerge) {
                        if (@{$entry}) {
                            push @{$entry}, $other;
                            $other = shift @{$entry};
                        } else {
                            push @{$entry}, $other;
                            next BLOCK;
                        }
                    }
                    if (@{$entry} == 0) {
                        delete $tails{$head};
                    }

                    # a match was found
                    my $merge = $blkarray[$other]
                        . substr($blkarray[$tomerge], $j * 2);
                    $blocklen -= $j;
                    $blknum--;

                    if ($other < $tomerge) {
                        $blkarray[$tomerge] = undef;
                        $blkarray[$other] = $merge;
                        my $len = length $merge;
                        my $tail = substr $merge, $len - $j * 2;
                        $tails{$tail} = [ map { $_ == $tomerge ? $other : $_ }
                                          @{$tails{$tail}} ];
                        next BLOCK;
                    }
                    $blkarray[$tomerge] = $merge;
                    $blkarray[$other] = undef;
                }
            }
        }
        my $blockstr;
        for $k (0 .. $#blkarray) {
            $blockstr .= $blkarray[$k] if defined $blkarray[$k];
        }

        die "Unexpected $blocklen" if length($blockstr) != 2 * $blocklen;
        my $estimate = 2 * $blocklen + (0x20000 >> $i);
  
        printf " after merge %5d: %6d bytes\n", $blocklen, $estimate;
        if ($estimate < $bestest) {
            $bestest = $estimate;
            $bestshift[$plane] = $i;
            $bestblkstr[$plane] = $blockstr;
        }
    }
    $blksize[$plane] = 1 << $bestshift[$plane];
    print "best shift: ", $bestshift[$plane];
    print "     blksize: ", $blksize[$plane];
}
my @blocksArray = \((), (), (), (), (), (), (), (), 
    (), (), (), (), (), (), (), (), ());

for my $plane (0 .. 0x10) {
    for (my $j = 0; $j < 0x10000; $j += $blksize[$plane]) {
	my $blkkey = substr $data[$plane], 2 * $j, 2 * $blksize[$plane];
        my $index = index $bestblkstr[$plane], $blkkey;
        while ($index & 1) {
            die "not found: $j" if $index == -1;
            $index = index $bestblkstr[$plane], $blkkey, $index + 1;
        }
        push @{$blocksArray[$plane]}, ($index / 2 - $j) & 0xffff;
    }
}


################################################################################
################################################################################
## Stage 3: Generate the file
for my $plane (0 .. 0x10) {
    die "UTF-8 limit of blocks may be exceeded for plane $plane: " . scalar(@{$blocksArray[$plane]}) . "\n"
        if @{$blocksArray[$plane]} > 0xffff / 3;
    die "UTF-8 limit of data may be exceeded for plane $plane: " . length($bestblkstr[$plane]) . "\n"
        if length($bestblkstr[$plane]) > 0xffff / 3;
}

{
    print "\nGenerating $ARGV[2].";
    my ($i, $j);

    open OUTPUT, "> $ARGV[2]" or die "Failed creating output file: $!\n";
    print OUTPUT <<EOF;
/* java-chartables.h -- Character tables for java.lang.Character -*- c++ -*-
   Copyright (C) 2002, 2006 Free Software Foundation, Inc.
   *** This file is generated by scripts/unicode-to-chartables.pl ***

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

#ifndef __JAVA_CHARTABLES_H__
#define __JAVA_CHARTABLES_H__

// These tables are automatically generated by scripts/unicode_to_chartables.pl.
// The Unicode data comes from www.unicode.org; this header is based on
// UnicodeData-4.0.0.txt. JDK 1.5 uses Unicode version 4.0.0.
// DO NOT EDIT the tables.  Instead, fix the upstream scripts and run
// them again.

// The data is stored in C style arrays of the appropriate CNI types, to
// guarantee that the data is constant and non-relocatable.  The field
// <code>blocks</code> stores the offset of a block of 2<sup>SHIFT</sup>
// characters within <code>data</code>. The data field, in turn, stores
// information about each character in the low order bits, and an offset
// into the attribute tables <code>upper</code>, <code>lower</code>,
// <code>numValue</code>, and <code>direction</code>.  Notice that the
// attribute tables are much smaller than 0xffff entries; as many characters
// in Unicode share common attributes.  Finally, there is a listing for
// <code>title</code> exceptions (most characters just have the same title
// case as upper case).

// This file should only be included by natCharacter.cc

/**
 * The array containing the numeric values that are too large to be stored as
 * chars in NUM_VALUE.  NUM_VALUE in this case will contain a negative integer
 * N such that LARGENUMS[-N - 3] contains the correct numeric value.
 */
EOF
  print OUTPUT "static const jint largenums[] = {\n    ";
  for ($i = 0; $i < @largeNums; $i++) {
      print OUTPUT $largeNums[$i], ", ";
  }
  print OUTPUT "}";
  print OUTPUT <<EOF;
;

/**
 * The character shift amount to look up the block offset. In other words,
 * <code>(char) (blocks[p][off >> SHIFT[p]] + off)</code> is the index where
 * <code>ch</code> is described in <code>data</code>, where <code>off</code>
 * is ch & 0xffff and <code>p</code> is the plane the character belongs to.
 */
EOF
  print OUTPUT "static const int shift[] = {\n    ";
  for ($i = 0; $i < @bestshift; $i++) {
      print OUTPUT $bestshift[$i], ", ";
  }
  print OUTPUT "}";
  print OUTPUT <<EOF;
;

/**
 * The mapping of character blocks to their location in <code>data</code>.
 * Each entry has been adjusted so that a modulo 16 sum with the desired
 * character gives the actual index into <code>data</code>.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14) ;
      
      print OUTPUT "static const jchar blocks", $plane, "[] = {\n";
      for ($i = 0; $i < @{$blocksArray[$plane]} / 10; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 9) {
	      last if @{$blocksArray[$plane]} <= $i * 10 + $j;
	      my $val = $blocksArray[$plane]->[$i * 10 + $j];
	      print OUTPUT $val, ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int blocks_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT scalar(@{$blocksArray[$plane]}), ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jchar* blocks[] = {
    blocks0, blocks1, blocks2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, blocks14, NULL, NULL};

/**
 * Information about each character.  The low order 5 bits form the
 * character type, the next bit is a flag for non-breaking spaces, and the
 * next bit is a flag for mirrored directionality.  The high order 9 bits
 * form the offset into the attribute tables.  Note that this limits the
 * number of unique character attributes per plane to 512, which is not a 
 * problem as of Unicode version 4.0.0, but may soon become one.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14);
      
      print OUTPUT "static const jchar data", $plane, "[] = {\n";
      my $len = length($bestblkstr[$plane]) / 2;
      for ($i = 0; $i < $len / 10; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 9) {
	      last if $len <= $i * 10 + $j;
	      my $val = unpack "n", substr($bestblkstr[$plane], 2 * ($i * 10 + $j), 2);
	      print OUTPUT $val, ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int data_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT length($bestblkstr[$plane]) / 2, ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jchar* data[] = {
    data0, data1, data2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, data14, NULL, NULL};


/**
 * This is the attribute table for computing the numeric value of a
 * character.  The value is -1 if Unicode does not define a value, -2
 * if the value is not a positive integer, otherwise it is the value.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14);
      
      print OUTPUT "static const jshort numValue", $plane, "[] = {\n";
      $len = @{$charinfoArray[$plane]};
      for ($i = 0; $i < $len / 13; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 12) {
	      last if $len <= $i * 13 + $j;
	      my $val = $charinfoArray[$plane]->[$i * 13 + $j][0];
	      print OUTPUT cShort($val), ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int numValue_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT scalar(@{$charinfoArray[$plane]}), ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jshort* numValue[] = {
    numValue0, numValue1, numValue2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, numValue14, NULL, NULL};



/**
 * This is the attribute table for computing the uppercase representation
 * of a character.  The value is the difference between the character and
 * its uppercase version.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14);
      
      print OUTPUT "static const jshort upper", $plane, "[] = {\n";
      $len = @{$charinfoArray[$plane]};
      for ($i = 0; $i < $len / 13; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 12) {
	      last if $len <= $i * 13 + $j;
	      my $val = $charinfoArray[$plane]->[$i * 13 + $j][1];
	      print OUTPUT cShort($val), ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int upper_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT scalar(@{$charinfoArray[$plane]}), ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jshort* upper[] = {
    upper0, upper1, upper2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, upper14, NULL, NULL};


/**
 * This is the attribute table for computing the lowercase representation
 * of a character.  The value is the difference between the character and
 * its lowercase version.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14);
      
      print OUTPUT "static const jshort lower", $plane, "[] = {\n";
      $len = @{$charinfoArray[$plane]};
      for ($i = 0; $i < $len / 13; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 12) {
	      last if $len <= $i * 13 + $j;
	      my $val = $charinfoArray[$plane]->[$i * 13 + $j][2];
	      print OUTPUT cShort($val), ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int lower_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT scalar(@{$charinfoArray[$plane]}), ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jshort* lower[] = {
    lower0, lower1, lower2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, lower14, NULL, NULL};


/**
 * This is the attribute table for computing the directionality class
 * of a character.  At present, the value is in the range 0 - 18 if the
 * character has a direction, otherwise it is -1.
 */
EOF
  for ($plane = 0; $plane <= 0x10; $plane++) {
      # The following if statement handles the cases of unassigned planes
      # specially so we don't waste space with unused Strings.  As of 
      # Unicode version 4.0.0 only planes 0, 1, 2, and 14 are used.  If
      # you are updating this script to work with a later version of 
      # Unicode you may have to alter this if statement.
      next if ($plane > 2 && $plane != 14);
      
      print OUTPUT "static const jbyte direction", $plane, "[] = {\n";
      $len = @{$charinfoArray[$plane]};
      for ($i = 0; $i < $len / 19; $i++) {
	  print OUTPUT "    ";
	  for $j (0 .. 18) {
	      last if $len <= $i * 19 + $j;
	      my $val = $charinfoArray[$plane]->[$i * 19 + $j][3];
	      $val >>= 2;
	      if ($val < 0 || $val > 18){
		  $val = -1;
	      }
	      print OUTPUT cShort($val), ", ";
	  }
	  print OUTPUT "\n";
      }
      print OUTPUT "};\n\n";
  }
  print OUTPUT "static const int direction_length[] = {\n    ";
  for ($plane = 0; $plane <= 0x10; $plane++) {
      if ($plane > 2 && $plane != 14){
	  print OUTPUT "-1, ";
      }
      else {
	  print OUTPUT scalar(@{$charinfoArray[$plane]}), ", ";
      }
  }
  print OUTPUT "};\n";
  print OUTPUT <<EOF;
static const jbyte* direction[] = {
    direction0, direction1, direction2, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL, NULL, NULL, NULL, direction14, NULL, NULL};


/**
 * This is the listing of titlecase special cases (all other character
 * can use <code>upper</code> to determine their titlecase).  The listing
 * is a sequence of character pairs; converting the first character of the
 * pair to titlecase produces the second character.
 */
static const jchar title[] = {
EOF

  $len = length($titlecase) / 2;
  for ($i = 0; $i < $len / 10; $i++) {
      print OUTPUT $i ? "\n    " : "    ";
      for $j (0 .. 9) {
	  last if $len <= $i * 10 + $j;
	  my $val = unpack "n", substr($titlecase, 2 * ($i * 10 + $j), 2);
	  print OUTPUT $val, ", ";
      }
  }

  print OUTPUT "\n  };";
  print OUTPUT "\n/** Length of title. */\nstatic const int title_length = ", $len;
  print OUTPUT <<EOF;
;

#endif /* __JAVA_CHARTABLES_H__ */
EOF
  close OUTPUT;
}
print "\nDone.\n";