1a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes/*
279179d5284bdc6854d1366226d26eec8b766d1acElliott Hughes * Copyright (C) 2010 The Android Open Source Project
321557bb6a8f35a2f9889adba449cac950c9d41b9Elliott 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
721557bb6a8f35a2f9889adba449cac950c9d41b9Elliott Hughes *
8a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
921557bb6a8f35a2f9889adba449cac950c9d41b9Elliott 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 "JNIHelp.h"
20e22935d3c7040c22b48d53bd18878844f381287cElliott Hughes#include "JniConstants.h"
21bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes#include "JniException.h"
22a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "ScopedJavaUnicodeString.h"
23a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes#include "unicode/normlzr.h"
24a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes
2521557bb6a8f35a2f9889adba449cac950c9d41b9Elliott Hughesstatic jstring NativeNormalizer_normalizeImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
26a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    ScopedJavaUnicodeString src(env, s);
27a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
28bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes    UErrorCode status = U_ZERO_ERROR;
29a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    UnicodeString dst;
30bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes    Normalizer::normalize(src.unicodeString(), mode, 0, dst, status);
315ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    maybeThrowIcuException(env, "Normalizer::normalize", status);
32a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    return dst.isBogus() ? NULL : env->NewString(dst.getBuffer(), dst.length());
33a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes}
34a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes
3521557bb6a8f35a2f9889adba449cac950c9d41b9Elliott Hughesstatic jboolean NativeNormalizer_isNormalizedImpl(JNIEnv* env, jclass, jstring s, jint intMode) {
36a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    ScopedJavaUnicodeString src(env, s);
37a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    UNormalizationMode mode = static_cast<UNormalizationMode>(intMode);
38bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes    UErrorCode status = U_ZERO_ERROR;
39bef9ec33e1368f57c731fce63b6a8c61628c64b0Elliott Hughes    UBool result = Normalizer::isNormalized(src.unicodeString(), mode, status);
405ec69b20ab9b3e2dcbe225d548168b09afbbbac2Elliott Hughes    maybeThrowIcuException(env, "Normalizer::isNormalized", status);
41a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes    return result;
42a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes}
43a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes
44a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughesstatic JNINativeMethod gMethods[] = {
45e22935d3c7040c22b48d53bd18878844f381287cElliott Hughes    NATIVE_METHOD(NativeNormalizer, normalizeImpl, "(Ljava/lang/String;I)Ljava/lang/String;"),
46e22935d3c7040c22b48d53bd18878844f381287cElliott Hughes    NATIVE_METHOD(NativeNormalizer, isNormalizedImpl, "(Ljava/lang/String;I)Z"),
47a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes};
487cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughesvoid register_libcore_icu_NativeNormalizer(JNIEnv* env) {
497cd6760f7045d771faae8080a8c6150bf678f679Elliott Hughes    jniRegisterNativeMethods(env, "libcore/icu/NativeNormalizer", gMethods, NELEM(gMethods));
50a656b24ce0513b5f6115c40e6bb9181c5cea93d6Elliott Hughes}
51