forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatch.h
104 lines (86 loc) · 2.61 KB
/
Match.h
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
/* This file is part of RTags.
RTags is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
RTags is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with RTags. If not, see <http://www.gnu.org/licenses/>. */
#ifndef Match_h
#define Match_h
#include <rct/String.h>
#include <rct/RegExp.h>
#include <rct/Log.h>
class Match
{
public:
enum Flag {
Flag_None = 0x0,
Flag_StringMatch = 0x1,
Flag_RegExp = 0x2,
Flag_CaseInsensitive = 0x4
};
inline Match()
: mFlags(0)
{}
inline Match(const String &pattern, unsigned int flags = Flag_StringMatch)
: mFlags(flags)
{
if (flags & Flag_RegExp)
mRegExp = pattern;
mPattern = pattern;
}
unsigned int flags() const { return mFlags; }
inline Match(const RegExp ®Exp)
: mRegExp(regExp), mPattern(regExp.pattern()), mFlags(Flag_RegExp)
{}
inline bool match(const String &text) const
{
if (indexIn(text) != -1)
return true;
if (mFlags & Flag_StringMatch)
return mPattern.indexOf(text, 0, mFlags & Flag_CaseInsensitive ? String::CaseInsensitive : String::CaseSensitive) != -1;
return false;
}
inline int indexIn(const String &text) const
{
int index = -1;
if (mFlags & Flag_StringMatch)
index = text.indexOf(mPattern, 0, mFlags & Flag_CaseInsensitive ? String::CaseInsensitive : String::CaseSensitive);
if (index == -1 && mFlags & Flag_RegExp)
index = mRegExp.indexIn(text);
return index;
}
inline bool isEmpty() const
{
return !mFlags || mPattern.isEmpty();
}
inline RegExp regExp() const
{
return mRegExp;
}
inline String pattern() const
{
return mPattern;
}
private:
RegExp mRegExp;
String mPattern;
unsigned int mFlags;
};
inline Log operator<<(Log log, const Match &match)
{
String ret = "Match(flags: ";
ret += String::number(match.flags(), 16);
if (match.regExp().isValid())
ret += " rx: " + match.regExp().pattern();
if (!match.pattern().isEmpty())
ret += " pattern: " + match.pattern();
ret += ")";
log << ret;
return log;
}
#endif