2016-05-14 Fritz Reese <fritzoreese@gmail.com>
[official-gcc.git] / libjava / java / lang / natCharacter.cc
blobdea2086eb52b79daf32666383bc1e3b501eaf40d
1 /* java.lang.Character -- Wrapper class for char, and Unicode subsets
2 Copyright (C) 1998, 1999, 2001, 2002, 2007 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 #include <config.h>
40 #include <gcj/cni.h>
41 #include <jvm.h>
42 #include <java/lang/Character.h>
44 #include <java-chartables.h>
48 // These constants define the return values for characters that are unassigned
49 // or reserved for private use.
50 #define UNASSIGNED_TYPE 0
51 #define UNASSIGNED_DIGIT -1
52 #define UNASSIGNED_DIRECTION -1
53 #define UNASSIGNED_NUMERIC_VALUE -1
55 #define PRIVATE_TYPE 18
56 #define PRIVATE_DIRECTION 0
58 // The methods that take a char as an argument all have counterparts that
59 // take ints. The ones that take chars only work for the BMP or plane 0 of the
60 // Unicode standard but the ones that take ints work for all Unicode code
61 // points. However, the ones that take chars don't simply redirect the calls
62 // because the BMP is by far the most used plane so saving a little time on
63 // each call makes sense.
65 jchar
66 java::lang::Character::readChar(jchar ch)
68 // Perform 16-bit addition to find the correct entry in data.
69 return data[0][(jchar) (blocks[0][ch >> shift[0]] + ch)];
72 jchar
73 java::lang::Character::readCodePoint(jint codePoint)
75 jint plane = codePoint >> 16;
76 jchar offset = (jchar)(codePoint & 0xffff);
77 // Be careful not to call this method with an unassigned character. The only
78 // characters assigned as of Unicode 4.0.0 belong to planes 0, 1, 2, and 14.
79 return data[plane][(jchar) (blocks[plane][offset >> shift[plane]] + offset)];
82 jint
83 java::lang::Character::getType(jchar ch)
85 // Perform 16-bit addition to find the correct entry in data.
86 return (jint) (data[0][(jchar) (blocks[0][ch >> shift[0]] + ch)] & TYPE_MASK);
89 jint
90 java::lang::Character::getType(jint codePoint)
92 jint plane = codePoint >> 16;
93 if (plane < 0 || (plane > 2 && plane != 14))
95 if (plane > 14 && ((codePoint & 0xffff) < 0xfffe))
96 return (jint) PRIVATE_TYPE;
97 return (jint) UNASSIGNED_TYPE;
99 jint offset = codePoint & 0xffff;
100 return (jint)
101 (data[plane]
102 [(jchar) (blocks[plane][offset >> shift[plane]] + offset)] & TYPE_MASK);
105 jchar
106 java::lang::Character::toLowerCase(jchar ch)
108 return (jchar) (ch + lower[0][readChar(ch) >> 7]);
111 jint
112 java::lang::Character::toLowerCase(jint codePoint)
114 jint plane = codePoint >> 16;
115 if (plane < 0 || (plane > 2 && plane != 14))
116 return codePoint;
117 return (lower[plane][readCodePoint(codePoint) >> 7]) + codePoint;
120 jchar
121 java::lang::Character::toUpperCase(jchar ch)
123 return (jchar) (ch + upper[0][readChar(ch) >> 7]);
126 jint
127 java::lang::Character::toUpperCase(jint codePoint)
129 jint plane = codePoint >> 16;
130 if (plane < 0 || (plane > 2 && plane != 14))
131 return codePoint;
132 return (upper[plane][readCodePoint(codePoint) >> 7]) + codePoint;
135 jchar
136 java::lang::Character::toTitleCase(jchar ch)
138 // As title is short, it doesn't hurt to exhaustively iterate over it.
139 for (int i = title_length - 2; i >= 0; i -= 2)
140 if (title[i] == ch)
141 return title[i + 1];
142 return toUpperCase(ch);
145 jint
146 java::lang::Character::toTitleCase(jint codePoint)
148 // As of Unicode 4.0.0 no characters outside of plane 0 have titlecase
149 // mappings that are different from their uppercase mapping.
150 if (codePoint >= 0 && codePoint < 0x10000)
151 return toTitleCase((jchar)codePoint);
152 return toUpperCase(codePoint);
155 jint
156 java::lang::Character::digit(jchar ch, jint radix)
158 if (radix < MIN_RADIX || radix > MAX_RADIX)
159 return (jint) -1;
160 jchar attr = readChar(ch);
161 if (((1 << (attr & TYPE_MASK))
162 & ((1 << UPPERCASE_LETTER)
163 | (1 << LOWERCASE_LETTER)
164 | (1 << DECIMAL_DIGIT_NUMBER))))
166 // Signedness doesn't matter; 0xffff vs. -1 are both rejected.
167 jint digit = (jint) numValue[0][attr >> 7];
168 return (digit >= 0 && digit < radix) ? digit : (jint) -1;
170 return (jint) -1;
173 jint
174 java::lang::Character::digit(jint codePoint, jint radix)
176 if (radix < MIN_RADIX || radix > MAX_RADIX)
177 return (jint) -1;
179 jint plane = codePoint >> 16;
180 if (plane < 0 || (plane > 2 && plane != 14))
181 return UNASSIGNED_DIGIT;
183 jchar attr = readCodePoint(codePoint);
184 if (((1 << (attr & TYPE_MASK))
185 & ((1 << UPPERCASE_LETTER)
186 | (1 << LOWERCASE_LETTER)
187 | (1 << DECIMAL_DIGIT_NUMBER))))
189 // Signedness doesn't matter; 0xffff vs. -1 are both rejected.
190 jint digit = (jint) numValue[plane][attr >> 7];
191 if (digit <= -3)
192 digit = largenums[-digit -3];
193 return (digit >= 0 && digit < radix) ? digit : (jint) -1;
195 return (jint) -1;
199 jint
200 java::lang::Character::getNumericValue(jchar ch)
202 // numValue is stored as an array of jshort, since 10000 is the maximum.
203 return (jint) numValue[0][readChar(ch) >> 7];
206 jint
207 java::lang::Character::getNumericValue(jint codePoint)
209 jint plane = codePoint >> 16;
210 if (plane < 0 || (plane > 2 && plane != 14))
211 return UNASSIGNED_NUMERIC_VALUE;
212 jshort num = numValue[plane][readCodePoint(codePoint) >> 7];
213 if (num <= -3)
214 return largenums[-num - 3];
215 return num;
218 jbyte
219 java::lang::Character::getDirectionality(jchar ch)
221 return direction[0][readChar(ch) >> 7];
224 jbyte
225 java::lang::Character::getDirectionality(jint codePoint)
227 jint plane = codePoint >> 16;
228 if (plane < 0 || (plane > 2 && plane != 14))
230 if (plane > 14 && ((codePoint & 0xffff) < 0xfffe))
231 return (jint) PRIVATE_DIRECTION;
232 return (jint) UNASSIGNED_DIRECTION;
234 return direction[plane][readCodePoint(codePoint) >> 7];