1 /* 2 * Copyright (c) 2021 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 16 #ifndef PLURALFORMAT_IMPL_H 17 #define PLURALFORMAT_IMPL_H 18 19 #include "data_resource.h" 20 #include "locale_info.h" 21 #include "plural_rules.h" 22 #include "types.h" 23 24 namespace OHOS { 25 namespace I18N { 26 class PluralFormatImpl { 27 public: 28 PluralFormatImpl(LocaleInfo &locale, I18nStatus &status); 29 virtual ~PluralFormatImpl(); 30 int GetPluralRuleIndex(int number, I18nStatus status) const; 31 int GetPluralRuleIndex(double number, I18nStatus status) const; 32 bool Init(const DataResource &resource); 33 34 private: 35 PluralRules *mPluralRules = nullptr; 36 PluralRules *mDecimalPluralRules = nullptr; 37 LocaleInfo mLocale; 38 PluralRules *GetPluralData(I18nStatus status) const; 39 bool ParseRule(const std::string &rule, const int ruleSize, const int number) const; 40 bool ParseFormula(const std::string &rule, const int ruleSize, int &index, const int number) const; 41 bool CompareResult(const std::string &rule, const int ruleSize, int &index, const int number) const; 42 bool CompareNotEqualResult(const std::string &rule, const int ruleSize, int &index, const int number) const; 43 int ParseNumber(const std::string &rule, const int ruleSize, int &index) const; 44 bool ParseDecimalRule(const std::string &rule, const int ruleSize, const int *numberInfo) const; 45 bool ParseDecimalFormula(const std::string &rule, const int ruleSize, int &index, const int *numberInfo) const; 46 void ComputeDecimalInfo(double number, int integerNumber, int *numberInfo) const; 47 PluralRules *InitPluralRules(std::string unprocessedPluralData); 48 bool CheckContainsIntegerRule() const; 49 const int SYMBOL_LENGTH = 1; 50 const int SKIP_SYMBOL_LENGTH = 2; 51 const char EQUAL = '='; 52 const char NOT_EQUAL = '!'; 53 const char MOD = '%'; 54 const char AND = 'a'; 55 const char OR = 'o'; 56 const char TO = '<'; 57 const char COMMA = ','; 58 const char NUM_OF_FRACTION = 'v'; 59 const char FRACTION_NUMBER = 'f'; 60 const char FRACTION_NUMBER_WITH_ZERO = 't'; 61 const int INTEGER_NUMBER_INDEX = 0; 62 const int FRACTION_NUMBER_INDEX = 1; 63 const int NUM_OF_FRACTION_INDEX = 2; 64 static constexpr int NUMBER_INFO_SIZE = 3; 65 const double EPS = 1e-6; 66 const int MAX_FRACTION_NUMBERS = 6; 67 const int DECIMALISM = 10; 68 }; 69 } // namespace I18N 70 } // namespace OHOS 71 72 #endif