unicode-muncher.pl 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  1. #!/usr/bin/perl -w
  2. # unicode-muncher.pl -- generate Unicode database for java.lang.Character
  3. # Copyright (C) 1998, 2002, 2004 Free Software Foundation, Inc.
  4. #
  5. # This file is part of GNU Classpath.
  6. #
  7. # GNU Classpath is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2, or (at your option)
  10. # any later version.
  11. #
  12. # GNU Classpath is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with GNU Classpath; see the file COPYING. If not, write to the
  19. # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. # 02110-1301 USA.
  21. #
  22. # Linking this library statically or dynamically with other modules is
  23. # making a combined work based on this library. Thus, the terms and
  24. # conditions of the GNU General Public License cover the whole
  25. # combination.
  26. #
  27. # As a special exception, the copyright holders of this library give you
  28. # permission to link this library with independent modules to produce an
  29. # executable, regardless of the license terms of these independent
  30. # modules, and to copy and distribute the resulting executable under
  31. # terms of your choice, provided that you also meet, for each linked
  32. # independent module, the terms and conditions of the license of that
  33. # module. An independent module is a module which is not derived from
  34. # or based on this library. If you modify this library, you may extend
  35. # this exception to your version of the library, but you are not
  36. # obligated to do so. If you do not wish to do so, delete this
  37. # exception statement from your version.
  38. # Code for reading UnicodeData-3.0.0.txt and SpecialCasing-2.txt to generate
  39. # the code for gnu.java.lang.CharData. The relevant files can be found here:
  40. #
  41. # http://www.unicode.org/Public/3.0-Update/UnicodeData-3.0.0.txt
  42. # http://www.unicode.org/Public/3.0-Update/SpecialCasing-2.txt
  43. #
  44. # Inspired by code from Jochen Hoenicke.
  45. # author Eric Blake <ebb9@email.byu.edu>
  46. #
  47. # Usage: ./unicode-muncher <UnicodeData.txt> <SpecialCasing> <CharData.java>
  48. # where <UnicodeData.txt> is obtained from www.unicode.org (named
  49. # UnicodeData-3.0.0.txt for Unicode version 3.0.0), <SpecialCasing>
  50. # is obtained from www.unicode too (named SpecialCasing-2.txt for Unicode
  51. # version 3.0.0), and <CharData.java> is the final location for the Java
  52. # interface gnu.java.lang.CharData. As of JDK 1.4, use Unicode version 3.0.0
  53. # for best results.
  54. ##
  55. ## Convert a 16-bit integer to a Java source code String literal character
  56. ##
  57. sub javaChar($) {
  58. my ($char) = @_;
  59. die "Out of range: $char\n" if $char < -0x8000 or $char > 0xffff;
  60. $char += 0x10000 if $char < 0;
  61. # Special case characters that must be escaped, or are shorter as ASCII
  62. return sprintf("\\%03o", $char) if $char < 0x20;
  63. return "\\\"" if $char == 0x22;
  64. return "\\\\" if $char == 0x5c;
  65. return pack("C", $char) if $char < 0x7f;
  66. return sprintf("\\u%04x", $char);
  67. }
  68. ##
  69. ## Convert the text UnicodeData file from www.unicode.org into a Java
  70. ## interface with string constants holding the compressed information.
  71. ##
  72. my @TYPECODES = qw(Cn Lu Ll Lt Lm Lo Mn Me Mc Nd Nl No Zs Zl Zp Cc Cf
  73. SKIPPED Co Cs Pd Ps Pe Pc Po Sm Sc Sk So Pi Pf);
  74. my @DIRCODES = qw(L R AL EN ES ET AN CS NSM BN B S WS ON LRE LRO RLE RLO PDF);
  75. my $NOBREAK_FLAG = 32;
  76. my $MIRRORED_FLAG = 64;
  77. my %special = ();
  78. my @info = ();
  79. my $titlecase = "";
  80. my $count = 0;
  81. my $range = 0;
  82. die "Usage: $0 <UnicodeData.txt> <SpecialCasing.txt> <CharData.java>"
  83. unless @ARGV == 3;
  84. $| = 1;
  85. print "GNU Classpath Unicode Attribute Database Generator 2.1\n";
  86. print "Copyright (C) 1998, 2002 Free Software Foundation, Inc.\n";
  87. # Stage 0: Parse the special casing file
  88. print "Parsing special casing file\n";
  89. open (SPECIAL, "< $ARGV[1]") || die "Can't open special casing file: $!\n";
  90. while (<SPECIAL>) {
  91. next if /^\#/;
  92. my ($ch, undef, undef, $upper) = split / *; */;
  93. # This grabs only the special casing for multi-char uppercase. Note that
  94. # there are no multi-char lowercase, and that Sun ignores multi-char
  95. # titlecase rules. This script omits 3 special cases in Unicode 3.0.0,
  96. # which must be hardcoded in java.lang.String:
  97. # \u03a3 (Sun ignores this special case)
  98. # \u0049 - lowercases to \u0131, but only in Turkish locale
  99. # \u0069 - uppercases to \u0130, but only in Turkish locale
  100. next unless defined $upper and $upper =~ / /;
  101. $special{hex $ch} = [map {hex} split ' ', $upper];
  102. }
  103. close SPECIAL;
  104. # Stage 1: Parse the attribute file
  105. print "Parsing attributes file";
  106. open (UNICODE, "< $ARGV[0]") || die "Can't open Unicode attribute file: $!\n";
  107. while (<UNICODE>) {
  108. print "." unless $count++ % 1000;
  109. chomp;
  110. s/\r//g;
  111. my ($ch, $name, $category, undef, $bidir, $decomp, undef, undef, $numeric,
  112. $mirrored, undef, undef, $upcase, $lowcase, $title) = split ';';
  113. $ch = hex($ch);
  114. next if $ch > 0xffff; # Ignore surrogate pairs, since Java does
  115. my ($type, $numValue, $upperchar, $lowerchar, $direction);
  116. $type = 0;
  117. while ($category !~ /^$TYPECODES[$type]$/) {
  118. if (++$type == @TYPECODES) {
  119. die "$ch: Unknown type: $category";
  120. }
  121. }
  122. $type |= $NOBREAK_FLAG if ($decomp =~ /noBreak/);
  123. $type |= $MIRRORED_FLAG if ($mirrored =~ /Y/);
  124. if ($numeric =~ /^[0-9]+$/) {
  125. $numValue = $numeric;
  126. die "numValue too big: $ch, $numValue\n" if $numValue >= 0x7fff;
  127. } elsif ($numeric eq "") {
  128. # Special case sequences of 'a'-'z'
  129. if ($ch >= 0x0041 && $ch <= 0x005a) {
  130. $numValue = $ch - 0x0037;
  131. } elsif ($ch >= 0x0061 && $ch <= 0x007a) {
  132. $numValue = $ch - 0x0057;
  133. } elsif ($ch >= 0xff21 && $ch <= 0xff3a) {
  134. $numValue = $ch - 0xff17;
  135. } elsif ($ch >= 0xff41 && $ch <= 0xff5a) {
  136. $numValue = $ch - 0xff37;
  137. } else {
  138. $numValue = -1;
  139. }
  140. } else {
  141. $numValue = -2;
  142. }
  143. $upperchar = $upcase ? hex($upcase) - $ch : 0;
  144. $lowerchar = $lowcase ? hex($lowcase) - $ch : 0;
  145. if ($title ne $upcase) {
  146. my $titlechar = $title ? hex($title) : $ch;
  147. $titlecase .= pack("n2", $ch, $titlechar);
  148. }
  149. $direction = 0;
  150. while ($bidir !~ /^$DIRCODES[$direction]$/) {
  151. if (++$direction == @DIRCODES) {
  152. $direction = -1;
  153. last;
  154. }
  155. }
  156. $direction <<= 2;
  157. $direction += $#{$special{$ch}} if defined $special{$ch};
  158. if ($range) {
  159. die "Expecting end of range at $ch\n" unless $name =~ /Last>$/;
  160. for ($range + 1 .. $ch - 1) {
  161. $info[$_] = pack("n5", $type, $numValue, $upperchar,
  162. $lowerchar, $direction);
  163. }
  164. $range = 0;
  165. } elsif ($name =~ /First>$/) {
  166. $range = $ch;
  167. }
  168. $info[$ch] = pack("n5", $type, $numValue, $upperchar, $lowerchar,
  169. $direction);
  170. }
  171. close UNICODE;
  172. # Stage 2: Compress the data structures
  173. printf "\nCompressing data structures";
  174. $count = 0;
  175. my $info = ();
  176. my %charhash = ();
  177. my @charinfo = ();
  178. for my $ch (0 .. 0xffff) {
  179. print "." unless $count++ % 0x1000;
  180. $info[$ch] = pack("n5", 0, -1, 0, 0, -4) unless defined $info[$ch];
  181. my ($type, $numVal, $upper, $lower, $direction) = unpack("n5", $info[$ch]);
  182. if (! exists $charhash{$info[$ch]}) {
  183. push @charinfo, [ $numVal, $upper, $lower, $direction ];
  184. $charhash{$info[$ch]} = $#charinfo;
  185. }
  186. $info .= pack("n", ($charhash{$info[$ch]} << 7) | $type);
  187. }
  188. my $charlen = @charinfo;
  189. my $bestshift;
  190. my $bestest = 1000000;
  191. my $bestblkstr;
  192. die "Too many unique character entries: $charlen\n" if $charlen > 512;
  193. print "\nUnique character entries: $charlen\n";
  194. for my $i (3 .. 8) {
  195. my $blksize = 1 << $i;
  196. my %blocks = ();
  197. my @blkarray = ();
  198. my ($j, $k);
  199. print "shift: $i";
  200. for ($j = 0; $j < 0x10000; $j += $blksize) {
  201. my $blkkey = substr $info, 2 * $j, 2 * $blksize;
  202. if (! exists $blocks{$blkkey}) {
  203. push @blkarray, $blkkey;
  204. $blocks{$blkkey} = $#blkarray;
  205. }
  206. }
  207. my $blknum = @blkarray;
  208. my $blocklen = $blknum * $blksize;
  209. printf " before %5d", $blocklen;
  210. # Now we try to pack the blkarray as tight as possible by finding matching
  211. # heads and tails.
  212. for ($j = $blksize - 1; $j > 0; $j--) {
  213. my %tails = ();
  214. for $k (0 .. $#blkarray) {
  215. next unless defined $blkarray[$k];
  216. my $len = length $blkarray[$k];
  217. my $tail = substr $blkarray[$k], $len - $j * 2;
  218. if (exists $tails{$tail}) {
  219. push @{$tails{$tail}}, $k;
  220. } else {
  221. $tails{$tail} = [ $k ];
  222. }
  223. }
  224. # tails are calculated, now calculate the heads and merge.
  225. BLOCK:
  226. for $k (0 .. $#blkarray) {
  227. next unless defined $blkarray[$k];
  228. my $tomerge = $k;
  229. while (1) {
  230. my $head = substr($blkarray[$tomerge], 0, $j * 2);
  231. my $entry = $tails{$head};
  232. next BLOCK unless defined $entry;
  233. my $other = shift @{$entry};
  234. if ($other == $tomerge) {
  235. if (@{$entry}) {
  236. push @{$entry}, $other;
  237. $other = shift @{$entry};
  238. } else {
  239. push @{$entry}, $other;
  240. next BLOCK;
  241. }
  242. }
  243. if (@{$entry} == 0) {
  244. delete $tails{$head};
  245. }
  246. # a match was found
  247. my $merge = $blkarray[$other]
  248. . substr($blkarray[$tomerge], $j * 2);
  249. $blocklen -= $j;
  250. $blknum--;
  251. if ($other < $tomerge) {
  252. $blkarray[$tomerge] = undef;
  253. $blkarray[$other] = $merge;
  254. my $len = length $merge;
  255. my $tail = substr $merge, $len - $j * 2;
  256. $tails{$tail} = [ map { $_ == $tomerge ? $other : $_ }
  257. @{$tails{$tail}} ];
  258. next BLOCK;
  259. }
  260. $blkarray[$tomerge] = $merge;
  261. $blkarray[$other] = undef;
  262. }
  263. }
  264. }
  265. my $blockstr;
  266. for $k (0 .. $#blkarray) {
  267. $blockstr .= $blkarray[$k] if defined $blkarray[$k];
  268. }
  269. die "Unexpected $blocklen" if length($blockstr) != 2 * $blocklen;
  270. my $estimate = 2 * $blocklen + (0x20000 >> $i);
  271. printf " after merge %5d: %6d bytes\n", $blocklen, $estimate;
  272. if ($estimate < $bestest) {
  273. $bestest = $estimate;
  274. $bestshift = $i;
  275. $bestblkstr = $blockstr;
  276. }
  277. }
  278. my @blocks;
  279. my $blksize = 1 << $bestshift;
  280. for (my $j = 0; $j < 0x10000; $j += $blksize) {
  281. my $blkkey = substr $info, 2 * $j, 2 * $blksize;
  282. my $index = index $bestblkstr, $blkkey;
  283. while ($index & 1) {
  284. die "not found: $j" if $index == -1;
  285. $index = index $bestblkstr, $blkkey, $index + 1;
  286. }
  287. push @blocks, ($index / 2 - $j) & 0xffff;
  288. }
  289. # Phase 3: Generate the file
  290. die "UTF-8 limit of blocks may be exceeded: " . scalar(@blocks) . "\n"
  291. if @blocks > 0xffff / 3;
  292. die "UTF-8 limit of data may be exceeded: " . length($bestblkstr) . "\n"
  293. if length($bestblkstr) > 0xffff / 3;
  294. {
  295. print "Generating $ARGV[2] with shift of $bestshift";
  296. my ($i, $j);
  297. open OUTPUT, "> $ARGV[2]" or die "Failed creating output file: $!\n";
  298. print OUTPUT <<EOF;
  299. /* gnu/java/lang/CharData -- Database for java.lang.Character Unicode info
  300. Copyright (C) 2002 Free Software Foundation, Inc.
  301. *** This file is generated by scripts/unicode-muncher.pl ***
  302. This file is part of GNU Classpath.
  303. GNU Classpath is free software; you can redistribute it and/or modify
  304. it under the terms of the GNU General Public License as published by
  305. the Free Software Foundation; either version 2, or (at your option)
  306. any later version.
  307. GNU Classpath is distributed in the hope that it will be useful, but
  308. WITHOUT ANY WARRANTY; without even the implied warranty of
  309. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  310. General Public License for more details.
  311. You should have received a copy of the GNU General Public License
  312. along with GNU Classpath; see the file COPYING. If not, write to the
  313. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  314. 02110-1301 USA.
  315. Linking this library statically or dynamically with other modules is
  316. making a combined work based on this library. Thus, the terms and
  317. conditions of the GNU General Public License cover the whole
  318. combination.
  319. As a special exception, the copyright holders of this library give you
  320. permission to link this library with independent modules to produce an
  321. executable, regardless of the license terms of these independent
  322. modules, and to copy and distribute the resulting executable under
  323. terms of your choice, provided that you also meet, for each linked
  324. independent module, the terms and conditions of the license of that
  325. module. An independent module is a module which is not derived from
  326. or based on this library. If you modify this library, you may extend
  327. this exception to your version of the library, but you are not
  328. obligated to do so. If you do not wish to do so, delete this
  329. exception statement from your version. */
  330. package gnu.java.lang;
  331. /**
  332. * This contains the info about the unicode characters, that
  333. * java.lang.Character needs. It is generated automatically from
  334. * <code>$ARGV[0]</code> and
  335. * <code>$ARGV[1]</code>, by some
  336. * perl scripts. These Unicode definition files can be found on the
  337. * <a href="http://www.unicode.org">http://www.unicode.org</a> website.
  338. * JDK 1.4 uses Unicode version 3.0.0.
  339. *
  340. * The data is stored as string constants, but Character will convert these
  341. * Strings to their respective <code>char[]</code> components. The field
  342. * <code>BLOCKS</code> stores the offset of a block of 2<sup>SHIFT</sup>
  343. * characters within <code>DATA</code>. The DATA field, in turn, stores
  344. * information about each character in the low order bits, and an offset
  345. * into the attribute tables <code>UPPER</code>, <code>LOWER</code>,
  346. * <code>NUM_VALUE</code>, and <code>DIRECTION</code>. Notice that the
  347. * attribute tables are much smaller than 0xffff entries; as many characters
  348. * in Unicode share common attributes. The DIRECTION table also contains
  349. * a field for detecting characters with multi-character uppercase expansions.
  350. * Next, there is a listing for <code>TITLE</code> exceptions (most characters
  351. * just have the same title case as upper case). Finally, there are two
  352. * tables for multi-character capitalization, <code>UPPER_SPECIAL</code>
  353. * which lists the characters which are special cased, and
  354. * <code>UPPER_EXPAND</code>, which lists their expansion.
  355. *
  356. * \@author scripts/unicode-muncher.pl (written by Jochen Hoenicke,
  357. * Eric Blake)
  358. * \@see Character
  359. * \@see String
  360. */
  361. public interface CharData
  362. {
  363. /**
  364. * The Unicode definition file that was parsed to build this database.
  365. */
  366. String SOURCE = \"$ARGV[0]\";
  367. /**
  368. * The character shift amount to look up the block offset. In other words,
  369. * <code>(char) (BLOCKS.value[ch >> SHIFT] + ch)</code> is the index where
  370. * <code>ch</code> is described in <code>DATA</code>.
  371. */
  372. int SHIFT = $bestshift;
  373. /**
  374. * The mapping of character blocks to their location in <code>DATA</code>.
  375. * Each entry has been adjusted so that the 16-bit sum with the desired
  376. * character gives the actual index into <code>DATA</code>.
  377. */
  378. String BLOCKS
  379. EOF
  380. for ($i = 0; $i < @blocks / 11; $i++) {
  381. print OUTPUT $i ? "\n + \"" : " = \"";
  382. for $j (0 .. 10) {
  383. last if @blocks <= $i * 11 + $j;
  384. my $val = $blocks[$i * 11 + $j];
  385. print OUTPUT javaChar($val);
  386. }
  387. print OUTPUT "\"";
  388. }
  389. print OUTPUT <<EOF;
  390. ;
  391. /**
  392. * Information about each character. The low order 5 bits form the
  393. * character type, the next bit is a flag for non-breaking spaces, and the
  394. * next bit is a flag for mirrored directionality. The high order 9 bits
  395. * form the offset into the attribute tables. Note that this limits the
  396. * number of unique character attributes to 512, which is not a problem
  397. * as of Unicode version 3.2.0, but may soon become one.
  398. */
  399. String DATA
  400. EOF
  401. my $len = length($bestblkstr) / 2;
  402. for ($i = 0; $i < $len / 11; $i++) {
  403. print OUTPUT $i ? "\n + \"" : " = \"";
  404. for $j (0 .. 10) {
  405. last if $len <= $i * 11 + $j;
  406. my $val = unpack "n", substr($bestblkstr, 2 * ($i * 11 + $j), 2);
  407. print OUTPUT javaChar($val);
  408. }
  409. print OUTPUT "\"";
  410. }
  411. print OUTPUT <<EOF;
  412. ;
  413. /**
  414. * This is the attribute table for computing the numeric value of a
  415. * character. The value is -1 if Unicode does not define a value, -2
  416. * if the value is not a positive integer, otherwise it is the value.
  417. * Note that this is a signed value, but stored as an unsigned char
  418. * since this is a String literal.
  419. */
  420. String NUM_VALUE
  421. EOF
  422. $len = @charinfo;
  423. for ($i = 0; $i < $len / 11; $i++) {
  424. print OUTPUT $i ? "\n + \"" : " = \"";
  425. for $j (0 .. 10) {
  426. last if $len <= $i * 11 + $j;
  427. my $val = $charinfo[$i * 11 + $j][0];
  428. print OUTPUT javaChar($val);
  429. }
  430. print OUTPUT "\"";
  431. }
  432. print OUTPUT <<EOF;
  433. ;
  434. /**
  435. * This is the attribute table for computing the single-character uppercase
  436. * representation of a character. The value is the signed difference
  437. * between the character and its uppercase version. Note that this is
  438. * stored as an unsigned char since this is a String literal. When
  439. * capitalizing a String, you must first check if a multi-character uppercase
  440. * sequence exists before using this character.
  441. */
  442. String UPPER
  443. EOF
  444. $len = @charinfo;
  445. for ($i = 0; $i < $len / 11; $i++) {
  446. print OUTPUT $i ? "\n + \"" : " = \"";
  447. for $j (0 .. 10) {
  448. last if $len <= $i * 11 + $j;
  449. my $val = $charinfo[$i * 11 + $j][1];
  450. print OUTPUT javaChar($val);
  451. }
  452. print OUTPUT "\"";
  453. }
  454. print OUTPUT <<EOF;
  455. ;
  456. /**
  457. * This is the attribute table for computing the lowercase representation
  458. * of a character. The value is the signed difference between the
  459. * character and its lowercase version. Note that this is stored as an
  460. * unsigned char since this is a String literal.
  461. */
  462. String LOWER
  463. EOF
  464. $len = @charinfo;
  465. for ($i = 0; $i < $len / 13; $i++) {
  466. print OUTPUT $i ? "\n + \"" : " = \"";
  467. for $j (0 .. 12) {
  468. last if $len <= $i * 13 + $j;
  469. my $val = $charinfo[$i * 13 + $j][2];
  470. print OUTPUT javaChar($val);
  471. }
  472. print OUTPUT "\"";
  473. }
  474. print OUTPUT <<EOF;
  475. ;
  476. /**
  477. * This is the attribute table for computing the directionality class
  478. * of a character, as well as a marker of characters with a multi-character
  479. * capitalization. The direction is taken by performing a signed shift
  480. * right by 2 (where a result of -1 means an unknown direction, such as
  481. * for undefined characters). The lower 2 bits form a count of the
  482. * additional characters that will be added to a String when performing
  483. * multi-character uppercase expansion. This count is also used, along with
  484. * the offset in UPPER_SPECIAL, to determine how much of UPPER_EXPAND to use
  485. * when performing the case conversion. Note that this information is stored
  486. * as an unsigned char since this is a String literal.
  487. */
  488. String DIRECTION
  489. EOF
  490. $len = @charinfo;
  491. for ($i = 0; $i < $len / 17; $i++) {
  492. print OUTPUT $i ? "\n + \"" : " = \"";
  493. for $j (0 .. 16) {
  494. last if $len <= $i * 17 + $j;
  495. my $val = $charinfo[$i * 17 + $j][3];
  496. print OUTPUT javaChar($val);
  497. }
  498. print OUTPUT "\"";
  499. }
  500. print OUTPUT <<EOF;
  501. ;
  502. /**
  503. * This is the listing of titlecase special cases (all other characters
  504. * can use <code>UPPER</code> to determine their titlecase). The listing
  505. * is a sorted sequence of character pairs; converting the first character
  506. * of the pair to titlecase produces the second character.
  507. */
  508. String TITLE
  509. EOF
  510. $len = length($titlecase) / 2;
  511. for ($i = 0; $i < $len / 11; $i++) {
  512. print OUTPUT $i ? "\n + \"" : " = \"";
  513. for $j (0 .. 10) {
  514. last if $len <= $i * 11 + $j;
  515. my $val = unpack "n", substr($titlecase, 2 * ($i * 11 + $j), 2);
  516. print OUTPUT javaChar($val);
  517. }
  518. print OUTPUT "\"";
  519. }
  520. print OUTPUT <<EOF;
  521. ;
  522. /**
  523. * This is a listing of characters with multi-character uppercase sequences.
  524. * A character appears in this list exactly when it has a non-zero entry
  525. * in the low-order 2-bit field of DIRECTION. The listing is a sorted
  526. * sequence of pairs (hence a binary search on the even elements is an
  527. * efficient way to lookup a character). The first element of a pair is the
  528. * character with the expansion, and the second is the index into
  529. * UPPER_EXPAND where the expansion begins. Use the 2-bit field of
  530. * DIRECTION to determine where the expansion ends.
  531. */
  532. String UPPER_SPECIAL
  533. EOF
  534. my @list = sort {$a <=> $b} keys %special;
  535. my $expansion = "";
  536. my $offset = 0;
  537. $len = @list;
  538. for ($i = 0; $i < $len / 5; $i++) {
  539. print OUTPUT $i ? "\n + \"" : " = \"";
  540. for $j (0 .. 4) {
  541. last if $len <= $i * 5 + $j;
  542. my $ch = $list[$i * 5 + $j];
  543. print OUTPUT javaChar($ch);
  544. print OUTPUT javaChar($offset);
  545. $offset += @{$special{$ch}};
  546. $expansion .= pack "n*", @{$special{$ch}};
  547. }
  548. print OUTPUT "\"";
  549. }
  550. print OUTPUT <<EOF;
  551. ;
  552. /**
  553. * This is the listing of special case multi-character uppercase sequences.
  554. * Characters listed in UPPER_SPECIAL index into this table to find their
  555. * uppercase expansion. Remember that you must also perform special-casing
  556. * on two single-character sequences in the Turkish locale, which are not
  557. * covered here in CharData.
  558. */
  559. String UPPER_EXPAND
  560. EOF
  561. $len = length($expansion) / 2;
  562. for ($i = 0; $i < $len / 11; $i++) {
  563. print OUTPUT $i ? "\n + \"" : " = \"";
  564. for $j (0 .. 10) {
  565. last if $len <= $i * 11 + $j;
  566. my $val = unpack "n", substr($expansion, 2 * ($i * 11 + $j), 2);
  567. print OUTPUT javaChar($val);
  568. }
  569. print OUTPUT "\"";
  570. }
  571. print OUTPUT ";\n}\n";
  572. close OUTPUT;
  573. }
  574. print "\nDone.\n";