OSDN Git Service

* gcc-interface/Makefile.in (INCLUDES_FOR_SUBDIR): Add $(fsrcdir) by
[pf3gnuchains/gcc-fork.git] / libjava / scripts / unicode-blocks.pl
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
39
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/.
47 #
48 # author Eric Blake <ebb9@email.byu.edu>
49 #
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).
53
54
55 die "Usage: $0 <blocks.txt>" unless @ARGV == 1;
56 open (BLOCKS, $ARGV[0]) || die "Can't open Unicode block file: $!\n";
57
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",
79                  );
80
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.
85    *
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.
91    *
92    * @author scripts/unicode-blocks.pl (written by Eric Blake)
93    * @since 1.2
94    */
95   public static final class UnicodeBlock extends Subset
96   {
97     /** The start of the subset. */
98     private final char start;
99
100     /** The end of the subset. */
101     private final char end;
102
103     /**
104      * Constructor for strictly defined blocks.
105      *
106      * @param start the start character of the range
107      * @param end the end character of the range
108      * @param name the block name
109      */
110     private UnicodeBlock(char start, char end, String name)
111     {
112       super(name);
113       this.start = start;
114       this.end = end;
115     }
116
117     /**
118      * Returns the Unicode character block which a character belongs to.
119      *
120      * @param ch the character to look up
121      * @return the set it belongs to, or null if it is not in one
122      */
123     public static UnicodeBlock of(char ch)
124     {
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)
132         {
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;
141         }
142       return null;
143     }
144 EOF
145
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;              
160     }
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";
173         }
174     }
175     # Special case the name of PRIVATE_USE_AREA.
176     $block =~ s/(Private Use)/$1 Area/;
177
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;
184
185     /**
186      * $block.
187      * $extra'\\u$start' - '\\u$end'.$since
188      */
189     public static final UnicodeBlock $name
190       = new UnicodeBlock('\\u$start', '\\u$end',
191                          "$name");
192 EOF
193 }
194
195 print <<EOF;
196
197     /**
198      * The defined subsets.
199      */
200     private static final UnicodeBlock sets[] = {
201 EOF
202
203 foreach (@names) {
204     print "      $_,\n";
205 }
206
207 print <<EOF;
208     };
209   } // class UnicodeBlock
210 EOF