Commit 7bd466ec authored by Kåre Särs's avatar Kåre Särs
Browse files

S&R: More good review tips by Christoph Cullmann

- Pass a regexp copy in stead of making the copy in the function
- Add ^ and $ to the regexp to ensure we always match the whole range
parent 897f854e
Pipeline #135612 passed with stage
in 2 minutes and 14 seconds
......@@ -260,30 +260,29 @@ void MatchModel::updateMatchRanges(const QVector<KTextEditor::MovingRange *> &ra
dataChanged(index(0, 0, rootFileIndex), index(matches.count() - 1, 0, rootFileIndex));
}
QRegularExpressionMatch MatchModel::rangeTextMatches(const QString &rangeText, const QRegularExpression &regExp)
QRegularExpressionMatch MatchModel::rangeTextMatches(const QString &rangeText, QRegularExpression regExp)
{
// special handling for lookahead and lookbehind
QRegularExpression tmpReg = regExp;
QString pattern = tmpReg.pattern();
QString pattern = regExp.pattern();
// NOTE: Negative look-ahead/behind are not a problem as they are not part of the range
static const QRegularExpression lookaheadRegex(QStringLiteral(".*(\\(\\?=[^\\)]+\\))"));
static const QRegularExpression lookbehindRegex(QStringLiteral("(\\(\\?<=[^\\)]+\\)).*"));
static const QRegularExpression lookaheadRegex(QStringLiteral("^.*(\\(\\?=[^\\)]+\\))$"));
static const QRegularExpression lookbehindRegex(QStringLiteral("^(\\(\\?<=[^\\)]+\\)).*$"));
// Remove possible lookahead as we do not have the tail to compare with
auto lookMatch = lookaheadRegex.match(pattern);
if (lookMatch.hasMatch()) {
pattern.remove(lookMatch.capturedStart(1), lookMatch.capturedLength(1));
tmpReg.setPattern(pattern);
regExp.setPattern(pattern);
}
// Remove possible lookbehind as we do not have the prefix
lookMatch = lookbehindRegex.match(pattern);
if (lookMatch.hasMatch()) {
pattern.remove(lookMatch.capturedStart(1), lookMatch.capturedLength(1));
tmpReg.setPattern(pattern);
regExp.setPattern(pattern);
}
return tmpReg.match(rangeText);
return regExp.match(rangeText);
}
/** This function is used to replace a match */
......
......@@ -191,7 +191,7 @@ public:
* @param rangeText is the QString for the range.
* @param regExp is the regular-expression to check.
* @return the match object that has captures on success or none on failure to match the previously found range.*/
static QRegularExpressionMatch rangeTextMatches(const QString &rangeText, const QRegularExpression &regExp);
static QRegularExpressionMatch rangeTextMatches(const QString &rangeText, QRegularExpression regExp);
private Q_SLOTS:
void doReplaceNextMatch();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment