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