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