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();
|
|
|
|
}
|
|
|
|
|
2021-08-04 13:16:15 +08:00
|
|
|
int SynSearchRegex::findAll(const QString &keyword)
|
2021-08-02 17:24:11 +08:00
|
|
|
{
|
|
|
|
if (pattern().isEmpty())
|
|
|
|
return 0;
|
|
|
|
mResults.clear();
|
|
|
|
mLengths.clear();
|
2021-08-04 13:16:15 +08:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|