regexp.cpp Example File
regexp/src/regexp.cpp
#include "regexp.hpp"
static const int s_capturesCount = 6;
RegExp::RegExp(QObject *parent)
: QObject(parent)
, m_pattern("[A-Za-z_]+([A-Za-z_0-9]*)")
, m_patternSyntax(QRegExp::RegExp)
, m_caseSensitive(true)
, m_minimal(false)
, m_text("(10 + delta4) * 32")
, m_matchIndex(-1)
, m_matchLength(-1)
{
for (int i = 0; i < s_capturesCount; ++i) {
Capture capture;
capture.available = false;
m_captures << capture;
}
refresh();
}
void RegExp::setPattern(const QString &pattern)
{
if (m_pattern == pattern)
return;
m_pattern = pattern;
refresh();
emit patternChanged();
}
QString RegExp::pattern() const
{
return m_pattern;
}
void RegExp::setPatternSyntax(QRegExp::PatternSyntax syntax)
{
if (m_patternSyntax == syntax)
return;
m_patternSyntax = syntax;
refresh();
}
QString RegExp::escapedPattern() const
{
return m_escapedPattern;
}
void RegExp::setCaseSensitive(bool caseSensitive)
{
if (m_caseSensitive == caseSensitive)
return;
m_caseSensitive = caseSensitive;
refresh();
emit caseSensitiveChanged();
}
bool RegExp::caseSensitive() const
{
return m_caseSensitive;
}
void RegExp::setMinimal(bool minimal)
{
if (m_minimal == minimal)
return;
m_minimal = minimal;
refresh();
emit minimalChanged();
}
bool RegExp::minimal() const
{
return m_minimal;
}
void RegExp::setText(const QString &text)
{
if (m_text == text)
return;
m_text = text;
refresh();
emit textChanged();
}
QString RegExp::text() const
{
return m_text;
}
int RegExp::matchIndex() const
{
return m_matchIndex;
}
int RegExp::matchLength() const
{
return m_matchLength;
}
bool RegExp::capture0Available() const
{
return m_captures.at(0).available;
}
QString RegExp::capture0Text() const
{
return m_captures.at(0).text;
}
bool RegExp::capture1Available() const
{
return m_captures.at(1).available;
}
QString RegExp::capture1Text() const
{
return m_captures.at(1).text;
}
bool RegExp::capture2Available() const
{
return m_captures.at(2).available;
}
QString RegExp::capture2Text() const
{
return m_captures.at(2).text;
}
bool RegExp::capture3Available() const
{
return m_captures.at(3).available;
}
QString RegExp::capture3Text() const
{
return m_captures.at(3).text;
}
bool RegExp::capture4Available() const
{
return m_captures.at(4).available;
}
QString RegExp::capture4Text() const
{
return m_captures.at(4).text;
}
bool RegExp::capture5Available() const
{
return m_captures.at(5).available;
}
QString RegExp::capture5Text() const
{
return m_captures.at(5).text;
}
void RegExp::refresh()
{
QString escaped = m_pattern;
escaped.replace("\\", "\\\\");
escaped.replace("\"", "\\\"");
escaped.prepend("\"");
escaped.append("\"");
m_escapedPattern = escaped;
QRegExp regexp(m_pattern);
regexp.setCaseSensitivity(m_caseSensitive ? Qt::CaseSensitive : Qt::CaseInsensitive);
regexp.setMinimal(m_minimal);
regexp.setPatternSyntax(m_patternSyntax);
m_matchIndex = regexp.indexIn(m_text);
m_matchLength = regexp.matchedLength();
for (int i = 0; i < s_capturesCount; ++i) {
m_captures[i].available = (i <= regexp.captureCount());
m_captures[i].text = regexp.cap(i);
}
emit regexpChanged();
}