forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Match.h
85 lines (72 loc) · 1.94 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
#ifndef Match_h
#define Match_h
#include "String.h"
#include "RegExp.h"
#include "Log.h"
class Match
{
public:
enum Flag {
Flag_None = 0x0,
Flag_StringMatch = 0x1,
Flag_RegExp = 0x2,
Flag_CaseInsensitive = 0x4
};
inline Match(const String &pattern = String(), unsigned flags = Flag_StringMatch)
: mFlags(flags)
{
if (flags & Flag_RegExp)
mRegExp = pattern;
mPattern = pattern;
}
unsigned 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 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