-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd_regex.h
82 lines (71 loc) · 1.38 KB
/
d_regex.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
// url: <protocal>://<host>/<path>
#ifndef D_REGEX_H
#define D_REGEX_H
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
char* name;
int name_len;
char* txt;
int txt_len;
} dr_region;
typedef struct {
bool success;
dr_region* regions;
int num_regions;
} d_regex_res;
d_regex_res d_regex_match(char* regex, char* txt);
#endif
#ifdef D_IMPL
#define D_REGEX_IMPL
#endif
#ifdef D_REGEX_IMPL
#ifndef D_REGEX_IMPL_ONCE
#define D_REGEX_IMPL_ONCE
#include <stdlib.h>
#include <string.h>
d_regex_res d_regex_match(char* regex, char* txt) {
char* r = regex;
char* t = txt;
int num_regions = 0;
dr_region* regions = malloc(0);
for (;;) {
if (*r == '\0') {
break;
}
if (*r == '<') {
char* name_start = r + 1;
while (*r != '>') r++;
int name_len = r - name_start;
char* txt_start = t;
char stop = *++r;
while (*t != stop) t++;
int txt_len = t - txt_start;
regions = realloc(regions, (num_regions + 1) * sizeof(dr_region));
dr_region r = (dr_region) {
.name = name_start,
.name_len = name_len,
.txt = txt_start,
.txt_len = txt_len,
};
regions[num_regions++] = r;
} else {
if (*r != *t) {
return (dr_res) {
.success = false,
.regions = regions,
.num_regions = num_regions,
};
}
r++;
t++;
}
}
return (dr_res) {
.success = true,
.regions = regions,
.num_regions = num_regions,
};
}
#endif
#endif