• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "character.h"
16 
17 #include <sys/types.h>
18 #include <set>
19 #include <string>
20 
21 #include "cctype"
22 #include "map"
23 #include "string"
24 #include "unicode/uchar.h"
25 #include "unicode/umachine.h"
26 #include "unicode/unistr.h"
27 #include "unicode/urename.h"
28 
29 namespace OHOS {
30 namespace Global {
31 namespace I18n {
32 static std::set<UCharDirection> RTLDirectionSet = {
33     U_RIGHT_TO_LEFT,
34     U_RIGHT_TO_LEFT_ARABIC,
35     U_RIGHT_TO_LEFT_EMBEDDING,
36     U_RIGHT_TO_LEFT_OVERRIDE,
37     U_RIGHT_TO_LEFT_ISOLATE
38 };
39 
IsDigit(const std::string & character)40 bool IsDigit(const std::string &character)
41 {
42     icu::UnicodeString unicodeString(character.c_str());
43     UChar32 char32 = unicodeString.char32At(0);
44     return u_isdigit(char32);
45 }
46 
IsSpaceChar(const std::string & character)47 bool IsSpaceChar(const std::string &character)
48 {
49     icu::UnicodeString unicodeString(character.c_str());
50     UChar32 char32 = unicodeString.char32At(0);
51     return u_isJavaSpaceChar(char32);
52 }
53 
IsWhiteSpace(const std::string & character)54 bool IsWhiteSpace(const std::string &character)
55 {
56     icu::UnicodeString unicodeString(character.c_str());
57     UChar32 char32 = unicodeString.char32At(0);
58     return u_isWhitespace(char32);
59 }
60 
IsRTLCharacter(const std::string & character)61 bool IsRTLCharacter(const std::string &character)
62 {
63     icu::UnicodeString unicodeString(character.c_str());
64     UChar32 char32 = unicodeString.char32At(0);
65     UCharDirection direction = u_charDirection(char32);
66     if (RTLDirectionSet.find(direction) != RTLDirectionSet.end()) {
67         return true;
68     }
69     return false;
70 }
71 
IsIdeoGraphic(const std::string & character)72 bool IsIdeoGraphic(const std::string &character)
73 {
74     icu::UnicodeString unicodeString(character.c_str());
75     UChar32 char32 = unicodeString.char32At(0);
76     return u_hasBinaryProperty(char32, UCHAR_IDEOGRAPHIC);
77 }
78 
IsLetter(const std::string & character)79 bool IsLetter(const std::string &character)
80 {
81     icu::UnicodeString unicodeString(character.c_str());
82     UChar32 char32 = unicodeString.char32At(0);
83     return isalpha(char32);
84 }
85 
IsLowerCase(const std::string & character)86 bool IsLowerCase(const std::string &character)
87 {
88     icu::UnicodeString unicodeString(character.c_str());
89     UChar32 char32 = unicodeString.char32At(0);
90     return u_islower(char32);
91 }
92 
IsUpperCase(const std::string & character)93 bool IsUpperCase(const std::string &character)
94 {
95     icu::UnicodeString unicodeString(character.c_str());
96     UChar32 char32 = unicodeString.char32At(0);
97     return u_isupper(char32);
98 }
99 
100 std::map<UCharCategory, std::string> categoryMap = {
101     { U_UNASSIGNED, "U_UNASSIGNED" },
102     { U_GENERAL_OTHER_TYPES, "U_GENERAL_OTHER_TYPES" },
103     { U_UPPERCASE_LETTER, "U_UPPERCASE_LETTER" },
104     { U_LOWERCASE_LETTER, "U_LOWERCASE_LETTER" },
105     { U_TITLECASE_LETTER, "U_TITLECASE_LETTER" },
106     { U_MODIFIER_LETTER, "U_MODIFIER_LETTER" },
107     { U_OTHER_LETTER, "U_OTHER_LETTER" },
108     { U_NON_SPACING_MARK, "U_NON_SPACING_MARK" },
109     { U_ENCLOSING_MARK, "U_ENCLOSING_MARK" },
110     { U_COMBINING_SPACING_MARK, "U_COMBINING_SPACING_MARK" },
111     { U_DECIMAL_DIGIT_NUMBER, "U_DECIMAL_DIGIT_NUMBER" },
112     { U_LETTER_NUMBER, "U_LETTER_NUMBER" },
113     { U_OTHER_NUMBER, "U_OTHER_NUMBER" },
114     { U_SPACE_SEPARATOR, "U_SPACE_SEPARATOR" },
115     { U_LINE_SEPARATOR, "U_LINE_SEPARATOR" },
116     { U_PARAGRAPH_SEPARATOR, "U_PARAGRAPH_SEPARATOR" },
117     { U_CONTROL_CHAR, "U_CONTROL_CHAR" },
118     { U_FORMAT_CHAR, "U_FORMAT_CHAR" },
119     { U_PRIVATE_USE_CHAR, "U_PRIVATE_USE_CHAR" },
120     { U_SURROGATE, "U_SURROGATE" },
121     { U_DASH_PUNCTUATION, "U_DASH_PUNCTUATION" },
122     { U_START_PUNCTUATION, "U_START_PUNCTUATION" },
123     { U_END_PUNCTUATION, "U_END_PUNCTUATION" },
124     { U_CONNECTOR_PUNCTUATION, "U_CONNECTOR_PUNCTUATION" },
125     { U_OTHER_PUNCTUATION, "U_OTHER_PUNCTUATION" },
126     { U_MATH_SYMBOL, "U_MATH_SYMBOL" },
127     { U_CURRENCY_SYMBOL, "U_CURRENCY_SYMBOL" },
128     { U_MODIFIER_SYMBOL, "U_MODIFIER_SYMBOL" },
129     { U_OTHER_SYMBOL, "U_OTHER_SYMBOL" },
130     { U_INITIAL_PUNCTUATION, "U_INITIAL_PUNCTUATION" },
131     { U_FINAL_PUNCTUATION, "U_FINAL_PUNCTUATION" },
132     { U_CHAR_CATEGORY_COUNT, "U_CHAR_CATEGORY_COUNT" },
133 };
134 
GetType(const std::string & character)135 std::string GetType(const std::string &character)
136 {
137     icu::UnicodeString unicodeString(character.c_str());
138     UChar32 char32 = unicodeString.char32At(0);
139     int8_t category = u_charType(char32);
140     return categoryMap[UCharCategory(category)];
141 }
142 } // namespace I18n
143 } // namespace Global
144 } // namespace OHOS