/
QuantifiedRegexObject.cpp
126 lines (114 loc) · 2.57 KB
/
QuantifiedRegexObject.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
#include "QuantifiedRegexObject.h"
#include "Regex.h"
QuantifiedRegexObject::QuantifiedRegexObject()
{
type = QuantifierType::NONE;
greedy = true;
regexObject = NULL;
range = make_pair(-1, -1);
}
QuantifiedRegexObject::QuantifiedRegexObject(const QuantifiedRegexObject& rhs)
{
type = rhs.type;
greedy = rhs.greedy;
range = rhs.range;
if (rhs.regexObject != NULL)
regexObject = new RegexObject(*rhs.regexObject);
else regexObject = NULL;
}
void QuantifiedRegexObject::parse(Regex* rgx)
{
regexObject = new RegexObject;
regexObject->parse(rgx);
char c = rgx->src.peek();
if (c == '+' || c == '*' || c == '?')
{
rgx->src.get();
if (c == '+') type = QuantifierType::KLEENE_PLUS;
else if (c == '*') type = QuantifierType::KLEENE_STAR;
else type = QuantifierType::QUESTION_MARK;
if (rgx->src.peek() == '?')
{
greedy = false;
rgx->src.get();
}
}
else if (c == '{')
{
char x;
int minVal, maxVal;
minVal = Utility::parseInt(rgx);
if (rgx->src.get() != ',')
{
rgx->src.moveBack();
maxVal = minVal;
}
else
{
if (Utility::isDigit(rgx->src.peek()))
{
maxVal = Utility::parseInt(rgx);
}
else
{
maxVal = oo;
}
}
if (rgx->src.get() != '}') throw ParseError("");
if (maxVal < minVal) swap(minVal, maxVal);
range = make_pair(minVal, maxVal);
type = QuantifierType::RANGED;
}
}
QuantifiedRegexObject::~QuantifiedRegexObject()
{
if (regexObject) delete regexObject;
}
QuantifiedRegexObject& QuantifiedRegexObject::operator=(const QuantifiedRegexObject& rhs)
{
if (this != &rhs)
{
if (regexObject) delete regexObject;
type = rhs.type;
greedy = rhs.greedy;
range = rhs.range;
if (rhs.regexObject != NULL)
regexObject = new RegexObject(*rhs.regexObject);
else regexObject = NULL;
}
return *this;
}
bool QuantifiedRegexObject::matches(Regex* rgx)
{
try{
if (type == QuantifierType::NONE)
{
return regexObject->matches(rgx);
}
else if (type == QuantifierType::KLEENE_PLUS)
{
if (!regexObject->matches(rgx)) return false;
while (!rgx->matchString.isDone() && regexObject->matches(rgx));
return true;
}
else if (type == QuantifierType::KLEENE_STAR)
{
while (!rgx->matchString.isDone() && regexObject->matches(rgx));
return true;
}
else if (type == QuantifierType::QUESTION_MARK)
{
regexObject->matches(rgx);
return true;
}
else if (type == QuantifierType::RANGED)
{
int x = 0;
while (!rgx->matchString.isDone() && regexObject->matches(rgx)) x++;
if (x > range.second || x < range.first) return false;
else return true;
}
}
catch (...){}
return true;
}