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 #include "error_util.h"
17 #include "i18n_hilog.h"
18 #include "i18n_unicode_addon.h"
19 #include "js_utils.h"
20 #include "variable_convertor.h"
21 
22 namespace OHOS {
23 namespace Global {
24 namespace I18n {
I18nUnicodeAddon()25 I18nUnicodeAddon::I18nUnicodeAddon() {}
26 
~I18nUnicodeAddon()27 I18nUnicodeAddon::~I18nUnicodeAddon() {}
28 
Destructor(napi_env env,void * nativeObject,void * hint)29 void I18nUnicodeAddon::Destructor(napi_env env, void *nativeObject, void *hint)
30 {
31     if (!nativeObject) {
32         return;
33     }
34     delete reinterpret_cast<I18nUnicodeAddon *>(nativeObject);
35     nativeObject = nullptr;
36 }
37 
InitI18nUnicode(napi_env env,napi_value exports)38 napi_value I18nUnicodeAddon::InitI18nUnicode(napi_env env, napi_value exports)
39 {
40     napi_property_descriptor properties[] = {
41         DECLARE_NAPI_STATIC_FUNCTION("isDigit", IsDigitAddon),
42         DECLARE_NAPI_STATIC_FUNCTION("isSpaceChar", IsSpaceCharAddon),
43         DECLARE_NAPI_STATIC_FUNCTION("isWhitespace", IsWhiteSpaceAddon),
44         DECLARE_NAPI_STATIC_FUNCTION("isRTL", IsRTLCharacterAddon),
45         DECLARE_NAPI_STATIC_FUNCTION("isIdeograph", IsIdeoGraphicAddon),
46         DECLARE_NAPI_STATIC_FUNCTION("isLetter", IsLetterAddon),
47         DECLARE_NAPI_STATIC_FUNCTION("isLowerCase", IsLowerCaseAddon),
48         DECLARE_NAPI_STATIC_FUNCTION("isUpperCase", IsUpperCaseAddon),
49         DECLARE_NAPI_STATIC_FUNCTION("getType", GetTypeAddon),
50     };
51     napi_value constructor = nullptr;
52     napi_status status = napi_define_class(env, "Unicode", NAPI_AUTO_LENGTH, JSUtils::DefaultConstructor, nullptr,
53         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
54     if (status != napi_ok) {
55         HILOG_ERROR_I18N("InitI18nUnicode: Define class failed when InitUnicode.");
56         return nullptr;
57     }
58 
59     status = napi_set_named_property(env, exports, "Unicode", constructor);
60     if (status != napi_ok) {
61         HILOG_ERROR_I18N("InitI18nUnicode: Set property failed when InitUnicode.");
62         return nullptr;
63     }
64     return exports;
65 }
66 
InitCharacter(napi_env env,napi_value exports)67 napi_value I18nUnicodeAddon::InitCharacter(napi_env env, napi_value exports)
68 {
69     napi_status status = napi_ok;
70     napi_property_descriptor properties[] = {
71         DECLARE_NAPI_FUNCTION("isDigit", IsDigitAddon),
72         DECLARE_NAPI_FUNCTION("isSpaceChar", IsSpaceCharAddon),
73         DECLARE_NAPI_FUNCTION("isWhitespace", IsWhiteSpaceAddon),
74         DECLARE_NAPI_FUNCTION("isRTL", IsRTLCharacterAddon),
75         DECLARE_NAPI_FUNCTION("isIdeograph", IsIdeoGraphicAddon),
76         DECLARE_NAPI_FUNCTION("isLetter", IsLetterAddon),
77         DECLARE_NAPI_FUNCTION("isLowerCase", IsLowerCaseAddon),
78         DECLARE_NAPI_FUNCTION("isUpperCase", IsUpperCaseAddon),
79         DECLARE_NAPI_FUNCTION("getType", GetTypeAddon)
80     };
81 
82     napi_value constructor = nullptr;
83     status = napi_define_class(env, "Character", NAPI_AUTO_LENGTH, ObjectConstructor, nullptr,
84         sizeof(properties) / sizeof(napi_property_descriptor), properties, &constructor);
85     if (status != napi_ok) {
86         HILOG_ERROR_I18N("Define class failed when InitCharacter");
87         return nullptr;
88     }
89 
90     status = napi_set_named_property(env, exports, "Character", constructor);
91     if (status != napi_ok) {
92         HILOG_ERROR_I18N("Set property failed when InitCharacter");
93         return nullptr;
94     }
95     return exports;
96 }
97 
IsDigitAddon(napi_env env,napi_callback_info info)98 napi_value I18nUnicodeAddon::IsDigitAddon(napi_env env, napi_callback_info info)
99 {
100     size_t argc = 1;
101     napi_value argv[1] = { 0 };
102     napi_value thisVar = nullptr;
103     void *data = nullptr;
104     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
105     if (status != napi_ok) {
106         return nullptr;
107     }
108     napi_valuetype valueType = napi_valuetype::napi_undefined;
109     napi_typeof(env, argv[0], &valueType);
110     if (valueType != napi_valuetype::napi_string) {
111         HILOG_ERROR_I18N("IsDigitAddon: Parameter type does not match");
112         return nullptr;
113     }
114     int32_t code = 0;
115     std::string character = VariableConvertor::GetString(env, argv[0], code);
116     if (code) {
117         return nullptr;
118     }
119     bool isDigit = IsDigit(character);
120     napi_value result = nullptr;
121     status = napi_get_boolean(env, isDigit, &result);
122     if (status != napi_ok) {
123         HILOG_ERROR_I18N("IsDigitAddon: Create isDigit boolean value failed");
124         return nullptr;
125     }
126     return result;
127 }
128 
IsSpaceCharAddon(napi_env env,napi_callback_info info)129 napi_value I18nUnicodeAddon::IsSpaceCharAddon(napi_env env, napi_callback_info info)
130 {
131     size_t argc = 1;
132     napi_value argv[1] = { 0 };
133     napi_value thisVar = nullptr;
134     void *data = nullptr;
135     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
136     if (status != napi_ok) {
137         return nullptr;
138     }
139     napi_valuetype valueType = napi_valuetype::napi_undefined;
140     napi_typeof(env, argv[0], &valueType);
141     if (valueType != napi_valuetype::napi_string) {
142         HILOG_ERROR_I18N("IsSpaceCharAddon: Parameter type does not match");
143         return nullptr;
144     }
145     int32_t code = 0;
146     std::string character = VariableConvertor::GetString(env, argv[0], code);
147     if (code) {
148         return nullptr;
149     }
150     bool isSpaceChar = IsSpaceChar(character);
151     napi_value result = nullptr;
152     status = napi_get_boolean(env, isSpaceChar, &result);
153     if (status != napi_ok) {
154         HILOG_ERROR_I18N("IsSpaceCharAddon: Create boolean value failed");
155         return nullptr;
156     }
157     return result;
158 }
159 
IsWhiteSpaceAddon(napi_env env,napi_callback_info info)160 napi_value I18nUnicodeAddon::IsWhiteSpaceAddon(napi_env env, napi_callback_info info)
161 {
162     size_t argc = 1;
163     napi_value argv[1] = { 0 };
164     napi_value thisVar = nullptr;
165     void *data = nullptr;
166     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
167     if (status != napi_ok) {
168         return nullptr;
169     }
170     napi_valuetype valueType = napi_valuetype::napi_undefined;
171     napi_typeof(env, argv[0], &valueType);
172     if (valueType != napi_valuetype::napi_string) {
173         HILOG_ERROR_I18N("IsWhiteSpaceAddon: Parameter type does not match");
174         return nullptr;
175     }
176     int32_t code = 0;
177     std::string character = VariableConvertor::GetString(env, argv[0], code);
178     if (code) {
179         return nullptr;
180     }
181     bool isWhiteSpace = IsWhiteSpace(character);
182     napi_value result = nullptr;
183     status = napi_get_boolean(env, isWhiteSpace, &result);
184     if (status != napi_ok) {
185         HILOG_ERROR_I18N("IsWhiteSpaceAddon: Create boolean value failed");
186         return nullptr;
187     }
188     return result;
189 }
190 
IsRTLCharacterAddon(napi_env env,napi_callback_info info)191 napi_value I18nUnicodeAddon::IsRTLCharacterAddon(napi_env env, napi_callback_info info)
192 {
193     size_t argc = 1;
194     napi_value argv[1] = { 0 };
195     napi_value thisVar = nullptr;
196     void *data = nullptr;
197     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
198     if (status != napi_ok) {
199         return nullptr;
200     }
201     napi_valuetype valueType = napi_valuetype::napi_undefined;
202     napi_typeof(env, argv[0], &valueType);
203     if (valueType != napi_valuetype::napi_string) {
204         HILOG_ERROR_I18N("IsRTLCharacterAddon: Parameter type does not match");
205         return nullptr;
206     }
207     int32_t code = 0;
208     std::string character = VariableConvertor::GetString(env, argv[0], code);
209     if (code) {
210         return nullptr;
211     }
212     bool isRTLCharacter = IsRTLCharacter(character);
213     napi_value result = nullptr;
214     status = napi_get_boolean(env, isRTLCharacter, &result);
215     if (status != napi_ok) {
216         HILOG_ERROR_I18N("IsRTLCharacterAddon: Create boolean value failed");
217         return nullptr;
218     }
219     return result;
220 }
221 
IsIdeoGraphicAddon(napi_env env,napi_callback_info info)222 napi_value I18nUnicodeAddon::IsIdeoGraphicAddon(napi_env env, napi_callback_info info)
223 {
224     size_t argc = 1;
225     napi_value argv[1] = { 0 };
226     napi_value thisVar = nullptr;
227     void *data = nullptr;
228     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
229     if (status != napi_ok) {
230         return nullptr;
231     }
232     napi_valuetype valueType = napi_valuetype::napi_undefined;
233     napi_typeof(env, argv[0], &valueType);
234     if (valueType != napi_valuetype::napi_string) {
235         HILOG_ERROR_I18N("IsIdeoGraphicAddon: Parameter type does not match");
236         return nullptr;
237     }
238     int32_t code = 0;
239     std::string character = VariableConvertor::GetString(env, argv[0], code);
240     if (code) {
241         return nullptr;
242     }
243     bool isIdeoGraphic = IsIdeoGraphic(character);
244     napi_value result = nullptr;
245     status = napi_get_boolean(env, isIdeoGraphic, &result);
246     if (status != napi_ok) {
247         HILOG_ERROR_I18N("IsIdeoGraphicAddon: Create boolean value failed");
248         return nullptr;
249     }
250     return result;
251 }
252 
IsLetterAddon(napi_env env,napi_callback_info info)253 napi_value I18nUnicodeAddon::IsLetterAddon(napi_env env, napi_callback_info info)
254 {
255     size_t argc = 1;
256     napi_value argv[1] = { 0 };
257     napi_value thisVar = nullptr;
258     void *data = nullptr;
259     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
260     if (status != napi_ok) {
261         return nullptr;
262     }
263     napi_valuetype valueType = napi_valuetype::napi_undefined;
264     napi_typeof(env, argv[0], &valueType);
265     if (valueType != napi_valuetype::napi_string) {
266         HILOG_ERROR_I18N("IsLetterAddon: Parameter type does not match");
267         return nullptr;
268     }
269     int32_t code = 0;
270     std::string character = VariableConvertor::GetString(env, argv[0], code);
271     if (code) {
272         return nullptr;
273     }
274     bool isLetter = IsLetter(character);
275     napi_value result = nullptr;
276     status = napi_get_boolean(env, isLetter, &result);
277     if (status != napi_ok) {
278         HILOG_ERROR_I18N("IsLetterAddon: Create boolean value failed");
279         return nullptr;
280     }
281     return result;
282 }
283 
IsLowerCaseAddon(napi_env env,napi_callback_info info)284 napi_value I18nUnicodeAddon::IsLowerCaseAddon(napi_env env, napi_callback_info info)
285 {
286     size_t argc = 1;
287     napi_value argv[1] = { 0 };
288     napi_value thisVar = nullptr;
289     void *data = nullptr;
290     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
291     if (status != napi_ok) {
292         return nullptr;
293     }
294     napi_valuetype valueType = napi_valuetype::napi_undefined;
295     napi_typeof(env, argv[0], &valueType);
296     if (valueType != napi_valuetype::napi_string) {
297         HILOG_ERROR_I18N("IsLowerCaseAddon: Parameter type does not match");
298         return nullptr;
299     }
300     int32_t code = 0;
301     std::string character = VariableConvertor::GetString(env, argv[0], code);
302     if (code) {
303         return nullptr;
304     }
305     bool isLowerCase = IsLowerCase(character);
306     napi_value result = nullptr;
307     status = napi_get_boolean(env, isLowerCase, &result);
308     if (status != napi_ok) {
309         HILOG_ERROR_I18N("IsLowerCaseAddon: Create isLowerCase boolean value failed");
310         return nullptr;
311     }
312     return result;
313 }
314 
IsUpperCaseAddon(napi_env env,napi_callback_info info)315 napi_value I18nUnicodeAddon::IsUpperCaseAddon(napi_env env, napi_callback_info info)
316 {
317     size_t argc = 1;
318     napi_value argv[1] = { 0 };
319     napi_value thisVar = nullptr;
320     void *data = nullptr;
321     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
322     if (status != napi_ok) {
323         return nullptr;
324     }
325     napi_valuetype valueType = napi_valuetype::napi_undefined;
326     napi_typeof(env, argv[0], &valueType);
327     if (valueType != napi_valuetype::napi_string) {
328         HILOG_ERROR_I18N("IsUpperCaseAddon: Parameter type does not match");
329         return nullptr;
330     }
331     int32_t code = 0;
332     std::string character = VariableConvertor::GetString(env, argv[0], code);
333     if (code) {
334         return nullptr;
335     }
336     bool isUpperCase = IsUpperCase(character);
337     napi_value result = nullptr;
338     status = napi_get_boolean(env, isUpperCase, &result);
339     if (status != napi_ok) {
340         HILOG_ERROR_I18N("IsUpperCaseAddon: Create boolean value failed");
341         return nullptr;
342     }
343     return result;
344 }
345 
GetTypeAddon(napi_env env,napi_callback_info info)346 napi_value I18nUnicodeAddon::GetTypeAddon(napi_env env, napi_callback_info info)
347 {
348     size_t argc = 1;
349     napi_value argv[1] = { 0 };
350     napi_value thisVar = nullptr;
351     void *data = nullptr;
352     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
353     if (status != napi_ok) {
354         return nullptr;
355     }
356     napi_valuetype valueType = napi_valuetype::napi_undefined;
357     napi_typeof(env, argv[0], &valueType);
358     if (valueType != napi_valuetype::napi_string) {
359         HILOG_ERROR_I18N("GetTypeAddon: Parameter type does not match");
360         return nullptr;
361     }
362     int32_t code = 0;
363     std::string character = VariableConvertor::GetString(env, argv[0], code);
364     if (code) {
365         return nullptr;
366     }
367     std::string type = GetType(character);
368     napi_value result = nullptr;
369     status = napi_create_string_utf8(env, type.c_str(), NAPI_AUTO_LENGTH, &result);
370     if (status != napi_ok) {
371         HILOG_ERROR_I18N("GetTypeAddon: Create getType string value failed");
372         return nullptr;
373     }
374     return result;
375 }
376 
ObjectConstructor(napi_env env,napi_callback_info info)377 napi_value I18nUnicodeAddon::ObjectConstructor(napi_env env, napi_callback_info info)
378 {
379     napi_value thisVar = nullptr;
380     void *data = nullptr;
381     napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
382     if (status != napi_ok) {
383         return nullptr;
384     }
385     std::unique_ptr<I18nUnicodeAddon> obj = nullptr;
386     obj = std::make_unique<I18nUnicodeAddon>();
387     status =
388         napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nUnicodeAddon::Destructor, nullptr, nullptr);
389     if (status != napi_ok) {
390         HILOG_ERROR_I18N("Wrap I18nAddon failed");
391         return nullptr;
392     }
393     obj.release();
394     return thisVar;
395 }
396 } // namespace I18n
397 } // namespace Global
398 } // namespace OHOS