forked from Fordi/nbasic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegEx.cpp
executable file
·217 lines (206 loc) · 4.75 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
#include "RegEx.h"
#include <stdio.h>
#include <string.h>
extern void Exit(const char*);
//void Exit(const char*){}
RegEx::RegEx(const char *s)
{
next = NULL;
value = NULL;
repeat = false;
plainstring = true;
valuelen = 0;
if (s) {
Load(s);
}
}
RegEx::~RegEx()
{
if (next != NULL) delete next;
if (value != NULL) delete value;
}
void RegEx::Load(const char* s)
{
int stringlen = strlen(s);
if (!SetContains(s, '('))
{
//it's just a string
value = new char[stringlen + 1];
strcpy(value, s);
value[stringlen] = 0;
plainstring = true;
repeat = false;
valuelen = stringlen;
//printf("string %s\n", value);
next = NULL;
#ifdef REGEX_DEBUG
printf("loaded string %s, length=%i\n",value,valuelen);
#endif
return;
}
int setbegin = WhereInSet(s, '(');
if (setbegin > 0)
{
//first build the string
value = new char[setbegin+1];
strncpy(value, s, setbegin);
value[setbegin+1] = 0;
plainstring = true;
repeat = false;
valuelen = strlen(value);
#ifdef REGEX_DEBUG
printf("begin string %s, length=%i\n", value,valuelen);
#endif
//then tack on the ending regex
next = new RegEx();
next->Load(&s[setbegin]);
}
else //setbegin == 0
{
//build the set from here
int setend = WhereInSet(s, ')');
if (setend == -1)
{
Exit("Error building regular expression set. No set close found. Ignoring.\n");
}
int beginrange=-1, endrange=-1;
int index = 1;
char tempset[256];
memset(tempset, 0, 256);
while(index < setend)
{
#ifdef REGEX_DEBUG
printf("starting char range search\n");
#endif
if (s[index] != '\'')
{
Exit("Error building regular expression set. Invalid range definition. Ignoring.\n");
}
beginrange = ParseChar(s,index); //auto-increments index past char close
#ifdef REGEX_DEBUG
printf("beginrange=%c\n",beginrange);
#endif
if (s[index] == '-')
{
//find the end of the range
index++;
endrange = ParseChar(s,index);
}
else if(s[index] == ',' || index==setend)
{
//range is one character
#ifdef REGEX_DEBUG
printf("found comma: index=%i, setend=%i\n",index, setend);
#endif
endrange = beginrange;
index++;
}
else if(index < setend)
{
//oops! invalid
Exit("Error building regular expression set. Invalid range definition. Ignoring.\n");
}
for(int i=beginrange; i<=endrange; i++)
if(i>0) tempset[strlen(tempset)] = i;
if (s[index]==',') index++;
}
plainstring = false;
valuelen = strlen(tempset);
value = new char[valuelen+1];
memset(value, 0, valuelen+1);
strcpy(value, tempset);
//check if it's a repeating set
if (setend+1 < stringlen && s[setend+1]=='*')
repeat = true;
else
repeat = false;
#ifdef REGEX_DEBUG
printf("set %s %s\n",value,(repeat?"repeats":"does not repeat"));
#endif
//next regex
if (setend+(repeat ? 2 : 1) < stringlen)
{
next = new RegEx();
next->Load(&s[setend+(repeat?2:1)]);
}
else
{
next = NULL;
return;
}
}
}
int RegEx::Matches(const char* s) const
{
int stringlen = strlen(s);
if (plainstring)
{
#ifdef REGEX_DEBUG
printf("searching for plainstring match %s\n",value);
printf("stringlen=%i, valuelen=%i\n",stringlen,valuelen);
#endif
if (stringlen < valuelen) return -1;
if (valuelen == stringlen) return (strcmp(s,value) ? -1 : valuelen);
if (strncmp(s,value,valuelen)) return -1;
if (!next) return valuelen;
int rest = next->Matches(&s[valuelen]);
return (rest==-1 ? -1 : rest + valuelen);
}
//else we've got a set
if (!repeat)
{
#ifdef REGEX_DEBUG
printf("searching non-repeating set %s\n",value);
#endif
if (stringlen <= 1) return -1;
if (!SetContains(value, s[0])) return -1;
if (next == NULL) return 1;
int rest = next->Matches(&s[1]);
return (rest==-1 ? -1 : rest + 1);
}
//else repeat it any number of times
#ifdef REGEX_DEBUG
printf("searching repeating set %s\n",value);
#endif
int index = 0;
while(SetContains(value, s[index])) index++;
if (next == NULL) return index;
int rest = next->Matches(&s[index]);
return (rest==-1 ? -1 : rest + index);
}
bool RegEx::SetContains(const char* set, char c) const
{
int setlen = strlen(set);
for(int i=0; i<setlen; i++)
if (c==set[i]) return true;
return false;
}
int RegEx::WhereInSet(const char* set, char c) const
{
int setlen = strlen(set);
for(int i=0; i<setlen; i++)
if (c==set[i]) return i;
return -1;
}
char RegEx::ParseChar(const char* s, int &i) const
{
if(s[i] == '\'' && s[i+2] == '\'')
{
char c = s[i+1];
i+=3;
return c;
}
Exit("Error parsing character in regular expression\n");
return -1;
}
/*
int main()
{
//char expression[] = "$('0'-'9','a'-'f','A'-'F')('0'-'9','a'-'f','A'-'F')*";
char expression[] = "//(' '-'~','\r')*\n";
RegEx r;
r.Load(expression);
printf("%i\n",r.Matches("//hello123\nabcdefghijklmnop"));
return 0;
}
*/