19596a2c1Sopenharmony_ci/*
29596a2c1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License.
59596a2c1Sopenharmony_ci * You may obtain a copy of the License at
69596a2c1Sopenharmony_ci *
79596a2c1Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
89596a2c1Sopenharmony_ci *
99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and
139596a2c1Sopenharmony_ci * limitations under the License.
149596a2c1Sopenharmony_ci */
159596a2c1Sopenharmony_ci#include "i18n_normalizer.h"
169596a2c1Sopenharmony_ci
179596a2c1Sopenharmony_cinamespace OHOS {
189596a2c1Sopenharmony_cinamespace Global {
199596a2c1Sopenharmony_cinamespace I18n {
209596a2c1Sopenharmony_ciI18nNormalizer::I18nNormalizer(I18nNormalizerMode mode, I18nErrorCode &errorCode)
219596a2c1Sopenharmony_ci{
229596a2c1Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
239596a2c1Sopenharmony_ci    if (mode == I18nNormalizerMode::NFC) {
249596a2c1Sopenharmony_ci        normalizer = icu::Normalizer2::getNFCInstance(status);
259596a2c1Sopenharmony_ci    } else if (mode == I18nNormalizerMode::NFD) {
269596a2c1Sopenharmony_ci        normalizer = icu::Normalizer2::getNFDInstance(status);
279596a2c1Sopenharmony_ci    } else if (mode == I18nNormalizerMode::NFKC) {
289596a2c1Sopenharmony_ci        normalizer = icu::Normalizer2::getNFKCInstance(status);
299596a2c1Sopenharmony_ci    } else {
309596a2c1Sopenharmony_ci        // mode == I18nNormalizerMode::NFKD
319596a2c1Sopenharmony_ci        normalizer = icu::Normalizer2::getNFKDInstance(status);
329596a2c1Sopenharmony_ci    }
339596a2c1Sopenharmony_ci    if (U_FAILURE(status)) {
349596a2c1Sopenharmony_ci        errorCode = I18nErrorCode::FAILED;
359596a2c1Sopenharmony_ci    }
369596a2c1Sopenharmony_ci}
379596a2c1Sopenharmony_ci
389596a2c1Sopenharmony_ciI18nNormalizer::~I18nNormalizer()
399596a2c1Sopenharmony_ci{
409596a2c1Sopenharmony_ci}
419596a2c1Sopenharmony_ci
429596a2c1Sopenharmony_cistd::string I18nNormalizer::Normalize(const char *text, int32_t length, I18nErrorCode &errorCode)
439596a2c1Sopenharmony_ci{
449596a2c1Sopenharmony_ci    UErrorCode status = U_ZERO_ERROR;
459596a2c1Sopenharmony_ci    std::string result;
469596a2c1Sopenharmony_ci    if (text == nullptr) {
479596a2c1Sopenharmony_ci        return result;
489596a2c1Sopenharmony_ci    }
499596a2c1Sopenharmony_ci    icu::UnicodeString input(text, length);
509596a2c1Sopenharmony_ci    icu::UnicodeString output = normalizer->normalize(input, status);
519596a2c1Sopenharmony_ci    if (U_FAILURE(status)) {
529596a2c1Sopenharmony_ci        errorCode = I18nErrorCode::FAILED;
539596a2c1Sopenharmony_ci        return "";
549596a2c1Sopenharmony_ci    }
559596a2c1Sopenharmony_ci    output.toUTF8String(result);
569596a2c1Sopenharmony_ci    return result;
579596a2c1Sopenharmony_ci}
589596a2c1Sopenharmony_ci} // namespace I18n
599596a2c1Sopenharmony_ci} // namespace Global
609596a2c1Sopenharmony_ci} // namespace OHOS