forked from cesanta/v7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregex.c
171 lines (136 loc) · 5.48 KB
/
regex.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* Copyright (c) 2014 Cesanta Software Limited
* All rights reserved
*/
#include "internal.h"
#if V7_ENABLE__RegExp
V7_PRIVATE val_t to_string(struct v7 *, val_t);
V7_PRIVATE val_t Regex_ctor(struct v7 *v7, val_t this_obj, val_t args) {
long argnum = v7_array_length(v7, args);
if (argnum > 0) {
val_t ro = to_string(v7, v7_array_get(v7, args, 0));
size_t re_len, flags_len = 0;
const char *re = v7_to_string(v7, &ro, &re_len), *flags = NULL;
struct slre_prog *p = NULL;
struct v7_regexp *rp;
(void) this_obj;
if (argnum > 1) {
val_t fl = to_string(v7, v7_array_get(v7, args, 1));
flags = v7_to_string(v7, &fl, &flags_len);
}
if (slre_compile(re, re_len, flags, flags_len, &p, 1) != SLRE_OK ||
p == NULL) {
throw_exception(v7, TYPE_ERROR, "Invalid regex");
return v7_create_undefined();
} else {
rp = (struct v7_regexp *) malloc(sizeof(*rp));
rp->regexp_string = v7_create_string(v7, re, re_len, 1);
rp->compiled_regexp = p;
rp->lastIndex = 0;
return v7_pointer_to_value(rp) | V7_TAG_REGEXP;
}
}
return v7_create_regexp(v7, "(?:)", 4, NULL, 0);
}
static val_t Regex_global(struct v7 *v7, val_t this_obj, val_t args) {
int flags = 0;
val_t r = i_value_of(v7, this_obj);
(void) args;
if (v7_is_regexp(r)) flags = slre_get_flags(v7_to_regexp(r)->compiled_regexp);
return v7_create_boolean(flags & SLRE_FLAG_G);
}
static val_t Regex_ignoreCase(struct v7 *v7, val_t this_obj, val_t args) {
int flags = 0;
val_t r = i_value_of(v7, this_obj);
(void) args;
if (v7_is_regexp(r)) flags = slre_get_flags(v7_to_regexp(r)->compiled_regexp);
return v7_create_boolean(flags & SLRE_FLAG_I);
}
static val_t Regex_multiline(struct v7 *v7, val_t this_obj, val_t args) {
int flags = 0;
val_t r = i_value_of(v7, this_obj);
(void) args;
if (v7_is_regexp(r)) flags = slre_get_flags(v7_to_regexp(r)->compiled_regexp);
return v7_create_boolean(flags & SLRE_FLAG_M);
}
static val_t Regex_source(struct v7 *v7, val_t this_obj, val_t args) {
val_t r = i_value_of(v7, this_obj);
const char *buf = 0;
size_t len = 0;
(void) args;
if (v7_is_regexp(r))
buf = v7_to_string(v7, &v7_to_regexp(r)->regexp_string, &len);
return v7_create_string(v7, buf, len, 1);
}
static val_t Regex_get_lastIndex(struct v7 *v7, val_t this_obj, val_t args) {
long lastIndex = 0;
(void) v7;
(void) args;
if (v7_is_regexp(this_obj)) lastIndex = v7_to_regexp(this_obj)->lastIndex;
return v7_create_number(lastIndex);
}
static val_t Regex_set_lastIndex(struct v7 *v7, val_t this_obj, val_t args) {
long lastIndex = 0;
if (v7_is_regexp(this_obj))
v7_to_regexp(this_obj)->lastIndex = lastIndex = arg_long(v7, args, 0, 0);
return v7_create_number(lastIndex);
}
V7_PRIVATE val_t rx_exec(struct v7 *v7, val_t rx, val_t str, int lind) {
if (v7_is_regexp(rx)) {
val_t s = to_string(v7, str);
size_t len;
struct slre_loot sub;
struct slre_cap *ptok = sub.caps;
char *const str = (char *) v7_to_string(v7, &s, &len);
const char *const end = str + len;
const char *begin = str;
struct v7_regexp *rp = v7_to_regexp(rx);
int flag_g = slre_get_flags(rp->compiled_regexp) & SLRE_FLAG_G;
if (rp->lastIndex < 0) rp->lastIndex = 0;
if (flag_g || lind) begin = utfnshift(str, rp->lastIndex);
if (!slre_exec(rp->compiled_regexp, 0, begin, end, &sub)) {
int i;
val_t arr = v7_create_array(v7);
for (i = 0; i < sub.num_captures; i++, ptok++)
v7_array_push(v7, arr, v7_create_string(v7, ptok->start,
ptok->end - ptok->start, 1));
if (flag_g) rp->lastIndex = utfnlen(str, sub.caps->end - str);
v7_set_property(v7, arr, "index", 5, V7_PROPERTY_READ_ONLY,
v7_create_number(utfnlen(str, sub.caps->start - str)));
return arr;
} else
rp->lastIndex = 0;
}
return v7_create_null();
}
static val_t Regex_exec(struct v7 *v7, val_t this_obj, val_t args) {
if (v7_array_length(v7, args) > 0) {
return rx_exec(v7, this_obj, v7_array_get(v7, args, 0), 0);
}
return v7_create_null();
}
static val_t Regex_test(struct v7 *v7, val_t this_obj, val_t args) {
return v7_create_boolean(!v7_is_null(Regex_exec(v7, this_obj, args)));
}
V7_PRIVATE void init_regex(struct v7 *v7) {
val_t ctor =
v7_create_cfunction_ctor(v7, v7->regexp_prototype, Regex_ctor, 1);
val_t lastIndex = v7_create_dense_array(v7);
v7_set_property(v7, v7->global_object, "RegExp", 6, V7_PROPERTY_DONT_ENUM,
ctor);
set_cfunc_prop(v7, v7->regexp_prototype, "exec", Regex_exec);
set_cfunc_prop(v7, v7->regexp_prototype, "test", Regex_test);
v7_set_property(v7, v7->regexp_prototype, "global", 6, V7_PROPERTY_GETTER,
v7_create_cfunction(Regex_global));
v7_set_property(v7, v7->regexp_prototype, "ignoreCase", 10,
V7_PROPERTY_GETTER, v7_create_cfunction(Regex_ignoreCase));
v7_set_property(v7, v7->regexp_prototype, "multiline", 9, V7_PROPERTY_GETTER,
v7_create_cfunction(Regex_multiline));
v7_set_property(v7, v7->regexp_prototype, "source", 6, V7_PROPERTY_GETTER,
v7_create_cfunction(Regex_source));
v7_array_set(v7, lastIndex, 0, v7_create_cfunction(Regex_get_lastIndex));
v7_array_set(v7, lastIndex, 1, v7_create_cfunction(Regex_set_lastIndex));
v7_set_property(v7, v7->regexp_prototype, "lastIndex", 9,
V7_PROPERTY_GETTER | V7_PROPERTY_SETTER, lastIndex);
}
#endif /* V7_ENABLE__RegExp */