1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * This code is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 2 only, as
6  * published by the Free Software Foundation.  The Android Open Source
7  * Project designates this particular file as subject to the "Classpath"
8  * exception as provided by The Android Open Source Project in the LICENSE
9  * file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 
22 #include "jni.h"
23 #include "jvm.h"
24 #include <nativehelper/JNIHelp.h>
25 #include "nativehelper/jni_macros.h"
26 #include "unicode/uchar.h"
27 #include "unicode/uscript.h"
28 #include <math.h>
29 #include <stdio.h> // For BUFSIZ
30 
31 JNIEXPORT jboolean JNICALL
Character_isLowerCaseImpl(JNIEnv * env,jclass,jint codePoint)32 Character_isLowerCaseImpl(JNIEnv* env, jclass, jint codePoint) {
33   return u_islower(codePoint);
34 }
35 
36 JNIEXPORT jboolean JNICALL
Character_isUpperCaseImpl(JNIEnv * env,jclass,jint codePoint)37 Character_isUpperCaseImpl(JNIEnv* env, jclass, jint codePoint) {
38   return u_isupper(codePoint);
39 }
40 
41 JNIEXPORT jboolean JNICALL
Character_isTitleCaseImpl(JNIEnv * env,jclass,jint codePoint)42 Character_isTitleCaseImpl(JNIEnv* env, jclass, jint codePoint) {
43   return u_istitle(codePoint);
44 }
45 
46 JNIEXPORT jboolean JNICALL
Character_isDigitImpl(JNIEnv * env,jclass,jint codePoint)47 Character_isDigitImpl(JNIEnv* env, jclass, jint codePoint) {
48   return u_isdigit(codePoint);
49 }
50 
51 JNIEXPORT jboolean JNICALL
Character_isLetterImpl(JNIEnv * env,jclass,jint codePoint)52 Character_isLetterImpl(JNIEnv* env, jclass, jint codePoint) {
53   return u_isalpha(codePoint);
54 }
55 
56 JNIEXPORT jboolean JNICALL
Character_isLetterOrDigitImpl(JNIEnv * env,jclass,jint codePoint)57 Character_isLetterOrDigitImpl(JNIEnv* env, jclass, jint codePoint) {
58   return u_isalnum(codePoint);
59 }
60 
61 JNIEXPORT jboolean JNICALL
Character_isAlphabeticImpl(JNIEnv * env,jclass,jint codePoint)62 Character_isAlphabeticImpl(JNIEnv* env, jclass, jint codePoint) {
63   return u_hasBinaryProperty(codePoint, UCHAR_ALPHABETIC);
64 }
65 
66 JNIEXPORT jboolean JNICALL
Character_isIdeographicImpl(JNIEnv * env,jclass,jint codePoint)67 Character_isIdeographicImpl(JNIEnv* env, jclass, jint codePoint) {
68   return u_hasBinaryProperty(codePoint, UCHAR_IDEOGRAPHIC);
69 }
70 
71 JNIEXPORT jint JNICALL
Character_getTypeImpl(JNIEnv * env,jclass,jint codePoint)72 Character_getTypeImpl(JNIEnv* env, jclass, jint codePoint) {
73   return u_charType(codePoint);
74 }
75 
76 JNIEXPORT jboolean JNICALL
Character_isUnicodeIdentifierStartImpl(JNIEnv * env,jclass,jint codePoint)77 Character_isUnicodeIdentifierStartImpl(JNIEnv* env, jclass, jint codePoint) {
78   return u_isIDStart(codePoint);
79 }
80 
81 JNIEXPORT jboolean JNICALL
Character_isUnicodeIdentifierPartImpl(JNIEnv * env,jclass,jint codePoint)82 Character_isUnicodeIdentifierPartImpl(JNIEnv* env, jclass, jint codePoint) {
83   return u_isIDPart(codePoint);
84 }
85 
86 JNIEXPORT jint JNICALL
Character_toLowerCaseImpl(JNIEnv * env,jclass,jint codePoint)87 Character_toLowerCaseImpl(JNIEnv* env, jclass, jint codePoint) {
88   return u_tolower(codePoint);
89 }
90 
91 JNIEXPORT jint JNICALL
Character_toUpperCaseImpl(JNIEnv * env,jclass,jint codePoint)92 Character_toUpperCaseImpl(JNIEnv* env, jclass, jint codePoint) {
93   return u_toupper(codePoint);
94 }
95 
96 JNIEXPORT jint JNICALL
Character_toTitleCaseImpl(JNIEnv * env,jclass,jint codePoint)97 Character_toTitleCaseImpl(JNIEnv* env, jclass, jint codePoint) {
98   return u_totitle(codePoint);
99 }
100 
101 JNIEXPORT jint JNICALL
Character_digitImpl(JNIEnv * env,jclass,jint codePoint,jint radix)102 Character_digitImpl(JNIEnv* env, jclass, jint codePoint, jint radix) {
103   return u_digit(codePoint, radix);
104 }
105 
106 JNIEXPORT jint JNICALL
Character_getNumericValueImpl(JNIEnv * env,jclass,jint codePoint)107 Character_getNumericValueImpl(JNIEnv* env, jclass, jint codePoint) {
108   double result = u_getNumericValue(codePoint);
109   if (result == U_NO_NUMERIC_VALUE) {
110     return -1;
111   } else if (result < 0 || floor(result + 0.5) != result) {
112     return -2;
113   }
114   return static_cast<jint>(result);
115 }
116 
117 JNIEXPORT jboolean JNICALL
Character_isWhitespaceImpl(JNIEnv * env,jclass,jint codePoint)118 Character_isWhitespaceImpl(JNIEnv* env, jclass, jint codePoint) {
119   return u_isWhitespace(codePoint);
120 }
121 
122 JNIEXPORT jbyte JNICALL
Character_getDirectionalityImpl(JNIEnv * env,jclass,jint codePoint)123 Character_getDirectionalityImpl(JNIEnv* env, jclass, jint codePoint) {
124   return u_charDirection(codePoint);
125 }
126 
127 JNIEXPORT jboolean JNICALL
Character_isMirroredImpl(JNIEnv * env,jclass,jint codePoint)128 Character_isMirroredImpl(JNIEnv* env, jclass, jint codePoint) {
129   return u_isMirrored(codePoint);
130 }
131 
132 JNIEXPORT jboolean JNICALL
Character_isDefinedImpl(JNIEnv * env,jclass,jint codePoint)133 Character_isDefinedImpl(JNIEnv* env, jclass, jint codePoint) {
134   return u_isdefined(codePoint);
135 }
136 
137 JNIEXPORT jboolean JNICALL
Character_isIdentifierIgnorableImpl(JNIEnv * env,jclass,jint codePoint)138 Character_isIdentifierIgnorableImpl(JNIEnv* env, jclass, jint codePoint) {
139     return u_isIDIgnorable(codePoint);
140 }
141 
142 JNIEXPORT jboolean JNICALL
Character_isSpaceCharImpl(JNIEnv *,jclass,jint codePoint)143 Character_isSpaceCharImpl(JNIEnv*, jclass, jint codePoint) {
144     return u_isJavaSpaceChar(codePoint);
145 }
146 
147 JNIEXPORT jstring JNICALL
Character_getNameImpl(JNIEnv * env,jclass,jint codePoint)148 Character_getNameImpl(JNIEnv* env, jclass, jint codePoint) {
149     // U_UNICODE_CHAR_NAME gives us the modern names for characters. For control characters,
150     // we need U_EXTENDED_CHAR_NAME to get "NULL" rather than "BASIC LATIN 0" and so on.
151     // We could just use U_EXTENDED_CHAR_NAME except that it returns strings for characters
152     // that aren't unassigned but that don't have names, and those strings aren't in the form
153     // Java specifies.
154     bool isControl = (codePoint <= 0x1f || (codePoint >= 0x7f && codePoint <= 0x9f));
155     UCharNameChoice nameType = isControl ? U_EXTENDED_CHAR_NAME : U_UNICODE_CHAR_NAME;
156     UErrorCode status = U_ZERO_ERROR;
157     char buf[BUFSIZ]; // TODO: is there a more sensible upper bound?
158     int32_t byteCount = u_charName(codePoint, nameType, &buf[0], sizeof(buf), &status);
159     return (U_FAILURE(status) || byteCount == 0) ? NULL : env->NewStringUTF(buf);
160 }
161 
162 static JNINativeMethod gMethods[] = {
163   FAST_NATIVE_METHOD(Character, digitImpl, "(II)I"),
164   FAST_NATIVE_METHOD(Character, getDirectionalityImpl, "(I)B"),
165   NATIVE_METHOD(Character, getNameImpl, "(I)Ljava/lang/String;"),
166   FAST_NATIVE_METHOD(Character, getNumericValueImpl, "(I)I"),
167   FAST_NATIVE_METHOD(Character, getTypeImpl, "(I)I"),
168   FAST_NATIVE_METHOD(Character, isAlphabeticImpl, "(I)Z"),
169   FAST_NATIVE_METHOD(Character, isDefinedImpl, "(I)Z"),
170   FAST_NATIVE_METHOD(Character, isDigitImpl, "(I)Z"),
171   FAST_NATIVE_METHOD(Character, isIdentifierIgnorableImpl, "(I)Z"),
172   FAST_NATIVE_METHOD(Character, isIdeographicImpl, "(I)Z"),
173   FAST_NATIVE_METHOD(Character, isLetterImpl, "(I)Z"),
174   FAST_NATIVE_METHOD(Character, isLetterOrDigitImpl, "(I)Z"),
175   FAST_NATIVE_METHOD(Character, isLowerCaseImpl, "(I)Z"),
176   FAST_NATIVE_METHOD(Character, isMirroredImpl, "(I)Z"),
177   FAST_NATIVE_METHOD(Character, isSpaceCharImpl, "(I)Z"),
178   FAST_NATIVE_METHOD(Character, isTitleCaseImpl, "(I)Z"),
179   FAST_NATIVE_METHOD(Character, isUnicodeIdentifierPartImpl, "(I)Z"),
180   FAST_NATIVE_METHOD(Character, isUnicodeIdentifierStartImpl, "(I)Z"),
181   FAST_NATIVE_METHOD(Character, isUpperCaseImpl, "(I)Z"),
182   FAST_NATIVE_METHOD(Character, isWhitespaceImpl, "(I)Z"),
183   FAST_NATIVE_METHOD(Character, toLowerCaseImpl, "(I)I"),
184   FAST_NATIVE_METHOD(Character, toTitleCaseImpl, "(I)I"),
185   FAST_NATIVE_METHOD(Character, toUpperCaseImpl, "(I)I"),
186 };
187 
register_java_lang_Character(JNIEnv * env)188 void register_java_lang_Character(JNIEnv* env) {
189   jniRegisterNativeMethods(env, "java/lang/Character", gMethods, NELEM(gMethods));
190 }
191