forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Match.h
126 lines (105 loc) · 3.25 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/* This file is part of RTags (http://rtags.net).
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 <regex>
#include "Location.h"
#include "rct/Flags.h"
#include "rct/Log.h"
#include "rct/Rct.h"
#include "rct/String.h"
class Match
{
public:
enum Flag {
Flag_None = 0x0,
Flag_StringMatch = 0x1,
Flag_Regex = 0x2,
Flag_CaseInsensitive = 0x4
};
inline Match()
{}
inline Match(const String &pattern, Flags<Flag> f = Flag_StringMatch);
inline Match(Match &&other) noexcept
: mRegex(std::move(other.mRegex)), mPattern(std::move(other.mPattern)), mFlags(other.mFlags)
{}
inline Match(const Match &other)
: mRegex(other.mRegex), mPattern(other.mPattern), mFlags(other.mFlags)
{}
Flags<Flag> flags() const { return mFlags; }
inline bool match(const String &text) const
{
if (indexIn(text) != String::npos)
return true;
if (mFlags & Flag_StringMatch)
return mPattern.indexOf(text, 0, (mFlags & Flag_CaseInsensitive
? String::CaseInsensitive
: String::CaseSensitive)) != String::npos;
return false;
}
inline size_t indexIn(const String &text) const
{
size_t index = String::npos;
if (mFlags & Flag_StringMatch)
index = text.indexOf(mPattern, 0, mFlags & Flag_CaseInsensitive ? String::CaseInsensitive : String::CaseSensitive);
if (index == String::npos && mFlags & Flag_Regex) {
index = Rct::indexIn(text, mRegex);
}
return index;
}
inline bool isEmpty() const
{
return !mFlags || mPattern.isEmpty();
}
inline std::regex regex() const
{
return mRegex;
}
inline String pattern() const
{
return mPattern;
}
uint32_t fileId() const
{
return Location::fileId(mPattern);
}
private:
std::regex mRegex;
String mPattern;
Flags<Flag> mFlags;
};
RCT_FLAGS(Match::Flag);
inline Log operator<<(Log log, const Match &match)
{
String ret = "Match(flags: ";
ret += String::number(match.flags().cast<unsigned int>(), 16);
if (match.flags() & Match::Flag_Regex)
ret += " rx";
if (!match.pattern().isEmpty())
ret += " pattern: " + match.pattern();
ret += ")";
log << ret;
return log;
}
inline Match::Match(const String &pattern, Flags<Flag> f)
: mFlags(f)
{
if (mFlags & Flag_Regex) {
try {
mRegex = pattern.ref();
} catch (const std::regex_error &err) {
mFlags &= ~Flag_Regex;
}
}
mPattern = pattern;
}
#endif