• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 namespace OHOS {
18 namespace Global {
19 namespace I18n {
I18nNormalizer(I18nNormalizerMode mode,I18nErrorCode & errorCode)20 I18nNormalizer::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     errorCode = U_FAILURE(status) ? I18nErrorCode::FAILED : errorCode;
34 }
35 
~I18nNormalizer()36 I18nNormalizer::~I18nNormalizer()
37 {
38 }
39 
Normalize(const char * text,int32_t length,I18nErrorCode & errorCode)40 std::string I18nNormalizer::Normalize(const char *text, int32_t length, I18nErrorCode &errorCode)
41 {
42     UErrorCode status = U_ZERO_ERROR;
43     std::string result;
44     if (text == nullptr) {
45         return result;
46     }
47     icu::UnicodeString input(text, length);
48     if (normalizer == nullptr) {
49         return "";
50     }
51     icu::UnicodeString output = normalizer->normalize(input, status);
52     if (U_FAILURE(status)) {
53         errorCode = I18nErrorCode::FAILED;
54         return "";
55     }
56     output.toUTF8String(result);
57     return result;
58 }
59 } // namespace I18n
60 } // namespace Global
61 } // namespace OHOS