Home | History | Annotate | Download | only in native
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #define LOG_TAG "NativeNormalizer"
     18 
     19 #include "IcuUtilities.h"
     20 #include "JNIHelp.h"
     21 #include "JniConstants.h"
     22 #include "JniException.h"
     23 #include "ScopedJavaUnicodeString.h"
     24 #include "unicode/normlzr.h"
     25 
     26 static jstring NativeNormalizer_normalizeImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
     27   ScopedJavaUnicodeString src(env, s);
     28   if (!src.valid()) {
     29     return NULL;
     30   }
     31   UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
     32   UErrorCode status = U_ZERO_ERROR;
     33   icu::UnicodeString dst;
     34   icu::Normalizer::normalize(src.unicodeString(), mode, 0, dst, status);
     35   maybeThrowIcuException(env, "Normalizer::normalize", status);
     36   return dst.isBogus() ? NULL : env->NewString(dst.getBuffer(), dst.length());
     37 }
     38 
     39 static jboolean NativeNormalizer_isNormalizedImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
     40   ScopedJavaUnicodeString src(env, s);
     41   if (!src.valid()) {
     42     return JNI_FALSE;
     43   }
     44   UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
     45   UErrorCode status = U_ZERO_ERROR;
     46   UBool result = icu::Normalizer::isNormalized(src.unicodeString(), mode, status);
     47   maybeThrowIcuException(env, "Normalizer::isNormalized", status);
     48   return result;
     49 }
     50 
     51 static JNINativeMethod gMethods[] = {
     52   NATIVE_METHOD(NativeNormalizer, normalizeImpl, "(Ljava/lang/String;I)Ljava/lang/String;"),
     53   NATIVE_METHOD(NativeNormalizer, isNormalizedImpl, "(Ljava/lang/String;I)Z"),
     54 };
     55 void register_libcore_icu_NativeNormalizer(JNIEnv* env) {
     56   jniRegisterNativeMethods(env, "libcore/icu/NativeNormalizer", gMethods, NELEM(gMethods));
     57 }
     58