Initial revision
[gcc.git] / libjava / java / lang / Character.java
1 // Character.java - Character class.
2
3 /* Copyright (C) 1998, 1999 Cygnus Solutions
4
5 This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
9 details. */
10
11 package java.lang;
12
13 import java.io.Serializable;
14
15 /**
16 * @author Tom Tromey <tromey@cygnus.com>
17 * @date September 10, 1998
18 */
19
20 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
21 * "The Java Language Specification", ISBN 0-201-63451-1,
22 * online API docs for JDK 1.2 beta from http://www.javasoft.com,
23 * and The Unicode Standard Version 2.0.
24 * Status: Believed complete and correct for JDK 1.1; 1.2 methods
25 * unimplemented.
26 */
27
28 public final class Character implements Serializable, Comparable
29 {
30 public static final char MIN_VALUE = '\u0000';
31 public static final char MAX_VALUE = '\uffff';
32
33 public static final int MIN_RADIX = 2;
34 public static final int MAX_RADIX = 36;
35
36 // This initialization is seemingly circular, but it is accepted
37 // by javac, and is handled specially by gcc.
38 public static final Class TYPE = char.class;
39
40 // Space.
41 public static final byte SPACE_SEPARATOR = 12;
42 public static final byte LINE_SEPARATOR = 13;
43 public static final byte PARAGRAPH_SEPARATOR = 14;
44
45 // Letters.
46 public static final byte UPPERCASE_LETTER = 1;
47 public static final byte LOWERCASE_LETTER = 2;
48 public static final byte TITLECASE_LETTER = 3;
49 public static final byte MODIFIER_LETTER = 4;
50 public static final byte OTHER_LETTER = 5;
51
52 // Numbers.
53 public static final byte DECIMAL_DIGIT_NUMBER = 9;
54 public static final byte LETTER_NUMBER = 10;
55 public static final byte OTHER_NUMBER = 11;
56
57 // Marks.
58 public static final byte NON_SPACING_MARK = 6;
59 public static final byte ENCLOSING_MARK = 7;
60 public static final byte COMBINING_SPACING_MARK = 8;
61
62 // Punctuation.
63 public static final byte DASH_PUNCTUATION = 20;
64 public static final byte START_PUNCTUATION = 21;
65 public static final byte END_PUNCTUATION = 22;
66 public static final byte CONNECTOR_PUNCTUATION = 23;
67 public static final byte OTHER_PUNCTUATION = 24;
68
69 // Symbols.
70 public static final byte MATH_SYMBOL = 25;
71 public static final byte CURRENCY_SYMBOL = 26;
72 public static final byte MODIFIER_SYMBOL = 27;
73 public static final byte OTHER_SYMBOL = 28;
74
75 // Format controls.
76 public static final byte CONTROL = 15;
77 // Note: The JCL book says that both FORMAT and PRIVATE_USE are 18.
78 // However, FORMAT is actually 16.
79 public static final byte FORMAT = 16;
80
81 // Others.
82 public static final byte UNASSIGNED = 0;
83 public static final byte PRIVATE_USE = 18;
84 public static final byte SURROGATE = 19;
85
86
87 public Character (char ch)
88 {
89 value = ch;
90 }
91
92 public char charValue ()
93 {
94 return value;
95 }
96
97 // See if a character is a digit. If so, return the corresponding
98 // value. Otherwise return -1.
99 private static native int digit_value (char ch);
100
101 public static int digit (char ch, int radix)
102 {
103 if (radix < MIN_RADIX || radix > MAX_RADIX)
104 return -1;
105
106 int d = digit_value (ch);
107 if (d == -1)
108 {
109 if (ch >= 'A' && ch <= 'Z')
110 d = ch - 'A' + 10;
111 else if (ch >= 'a' && ch <= 'z')
112 d = ch - 'a' + 10;
113 else
114 return -1;
115 }
116 return d >= radix ? -1 : d;
117 }
118
119 public boolean equals (Object obj)
120 {
121 // Don't need to compare OBJ to null as instanceof will do this.
122 if (obj instanceof Character)
123 return value == ((Character) obj).value;
124 return false;
125 }
126
127 public static char forDigit (int d, int rdx)
128 {
129 if (d < 0 || d >= rdx || rdx < MIN_RADIX || rdx > MAX_RADIX)
130 return '\u0000';
131 if (d < 10)
132 return (char) ('0' + d);
133 // The Java Language Spec says to use lowercase, while the JCL
134 // says to use uppercase. We go with the former.
135 return (char) ('a' + d - 10);
136 }
137
138 public static native int getNumericValue (char ch);
139 public static native int getType (char ch);
140
141 public int hashCode ()
142 {
143 return value;
144 }
145
146 public static boolean isDefined (char ch)
147 {
148 return getType (ch) != UNASSIGNED;
149 }
150
151 public static boolean isDigit (char ch)
152 {
153 return digit_value (ch) != -1;
154 }
155
156 // The JCL book says that the argument here is a Character. That is
157 // wrong.
158 public static boolean isIdentifierIgnorable (char ch)
159 {
160 // This information comes from the Unicode Standard. It isn't
161 // auto-generated as it doesn't appear in the unidata table.
162 return ((ch >= '\u0000' && ch <= '\u0008')
163 || (ch >= '\u000e' && ch <= '\u001b')
164 // JDK 1.2 docs say that these are ignorable. The Unicode
165 // Standard is somewhat ambiguous on this issue.
166 || (ch >= '\u007f' && ch <= '\u009f')
167 || (ch >= '\u200c' && ch <= '\u200f')
168 // JCl says 200a through 200e, but that is a typo. The
169 // Unicode standard says the bidi controls are 202a
170 // through 202e.
171 || (ch >= '\u202a' && ch <= '\u202e')
172 || (ch >= '\u206a' && ch <= '\u206f')
173 || ch == '\ufeff');
174 }
175
176 public static boolean isISOControl (char c)
177 {
178 return ((c >= '\u0000' && c <= '\u001f')
179 || (c >= '\u007f' && c <= '\u009f'));
180 }
181
182 public static boolean isJavaIdentifierPart (char ch)
183 {
184 if (isIdentifierIgnorable (ch) || isDigit (ch))
185 return true;
186 int type = getType (ch);
187 return (type == COMBINING_SPACING_MARK || type == NON_SPACING_MARK
188 || type == CURRENCY_SYMBOL || type == CONNECTOR_PUNCTUATION
189 || type == UPPERCASE_LETTER || type == LOWERCASE_LETTER
190 || type == TITLECASE_LETTER || type == MODIFIER_LETTER
191 || type == OTHER_LETTER || type == LETTER_NUMBER);
192 }
193
194 public static boolean isJavaIdentifierStart (char ch)
195 {
196 int type = getType (ch);
197 return (type == CURRENCY_SYMBOL || type == CONNECTOR_PUNCTUATION
198 || type == UPPERCASE_LETTER || type == LOWERCASE_LETTER
199 || type == TITLECASE_LETTER || type == MODIFIER_LETTER
200 || type == OTHER_LETTER);
201 }
202
203 // Deprecated in 1.2.
204 public static boolean isJavaLetter (char ch)
205 {
206 return ch == '$' || ch == '_' || isLetter (ch);
207 }
208
209 // Deprecated in 1.2.
210 public static boolean isJavaLetterOrDigit (char ch)
211 {
212 return ch == '$' || ch == '_' || isLetterOrDigit (ch);
213 }
214
215 public static boolean isLetter (char ch)
216 {
217 int type = getType (ch);
218 return (type == UPPERCASE_LETTER || type == LOWERCASE_LETTER
219 || type == TITLECASE_LETTER || type == MODIFIER_LETTER
220 || type == OTHER_LETTER);
221 }
222
223 public static boolean isLetterOrDigit (char ch)
224 {
225 return isDigit (ch) || isLetter (ch);
226 }
227
228 public static native boolean isLowerCase (char ch);
229
230 // Deprecated in JCL.
231 public static boolean isSpace (char ch)
232 {
233 return ch == '\n' || ch == '\t' || ch == '\f' || ch == '\r' || ch == ' ';
234 }
235
236 public static native boolean isSpaceChar (char ch);
237 public static native boolean isTitleCase (char ch);
238
239 public static boolean isUnicodeIdentifierPart (char ch)
240 {
241 if (isIdentifierIgnorable (ch) || isDigit (ch))
242 return true;
243 int type = getType (ch);
244 return (type == CONNECTOR_PUNCTUATION || type == LETTER_NUMBER
245 || type == COMBINING_SPACING_MARK || type == NON_SPACING_MARK
246 || type == UPPERCASE_LETTER || type == LOWERCASE_LETTER
247 || type == TITLECASE_LETTER || type == MODIFIER_LETTER
248 || type == OTHER_LETTER);
249 }
250
251 public static boolean isUnicodeIdentifierStart (char ch)
252 {
253 return isLetter (ch);
254 }
255
256 public static native boolean isUpperCase (char ch);
257
258 public static boolean isWhitespace (char ch)
259 {
260 return ((ch >= '\u0009' && ch <= '\r')
261 || (ch >= '\u001c' && ch <= '\u001f')
262 || (ch != '\u00a0' && ch != '\ufeff' && isSpaceChar (ch)));
263 }
264
265 public static native char toLowerCase (char ch);
266 public static native char toTitleCase (char ch);
267 public static native char toUpperCase (char ch);
268
269 public String toString ()
270 {
271 return String.valueOf(value);
272 }
273
274 public int compareTo (Character anotherCharacter)
275 {
276 return value - anotherCharacter.value;
277 }
278
279 public int compareTo (Object o)
280 {
281 return compareTo ((Character) o);
282 }
283
284 // Private data.
285 private char value;
286 }