1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#include "i18n_normalizer.h" 16 17namespace OHOS { 18namespace Global { 19namespace I18n { 20I18nNormalizer::I18nNormalizer(I18nNormalizerMode mode, I18nErrorCode &errorCode) 21{ 22 UErrorCode status = U_ZERO_ERROR; 23 if (mode == I18nNormalizerMode::NFC) { 24 normalizer = icu::Normalizer2::getNFCInstance(status); 25 } else if (mode == I18nNormalizerMode::NFD) { 26 normalizer = icu::Normalizer2::getNFDInstance(status); 27 } else if (mode == I18nNormalizerMode::NFKC) { 28 normalizer = icu::Normalizer2::getNFKCInstance(status); 29 } else { 30 // mode == I18nNormalizerMode::NFKD 31 normalizer = icu::Normalizer2::getNFKDInstance(status); 32 } 33 if (U_FAILURE(status)) { 34 errorCode = I18nErrorCode::FAILED; 35 } 36} 37 38I18nNormalizer::~I18nNormalizer() 39{ 40} 41 42std::string I18nNormalizer::Normalize(const char *text, int32_t length, I18nErrorCode &errorCode) 43{ 44 UErrorCode status = U_ZERO_ERROR; 45 std::string result; 46 if (text == nullptr) { 47 return result; 48 } 49 icu::UnicodeString input(text, length); 50 icu::UnicodeString output = normalizer->normalize(input, status); 51 if (U_FAILURE(status)) { 52 errorCode = I18nErrorCode::FAILED; 53 return ""; 54 } 55 output.toUTF8String(result); 56 return result; 57} 58} // namespace I18n 59} // namespace Global 60} // namespace OHOS