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 #ifndef GLOBAL_I18N_COLLATOR_H 16 #define GLOBAL_I18N_COLLATOR_H 17 18 #include <map> 19 #include <memory> 20 #include <set> 21 #include <string> 22 #include <unordered_set> 23 #include <vector> 24 25 #include "unicode/coll.h" 26 #include "unicode/locid.h" 27 #include "unicode/utypes.h" 28 29 #include "i18n_types.h" 30 #include "locale_info.h" 31 32 namespace OHOS { 33 namespace Global { 34 namespace I18n { 35 typedef enum CompareResult { 36 INVALID = -2, 37 SMALLER, 38 EQUAL, 39 GREATER, 40 } CompareResult; 41 42 class Collator { 43 public: 44 static std::vector<std::string> SupportedLocalesOf(const std::vector<std::string> &requestLocales, 45 const std::map<std::string, std::string> &configs, 46 I18nErrorCode &status); 47 48 Collator(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options); 49 Collator(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options, 50 const std::string &defaultLocale); 51 ~Collator(); 52 CompareResult Compare(const std::string &first, const std::string &second); 53 void ResolvedOptions(std::map<std::string, std::string> &options); 54 I18nErrorCode GetError() const; 55 56 private: 57 std::string localeStr; 58 std::string localeMatcher; 59 std::string usage; 60 std::string sensitivity; 61 std::string ignorePunctuation; 62 std::string numeric; 63 std::string caseFirst; 64 std::string collation; 65 66 std::unique_ptr<LocaleInfo> localeInfo = nullptr; 67 icu::Locale locale; 68 icu::Collator *collatorPtr = nullptr; 69 bool createSuccess = false; 70 I18nErrorCode i18nStatus = I18nErrorCode::SUCCESS; 71 72 static std::set<std::string> GetAvailableLocales(); 73 void ParseAllOptions(std::map<std::string, std::string> &options); 74 bool IsValidCollation(std::string &collation); 75 void SetCollation(); 76 void SetUsage(); 77 void SetNumeric(); 78 void SetCaseFirst(); 79 void SetSensitivity(); 80 void SetIgnorePunctuation(); 81 bool InitCollator(); 82 void Init(std::vector<std::string> &localeTags, std::map<std::string, std::string> &options, 83 const std::string &defaultLocale); 84 }; 85 } // namespace I18n 86 } // namespace Global 87 } // namespace OHOS 88 #endif 89