regex.cpp 4.64 KB
Newer Older
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
#include "crt.h"
#include "regex.h"
#include "stream.h"
#include <iostream>

namespace YAML
{
	RegEx::RegEx(REGEX_OP op): m_op(op), m_pOp(0)
	{
		SetOp();
	}

	RegEx::RegEx(const RegEx& rhs): m_pOp(0)
	{
		m_op = rhs.m_op;
		m_a = rhs.m_a;
		m_z = rhs.m_z;
		m_params = rhs.m_params;

		SetOp();
	}

	RegEx::RegEx(): m_op(REGEX_EMPTY), m_pOp(0)
	{
		SetOp();
	}

	RegEx::RegEx(char ch): m_op(REGEX_MATCH), m_pOp(0), m_a(ch)
	{
		SetOp();
	}

	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)
	{
		for(unsigned i=0;i<str.size();i++)
			m_params.push_back(RegEx(str[i]));

		SetOp();
	}

	RegEx::~RegEx()
	{
		delete m_pOp;
	}

	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;
	}

	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;
			case REGEX_AND: m_pOp = new AndOperator; break;
			case REGEX_NOT: m_pOp = new NotOperator; break;
			case REGEX_SEQ: m_pOp = new SeqOperator; break;
Jesse Beder's avatar
Jesse Beder committed
77
			default: break;
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
		}
	}

	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;
	}

93
	bool RegEx::Matches(const Buffer& buffer) const
94
	{
95
		return Match(buffer) >= 0;
96
97
	}
	
98
	bool RegEx::Matches(const Stream& in) const
99
100
101
102
103
104
105
106
107
108
	{
		return Match(in) >= 0;
	}

	// 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
	//   which is ALWAYS successful at matching zero characters).
109
110
	// . REMEMBER that we only match from the start of the buffer!
	int RegEx::Match(const Buffer& buffer) const
111
112
	{
		if(!m_pOp)
113
			return !buffer ? 0 : -1;  // the empty regex only is successful on the empty string
114

115
		return m_pOp->Match(buffer, *this);
116
117
	}

118
	int RegEx::Match(const std::string& str) const
119
	{
120
121
		Buffer buffer(str.c_str(), str.size());
		return Match(buffer);
122
	}
123

124
	// Match
125
	int RegEx::Match(const Stream& in) const
126
	{
127
		return Match(in.current());
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
	}

	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;
	}

	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;
	}

	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;
	}

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

	// MatchOperator
165
	int RegEx::MatchOperator::Match(const Buffer& buffer, const RegEx& regex) const
166
	{
167
		if(!buffer || buffer[0] != regex.m_a)
168
169
170
171
172
			return -1;
		return 1;
	}

	// RangeOperator
173
	int RegEx::RangeOperator::Match(const Buffer& buffer, const RegEx& regex) const
174
	{
175
		if(!buffer || regex.m_a > buffer[0] || regex.m_z < buffer[0])
176
177
178
179
180
			return -1;
		return 1;
	}

	// OrOperator
181
	int RegEx::OrOperator::Match(const Buffer& buffer, const RegEx& regex) const
182
183
	{
		for(unsigned i=0;i<regex.m_params.size();i++) {
184
			int n = regex.m_params[i].Match(buffer);
185
186
187
188
189
190
191
192
193
194
			if(n >= 0)
				return n;
		}
		return -1;
	}

	// 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.
195
	int RegEx::AndOperator::Match(const Buffer& buffer, const RegEx& regex) const
196
197
198
	{
		int first = -1;
		for(unsigned i=0;i<regex.m_params.size();i++) {
199
			int n = regex.m_params[i].Match(buffer);
200
201
202
203
204
205
206
207
208
			if(n == -1)
				return -1;
			if(i == 0)
				first = n;
		}
		return first;
	}

	// NotOperator
209
	int RegEx::NotOperator::Match(const Buffer& buffer, const RegEx& regex) const
210
211
212
	{
		if(regex.m_params.empty())
			return -1;
213
		if(regex.m_params[0].Match(buffer) >= 0)
214
215
216
217
218
			return -1;
		return 1;
	}

	// SeqOperator
219
	int RegEx::SeqOperator::Match(const Buffer& buffer, const RegEx& regex) const
220
221
222
	{
		int offset = 0;
		for(unsigned i=0;i<regex.m_params.size();i++) {
223
			int n = regex.m_params[i].Match(buffer + offset);
224
225
226
227
228
229
230
231
232
233
			if(n == -1)
				return -1;

			offset += n;
		}

		return offset;
	}
}