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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
* Copyright (c) 2020, Emanuel Sprung <emanuel.sprung@gmail.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibRegex/Regex.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#ifdef __serenity__
# include <regex.h>
#else
# include <LibC/regex.h>
#endif
struct internal_regex_t {
u8 cflags;
u8 eflags;
OwnPtr<Regex<PosixExtended>> re;
size_t re_pat_errpos;
ReError re_pat_err;
String re_pat;
size_t re_nsub;
};
static internal_regex_t* impl_from(regex_t* re)
{
if (!re)
return nullptr;
return reinterpret_cast<internal_regex_t*>(re->__data);
}
static const internal_regex_t* impl_from(const regex_t* re)
{
return impl_from(const_cast<regex_t*>(re));
}
extern "C" {
int regcomp(regex_t* reg, const char* pattern, int cflags)
{
if (!reg)
return REG_ESPACE;
// Note that subsequent uses of regcomp() without regfree() _will_ leak memory
// This could've been prevented if libc provided a reginit() or similar, but it does not.
reg->__data = new internal_regex_t { 0, 0, {}, 0, ReError::REG_NOERR, {}, 0 };
auto preg = impl_from(reg);
if (!(cflags & REG_EXTENDED))
return REG_ENOSYS;
preg->cflags = cflags;
String pattern_str(pattern);
preg->re = make<Regex<PosixExtended>>(pattern_str, PosixOptions {} | (PosixFlags)cflags | PosixFlags::SkipTrimEmptyMatches);
auto parser_result = preg->re->parser_result;
if (parser_result.error != regex::Error::NoError) {
preg->re_pat_errpos = parser_result.error_token.position();
preg->re_pat_err = (ReError)parser_result.error;
preg->re_pat = pattern;
dbgln("Have Error: {}", (int)parser_result.error);
return (ReError)parser_result.error;
}
preg->re_nsub = parser_result.capture_groups_count;
return REG_NOERR;
}
int regexec(const regex_t* reg, const char* string, size_t nmatch, regmatch_t pmatch[], int eflags)
{
auto preg = impl_from(reg);
if (!preg->re || preg->re_pat_err) {
if (preg->re_pat_err)
return preg->re_pat_err;
return REG_BADPAT;
}
RegexResult result;
if (eflags & REG_SEARCH)
result = preg->re->search(string, PosixOptions {} | (PosixFlags)eflags);
else
result = preg->re->match(string, PosixOptions {} | (PosixFlags)eflags);
if (result.success) {
auto size = result.matches.size();
if (size && nmatch && pmatch) {
pmatch[0].rm_cnt = size;
size_t match_index { 0 };
for (size_t i = 0; i < size; ++i) {
pmatch[match_index].rm_so = result.matches.at(i).global_offset;
pmatch[match_index].rm_eo = pmatch[match_index].rm_so + result.matches.at(i).view.length();
if (match_index > 0)
pmatch[match_index].rm_cnt = result.capture_group_matches.size();
++match_index;
if (match_index >= nmatch)
return REG_NOERR;
if (i < result.capture_group_matches.size()) {
auto capture_groups_size = result.capture_group_matches.at(i).size();
for (size_t j = 0; j < preg->re->parser_result.capture_groups_count; ++j) {
if (j >= capture_groups_size || !result.capture_group_matches.at(i).at(j).view.length()) {
pmatch[match_index].rm_so = -1;
pmatch[match_index].rm_eo = -1;
pmatch[match_index].rm_cnt = 0;
} else {
pmatch[match_index].rm_so = result.capture_group_matches.at(i).at(j).global_offset;
pmatch[match_index].rm_eo = pmatch[match_index].rm_so + result.capture_group_matches.at(i).at(j).view.length();
pmatch[match_index].rm_cnt = 1;
}
++match_index;
if (match_index >= nmatch)
return REG_NOERR;
}
}
}
if (match_index < nmatch) {
for (size_t i = match_index; i < nmatch; ++i) {
pmatch[i].rm_so = -1;
pmatch[i].rm_eo = -1;
pmatch[i].rm_cnt = 0;
}
}
}
return REG_NOERR;
} else {
if (nmatch && pmatch) {
pmatch[0].rm_so = -1;
pmatch[0].rm_eo = -1;
pmatch[0].rm_cnt = 0;
}
}
return REG_NOMATCH;
}
inline static String get_error(ReError errcode)
{
String error;
switch ((ReError)errcode) {
case REG_NOERR:
error = "No error";
break;
case REG_NOMATCH:
error = "regexec() failed to match.";
break;
case REG_BADPAT:
error = "Invalid regular expression.";
break;
case REG_ECOLLATE:
error = "Invalid collating element referenced.";
break;
case REG_ECTYPE:
error = "Invalid character class type referenced.";
break;
case REG_EESCAPE:
error = "Trailing \\ in pattern.";
break;
case REG_ESUBREG:
error = "Number in \\digit invalid or in error.";
break;
case REG_EBRACK:
error = "[ ] imbalance.";
break;
case REG_EPAREN:
error = "\\( \\) or ( ) imbalance.";
break;
case REG_EBRACE:
error = "\\{ \\} imbalance.";
break;
case REG_BADBR:
error = "Content of \\{ \\} invalid: not a number, number too large, more than two numbers, first larger than second.";
break;
case REG_ERANGE:
error = "Invalid endpoint in range expression.";
break;
case REG_ESPACE:
error = "Out of memory.";
break;
case REG_BADRPT:
error = "?, * or + not preceded by valid regular expression.";
break;
case REG_ENOSYS:
error = "The implementation does not support the function.";
break;
case REG_EMPTY_EXPR:
error = "Empty expression provided";
break;
}
return error;
}
size_t regerror(int errcode, const regex_t* reg, char* errbuf, size_t errbuf_size)
{
String error;
auto preg = impl_from(reg);
if (!preg)
error = get_error((ReError)errcode);
else
error = preg->re->error_string(get_error(preg->re_pat_err));
if (!errbuf_size)
return error.length();
if (!error.copy_characters_to_buffer(errbuf, errbuf_size))
return 0;
return error.length();
}
void regfree(regex_t* reg)
{
auto preg = impl_from(reg);
if (preg) {
delete preg;
reg->__data = nullptr;
}
}
}
|