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 NUMBER_DATA_H 17 #define NUMBER_DATA_H 18 19 #define NUMBER_SIGN { \ 20 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" \ 21 } 22 #define ARABIC_NOBREAK_ONE_MINUS 2 23 #define ARABIC_NOBREAK_TWO_MINUS 3 24 #define PERCENT_SIGN_INDEX 2 25 #define SIGNS_SIZE 3 26 #define PERCENT_INFO_SIZE 3 27 28 #include "types.h" 29 #include "data_resource.h" 30 31 namespace OHOS { 32 namespace I18N { 33 struct StyleData { 34 int decLen = 0; 35 int intLen = 0; 36 int percentIntLen = 0; 37 int maxDecimalLength = -1; 38 int minDecimalLength = -1; 39 char *numFormat = nullptr; // number format style 40 char *entireFormat = nullptr; 41 bool isTwoGroup = false; 42 StyleData() = default; 43 ~StyleData(); 44 StyleData(const StyleData &data); 45 StyleData& operator=(const StyleData &data); 46 }; 47 48 class NumberData { 49 public: 50 static const char NUMBER_DECIMAL = '.'; 51 static const char NUMBER_GROUPSIGN = ','; 52 static const char NUMBER_PERCENT = '%'; 53 static const int NUMBER_GROUP = 3; 54 static const int TWO_GROUP = 2; 55 static constexpr int NUM_SIZE = 10; 56 static constexpr int INFO_SIZE = 3; 57 std::string nativeNums[NUM_SIZE] = {}; // used to store 0-9 letters in current language 58 char *decimal = nullptr; 59 char *group = nullptr; 60 char *percent = nullptr; 61 std::string minusSign; 62 bool isNative = false; 63 StyleData style; 64 friend class NumberFormatImpl; 65 NumberData(); 66 NumberData(const char *pat, const char *percentPat, std::string decSign, std::string groupSign, 67 std::string perSign); 68 virtual ~NumberData(); 69 void SetNumSystem(std::string *numSym, const int numSize); 70 void SetMinDecimalLength(int length); 71 void SetMaxDecimalLength(int length); 72 void SetMinusSign(const std::string &minus); 73 std::string GetMinusSign(); 74 75 private: 76 static void GetNumberingSystem(const char *numberingSystem, std::string &numberFormatString, 77 std::string &digitsRet); 78 static bool IsNoBreakSpace(const char *pattern, const int len, bool order); 79 void Init(const char *pat, int patLen, const char *percentPat, int perPatLen); 80 void InitSign(const std::string *signs, const int signLength); 81 void ParsePattern(const char *pattern, const int len); 82 void ParsePercentPattern(const char *pattern, const int len); 83 void ParseOtherPerPattern(const char *pattern, const int len, const int perSignPos, 84 const int space, const int hasSpace); 85 void CalculateIntLength(int intEndPos, const char *pattern, const int len, bool isDec); 86 bool CalculateDecLength(const char *pattern, const int len); 87 bool IsSuccess(); 88 void UpdateNumberFormat(); 89 int GetNumberFormatLength(); 90 char *numberFormatPattern = nullptr; 91 char *percentFormatPattern = nullptr; 92 bool isSucc = true; 93 bool isPercent = false; 94 int maxDecimalLength = -1; 95 const char *NUMBER_FORMAT = "%%.%df"; 96 const int NUMBER_FORMAT_LENGTH = 5; 97 static const int ARABIC_NOBREAK_ONE = -96; 98 static const int ARABIC_NOBREAK_TWO = -62; 99 }; 100 101 enum EPercentLocation { 102 UNKNOWN = 0, 103 LEFT = 1, 104 RIGHT = 2 105 }; 106 } // namespace I18N 107 } // namespace OHOS 108 #endif 109