regex.cpp 6.38 KB
Newer Older
1
#include "crt.h"
2
3
4
#include "regex.h"
#include "stream.h"
#include <iostream>
5
6
7

namespace YAML
{
8
	RegEx::RegEx(REGEX_OP op): m_op(op), m_pOp(0)
9
	{
10
		SetOp();
11
12
	}

13
	RegEx::RegEx(const RegEx& rhs): m_pOp(0)
14
	{
15
16
17
18
19
20
		m_op = rhs.m_op;
		m_a = rhs.m_a;
		m_z = rhs.m_z;
		m_params = rhs.m_params;

		SetOp();
21
22
	}

23
	RegEx::RegEx(): m_op(REGEX_EMPTY), m_pOp(0)
24
	{
25
		SetOp();
26
27
	}

28
	RegEx::RegEx(char ch): m_op(REGEX_MATCH), m_pOp(0), m_a(ch)
29
	{
30
		SetOp();
31
32
	}

33
34
35
36
37
38
	RegEx::RegEx(char a, char z): m_op(REGEX_RANGE), m_pOp(0), m_a(a), m_z(z)
	{
		SetOp();
	}

	RegEx::RegEx(const std::string& str, REGEX_OP op): m_op(op), m_pOp(0)
39
40
	{
		for(unsigned i=0;i<str.size();i++)
41
			m_params.push_back(RegEx(str[i]));
42
43

		SetOp();
44
45
46
47
	}

	RegEx::~RegEx()
	{
48
49
50
		delete m_pOp;
	}

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
	RegEx& RegEx::operator = (const RegEx& rhs)
	{
		delete m_pOp;
		m_pOp = 0;

		m_op = rhs.m_op;
		m_a = rhs.m_a;
		m_z = rhs.m_z;
		m_params = rhs.m_params;

		SetOp();

		return *this;
	}

66
67
68
69
70
71
72
73
	void RegEx::SetOp()
	{
		delete m_pOp;
		m_pOp = 0;
		switch(m_op) {
			case REGEX_MATCH: m_pOp = new MatchOperator; break;
			case REGEX_RANGE: m_pOp = new RangeOperator; break;
			case REGEX_OR: m_pOp = new OrOperator; break;
74
			case REGEX_AND: m_pOp = new AndOperator; break;
75
76
77
			case REGEX_NOT: m_pOp = new NotOperator; break;
			case REGEX_SEQ: m_pOp = new SeqOperator; break;
		}
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	}

	bool RegEx::Matches(char ch) const
	{
		std::string str;
		str += ch;
		return Matches(str);
	}

	bool RegEx::Matches(const std::string& str) const
	{
		return Match(str) >= 0;
	}

92
	bool RegEx::Matches(std::istream& in) const
93
94
95
96
97
	{
		return Match(in) >= 0;
	}
	
	bool RegEx::Matches(Stream& in) const
98
99
100
101
	{
		return Match(in) >= 0;
	}

102
103
104
105
106
	// Match
	// . Matches the given string against this regular expression.
	// . Returns the number of characters matched.
	// . Returns -1 if no characters were matched (the reason for
	//   not returning zero is that we may have an empty regex
107
	//   which is ALWAYS successful at matching zero characters).
108
109
	int RegEx::Match(const std::string& str) const
	{
110
		if(!m_pOp)
111
			return 0;
112

113
114
115
		return m_pOp->Match(str, *this);
	}

116
117
118
119
120
121
	// Match
	int RegEx::Match(Stream& in) const
	{
		return Match(in.stream());
	}
	
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	// Match
	// . The stream version does the same thing as the string version;
	//   REMEMBER that we only match from the start of the stream!
	// . Note: the istream is not a const reference, but we guarantee
	//   that the pointer will be in the same spot, and we'll clear its
	//   flags before we end.
	int RegEx::Match(std::istream& in) const
	{
		if(!m_pOp)
			return -1;

		int pos = in.tellg();
		int ret = m_pOp->Match(in, *this);

		// reset input stream!
		in.clear();
		in.seekg(pos);

		return ret;
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	}

