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