• 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 #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     status = napi_typeof(env, argv[0], &valueType);
110     if (status != napi_ok) {
111         return nullptr;
112     }
113     if (valueType != napi_valuetype::napi_string) {
114         HILOG_ERROR_I18N("IsDigitAddon: Parameter type does not match");
115         return nullptr;
116     }
117     int32_t code = 0;
118     std::string character = VariableConvertor::GetString(env, argv[0], code);
119     if (code) {
120         return nullptr;
121     }
122     bool isDigit = IsDigit(character);
123     napi_value result = nullptr;
124     status = napi_get_boolean(env, isDigit, &result);
125     if (status != napi_ok) {
126         HILOG_ERROR_I18N("IsDigitAddon: Create isDigit boolean value failed");
127         return nullptr;
128     }
129     return result;
130 }
131 
IsSpaceCharAddon(napi_env env,napi_callback_info info)132 napi_value I18nUnicodeAddon::IsSpaceCharAddon(napi_env env, napi_callback_info info)
133 {
134     size_t argc = 1;
135     napi_value argv[1] = { 0 };
136     napi_value thisVar = nullptr;
137     void *data = nullptr;
138     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
139     if (status != napi_ok) {
140         return nullptr;
141     }
142     napi_valuetype valueType = napi_valuetype::napi_undefined;
143     status = napi_typeof(env, argv[0], &valueType);
144     if (status != napi_ok) {
145         return nullptr;
146     }
147     if (valueType != napi_valuetype::napi_string) {
148         HILOG_ERROR_I18N("IsSpaceCharAddon: Parameter type does not match");
149         return nullptr;
150     }
151     int32_t code = 0;
152     std::string character = VariableConvertor::GetString(env, argv[0], code);
153     if (code) {
154         return nullptr;
155     }
156     bool isSpaceChar = IsSpaceChar(character);
157     napi_value result = nullptr;
158     status = napi_get_boolean(env, isSpaceChar, &result);
159     if (status != napi_ok) {
160         HILOG_ERROR_I18N("IsSpaceCharAddon: Create boolean value failed");
161         return nullptr;
162     }
163     return result;
164 }
165 
IsWhiteSpaceAddon(napi_env env,napi_callback_info info)166 napi_value I18nUnicodeAddon::IsWhiteSpaceAddon(napi_env env, napi_callback_info info)
167 {
168     size_t argc = 1;
169     napi_value argv[1] = { 0 };
170     napi_value thisVar = nullptr;
171     void *data = nullptr;
172     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
173     if (status != napi_ok) {
174         return nullptr;
175     }
176     napi_valuetype valueType = napi_valuetype::napi_undefined;
177     status = napi_typeof(env, argv[0], &valueType);
178     if (status != napi_ok) {
179         return nullptr;
180     }
181     if (valueType != napi_valuetype::napi_string) {
182         HILOG_ERROR_I18N("IsWhiteSpaceAddon: Parameter type does not match");
183         return nullptr;
184     }
185     int32_t code = 0;
186     std::string character = VariableConvertor::GetString(env, argv[0], code);
187     if (code) {
188         return nullptr;
189     }
190     bool isWhiteSpace = IsWhiteSpace(character);
191     napi_value result = nullptr;
192     status = napi_get_boolean(env, isWhiteSpace, &result);
193     if (status != napi_ok) {
194         HILOG_ERROR_I18N("IsWhiteSpaceAddon: Create boolean value failed");
195         return nullptr;
196     }
197     return result;
198 }
199 
IsRTLCharacterAddon(napi_env env,napi_callback_info info)200 napi_value I18nUnicodeAddon::IsRTLCharacterAddon(napi_env env, napi_callback_info info)
201 {
202     size_t argc = 1;
203     napi_value argv[1] = { 0 };
204     napi_value thisVar = nullptr;
205     void *data = nullptr;
206     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
207     if (status != napi_ok) {
208         return nullptr;
209     }
210     napi_valuetype valueType = napi_valuetype::napi_undefined;
211     status = napi_typeof(env, argv[0], &valueType);
212     if (status != napi_ok) {
213         HILOG_ERROR_I18N("I18nUnicodeAddon::IsRTLCharacterAddon: get argv[0] type failed.");
214         return nullptr;
215     }
216     if (valueType != napi_valuetype::napi_string) {
217         HILOG_ERROR_I18N("IsRTLCharacterAddon: Parameter type does not match");
218         return nullptr;
219     }
220     int32_t code = 0;
221     std::string character = VariableConvertor::GetString(env, argv[0], code);
222     if (code) {
223         return nullptr;
224     }
225     bool isRTLCharacter = IsRTLCharacter(character);
226     napi_value result = nullptr;
227     status = napi_get_boolean(env, isRTLCharacter, &result);
228     if (status != napi_ok) {
229         HILOG_ERROR_I18N("IsRTLCharacterAddon: Create boolean value failed");
230         return nullptr;
231     }
232     return result;
233 }
234 
IsIdeoGraphicAddon(napi_env env,napi_callback_info info)235 napi_value I18nUnicodeAddon::IsIdeoGraphicAddon(napi_env env, napi_callback_info info)
236 {
237     size_t argc = 1;
238     napi_value argv[1] = { 0 };
239     napi_value thisVar = nullptr;
240     void *data = nullptr;
241     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
242     if (status != napi_ok) {
243         return nullptr;
244     }
245     napi_valuetype valueType = napi_valuetype::napi_undefined;
246     status = napi_typeof(env, argv[0], &valueType);
247     if (status != napi_ok) {
248         HILOG_ERROR_I18N("I18nUnicodeAddon::IsIdeoGraphicAddon: get argv[0] type failed.");
249         return nullptr;
250     }
251     if (valueType != napi_valuetype::napi_string) {
252         HILOG_ERROR_I18N("IsIdeoGraphicAddon: Parameter type does not match");
253         return nullptr;
254     }
255     int32_t code = 0;
256     std::string character = VariableConvertor::GetString(env, argv[0], code);
257     if (code) {
258         return nullptr;
259     }
260     bool isIdeoGraphic = IsIdeoGraphic(character);
261     napi_value result = nullptr;
262     status = napi_get_boolean(env, isIdeoGraphic, &result);
263     if (status != napi_ok) {
264         HILOG_ERROR_I18N("IsIdeoGraphicAddon: Create boolean value failed");
265         return nullptr;
266     }
267     return result;
268 }
269 
IsLetterAddon(napi_env env,napi_callback_info info)270 napi_value I18nUnicodeAddon::IsLetterAddon(napi_env env, napi_callback_info info)
271 {
272     size_t argc = 1;
273     napi_value argv[1] = { 0 };
274     napi_value thisVar = nullptr;
275     void *data = nullptr;
276     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
277     if (status != napi_ok) {
278         return nullptr;
279     }
280     napi_valuetype valueType = napi_valuetype::napi_undefined;
281     status = napi_typeof(env, argv[0], &valueType);
282     if (status != napi_ok) {
283         HILOG_ERROR_I18N("I18nUnicodeAddon::IsLetterAddon: get argv[0] type failed.");
284         return nullptr;
285     }
286     if (valueType != napi_valuetype::napi_string) {
287         HILOG_ERROR_I18N("IsLetterAddon: Parameter type does not match");
288         return nullptr;
289     }
290     int32_t code = 0;
291     std::string character = VariableConvertor::GetString(env, argv[0], code);
292     if (code) {
293         return nullptr;
294     }
295     bool isLetter = IsLetter(character);
296     napi_value result = nullptr;
297     status = napi_get_boolean(env, isLetter, &result);
298     if (status != napi_ok) {
299         HILOG_ERROR_I18N("IsLetterAddon: Create boolean value failed");
300         return nullptr;
301     }
302     return result;
303 }
304 
IsLowerCaseAddon(napi_env env,napi_callback_info info)305 napi_value I18nUnicodeAddon::IsLowerCaseAddon(napi_env env, napi_callback_info info)
306 {
307     size_t argc = 1;
308     napi_value argv[1] = { 0 };
309     napi_value thisVar = nullptr;
310     void *data = nullptr;
311     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
312     if (status != napi_ok) {
313         return nullptr;
314     }
315     napi_valuetype valueType = napi_valuetype::napi_undefined;
316     status = napi_typeof(env, argv[0], &valueType);
317     if (status != napi_ok) {
318         HILOG_ERROR_I18N("I18nUnicodeAddon::IsLowerCaseAddon: get argv[0] type failed.");
319         return nullptr;
320     }
321     if (valueType != napi_valuetype::napi_string) {
322         HILOG_ERROR_I18N("IsLowerCaseAddon: Parameter type does not match");
323         return nullptr;
324     }
325     int32_t code = 0;
326     std::string character = VariableConvertor::GetString(env, argv[0], code);
327     if (code) {
328         return nullptr;
329     }
330     bool isLowerCase = IsLowerCase(character);
331     napi_value result = nullptr;
332     status = napi_get_boolean(env, isLowerCase, &result);
333     if (status != napi_ok) {
334         HILOG_ERROR_I18N("IsLowerCaseAddon: Create isLowerCase boolean value failed");
335         return nullptr;
336     }
337     return result;
338 }
339 
IsUpperCaseAddon(napi_env env,napi_callback_info info)340 napi_value I18nUnicodeAddon::IsUpperCaseAddon(napi_env env, napi_callback_info info)
341 {
342     size_t argc = 1;
343     napi_value argv[1] = { 0 };
344     napi_value thisVar = nullptr;
345     void *data = nullptr;
346     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
347     if (status != napi_ok) {
348         return nullptr;
349     }
350     napi_valuetype valueType = napi_valuetype::napi_undefined;
351     status = napi_typeof(env, argv[0], &valueType);
352     if (status != napi_ok) {
353         HILOG_ERROR_I18N("I18nUnicodeAddon::IsUpperCaseAddon: get argv[0] type failed.");
354         return nullptr;
355     }
356     if (valueType != napi_valuetype::napi_string) {
357         HILOG_ERROR_I18N("IsUpperCaseAddon: Parameter type does not match");
358         return nullptr;
359     }
360     int32_t code = 0;
361     std::string character = VariableConvertor::GetString(env, argv[0], code);
362     if (code) {
363         return nullptr;
364     }
365     bool isUpperCase = IsUpperCase(character);
366     napi_value result = nullptr;
367     status = napi_get_boolean(env, isUpperCase, &result);
368     if (status != napi_ok) {
369         HILOG_ERROR_I18N("IsUpperCaseAddon: Create boolean value failed");
370         return nullptr;
371     }
372     return result;
373 }
374 
GetTypeAddon(napi_env env,napi_callback_info info)375 napi_value I18nUnicodeAddon::GetTypeAddon(napi_env env, napi_callback_info info)
376 {
377     size_t argc = 1;
378     napi_value argv[1] = { 0 };
379     napi_value thisVar = nullptr;
380     void *data = nullptr;
381     napi_status status = napi_get_cb_info(env, info, &argc, argv, &thisVar, &data);
382     if (status != napi_ok) {
383         return nullptr;
384     }
385     napi_valuetype valueType = napi_valuetype::napi_undefined;
386     status = napi_typeof(env, argv[0], &valueType);
387     if (status != napi_ok) {
388         HILOG_ERROR_I18N("I18nUnicodeAddon::GetTypeAddon: get argv[0] type failed.");
389         return nullptr;
390     }
391     if (valueType != napi_valuetype::napi_string) {
392         HILOG_ERROR_I18N("GetTypeAddon: Parameter type does not match");
393         return nullptr;
394     }
395     int32_t code = 0;
396     std::string character = VariableConvertor::GetString(env, argv[0], code);
397     if (code) {
398         return nullptr;
399     }
400     std::string type = GetType(character);
401     napi_value result = nullptr;
402     status = napi_create_string_utf8(env, type.c_str(), NAPI_AUTO_LENGTH, &result);
403     if (status != napi_ok) {
404         HILOG_ERROR_I18N("GetTypeAddon: Create getType string value failed");
405         return nullptr;
406     }
407     return result;
408 }
409 
ObjectConstructor(napi_env env,napi_callback_info info)410 napi_value I18nUnicodeAddon::ObjectConstructor(napi_env env, napi_callback_info info)
411 {
412     napi_value thisVar = nullptr;
413     void *data = nullptr;
414     napi_status status = napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, &data);
415     if (status != napi_ok) {
416         return nullptr;
417     }
418     std::unique_ptr<I18nUnicodeAddon> obj = nullptr;
419     obj = std::make_unique<I18nUnicodeAddon>();
420     status =
421         napi_wrap(env, thisVar, reinterpret_cast<void *>(obj.get()), I18nUnicodeAddon::Destructor, nullptr, nullptr);
422     if (status != napi_ok) {
423         HILOG_ERROR_I18N("Wrap I18nAddon failed");
424         return nullptr;
425     }
426     obj.release();
427     return thisVar;
428 }
429 } // namespace I18n
430 } // namespace Global
431 } // namespace OHOS