unicode-blocks.pl 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/usr/bin/perl -w
  2. # unicode-blocks.pl -- Script to generate java.lang.Character.UnicodeBlock
  3. # Copyright (C) 2002 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 Blocks.txt and generating (to standard out) the code for
  39. # java.lang.Character.UnicodeBlock, for pasting into java/lang/Character.java.
  40. # You should probably check that the results are accurate to the
  41. # specification, but I made sure it works OOB for Unicode 3.0.0 and JDK 1.4.
  42. # As the grammar for the Blocks.txt file is changing in Unicode 3.2.0, you
  43. # will have to tweak this some for future use. For now, the relevant
  44. # Unicode definition files are found in libjava/gnu/gcj/convert/.
  45. #
  46. # author Eric Blake <ebb9@email.byu.edu>
  47. #
  48. # usage: unicode-blocks.pl <blocks.txt>
  49. # where <blocks.txt> is obtained from www.unicode.org (named Blocks-3.txt
  50. # for Unicode version 3.0.0).
  51. die "Usage: $0 <blocks.txt>" unless @ARGV == 1;
  52. open (BLOCKS, $ARGV[0]) || die "Can't open Unicode block file: $!\n";
  53. # A hash of added fields and the JDK they were added in, to automatically
  54. # print @since tags. Maintaining this is optional (and tedious), but nice.
  55. my %additions = ("SYRIAC" => "1.4",
  56. "THAANA" => "1.4",
  57. "SINHALA" => "1.4",
  58. "MYANMAR" => "1.4",
  59. "ETHIOPIC" => "1.4",
  60. "CHEROKEE" => "1.4",
  61. "UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS" => "1.4",
  62. "OGHAM" => "1.4",
  63. "RUNIC" => "1.4",
  64. "KHMER" => "1.4",
  65. "MONGOLIAN" => "1.4",
  66. "BRAILLE_PATTERNS" => "1.4",
  67. "CJK_RADICALS_SUPPLEMENT" => "1.4",
  68. "KANGXI_RADICALS" => "1.4",
  69. "IDEOGRAPHIC_DESCRIPTION_CHARACTERS" => "1.4",
  70. "BOPOMOFO_EXTENDED" => "1.4",
  71. "CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A" => "1.4",
  72. "YI_SYLLABLES" => "1.4",
  73. "YI_RADICALS" => "1.4",
  74. );
  75. print <<'EOF';
  76. /**
  77. * A family of character subsets in the Unicode specification. A character
  78. * is in at most one of these blocks.
  79. *
  80. * This inner class was generated automatically from
  81. * <code>$ARGV[0]</code>, by some perl scripts.
  82. * This Unicode definition file can be found on the
  83. * <a href="http://www.unicode.org">http://www.unicode.org</a> website.
  84. * JDK 1.4 uses Unicode version 3.0.0.
  85. *
  86. * @author scripts/unicode-blocks.pl (written by Eric Blake)
  87. * @since 1.2
  88. */
  89. public static final class UnicodeBlock extends Subset
  90. {
  91. /** The start of the subset. */
  92. private final char start;
  93. /** The end of the subset. */
  94. private final char end;
  95. /**
  96. * Constructor for strictly defined blocks.
  97. *
  98. * @param start the start character of the range
  99. * @param end the end character of the range
  100. * @param name the block name
  101. */
  102. private UnicodeBlock(char start, char end, String name)
  103. {
  104. super(name);
  105. this.start = start;
  106. this.end = end;
  107. }
  108. /**
  109. * Returns the Unicode character block which a character belongs to.
  110. *
  111. * @param ch the character to look up
  112. * @return the set it belongs to, or null if it is not in one
  113. */
  114. public static UnicodeBlock of(char ch)
  115. {
  116. // Special case, since SPECIALS contains two ranges.
  117. if (ch == '\uFEFF')
  118. return SPECIALS;
  119. // Simple binary search for the correct block.
  120. int low = 0;
  121. int hi = sets.length - 1;
  122. while (low <= hi)
  123. {
  124. int mid = (low + hi) >> 1;
  125. UnicodeBlock b = sets[mid];
  126. if (ch < b.start)
  127. hi = mid - 1;
  128. else if (ch > b.end)
  129. low = mid + 1;
  130. else
  131. return b;
  132. }
  133. return null;
  134. }
  135. EOF
  136. my $seenSpecials = 0;
  137. my $seenSurrogates = 0;
  138. my $surrogateStart = 0;
  139. my @names = ();
  140. while (<BLOCKS>) {
  141. next if /^\#/;
  142. my ($start, $end, $block) = split(/; /);
  143. next unless defined $block;
  144. chomp $block;
  145. $block =~ s/ *$//;
  146. if (! $seenSpecials and $block =~ /Specials/) {
  147. # Special case SPECIALS, since it is two disjoint ranges
  148. $seenSpecials = 1;
  149. next;
  150. }
  151. if ($block =~ /Surrogates/) {
  152. # Special case SURROGATES_AREA, since it one range, not three
  153. # consecutive, in Java
  154. $seenSurrogates++;
  155. if ($seenSurrogates == 1) {
  156. $surrogateStart = $start;
  157. next;
  158. } elsif ($seenSurrogates == 2) {
  159. next;
  160. } else {
  161. $start = $surrogateStart;
  162. $block = "Surrogates Area";
  163. }
  164. }
  165. # Special case the name of PRIVATE_USE_AREA.
  166. $block =~ s/(Private Use)/$1 Area/;
  167. (my $name = $block) =~ tr/a-z -/A-Z__/;
  168. push @names, $name;
  169. my $since = (defined $additions{$name}
  170. ? "\n * \@since $additions{$name}" : "");
  171. my $extra = ($block =~ /Specials/ ? "'\\uFEFF', " : "");
  172. print <<EOF;
  173. /**
  174. * $block.
  175. * $extra'\\u$start' - '\\u$end'.$since
  176. */
  177. public static final UnicodeBlock $name
  178. = new UnicodeBlock('\\u$start', '\\u$end',
  179. "$name");
  180. EOF
  181. }
  182. print <<EOF;
  183. /**
  184. * The defined subsets.
  185. */
  186. private static final UnicodeBlock sets[] = {
  187. EOF
  188. foreach (@names) {
  189. print " $_,\n";
  190. }
  191. print <<EOF;
  192. };
  193. } // class UnicodeBlock
  194. EOF