2016-09-27 22:01:08 +03:00
|
|
|
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
|
|
|
|
file Copyright.txt or https://cmake.org/licensing for details. */
|
2015-11-19 00:40:59 +03:00
|
|
|
#include "RegexExplorer.h"
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
RegexExplorer::RegexExplorer(QWidget* p)
|
|
|
|
: QDialog(p)
|
|
|
|
, m_matched(false)
|
2015-11-19 00:40:59 +03:00
|
|
|
{
|
|
|
|
this->setupUi(this);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (int i = 1; i < cmsys::RegularExpression::NSUBEXP; ++i) {
|
|
|
|
matchNumber->addItem(QString("Match %1").arg(QString::number(i)),
|
|
|
|
QVariant(i));
|
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
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::Foreground, 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);
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!validExpression) {
|
2015-11-19 00:40:59 +03:00
|
|
|
m_regexParser.set_invalid();
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
|
|
|
setStatusColor(labelRegexValid, validExpression);
|
|
|
|
|
|
|
|
on_inputText_textChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegexExplorer::on_inputText_textChanged()
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (m_regexParser.is_valid()) {
|
2015-11-19 00:40:59 +03:00
|
|
|
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);
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-11-19 00:40:59 +03:00
|
|
|
m_matched = false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
|
|
|
setStatusColor(labelRegexMatch, m_matched);
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!m_matched) {
|
2015-11-19 00:40:59 +03:00
|
|
|
clearMatch();
|
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
|
|
|
#ifdef QT_NO_STL
|
|
|
|
QString matchText = m_regexParser.match(0).c_str();
|
|
|
|
#else
|
|
|
|
QString matchText = QString::fromStdString(m_regexParser.match(0));
|
|
|
|
#endif
|
|
|
|
match0->setPlainText(matchText);
|
|
|
|
|
|
|
|
on_matchNumber_currentIndexChanged(matchNumber->currentIndex());
|
|
|
|
}
|
|
|
|
|
|
|
|
void RegexExplorer::on_matchNumber_currentIndexChanged(int index)
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!m_matched) {
|
2015-11-19 00:40:59 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
|
|
|
QVariant itemData = matchNumber->itemData(index);
|
|
|
|
int idx = itemData.toInt();
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
if (idx < 1 || idx >= cmsys::RegularExpression::NSUBEXP) {
|
2015-11-19 00:40:59 +03:00
|
|
|
return;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
|
|
|
#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::clearMatch()
|
|
|
|
{
|
|
|
|
match0->clear();
|
|
|
|
matchN->clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RegexExplorer::stripEscapes(std::string& source)
|
|
|
|
{
|
|
|
|
const char* in = source.c_str();
|
|
|
|
|
|
|
|
std::string result;
|
|
|
|
result.reserve(source.size());
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
for (char inc = *in; inc != '\0'; inc = *++in) {
|
|
|
|
if (inc == '\\') {
|
2015-11-19 00:40:59 +03:00
|
|
|
char nextc = in[1];
|
2016-05-16 17:34:04 +03:00
|
|
|
if (nextc == 't') {
|
2015-11-19 00:40:59 +03:00
|
|
|
result.append(1, '\t');
|
|
|
|
in++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (nextc == 'n') {
|
2015-11-19 00:40:59 +03:00
|
|
|
result.append(1, '\n');
|
|
|
|
in++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (nextc == 't') {
|
2015-11-19 00:40:59 +03:00
|
|
|
result.append(1, '\t');
|
|
|
|
in++;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else if (isalnum(nextc) || nextc == '\0') {
|
2015-11-19 00:40:59 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
2015-11-19 00:40:59 +03:00
|
|
|
result.append(1, nextc);
|
|
|
|
in++;
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
} else {
|
|
|
|
result.append(1, inc);
|
2015-11-19 00:40:59 +03:00
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-11-19 00:40:59 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
source = result;
|
|
|
|
return true;
|
2015-11-19 00:40:59 +03:00
|
|
|
}
|