-
Notifications
You must be signed in to change notification settings - Fork 1
/
shre.c
327 lines (282 loc) · 7.6 KB
/
shre.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
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
/* shre.c
*
* Implementation of the regex interface.
*/
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "class.h"
#include "core.h"
#include "parser.h"
#include "factory.h"
#include "tokens.h"
#include "range.h"
#include "util.h"
#include "obhash.h"
#include "shre.h"
/* pattern
*
* Declaration of the pattern object, which is more or less
* a wrapper for a core object.
*/
struct _pattern {
core_t* core;
obhash_t* names; // named groups
char* regex; // the string passed into compile
};
/* match
*
* Declaration of match object. Has an array of group structs
* which represent group captures.
*/
struct _match {
obhash_t* names;
range_t* groups;
uint32_t offset;
};
/* scanner
*
* Declaration of scanner struct. obj keeps track of the search position
* in the string.
*/
struct _scanner {
pattern_t* pattern;
char* start;
char* curr;
};
// static functions
// The functions 'match_new' and 'free_pattern' shouldn't be called
// by the user.
/** match_new
*
* Make a new match object from the return value of core_match.
*/
static match_t* match_new(range_t*, obhash_t*, int);
/** free_pattern
*
* Deallocate the memory used by a pattern object. Don't free
* the regex string, since that will be freed in the hash table.
*/
static void free_pattern(pattern_t* pattern) {
obhash_free(pattern->names);
core_free(pattern->core);
free(pattern);
}
/*************************global variables***************************/
obhash_t* ptable = NULL; // global pattern hash table
class_t* word_characters = NULL; // values of word characters
char* trash;
/**************************regex engine functions********************/
void start_regex_engine() {
assert(!ptable);
ptable = obhash_new( (void (*)(void*)) &free_pattern);
word_characters = parse_class("[\\w]");
}
bool engine_is_initialized() {
return ptable;
}
int num_patterns() {
assert(ptable);
return obhash_size(ptable);
}
void clear_cache() {
assert(ptable);
obhash_clear(ptable);
}
void cleanup_regex_engine() {
assert(ptable);
obhash_free(ptable);
ptable = NULL;
class_free(word_characters);
word_characters = NULL;
}
/*****************************regex operations***********************/
pattern_t* shre_compile(char* regex) {
assert(ptable);
assert(regex);
// look for pattern in hashtable
pattern_t* pattern = (pattern_t*) obhash_find(ptable, regex);
if (pattern)
return pattern;
// build a new pattern
obhash_t* names = NULL;
tlist_t* tokens = parse_regex(regex, &names);
if (!tokens)
return NULL; // bad regular expression; shre_er is tree
pattern = malloc(sizeof(pattern_t));
assert(pattern);
pattern->regex = strdup(regex);
pattern->names = names;
pattern->core = build_core(tokens);
obhash_add(ptable, pattern->regex, pattern); // add new pattern
return pattern;
}
const char* shre_expression(pattern_t* obj) {
assert(ptable);
assert(obj);
return obj->regex;
}
match_t* shre_search(pattern_t* pattern, char* str) {
assert(ptable);
assert(pattern);
assert(str);
char* start = str;
for (;; ++str) {
range_t* groups = core_match(pattern->core, str,
NULL, NULL, NULL, 0, &trash, start);
if (groups)
return match_new(groups, pattern->names,
range_group(groups, 0)->begin - start);
if (*str == '\0')
break;
}
return NULL;
}
match_t* shre_entire(pattern_t* pattern, char* str) {
assert(ptable);
assert(pattern);
assert(str);
range_t* groups =
core_match(pattern->core, str, NULL, NULL,
NULL, 0, &trash, str);
if (!groups)
return NULL;
if (*(range_group(groups, 0)->end) == '\0') {
return match_new(groups, pattern->names,
range_group(groups, 0)->begin - str);
}
return NULL;
}
bool quick_search(char* regex, char* str) {
assert(regex);
assert(str);
pattern_t* pattern = shre_compile(regex);
if (!pattern)
return false;
char* start = str;
while (*str != '\0') {
range_t* groups =
core_match(pattern->core, ++str, NULL, NULL,
NULL, 0, &trash, start);
if (groups) {
range_free(groups);
return true;
}
}
return false;
}
bool quick_entire(char* regex, char* str) {
assert(regex);
assert(str);
pattern_t* pattern = shre_compile(regex);
if (!pattern)
return false;
range_t* groups =
core_match(pattern->core, str, NULL, NULL,
NULL, 0, &trash, str);
if (!groups)
return false;
if (*(range_group(groups, 0)->end) == '\0') {
range_free(groups);
return true;
}
range_free(groups);
return false;
}
/*****************************match operations************************/
char* match_get(match_t* match) {
assert(match);
group_t* main = range_group(match->groups, 0);
return substring(main->begin, main->end);
}
int match_num_groups(match_t* match) {
assert(match);
return range_size(match->groups);;
}
uint32_t match_offset(match_t* match) {
assert(match);
return match->offset;
}
char* match_group(match_t* match, int gr) {
assert(match);
group_t* captcha = range_group(match->groups, gr);
if (!captcha || !captcha->begin)
return NULL;
return substring(captcha->begin, captcha->end);
}
char* match_named_group(match_t* match, char* grname) {
assert(match);
int* gr = NULL;
if (match->names)
gr = (int*) obhash_find(match->names, grname);
if (!gr)
return NULL;
return match_group(match, *gr);
}
match_t* match_new(range_t* groups, obhash_t* names, int offset) {
match_t* match = malloc(sizeof(match_t));
assert(match);
match->offset = offset;
match->names = names;
match->groups = groups;
return match;
}
void match_free(match_t* match) {
if (match) {;
range_free(match->groups);
free(match);
}
}
/*************************scanner operations*************************/
scanner_t* scan_new(pattern_t* pattern, char* input) {
assert(pattern);
assert(input);
scanner_t* scanner = malloc(sizeof(scanner_t));
assert(scanner);
scanner->pattern = pattern;
scanner->start = scanner->curr = input;
return scanner;
}
match_t* scan_next(scanner_t* sc) {
assert(ptable);
assert(sc);
for (;; ++sc->curr) {
char* pos = sc->curr;
range_t* groups = core_match(sc->pattern->core, sc->curr,
NULL, NULL, NULL, 0, &sc->curr, sc->start);
if (groups) {
if (pos == sc->curr)
scan_increment(sc);
return match_new(groups, sc->pattern->names,
range_group(groups, 0)->begin - sc->start);
}
if (*sc->curr == '\0')
break;
}
return NULL;
}
match_t* scan_try(scanner_t* sc) {
assert(ptable);
assert(sc);
range_t* groups = core_match(sc->pattern->core, sc->curr, NULL,
NULL, NULL, 0, &trash, sc->start);
if (groups)
return match_new(groups, sc->pattern->names,
range_group(groups, 0)->begin - sc->start);
return NULL;
}
void scan_seek(scanner_t* sc, uint32_t seek) {
assert(sc);
uint32_t len = strlen(sc->start);
sc->curr = sc->start + (seek >= len ? len : seek);
}
uint32_t scan_tell(scanner_t* sc) {
assert(sc);
return sc->curr - sc->start;
}
void scan_increment(scanner_t* sc) {
assert(sc);
if (*sc->curr != '\0')
++sc->curr;
}
/********************************************************************/