-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrmatch.h
568 lines (532 loc) · 17.7 KB
/
rmatch.h
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#ifndef RMATCH_H
#define RMATCH_H
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
typedef struct rmatch_t {
unsigned int valid;
unsigned int start;
unsigned int length;
unsigned int position;
unsigned int binary;
long size;
} rmatch_t;
char * rmatch_compile(char *regexp){
char *optimized = (char *)malloc(strlen(regexp) + 1);
char *optimized_ptr = optimized;
char prev = 0;
int in_range = 1;
while(*regexp){
if(*regexp == '[' && *(regexp + 2) == ']'){
regexp++;
*optimized_ptr++ = *regexp++;
regexp++;
continue;
}
if(*regexp == '[' && prev != '\\'){
in_range = 1;
*optimized_ptr = *regexp;
regexp++;
optimized_ptr++;
continue;
}
if(*regexp == ']' && prev != '\\'){
in_range = 0;
}
prev = *optimized_ptr;
if(in_range && *(regexp) == '0' && *(regexp + 1) == '-' && *(regexp + 2) == '9'){
regexp++;
regexp++;
*optimized_ptr++ = '\\';
*optimized_ptr++ = 'd';
regexp++;
continue;
} else if(in_range && *(regexp) == 'a' && *(regexp + 1) == '-' && *(regexp + 2) == 'z'){
regexp++;
regexp++;
*optimized_ptr++ = '\\';
*optimized_ptr++ = 'l';
regexp++;
continue;
} else if(in_range && *(regexp) == 'A' && *(regexp + 1) == '-' && *(regexp + 2) == 'Z'){
regexp++;
regexp++;
*optimized_ptr++ = '\\';
*optimized_ptr++ = 'u';
regexp++;
continue;
}
*optimized_ptr = *regexp;
optimized_ptr++;
regexp++;
}
*optimized_ptr = '\0';
return optimized;
}
char rmatch_optimize = 1;
int rmatchhere(rmatch_t * rm,unsigned char *regexp, unsigned char *text);
inline int rmatchgreedy(rmatch_t * rm, int c, unsigned char *regexp, unsigned char *text, int required);
rmatch_t *rmatch(char *regexp, char **txt)
{
rmatch_optimize = 0;
char * compiled = rmatch_optimize ? rmatch_compile(regexp) : NULL;
if(compiled)
regexp = compiled;
static rmatch_t rm;
rm.start = 0;
rm.length = 0;
rm.valid = 0;
rm.binary = 0;
rm.position = 0;
rm.size = 0;
unsigned char *text = (unsigned char*)*txt;
static unsigned int result[3];
//result[0] = 0;
memset(result, 0, sizeof(result));
if (!*text)
return &rm;
if (regexp[0] == '^')
{
if ((result[2] = rmatchhere(&rm, (unsigned char *)(regexp + 1), text)))
{
result[2]--;
result[1] = 0;
result[0] = result[2] > 0;
*txt += result[2];
rm.start = 0;
rm.valid = result[0] ? 1 : 0;
rm.position = result[0] ? result[1] + result[2] : 0;
rm.length = result[2];
}
if(compiled)
free(compiled);
return &rm;
}
unsigned int steps_total = -1;
do
{
steps_total++;
rm.start = steps_total;
rm.position = rm.start;
/* if(!*text){
if(*regexp && *regexp != '$')
{
printf("HIERRS\n");
rm.valid = 0;
}
return &rm;
}*/
if (!(result[2] = rmatchhere(&rm, (unsigned char *)regexp, text)))
continue;
result[2]--;
result[0] = result[2] > 0;
if(result[0])
result[1] = rm.start;
else
result[1] = 0;
printf("%d\n",rm.length + rm.position);
*txt += result[1] + result[2];
rm.valid = result[0] ? 1 : 0;
rm.position = result[0] ? result[1] + result[2] : 0;
rm.length = result[0] ? result[2] : 0;
if(!result[0])
rm.start = 0;
if(compiled)
free(compiled);
return &rm;
} while (*text++ != '\0');
if(compiled)
free(compiled);
rm.valid = 0;
rm.start = 0;
// rm->position = 0;
rm.length = 0;
return &rm;
}
rmatch_t * rmatchb(unsigned char *regexp, unsigned char **txt, long size)
{
static rmatch_t rm;
rm.start = 0;
rm.length = 0;
rm.valid = 0;
rm.binary = 0;
rm.position = 0;
rm.size = size;
unsigned char *text = *txt;
static unsigned int result[3];
//result[0] = 0;
memset(result, 0, sizeof(result));
if (!*text)
return &rm;
if (regexp[0] == '^')
{
if ((result[2] = rmatchhere(&rm,regexp + 1, text)))
{
result[2]--;
result[1] = 0;
result[0] = result[2] > 0;
*txt += result[2];
rm.valid = result[0] ? 1 : 0;
}
return &rm;
}
unsigned int steps_total = -1;
do
{
steps_total++;
if (!(result[2] = rmatchhere(&rm,regexp, text)))
continue;
result[2]--;
result[0] = result[2] > 0;
result[1] = steps_total;
*txt += result[1] + result[2];
return &rm;
} while (*text++ || rm.position != rm.size);
return &rm;
}
int rmatchhere(rmatch_t * rm,unsigned char *regexp, unsigned char *text)
{
unsigned int res = 0;
rm->position += 1;
rm->length += 1;
if(rm->binary && rm->position == rm->size - 1)
{
return 1;
}
if(!*text){
return regexp[0] == '$' ? 2 : 1;
}
if(regexp[0] == '('){
rm->start = rm->position - 1;
rm->length = 1;
return rmatchhere(rm, regexp+1, text) + 1;;
}
if(regexp[0] == ')'){
regexp++;
//rm->length = rm->position - rm->start + 1;
rm->length = rmatchhere(rm, regexp+1, text) + rm->start + 1;
//return rm->length + rmatchhere(rm, regexp+1, text) + 1 ;
return rm->length + rm->start; // rm->position - rm->start + 1;
}
if (regexp[0] == '\0')
return 1;
if (regexp[0] == '\\' && regexp[1] == 'd'){
if(isdigit(*text))
return rmatchhere(rm, regexp + 2, text + 1) + 1 ;
else
return 0;
}
if (regexp[0] == '\\' && regexp[1] == 'w'){
if(isalpha(*text))
return rmatchhere(rm, regexp + 2, text + 1) + 1;
else
return 0;
}
if (regexp[0] == '\\' && regexp[1] == 'l'){
if(isalpha(*text) && islower(*text))
return rmatchhere(rm, regexp + 2, text + 1) + 1;
else
return 0;
}
if (regexp[0] == '\\' && regexp[1] == 'u'){
if(isalpha(*text) && !islower(*text))
return rmatchhere(rm, regexp + 2, text + 1) + 1;
else
return 0;
}
if (regexp[1] == '*')
if ((res = rmatchgreedy(rm, regexp[0], regexp + 2, text, 0)))
{
rm->position = res - 1;
return res - 1;
}
if (regexp[1] == '+')
if ((res = rmatchgreedy(rm, regexp[0], regexp + 2, text, 1)))
return res - 1;
if (regexp[0] == '[')
{
regexp++;
int block_true = 0;
do
{
if (!block_true && *regexp == *text)
block_true++;
regexp++;
} while((*regexp != ']'));
if (block_true)
if((res = rmatchhere(rm, regexp +1 , text + 1))){
rm->position = res + 1;
return res + 1;
}
return 0;
}
if (regexp[0] == '$' && regexp[1] == '\0')
return *text == '\0';
if (regexp[0] == '\\' && regexp[1] == '.')
return *text == '.' && rmatchhere(rm, regexp + 2, text + 1) + 1;
if (( ((!rm->binary && *text != '\0' ) && !(rm->binary && rm->position == rm->size) ) && !rm->binary) && (regexp[0] == '.' || regexp[0] == *text))
if ((res = rmatchhere(rm, regexp + 1, text + 1)))
{
rm->position = res + 1;
return res + 1;
}
return 0;
}
int rmatchgreedy(rmatch_t * rm, int c, unsigned char *regexp, unsigned char *text, int required)
{
int rmatch_success = 0;
int res = 0;
do
{
rmatch_success++;
if (!(res = rmatchhere(rm,regexp, text)))
continue;
res = rmatch_success + res;
if (!required)
{
// rm->length += res;
//rm->position = res;
return res;
}
else if (required && res > 1){
//rm->length = res;
//rm->position = res;
return res;
}
} while (*text != '\0' && ((*text++ == c) || c == ')' || c == '(' || c == '.'));
printf("HIERR?");
return rmatch_success;
}
char * rmatch_replace(char *regexpr, char ** txt, char *replacement){
char * txt_ptr = *txt;
rmatch_t * match_info = rmatch(regexpr, &txt_ptr);
if(!match_info->valid){
return NULL;
}
char * txt_end = txt_ptr;
if(*txt_ptr){
char tail[strlen(txt_ptr) + 1];
strcpy(tail,txt_end);
char * txt_start = txt_ptr - match_info->length; // test
*txt_start = 0;
strcat(txt_start,replacement);
strcat(txt_start,tail);
}else{
strcat(txt_ptr,replacement);
}
char * txt_original = *txt;
*txt = txt_ptr;
return txt_original;
}
char *rmatch_extract(char *regexp, char **txt)
{
printf("A:%s\n",*txt);
rmatch_t * result = rmatch(regexp, txt);
if (result->valid)
{
printf("A1:start:%d\n",result->start);
printf("A1:position:%d\n",result->position);
printf("A1:length:%d\n",result->length);
char *extracted = *txt - result->length;
char *str = (char *)malloc(result->length + 1);
strncpy(str, extracted, result->length);
printf("B:%s\n", extracted);
*txt = extracted + result->length;
printf("C:%s\n", *txt);
str[result->length] = 0;
return str;
}
return NULL;
}
unsigned int rmatch_count(char * expr, char * text){
unsigned occurence_count = 0;
while(1){
rmatch_t * match_info = rmatch(expr, &text);
if(!match_info->valid)
break;
occurence_count++;
}
return occurence_count;
}
void rmatch_test(char *expr, char *text, char **expected)
{
char *res;
printf("test: <%s> \"%s\"\n", expr, text);
int expected_index = 0;
while (*text && expected[expected_index] != NULL)
{
char *text_original = text;
res = rmatch_extract(expr, &text);
if ((!res && !(res == expected[expected_index])) || strncmp(res, expected[expected_index], strlen(expected[expected_index])))
{
printf("\e[31merror:\n");
printf(" expected: \"%s\"\n", expected[expected_index]);
printf(" got: \"%s\"\e[0m\n", res ? res : "[no match]");
assert(0);
}
else
{
printf("\e[32mcorrect:\e[0m <%s> \"%s\"\n", expr, text_original);
}
expected_index++;
}
if (*text && expected[expected_index] != NULL)
{
printf("\e[31merror:\n");
printf(" expected: %s\n", "empty string");
printf(" got: \"%s\"\e[0m\n", text);
assert(!*text);
}
assert(!expected[expected_index]);
}
void rmatch_example_one()
{
char *str = "testtest";
char *expr = "t.*t";
// Two times it equals to "test"
assert(!strcmp(rmatch_extract(expr, &str), "test"));
assert(!strcmp(rmatch_extract(expr, &str), "test"));
// Third time it equals to NULL
assert(!rmatch_extract(expr, &str));
}
void rmatch_example_two()
{
char *str = "testtest";
char *expr = "t.*t";
char *result = NULL;
// Loops until NULL
while ((result = rmatch_extract(expr, &str)))
{
printf("Found: \"%s\"\n", result);
}
// Be sure last result is invalid
assert(!result);
}
void rmatch_tests_compile(){
// printf("<%s>\n",rmatch_compile("a*bb*.*[a][b][0-9a-zA-ZA-B1-2a-b]"));
// assert(!strcmp("a*bb*.*ab[\\d\\l\\uA-B1-2a-b]", rmatch_compile("a*bb*.*[a][b][0-9a-zA-ZA-B1-2a-b]")));
}
void rmatch_tests_extract() {
char * expr = "src=\"(.*)\"";;
char * str = "<script langugage=\"javascript\" src=\"script.js\"src=\"some-longer-script-name.js\" tag-behind=\"value\"></script>";
char * res = rmatch_extract(expr,&str);
assert(!strcmp(res, "script.js"));
free(res);
res = rmatch_extract(expr,&str);
printf("%s\n", res);
assert(!strcmp(res, "some-longer-script-name.js"));
free(res);
printf("Test with match end. (Edge case)\n");
res = rmatch_extract(expr,&str);
assert(res == NULL);
res = rmatch_extract(expr,&str);
assert(res == NULL);
res = rmatch_extract(expr,&str);
assert(res == NULL);
}
void rmatch_tests_slash(){
rmatch_t * result ;
char * text = "TeS3";
char * text_ptr;
char optimized_original = rmatch_optimize;
// Test without optimizer
rmatch_optimize = 0;
text_ptr = text;
result = rmatch("\\u\\l\\w\\d",&text_ptr);
assert(result->valid);
assert(result->length == 4);
assert(result->start == 0);
assert(result->position == 4);
// Test with optimizer
rmatch_optimize = 1;
text_ptr = text;
result = rmatch("\\u\\l\\w\\d",&text_ptr);
assert(result->valid);
assert(result->length == 4);
assert(result->start == 0);
assert(result->position == 4);
// Restore original optimizer value
rmatch_optimize = optimized_original;
}
void rmatch_tests_replace(){
char str[1024];
strcpy(str, "testxtest");
char * ptr = str;
char * result = rmatch_replace("x", &ptr, "y");
assert(!strcmp(result, "testytest"));
assert(!strcmp(str, "testytest"));
assert(!strcmp(ptr,"test"));
result = rmatch_replace("es",&ptr,"zz");
assert(!strcmp(result,"tzzt"));
assert(!strcmp(str, "testytzzt"));
assert(!strcmp(ptr,"t"));
result = rmatch_replace("zz",&result,"dd");
assert(!strcmp(result,"tddt"));
assert(!strcmp(str, "testytddt"));
assert(!strcmp(ptr,"t"));
result = rmatch_replace("q",&ptr,"p");
assert(result == NULL); // not found
assert(!strcmp(ptr,"t")); // still the same
assert(!strcmp(str, "testytddt")); // still the same
ptr = str;
result = rmatch_replace("dd",&ptr,"woop");
assert(!strcmp(result,"testytwoopt"));
printf("%s\n",ptr);
assert(!strcmp(ptr,"opt"));
assert(!strcmp(str, "testytwoopt"));
// result = rmatch_replace("es",&str)
}
void rmatch_tests()
{
// Examples
printf("Testing example one.\n");
rmatch_example_one();
printf("Testing example two.\n");
rmatch_example_two();
// Roof
printf("Testing roof.\n");
rmatch_test("testje","testje", (char *[]){NULL});
rmatch_test("^testje","testje", (char *[]){NULL});
// Dollar
printf("Testing dollar\n");
rmatch_test("^testje$","testje", (char *[]){NULL});
rmatch_test("testje$","testjetestje", (char *[]){NULL});
// Star
printf("Testing star.\n");
rmatch_test(".*H.*ry P.*rS.*la", "Harry PotterSim SalaHarry PotterSimSalaHarry PotterSimSalaHarry PotterSimSala",
(char *[]){
"Harry PotterSim Sala",
"Harry PotterSimSala",
"Harry PotterSimSala",
"Harry PotterSimSala",
NULL});
char *text_fox = "The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.";
rmatch_test("The q.*ick b.*n f.*x j.*s over the lazy dog.$", text_fox, (char *[]){"The quick brown fox jumps over the lazy dog.", NULL});
char *text_fox2 = "The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.The quick brown fox jumps over the lazy dog.";
rmatch_test("T.*e q.*k b.*n f.*x j.*s o.*r t.*e l.*y d.*g.", text_fox2, (char *[]){"The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.", "The quick brown fox jumps over the lazy dog.", NULL});
// Block
printf("Testing block.\n");
char *text_fox3 = "The quick brown fox jumps over the lazy dog.";
rmatch_test("T.*e q.*k b.*n f.*x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox3, (char *[]){"The quick brown fox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n f.*x j.*s o.*r t.*e.*l.*[obcdefghia][zabcdefg].*y d[ao]g.", text_fox3, (char *[]){"The quick brown fox jumps over the lazy dog.", NULL});
/// Plus
printf("Testing plus.\n");
char *text_fox4 = "The quick brown fooox jumps over the lazy dog.";
rmatch_test("T.*e q.*k b.*n fo+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n f.+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n f+.*x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n fo+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){"The quick brown fooox jumps over the lazy dog.", NULL});
rmatch_test("T.*e q.*k b.*n f+x j.*s o.*r t.*e l[oa][az].*y d[ao]g.", text_fox4, (char *[]){NULL});
printf("Testing replace.\n");
rmatch_tests_replace();
printf("Testing compile.\n");
rmatch_tests_compile();
printf("Testing slash commands.\n");
rmatch_tests_slash();
printf("Testing extract.\n");
rmatch_tests_extract();
printf("Tests succesfully completed!\n");
}
#endif