RedPanda-CPP/RedPandaIDE/qsynedit/SearchRegex.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2021-08-02 17:24:11 +08:00
#include "SearchRegex.h"
#include <QRegExp>
SynSearchRegex::SynSearchRegex(QObject* parent):SynSearchBase(parent)
{
}
int SynSearchRegex::length(int aIndex)
{
if (aIndex<0 || aIndex >= mResults.length())
return -1;
return mLengths[aIndex];
}
int SynSearchRegex::result(int aIndex)
{
if (aIndex<0 || aIndex >= mResults.length())
return -1;
return mResults[aIndex];
}
int SynSearchRegex::resultCount()
{
return mResults.size();
}
int SynSearchRegex::findAll(const QString &keyword)
2021-08-02 17:24:11 +08:00
{
if (pattern().isEmpty())
return 0;
mResults.clear();
mLengths.clear();
QRegularExpressionMatchIterator it = mRegex.globalMatch(keyword);
2021-08-02 17:24:11 +08:00
while (it.hasNext()) {
QRegularExpressionMatch match = it.next();
mLengths.append(match.capturedLength());
mResults.append(match.capturedStart());
}
return mResults.size();
}
QString SynSearchRegex::replace(const QString &aOccurrence, const QString &aReplacement)
{
QString s=aOccurrence;
return s.replace(mRegex,aReplacement);
}
void SynSearchRegex::setPattern(const QString &value)
{
SynSearchBase::setPattern(value);
mRegex.setPattern(value);
updateRegexOptions();
}
void SynSearchRegex::setOptions(const SynSearchOptions &options)
{
SynSearchBase::setOptions(options);
updateRegexOptions();
}
void SynSearchRegex::updateRegexOptions()
{
if (options().testFlag(SynSearchOption::ssoMatchCase)) {
mRegex.setPatternOptions(
mRegex.patternOptions() &
~QRegularExpression::CaseInsensitiveOption);
} else {
mRegex.setPatternOptions(
mRegex.patternOptions() |
QRegularExpression::CaseInsensitiveOption);
}
}