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
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing for details. */
#include "RegexExplorer.h"
RegexExplorer::RegexExplorer(QWidget* p)
: QDialog(p)
, m_matched(false)
{
this->setupUi(this);
for (int i = 1; i < cmsys::RegularExpressionMatch::NSUBEXP; ++i) {
matchNumber->addItem(QString("Match %1").arg(QString::number(i)),
QVariant(i));
}
matchNumber->setCurrentIndex(0);
}
void RegexExplorer::setStatusColor(QWidget* widget, bool successful)
{
QColor color = successful ? QColor(0, 127, 0) : Qt::red;
QPalette palette = widget->palette();
palette.setColor(QPalette::WindowText, color);
widget->setPalette(palette);
}
void RegexExplorer::on_regularExpression_textChanged(const QString& text)
{
#ifdef QT_NO_STL
m_regex = text.toAscii().constData();
#else
m_regex = text.toStdString();
#endif
bool validExpression =
stripEscapes(m_regex) && m_regexParser.compile(m_regex);
if (!validExpression) {
m_regexParser.set_invalid();
}
setStatusColor(labelRegexValid, validExpression);
on_inputText_textChanged();
}
void RegexExplorer::on_inputText_textChanged()
{
if (m_regexParser.is_valid()) {
QString plainText = inputText->toPlainText();
#ifdef QT_NO_STL
m_text = plainText.toAscii().constData();
#else
m_text = plainText.toStdString();
#endif
m_matched = m_regexParser.find(m_text);
} else {
m_matched = false;
}
setStatusColor(labelRegexMatch, m_matched);
if (!m_matched) {
clearMatch();
return;
}
std::string matchingText;
if (matchAll->isChecked()) {
const char* p = m_text.c_str();
while (m_regexParser.find(p)) {
std::string::size_type l = m_regexParser.start();
std::string::size_type r = m_regexParser.end();
if (r - l == 0) {
// matched empty string
clearMatch();
return;
}
if (!matchingText.empty()) {
matchingText += ";";
}
matchingText += std::string(p + l, r - l);
p += r;
}
} else {
matchingText = m_regexParser.match(0);
}
#ifdef QT_NO_STL
QString matchText = matchingText.c_str();
#else
QString matchText = QString::fromStdString(matchingText);
#endif
match0->setPlainText(matchText);
on_matchNumber_currentIndexChanged(matchNumber->currentIndex());
}
void RegexExplorer::on_matchNumber_currentIndexChanged(int index)
{
if (!m_matched) {
return;
}
QVariant itemData = matchNumber->itemData(index);
int idx = itemData.toInt();
if (idx < 1 || idx >= cmsys::RegularExpressionMatch::NSUBEXP) {
return;
}
#ifdef QT_NO_STL
QString match = m_regexParser.match(idx).c_str();
#else
QString match = QString::fromStdString(m_regexParser.match(idx));
#endif
matchN->setPlainText(match);
}
void RegexExplorer::on_matchAll_toggled(bool checked)
{
Q_UNUSED(checked);
on_inputText_textChanged();
}
void RegexExplorer::clearMatch()
{
m_matched = false;
match0->clear();
matchN->clear();
}
bool RegexExplorer::stripEscapes(std::string& source)
{
const char* in = source.c_str();
std::string result;
result.reserve(source.size());
for (char inc = *in; inc != '\0'; inc = *++in) {
if (inc == '\\') {
char nextc = in[1];
if (nextc == 't') {
result.append(1, '\t');
in++;
} else if (nextc == 'n') {
result.append(1, '\n');
in++;
} else if (isalnum(nextc) || nextc == '\0') {
return false;
} else {
result.append(1, nextc);
in++;
}
} else {
result.append(1, inc);
}
}
source = result;
return true;
}
|