19596a2c1Sopenharmony_ci/* 29596a2c1Sopenharmony_ci * Copyright (c) 2021-2022 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 "index_util.h" 169596a2c1Sopenharmony_ci 179596a2c1Sopenharmony_ci#include "locale_config.h" 189596a2c1Sopenharmony_ci#include "unicode/locid.h" 199596a2c1Sopenharmony_ci#include "string" 209596a2c1Sopenharmony_ci#include "unicode/unistr.h" 219596a2c1Sopenharmony_ci#include "unicode/utypes.h" 229596a2c1Sopenharmony_ci#include "vector" 239596a2c1Sopenharmony_ci 249596a2c1Sopenharmony_cinamespace OHOS { 259596a2c1Sopenharmony_cinamespace Global { 269596a2c1Sopenharmony_cinamespace I18n { 279596a2c1Sopenharmony_ciIndexUtil::IndexUtil(const std::string &localeTag) 289596a2c1Sopenharmony_ci{ 299596a2c1Sopenharmony_ci UErrorCode status = U_ZERO_ERROR; 309596a2c1Sopenharmony_ci if (localeTag == "") { 319596a2c1Sopenharmony_ci icu::Locale locale(LocaleConfig::GetSystemLocale().c_str()); 329596a2c1Sopenharmony_ci index = std::make_unique<icu::AlphabeticIndex>(locale, status); 339596a2c1Sopenharmony_ci } else { 349596a2c1Sopenharmony_ci icu::Locale locale(localeTag.c_str()); 359596a2c1Sopenharmony_ci index = std::make_unique<icu::AlphabeticIndex>(locale, status); 369596a2c1Sopenharmony_ci } 379596a2c1Sopenharmony_ci if (U_SUCCESS(status)) { 389596a2c1Sopenharmony_ci createSuccess = true; 399596a2c1Sopenharmony_ci } 409596a2c1Sopenharmony_ci} 419596a2c1Sopenharmony_ci 429596a2c1Sopenharmony_ciIndexUtil::~IndexUtil() 439596a2c1Sopenharmony_ci{ 449596a2c1Sopenharmony_ci} 459596a2c1Sopenharmony_ci 469596a2c1Sopenharmony_cistd::vector<std::string> IndexUtil::GetIndexList() 479596a2c1Sopenharmony_ci{ 489596a2c1Sopenharmony_ci UErrorCode status = U_ZERO_ERROR; 499596a2c1Sopenharmony_ci std::vector<std::string> indexList; 509596a2c1Sopenharmony_ci if (!createSuccess) { 519596a2c1Sopenharmony_ci return indexList; 529596a2c1Sopenharmony_ci } 539596a2c1Sopenharmony_ci index->resetBucketIterator(status); 549596a2c1Sopenharmony_ci while (index->nextBucket(status)) { 559596a2c1Sopenharmony_ci if (U_SUCCESS(status)) { 569596a2c1Sopenharmony_ci icu::UnicodeString unicodeString = index->getBucketLabel(); 579596a2c1Sopenharmony_ci std::string label; 589596a2c1Sopenharmony_ci unicodeString.toUTF8String(label); 599596a2c1Sopenharmony_ci indexList.push_back(label); 609596a2c1Sopenharmony_ci } 619596a2c1Sopenharmony_ci } 629596a2c1Sopenharmony_ci return indexList; 639596a2c1Sopenharmony_ci} 649596a2c1Sopenharmony_ci 659596a2c1Sopenharmony_civoid IndexUtil::AddLocale(const std::string &localeTag) 669596a2c1Sopenharmony_ci{ 679596a2c1Sopenharmony_ci UErrorCode status = U_ZERO_ERROR; 689596a2c1Sopenharmony_ci icu::Locale locale(localeTag.c_str()); 699596a2c1Sopenharmony_ci if (createSuccess) { 709596a2c1Sopenharmony_ci index->addLabels(locale, status); 719596a2c1Sopenharmony_ci } 729596a2c1Sopenharmony_ci} 739596a2c1Sopenharmony_ci 749596a2c1Sopenharmony_cistd::string IndexUtil::GetIndex(const std::string &String) 759596a2c1Sopenharmony_ci{ 769596a2c1Sopenharmony_ci if (!createSuccess) { 779596a2c1Sopenharmony_ci return ""; 789596a2c1Sopenharmony_ci } 799596a2c1Sopenharmony_ci UErrorCode status = U_ZERO_ERROR; 809596a2c1Sopenharmony_ci icu::UnicodeString unicodeString(String.c_str()); 819596a2c1Sopenharmony_ci int32_t bucketNumber = index->getBucketIndex(unicodeString, status); 829596a2c1Sopenharmony_ci if (!U_SUCCESS(status)) { 839596a2c1Sopenharmony_ci return ""; 849596a2c1Sopenharmony_ci } 859596a2c1Sopenharmony_ci index->resetBucketIterator(status); 869596a2c1Sopenharmony_ci for (int32_t i = 0; i <= bucketNumber; i++) { 879596a2c1Sopenharmony_ci index->nextBucket(status); 889596a2c1Sopenharmony_ci if (!U_SUCCESS(status)) { 899596a2c1Sopenharmony_ci return ""; 909596a2c1Sopenharmony_ci } 919596a2c1Sopenharmony_ci } 929596a2c1Sopenharmony_ci icu::UnicodeString label = index->getBucketLabel(); 939596a2c1Sopenharmony_ci std::string result; 949596a2c1Sopenharmony_ci label.toUTF8String(result); 959596a2c1Sopenharmony_ci return result; 969596a2c1Sopenharmony_ci} 979596a2c1Sopenharmony_ci} // namespace I18n 989596a2c1Sopenharmony_ci} // namespace Global 999596a2c1Sopenharmony_ci} // namespace OHOS