• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #include "common/napi_helper.cpp"
17 #include "common/native_common.h"
18 #include "napi/native_api.h"
19 #include <cerrno>
20 #include <js_native_api.h>
21 #include <node_api.h>
22 #include <regex.h>
23 
24 #define ONE 1
25 #define PARAM_0 0
26 #define PARAM_1 1
27 #define PARAM_2 2
28 #define PARAM_UNNORMAL (-1)
29 #define RETURN_0 0
30 #define FAILD (-1)
31 #define ERRON_0 0
32 #define SIZE_10 10
33 #define SIZE_100 100
34 #define SIZE_4096 4096
35 #define SIZE_8192 8192
36 
Regcomp(napi_env env,napi_callback_info info)37 static napi_value Regcomp(napi_env env, napi_callback_info info)
38 {
39     regex_t r = {PARAM_0};
40     const char pstTern[] = "1234567890";
41     int regCompValue = regcomp(&r, pstTern, REG_EXTENDED);
42     napi_value result = nullptr;
43     napi_create_int32(env, regCompValue, &result);
44     return result;
45 }
46 
Regexec(napi_env env,napi_callback_info info)47 static napi_value Regexec(napi_env env, napi_callback_info info)
48 {
49     const char *pstTern = "1234567890";
50     char buff[10] = "1";
51     const size_t nMatch = ONE;
52     regex_t r = {PARAM_0};
53     regmatch_t pMatch[1];
54     napi_value result = nullptr;
55     int regCompValue = regcomp(&r, pstTern, REG_EXTENDED);
56     if (regCompValue == PARAM_0) {
57         int regExecValue = regexec(&r, buff, nMatch, pMatch, PARAM_0);
58         napi_create_int32(env, regExecValue, &result);
59     }
60     return result;
61 }
62 
Regerror(napi_env env,napi_callback_info info)63 static napi_value Regerror(napi_env env, napi_callback_info info)
64 {
65     char buf[200];
66     regex_t reg = {PARAM_0};
67     int resultValue = FAILD;
68     int error_value = regerror(PARAM_0, &reg, buf, sizeof buf);
69     if (error_value != PARAM_0) {
70         resultValue = RETURN_0;
71     }
72 
73     napi_value result = nullptr;
74     napi_create_int32(env, resultValue, &result);
75 
76     return result;
77 }
78 
Regfree(napi_env env,napi_callback_info info)79 static napi_value Regfree(napi_env env, napi_callback_info info)
80 {
81     regex_t reg = {PARAM_0};
82     const char *psttern = "1234567890";
83     regcomp(&reg, psttern, REG_EXTENDED);
84     int resultValue = FAILD;
85     errno = ERRON_0;
86     regfree(&reg);
87     if (errno == ERRON_0) {
88         resultValue = RETURN_0;
89     }
90 
91     napi_value result = nullptr;
92     napi_create_int32(env, resultValue, &result);
93 
94     return result;
95 }
96 
97 EXTERN_C_START
Init(napi_env env,napi_value exports)98 static napi_value Init(napi_env env, napi_value exports)
99 {
100     napi_property_descriptor desc[] = {
101         {"regcomp", nullptr, Regcomp, nullptr, nullptr, nullptr, napi_default, nullptr},
102         {"regexec", nullptr, Regexec, nullptr, nullptr, nullptr, napi_default, nullptr},
103         {"regerror", nullptr, Regerror, nullptr, nullptr, nullptr, napi_default, nullptr},
104         {"regfree", nullptr, Regfree, nullptr, nullptr, nullptr, napi_default, nullptr},
105     };
106     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
107     return exports;
108 }
109 
110 EXTERN_C_END
111 
112 static napi_module demoModule = {
113     .nm_version = 1,
114     .nm_flags = 0,
115     .nm_filename = nullptr,
116     .nm_register_func = Init,
117     .nm_modname = "regex",
118     .nm_priv = ((void *)0),
119     .reserved = {0},
120 };
121 
RegisterEntryModule(void)122 extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&demoModule); }
123