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,
45         const int numberInfoSize) const;
46     bool ParseDecimalFormula(const std::string &rule, const int ruleSize, int &index, const int *numberInfo,
47         const int numberInfoSize) const;
48     void ComputeDecimalInfo(double number, int integerNumber, int *numberInfo, const int numberInfoSize) const;
49     PluralRules *InitPluralRules(std::string unprocessedPluralData);
50     bool CheckContainsIntegerRule() const;
51     const int SYMBOL_LENGTH = 1;
52     const int SKIP_SYMBOL_LENGTH = 2;
53     const char EQUAL = '=';
54     const char NOT_EQUAL = '!';
55     const char MOD = '%';
56     const char AND = 'a';
57     const char OR = 'o';
58     const char TO = '<';
59     const char COMMA = ',';
60     const char NUM_OF_FRACTION = 'v';
61     const char FRACTION_NUMBER = 'f';
62     const char FRACTION_NUMBER_WITH_ZERO = 't';
63     const int INTEGER_NUMBER_INDEX = 0;
64     const int FRACTION_NUMBER_INDEX = 1;
65     const int NUM_OF_FRACTION_INDEX = 2;
66     static constexpr int NUMBER_INFO_SIZE = 3;
67     const double EPS = 1e-6;
68     const int MAX_FRACTION_NUMBERS = 6;
69     const int DECIMALISM = 10;
70 };
71 } // namespace I18N
72 } // namespace OHOS
73 
74 #endif