1 /*
2  * Copyright (c) 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 
16 #ifndef MEASUREFORMAT_IMPL_H
17 #define MEASUREFORMAT_IMPL_H
18 
19 #include <string>
20 #include "data_resource.h"
21 #include "locale_info.h"
22 #include "number_format.h"
23 #include "plural_format.h"
24 #include "securec.h"
25 #include "types.h"
26 
27 namespace OHOS {
28 namespace I18N {
29 class MeasureFormatImpl {
30 public:
31     MeasureFormatImpl(LocaleInfo &localeinfo, I18nStatus &status);
32     virtual ~MeasureFormatImpl();
33     std::string Format(double num, std::string &unit, MeasureFormatType type, I18nStatus &status);
34     std::string Format(int num, std::string &unit, MeasureFormatType type, I18nStatus &status);
35     bool Init(const DataResource &resource);
36 private:
37     std::string FormatInner(bool isInteger, std::string &unit, MeasureFormatType type, I18nStatus &status);
38     int SearchUnits(std::string &target, I18nStatus &status);
39     int ComputePluralIndex(bool isInteger, I18nStatus &status);
40     std::string ComputeFormattedUnit(int unitIndex, int pluralIndex, MeasureFormatType type, I18nStatus &status);
41     std::string ComputeFormattedNumber(bool isInteger, I18nStatus &status);
42     MeasureFormatType ConvertType(MeasureFormatType type, I18nStatus &status);
43     bool InitMeasureFormat(std::string &unprocessedMeasureData);
44     bool ParseUnits(std::string &unitsList);
45     bool AllocaFormattedUnits();
46     void FillFormattedUnits(int unitIndex, int typeIndex, std::string *items, int itemStartIndex);
47     void DeallocateMemory();
48     LocaleInfo locale;
49     PluralFormat *pluralFormat;
50     NumberFormat *numberFormat;
51     // # represets %d %s
52     std::string pattern;
53     bool isNumberFirst = true;
54     // units array, size is unitCount
55     int unitCount = 0;
56     std::string *units = nullptr;
57     // formatted units array, size is (unitCount * MEASURE_FORMAT_TYPE_NUM) * MEASURE_PLURAL_NUM
58     int formattedUnitCount = 0;
59     std::string **formattedUnits = nullptr;
60     // number to be formatted
61     int formattedInteger = 0;
62     double formattedDouble = 0;
63 };
64 } // namespace I18N
65 } // namespace OHOS
66 #endif
67