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