-
Notifications
You must be signed in to change notification settings - Fork 22
/
regex.cpp
229 lines (180 loc) · 5.62 KB
/
regex.cpp
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
#include "nfautil.h"
#include "regex.h"
#define DEREF(arr,i) ((*(arr))[(i)])
/* constructor for SimpleReBuilder */
void simpleReBuilder(SimpleReBuilder ** builder, int len) {
(*builder)->re = (char *)malloc(len+3);
(*builder)->size = len+3;
}
void _simpleReBuilder(SimpleReBuilder * builder) {
free(builder->re);
/* the rest is on the stack */
}
void regex_error(int i) {
fprintf(stderr, "Improper regex at character %d", i);
exit(1);
}
char * stringify(char * nonull, int j) {
char * proper = (char*)malloc(j+2);
memcpy(proper, nonull, j);
proper[j+1] = '\0';
return proper;
}
void insertIntoComplexRe(char ** complexRe, int where, int * len, char * toInsert) {
char * buf;
int insertLen = strlen(toInsert);
int i = where;
/* enough space for complexRe+the new range */
*len = *len+(insertLen+1);
*complexRe = (char*)realloc(*complexRe, *len);
/* buffer the rest */
buf = (char*)malloc(*len);
int k;
for (k = i+2; k < *len-(insertLen+1); k++) {
buf[k-(i+2)] = DEREF(complexRe,k);
}
/* insert the string */
for (k = 0; k < insertLen; k++) {
DEREF(complexRe, i++) = toInsert[k];
}
/* put the buffer back in */
for (k = i; k < *len; k++) {
DEREF(complexRe,k) = buf[k-i];
}
free(buf);
}
void handle_escape(SimpleReBuilder * builder, char ** complexRe, int * len, int * bi, int * ci) {
int i = *ci;
int j = *bi;
if (i+1 > *len)
regex_error(i);
i++; //go to escaped character and ignore '/'
switch(DEREF(complexRe,i)) {
case 't':
insertIntoComplexRe(complexRe, --i, len, "\t");
break;
case 'n':
insertIntoComplexRe(complexRe, --i, len, "\n");
break;
case 'd':
insertIntoComplexRe(complexRe, --i, len, "[0-9]");
break;
case 'w':
insertIntoComplexRe(complexRe, --i, len, "([a-z]|[A-Z]|_)");
break;
case 's':
insertIntoComplexRe(complexRe, --i, len, "( |\t|\n)");
break;
/* ... see www.cs.tut.fi/~jkorpela/perl/regexp.html */
// default is just ignoring the backslash and taking the
// LITERAL character after no matter what
default:
builder->re[j++] = DEREF(complexRe, i++);
break;
}
*ci = i-1; //-1 because we incremented at the end
*bi = j-1; //-1 because we incremented at the end
}
void putRange(SimpleReBuilder * builder, char start, char end, int * bi) {
int i = *bi;
int amount = ((end - start + 1)*2)+1;
builder->size += amount;
builder->re = (char *)realloc(builder->re, builder->size);
builder->re[i++] = PAREN_OPEN;
builder->re[i++] = start;
char k;
for (k = start+1; k <= end; k++) {
builder->re[i++] = ALTERNATE;
builder->re[i++] = k;
}
builder->re[i] = PAREN_CLOSE;
*bi = i;
}
void handle_range(SimpleReBuilder * builder, char * complexRe, int len, int * bi, int * ci) {
int i = *ci;
if (complexRe[i+4] != ']' || complexRe[i+2] != '-' || complexRe[i+1] > complexRe[i+3] || complexRe[i+1] <= 0x20) {
fprintf(stderr, "Invalid range at character %d\n", i);
exit(1);
}
putRange(builder, complexRe[i+1], complexRe[i+3], bi);
*ci = i+4;
}
SimpleReBuilder * simplifyRe(char ** complexRe, SimpleReBuilder * builder) {
int len = strlen(*complexRe);
simpleReBuilder(&builder, len);
int i,j;
for (i = 0, j = 0; i < len; i++, j++) {
switch(DEREF(complexRe, i)) {
case '\\':
handle_escape(builder, complexRe, &len, &j, &i);
break;
case '.':
builder->re[j] = ANY; //nak is ANY
break;
case '+':
builder->re[j] = PLUS; //0x01 is +
break;
case '?':
builder->re[j] = QUESTION; //0x02 is ?
break;
case '*':
builder->re[j] = STAR; //0x03 is *
break;
case '|':
builder->re[j] = ALTERNATE; //0x04 is |
break;
case '(':
builder->re[j] = PAREN_OPEN; //0x05 is (
break;
case ')':
builder->re[j] = PAREN_CLOSE; //0x06 is )
break;
case '[':
handle_range(builder, *complexRe, len, &j, &i);
break;
default:
builder->re[j] = DEREF(complexRe,i);
break;
}
}
builder->re[j] = '\0';
return builder;
}
char * stringifyRegex(const char * oldRegex) {
int len = strlen(oldRegex);
char * newRegex = (char*)malloc(len+1);
int i;
for (i = 0; i < len; i++) {
switch(oldRegex[i]) {
case ANY:
newRegex[i] = '.';
break;
case CONCATENATE:
newRegex[i] = '`';
break;
case ALTERNATE:
newRegex[i] = '|';
break;
case QUESTION:
newRegex[i] = '?';
break;
case STAR:
newRegex[i] = '*';
break;
case PLUS:
newRegex[i] = '+';
break;
case PAREN_OPEN:
newRegex[i] = '(';
break;
case PAREN_CLOSE:
newRegex[i] = ')';
break;
default:
newRegex[i] = oldRegex[i];
break;
}
}
newRegex[i] = '\0';
return newRegex;
}