libcore_icu_NativeNormalizer.cpp revision 757a7942eed2b0aa457f8517a0259d2ac82c5b18
1a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes/* 279179d5284bdc6854d1366226d26eec8b766d1acElliott Hughes * Copyright (C) 2010 The Android Open Source Project 3a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * 4a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License"); 5a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * you may not use this file except in compliance with the License. 6a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * You may obtain a copy of the License at 7a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * 8a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 9a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * 10a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * Unless required by applicable law or agreed to in writing, software 11a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 12a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * See the License for the specific language governing permissions and 14a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes * limitations under the License. 15a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes */ 16a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes 17757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes#define LOG_TAG "NativeNormalizer" 18757a7942eed2b0aa457f8517a0259d2ac82c5b18Elliott Hughes 19a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "ErrorCode.h" 20a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "JNIHelp.h" 21a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "ScopedJavaUnicodeString.h" 22a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "unicode/normlzr.h" 23a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes 24a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughesstatic jstring normalizeImpl(JNIEnv* env, jclass, jstring s, jint intMode) { 25a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes ScopedJavaUnicodeString src(env, s); 26a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UNormalizationMode mode = static_cast<UNormalizationMode>(intMode); 27a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UErrorCode errorCode = U_ZERO_ERROR; 28a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UnicodeString dst; 29a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes Normalizer::normalize(src.unicodeString(), mode, 0, dst, errorCode); 30a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes icu4jni_error(env, errorCode); 31a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes return dst.isBogus() ? NULL : env->NewString(dst.getBuffer(), dst.length()); 32a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes} 33a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes 34a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughesstatic jboolean isNormalizedImpl(JNIEnv* env, jclass, jstring s, jint intMode) { 35a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes ScopedJavaUnicodeString src(env, s); 36a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UNormalizationMode mode = static_cast<UNormalizationMode>(intMode); 37a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UErrorCode errorCode = U_ZERO_ERROR; 38a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes UBool result = Normalizer::isNormalized(src.unicodeString(), mode, errorCode); 39a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes icu4jni_error(env, errorCode); 40a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes return result; 41a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes} 42a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes 43a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughesstatic JNINativeMethod gMethods[] = { 44a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes /* name, signature, funcPtr */ 45a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes {"normalizeImpl", "(Ljava/lang/String;I)Ljava/lang/String;", (void*) normalizeImpl}, 46a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes {"isNormalizedImpl", "(Ljava/lang/String;I)Z", (void*) isNormalizedImpl}, 47a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes}; 48a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughesextern "C" int register_com_ibm_icu4jni_text_NativeNormalizer(JNIEnv* env) { 49a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes return jniRegisterNativeMethods(env, 50a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes "com/ibm/icu4jni/text/NativeNormalizer", gMethods, NELEM(gMethods)); 51a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes} 52