• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "index_util.h"
16 
17 #include "i18n_hilog.h"
18 #include "locale_config.h"
19 #include "unicode/locid.h"
20 #include "string"
21 #include "unicode/unistr.h"
22 #include "vector"
23 
24 namespace OHOS {
25 namespace Global {
26 namespace I18n {
IndexUtil(const std::string & localeTag)27 IndexUtil::IndexUtil(const std::string &localeTag)
28 {
29     UErrorCode status = U_ZERO_ERROR;
30     if (localeTag.empty()) {
31         std::string sysLocale = LocaleConfig::GetEffectiveLocale();
32         sysLocale = LocaleConfig::RemoveCustExtParam(sysLocale);
33         if (sysLocale.empty()) {
34             HILOG_ERROR_I18N("IndexUtil::IndexUtil: get system locale failed.");
35             return;
36         }
37         icu::Locale locale(sysLocale.c_str());
38         index = std::make_unique<icu::AlphabeticIndex>(locale, status);
39     } else {
40         std::string curLocale = LocaleConfig::RemoveCustExtParam(localeTag);
41         icu::Locale locale(curLocale.c_str());
42         index = std::make_unique<icu::AlphabeticIndex>(locale, status);
43     }
44     if (U_SUCCESS(status) && index != nullptr) {
45         createSuccess = true;
46     }
47 }
48 
~IndexUtil()49 IndexUtil::~IndexUtil()
50 {
51 }
52 
GetIndexList()53 std::vector<std::string> IndexUtil::GetIndexList()
54 {
55     UErrorCode status = U_ZERO_ERROR;
56     std::vector<std::string> indexList;
57     if (!IsCreateSuccess() || index == nullptr) {
58         return indexList;
59     }
60     index->resetBucketIterator(status);
61     while (index->nextBucket(status)) {
62         if (U_SUCCESS(status)) {
63             icu::UnicodeString unicodeString = index->getBucketLabel();
64             std::string label;
65             unicodeString.toUTF8String(label);
66             indexList.push_back(label);
67         }
68     }
69     return indexList;
70 }
71 
AddLocale(const std::string & localeTag)72 void IndexUtil::AddLocale(const std::string &localeTag)
73 {
74     UErrorCode status = U_ZERO_ERROR;
75     icu::Locale locale(localeTag.c_str());
76     if (createSuccess && index != nullptr) {
77         index->addLabels(locale, status);
78     }
79 }
80 
GetIndex(const std::string & String)81 std::string IndexUtil::GetIndex(const std::string &String)
82 {
83     if (!IsCreateSuccess() || index == nullptr) {
84         return "";
85     }
86     UErrorCode status = U_ZERO_ERROR;
87     icu::UnicodeString unicodeString(String.c_str());
88     int32_t bucketNumber = index->getBucketIndex(unicodeString, status);
89     if (!IsStatusSuccess(status)) {
90         return "";
91     }
92     index->resetBucketIterator(status);
93     for (int32_t i = 0; i <= bucketNumber; i++) {
94         index->nextBucket(status);
95         if (!IsStatusSuccess(status)) {
96             return "";
97         }
98     }
99     icu::UnicodeString label = index->getBucketLabel();
100     std::string result;
101     label.toUTF8String(result);
102     return result;
103 }
104 
IsCreateSuccess()105 bool IndexUtil::IsCreateSuccess()
106 {
107     return createSuccess;
108 }
109 
IsStatusSuccess(UErrorCode status)110 bool IndexUtil::IsStatusSuccess(UErrorCode status)
111 {
112     return U_SUCCESS(status);
113 }
114 } // namespace I18n
115 } // namespace Global
116 } // namespace OHOS