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 "ErrorCode.h"
20 #include "JNIHelp.h"
21 #include "JniConstants.h"
22 #include "ScopedJavaUnicodeString.h"
23 #include "unicode/normlzr.h"
24
NativeNormalizer_normalizeImpl(JNIEnv * env,jclass,jstring s,jint intMode)25 static jstring NativeNormalizer_normalizeImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
26 ScopedJavaUnicodeString src(env, s);
27 UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
28 UErrorCode errorCode = U_ZERO_ERROR;
29 UnicodeString dst;
30 Normalizer::normalize(src.unicodeString(), mode, 0, dst, errorCode);
31 icu4jni_error(env, errorCode);
32 return dst.isBogus() ? NULL : env->NewString(dst.getBuffer(), dst.length());
33 }
34
NativeNormalizer_isNormalizedImpl(JNIEnv * env,jclass,jstring s,jint intMode)35 static jboolean NativeNormalizer_isNormalizedImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
36 ScopedJavaUnicodeString src(env, s);
37 UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
38 UErrorCode errorCode = U_ZERO_ERROR;
39 UBool result = Normalizer::isNormalized(src.unicodeString(), mode, errorCode);
40 icu4jni_error(env, errorCode);
41 return result;
42 }
43
44 static JNINativeMethod gMethods[] = {
45 NATIVE_METHOD(NativeNormalizer, normalizeImpl, "(Ljava/lang/String;I)Ljava/lang/String;"),
46 NATIVE_METHOD(NativeNormalizer, isNormalizedImpl, "(Ljava/lang/String;I)Z"),
47 };
register_libcore_icu_NativeNormalizer(JNIEnv * env)48 int register_libcore_icu_NativeNormalizer(JNIEnv* env) {
49 return jniRegisterNativeMethods(env, "libcore/icu/NativeNormalizer", gMethods, NELEM(gMethods));
50 }
51