	RegEx operator ! (const RegEx& ex)
	{
		RegEx ret(REGEX_NOT);
		ret.m_params.push_back(ex);
		return ret;
	}

	RegEx operator || (const RegEx& ex1, const RegEx& ex2)
	{
		RegEx ret(REGEX_OR);
		ret.m_params.push_back(ex1);
		ret.m_params.push_back(ex2);
		return ret;
	}

158
159
160
161
162
163
164
165
	RegEx operator && (const RegEx& ex1, const RegEx& ex2)
	{
		RegEx ret(REGEX_AND);
		ret.m_params.push_back(ex1);
		ret.m_params.push_back(ex2);
		return ret;
	}

166
167
168
169
170
171
172
	RegEx operator + (const RegEx& ex1, const RegEx& ex2)
	{
		RegEx ret(REGEX_SEQ);
		ret.m_params.push_back(ex1);
		ret.m_params.push_back(ex2);
		return ret;
	}
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

	//////////////////////////////////////////////////////////////////////////////
	// Operators

	// MatchOperator
	int RegEx::MatchOperator::Match(const std::string& str, const RegEx& regex) const
	{
		if(str.empty() || str[0] != regex.m_a)
			return -1;
		return 1;
	}

	
	int RegEx::MatchOperator::Match(std::istream& in, const RegEx& regex) const
	{
		if(!in || in.peek() != regex.m_a)
			return -1;
		return 1;
	}

	// RangeOperator
	int RegEx::RangeOperator::Match(const std::string& str, const RegEx& regex) const
	{
		if(str.empty() || regex.m_a > str[0] || regex.m_z < str[0])
			return -1;
		return 1;
	}

	int RegEx::RangeOperator::Match(std::istream& in, const RegEx& regex) const
	{
		if(!in || regex.m_a > in.peek() || regex.m_z < in.peek())
			return -1;
		return 1;
	}

	// OrOperator
	int RegEx::OrOperator::Match(const std::string& str, const RegEx& regex) const
	{
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(str);
			if(n >= 0)
				return n;
		}
		return -1;
	}

	int RegEx::OrOperator::Match(std::istream& in, const RegEx& regex) const
	{
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(in);
			if(n >= 0)
				return n;
		}
		return -1;
	}

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
	// AndOperator
	// Note: 'AND' is a little funny, since we may be required to match things
	//       of different lengths. If we find a match, we return the length of
	//       the FIRST entry on the list.
	int RegEx::AndOperator::Match(const std::string& str, const RegEx& regex) const
	{
		int first = -1;
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(str);
			if(n == -1)
				return -1;
			if(i == 0)
				first = n;
		}
		return first;
	}

	int RegEx::AndOperator::Match(std::istream& in, const RegEx& regex) const
	{
		int first = -1;
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(in);
			if(n == -1)
				return -1;
			if(i == 0)
				first = n;
		}
		return first;
	}

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
	// NotOperator
	int RegEx::NotOperator::Match(const std::string& str, const RegEx& regex) const
	{
		if(regex.m_params.empty())
			return -1;
		if(regex.m_params[0].Match(str) >= 0)
			return -1;
		return 1;
	}

	int RegEx::NotOperator::Match(std::istream& in, const RegEx& regex) const
	{
		if(regex.m_params.empty())
			return -1;
		if(regex.m_params[0].Match(in) >= 0)
			return -1;
		return 1;
	}

	// SeqOperator
	int RegEx::SeqOperator::Match(const std::string& str, const RegEx& regex) const
	{
		int offset = 0;
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(str.substr(offset));
			if(n == -1)
				return -1;
			offset += n;
		}
		return offset;
	}
	
	int RegEx::SeqOperator::Match(std::istream& in, const RegEx& regex) const
	{
		int offset = 0;
		for(unsigned i=0;i<regex.m_params.size();i++) {
			int n = regex.m_params[i].Match(in);
			if(n == -1)
				return -1;

			offset += n;
300
			in.ignore(n);
301
302
303
304
		}

		return offset;
	}
305
}
306