summaryrefslogtreecommitdiff
path: root/plugins/SmileyAdd/src/regexp/WCMatcher.cpp
blob: 939e347afd81478414454dd7010b702f1e4d603a (plain)
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
#include "../stdafx.h"

const int WCMatcher::MATCH_ENTIRE_STRING = 0x01;

/*
  Detailed documentation is provided in this class' header file

  @author   Jeffery Stuart
  @since    November 2004
  @version  1.07.00
  */

WCMatcher::WCMatcher(WCPattern *pattern, const CMString &text)
{
	pat = pattern;
	str = &text;
	gc = pattern->groupCount;
	ncgc = -pattern->nonCapGroupCount;
	flags = 0;
	matchedSomething = false;
	starts = new int[gc + ncgc];
	ends = new int[gc + ncgc];
	groups = new int[gc + ncgc];
	groupPos = new int[gc + ncgc];
	groupIndeces = new int[gc + ncgc];
	starts = starts + ncgc;
	ends = ends + ncgc;
	groups = groups + ncgc;
	groupPos = groupPos + ncgc;
	groupIndeces = groupIndeces + ncgc;
	for (int i = 0; i < gc; ++i) starts[i] = ends[i] = 0;
}

WCMatcher::~WCMatcher()
{
	delete[](starts - ncgc);
	delete[](ends - ncgc);
	delete[](groups - ncgc);
	delete[](groupIndeces - ncgc);
	delete[](groupPos - ncgc);
}

void WCMatcher::clearGroups()
{
	int i;
	lm = 0;
	for (i = 0; i < gc; ++i)    groups[i] = starts[i] = ends[i] = -1;
	for (i = 1; i <= ncgc; ++i) groups[0 - i] = starts[0 - i] = ends[0 - i] = -1;
}

CMString WCMatcher::replaceWithGroups(const CMString &pStr)
{
	CMString ret;

	CMString t = pStr;
	while (t.GetLength() > 0) {
		if (t[0] == '\\') {
			t.Delete(0);
			if (t.GetLength() == 0)
				ret += L"\\";
			else if (t[0] < '0' || t[0] > '9') {
				ret += t[0];
				t.Delete(0);
			}
			else {
				int gn = 0;
				while (t.GetLength() > 0 && t[0] >= '0' && t[0] <= '9')
				{
					gn = gn * 10 + (t[0] - '0');
					t.Delete(0);
				}
				ret += getGroup(gn);
			}
		}
		else {
			ret += t[0];
			t.Delete(0);
		}
	}

	return ret;
}

unsigned long WCMatcher::getFlags() const
{
	return flags;
}

const CMString& WCMatcher::getText() const
{
	return *str;
}

bool WCMatcher::matches()
{
	flags = MATCH_ENTIRE_STRING;
	matchedSomething = false;
	clearGroups();
	lm = 0;
	return pat->head->match(*str, this, 0) == str->GetLength();
}

bool WCMatcher::findFirstMatch()
{
	starts[0] = 0;
	flags = 0;
	clearGroups();
	start = 0;
	lm = 0;
	ends[0] = pat->head->match(*str, this, 0);
	if (ends[0] >= 0) {
		matchedSomething = true;
		return 1;
	}
	return 0;
}

bool WCMatcher::findNextMatch()
{
	int s = starts[0], e = ends[0];

	if (!matchedSomething) return findFirstMatch();
	if (s == e) ++e;
	flags = 0;
	clearGroups();

	starts[0] = e;
	if (e >= str->GetLength()) return 0;
	start = e;
	lm = e;
	ends[0] = pat->head->match(*str, this, e);
	return ends[0] >= 0;
}

std::vector<CMString> WCMatcher::findAll()
{
	std::vector<CMString> ret;
	reset();
	while (findNextMatch())
		ret.push_back(getGroup());

	return ret;
}

void WCMatcher::reset()
{
	lm = 0;
	clearGroups();
	matchedSomething = false;
}

int WCMatcher::getStartingIndex(const int groupNum) const
{
	if (groupNum < 0 || groupNum >= gc)
		return -1;
	return starts[groupNum];
}

int WCMatcher::getEndingIndex(const int groupNum) const
{
	if (groupNum < 0 || groupNum >= gc)
		return -1;
	return ends[groupNum];
}

CMString WCMatcher::getGroup(const int groupNum) const
{
	if (groupNum < 0 || groupNum >= gc) return L"";
	if (starts[groupNum] < 0 || ends[groupNum] < 0) return L"";
	return str->Mid(starts[groupNum], ends[groupNum] - starts[groupNum]);
}

std::vector<CMString> WCMatcher::getGroups(const bool includeGroupZero) const
{
	std::vector<CMString> ret;

	for (int i = (includeGroupZero ? 0 : 1); i < gc; ++i)
		ret.push_back(getGroup(i));

	return ret;
}