• 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::GetSystemLocale();
32         if (sysLocale.empty()) {
33             HILOG_ERROR_I18N("IndexUtil::IndexUtil: get system locale failed.");
34             return;
35         }
36         icu::Locale locale(sysLocale.c_str());
37         index = std::make_unique<icu::AlphabeticIndex>(locale, status);
38     } else {
39         icu::Locale locale(localeTag.c_str());
40         index = std::make_unique<icu::AlphabeticIndex>(locale, status);
41     }
42     if (U_SUCCESS(status)) {
43         createSuccess = true;
44     }
45 }
46 
~IndexUtil()47 IndexUtil::~IndexUtil()
48 {
49 }
50 
GetIndexList()51 std::vector<std::string> IndexUtil::GetIndexList()
52 {
53     UErrorCode status = U_ZERO_ERROR;
54     std::vector<std::string> indexList;
55     if (!IsCreateSuccess()) {
56         return indexList;
57     }
58     index->resetBucketIterator(status);
59     while (index->nextBucket(status)) {
60         if (U_SUCCESS(status)) {
61             icu::UnicodeString unicodeString = index->getBucketLabel();
62             std::string label;
63             unicodeString.toUTF8String(label);
64             indexList.push_back(label);
65         }
66     }
67     return indexList;
68 }
69 
AddLocale(const std::string & localeTag)70 void IndexUtil::AddLocale(const std::string &localeTag)
71 {
72     UErrorCode status = U_ZERO_ERROR;
73     icu::Locale locale(localeTag.c_str());
74     if (createSuccess) {
75         index->addLabels(locale, status);
76     }
77 }
78 
GetIndex(const std::string & String)79 std::string IndexUtil::GetIndex(const std::string &String)
80 {
81     if (!IsCreateSuccess()) {
82         return "";
83     }
84     UErrorCode status = U_ZERO_ERROR;
85     icu::UnicodeString unicodeString(String.c_str());
86     int32_t bucketNumber = index->getBucketIndex(unicodeString, status);
87     if (!IsStatusSuccess(status)) {
88         return "";
89     }
90     index->resetBucketIterator(status);
91     for (int32_t i = 0; i <= bucketNumber; i++) {
92         index->nextBucket(status);
93         if (!IsStatusSuccess(status)) {
94             return "";
95         }
96     }
97     icu::UnicodeString label = index->getBucketLabel();
98     std::string result;
99     label.toUTF8String(result);
100     return result;
101 }
102 
IsCreateSuccess()103 bool IndexUtil::IsCreateSuccess()
104 {
105     return createSuccess;
106 }
107 
IsStatusSuccess(UErrorCode status)108 bool IndexUtil::IsStatusSuccess(UErrorCode status)
109 {
110     return U_SUCCESS(status);
111 }
112 } // namespace I18n
113 } // namespace Global
114 } // namespace OHOS