Commit 95310b28 authored by Andre Hartmann's avatar Andre Hartmann Committed by André Hartmann

CamelHumpMatcher: Modernize

* Use QRegularExpression instead QRegExp
* Use range-for instead foreach
* Remove QLatin1String/Char

Change-Id: I27516a4e3ca724c1f3cfab5b729d545547df873f
Reviewed-by: Orgad Shaneh's avatarOrgad Shaneh <orgads@gmail.com>
parent eb5de6a8
......@@ -27,7 +27,7 @@
#include "camelhumpmatcher.h"
#include <QRegExp>
#include <QRegularExpression>
#include <QString>
/**
......@@ -39,11 +39,11 @@
* does not affect wildcard style matching
* \return the regexp
*/
QRegExp CamelHumpMatcher::createCamelHumpRegExp(const QString &pattern,
CamelHumpMatcher::CaseSensitivity caseSensitivity)
QRegularExpression CamelHumpMatcher::createCamelHumpRegExp(
const QString &pattern, CamelHumpMatcher::CaseSensitivity caseSensitivity)
{
if (pattern.isEmpty())
return QRegExp();
return QRegularExpression();
/*
* This code builds a regular expression in order to more intelligently match
......@@ -66,30 +66,30 @@ QRegExp CamelHumpMatcher::createCamelHumpRegExp(const QString &pattern,
QString keyRegExp;
bool first = true;
const QChar asterisk = QLatin1Char('*');
const QChar question = QLatin1Char('?');
const QChar asterisk = '*';
const QChar question = '?';
const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
foreach (const QChar &c, pattern) {
for (const QChar &c : pattern) {
if (!c.isLetter()) {
if (c == question)
keyRegExp += QLatin1Char('.');
keyRegExp += '.';
else if (c == asterisk)
keyRegExp += QLatin1String(".*");
keyRegExp += ".*";
else
keyRegExp += QRegExp::escape(c);
keyRegExp += QRegularExpression::escape(c);
} else if (caseSensitivity == CaseSensitivity::CaseInsensitive ||
(caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) {
keyRegExp += QLatin1String("(?:");
keyRegExp += "(?:";
if (!first)
keyRegExp += uppercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toUpper());
keyRegExp += QLatin1Char('|');
keyRegExp += QRegularExpression::escape(c.toUpper());
keyRegExp += '|';
if (!first)
keyRegExp += lowercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toLower());
keyRegExp += QLatin1Char(')');
keyRegExp += QRegularExpression::escape(c.toLower());
keyRegExp += ')';
} else {
if (!first) {
if (c.isUpper())
......@@ -97,10 +97,10 @@ QRegExp CamelHumpMatcher::createCamelHumpRegExp(const QString &pattern,
else
keyRegExp += lowercaseWordContinuation;
}
keyRegExp += QRegExp::escape(c);
keyRegExp += QRegularExpression::escape(c);
}
first = false;
}
return QRegExp(keyRegExp);
return QRegularExpression(keyRegExp);
}
......@@ -30,7 +30,7 @@
#include "utils_global.h"
QT_BEGIN_NAMESPACE
class QRegExp;
class QRegularExpression;
class QString;
QT_END_NAMESPACE
......@@ -43,6 +43,6 @@ public:
FirstLetterCaseSensitive
};
static QRegExp createCamelHumpRegExp(const QString &pattern,
static QRegularExpression createCamelHumpRegExp(const QString &pattern,
CaseSensitivity caseSensitivity = CaseSensitivity::CaseInsensitive);
};
......@@ -30,7 +30,7 @@
#include <texteditor/completionsettings.h>
#include <QDebug>
#include <QRegExp>
#include <QRegularExpression>
#include <QtAlgorithms>
#include <QHash>
......@@ -257,13 +257,13 @@ void GenericProposalModel::filter(const QString &prefix)
const CamelHumpMatcher::CaseSensitivity caseSensitivity =
convertCaseSensitivity(TextEditorSettings::completionSettings().m_caseSensitivity);
const QRegExp regExp = CamelHumpMatcher::createCamelHumpRegExp(prefix, caseSensitivity);
const QRegularExpression regExp = CamelHumpMatcher::createCamelHumpRegExp(prefix, caseSensitivity);
m_currentItems.clear();
const QString lowerPrefix = prefix.toLower();
foreach (const auto &item, m_originalItems) {
const QString &text = item->text();
if (regExp.indexIn(text) == 0) {
if (regExp.match(text).hasMatch()) {
m_currentItems.append(item);
if (text.startsWith(prefix)) {
// Direct match
......
......@@ -43,7 +43,9 @@ void tst_CamelHumpMatcher::camelHumpMatcher()
QFETCH(QString, candidate);
QFETCH(int, expectedIndex);
QCOMPARE(CamelHumpMatcher::createCamelHumpRegExp(pattern).indexIn(candidate), expectedIndex);
const QRegularExpression regExp = CamelHumpMatcher::createCamelHumpRegExp(pattern);
const QRegularExpressionMatch match = regExp.match(candidate);
QCOMPARE(match.capturedStart(), expectedIndex);
}
void tst_CamelHumpMatcher::camelHumpMatcher_data()
......
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