FuzzyMatcher: Improve wildcard matching

Make it equivalent to current wildcard implementation, and replace the
current one with FuzzyMatcher.

Change-Id: If83a4a8dfc7c8930bd92b49972647a519886a831
Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
Orgad Shaneh
2017-10-30 21:48:36 +02:00
committed by Orgad Shaneh
parent 357caf3b44
commit 4300041d24
4 changed files with 5 additions and 35 deletions

View File

@@ -78,10 +78,10 @@ QRegularExpression FuzzyMatcher::createRegExp(
if (!c.isLetter()) { if (!c.isLetter()) {
if (c == question) { if (c == question) {
keyRegExp += '.'; keyRegExp += '.';
plainRegExp += '.'; plainRegExp += ").(";
} else if (c == asterisk) { } else if (c == asterisk) {
keyRegExp += ".*"; keyRegExp += ".*";
plainRegExp += ".*"; plainRegExp += ").*(";
} else { } else {
const QString escaped = QRegularExpression::escape(c); const QString escaped = QRegularExpression::escape(c);
keyRegExp += '(' + escaped + ')'; keyRegExp += '(' + escaped + ')';

View File

@@ -194,38 +194,9 @@ Qt::CaseSensitivity ILocatorFilter::caseSensitivity(const QString &str)
return str == str.toLower() ? Qt::CaseInsensitive : Qt::CaseSensitive; return str == str.toLower() ? Qt::CaseInsensitive : Qt::CaseSensitive;
} }
/*!
Returns whether the search term \a str contains wildcard characters.
Can be used for choosing an optimal matching strategy.
*/
bool ILocatorFilter::containsWildcard(const QString &str)
{
return str.contains(QLatin1Char('*')) || str.contains(QLatin1Char('?'));
}
/*!
* \brief Returns a simple regular expression to search for \a text.
*
* \a text may contain the simple '?' and '*' wildcards known from the shell.
* '?' matches exactly one character, '*' matches a number of characters
* (including none).
*
* The regular expression contains capture groups to allow highlighting
* matched characters after a match.
*/
static QRegularExpression createWildcardRegExp(const QString &text)
{
QString pattern = '(' + text + ')';
pattern.replace('?', ").(");
pattern.replace('*', ").*(");
pattern.remove("()");
return QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption);
}
QRegularExpression ILocatorFilter::createRegExp(const QString &text) QRegularExpression ILocatorFilter::createRegExp(const QString &text)
{ {
return containsWildcard(text) ? createWildcardRegExp(text) return FuzzyMatcher::createRegExp(text);
: FuzzyMatcher::createRegExp(text);
} }
LocatorFilterEntry::HighlightInfo ILocatorFilter::highlightInfo( LocatorFilterEntry::HighlightInfo ILocatorFilter::highlightInfo(

View File

@@ -143,7 +143,6 @@ public:
bool isEnabled() const; bool isEnabled() const;
static Qt::CaseSensitivity caseSensitivity(const QString &str); static Qt::CaseSensitivity caseSensitivity(const QString &str);
static bool containsWildcard(const QString &str);
static QRegularExpression createRegExp(const QString &text); static QRegularExpression createRegExp(const QString &text);
LocatorFilterEntry::HighlightInfo highlightInfo(const QRegularExpressionMatch &match, LocatorFilterEntry::HighlightInfo highlightInfo(const QRegularExpressionMatch &match,
LocatorFilterEntry::HighlightInfo::DataType dataType = LocatorFilterEntry::HighlightInfo::DisplayName); LocatorFilterEntry::HighlightInfo::DataType dataType = LocatorFilterEntry::HighlightInfo::DisplayName);

View File

@@ -141,9 +141,9 @@ void tst_FuzzyMatcher::highlighting_data()
QTest::newRow("numbers") << "4" << "TestJust4Fun" QTest::newRow("numbers") << "4" << "TestJust4Fun"
<< MatchStart{8} << MatchLength{1}; << MatchStart{8} << MatchLength{1};
QTest::newRow("wildcard-asterisk") << "Lo*Hu" << "VeryLongCamelHump" QTest::newRow("wildcard-asterisk") << "Lo*Hu" << "VeryLongCamelHump"
<< MatchStart{4} << MatchLength{11}; << MatchStart{4, 13} << MatchLength{2, 2};
QTest::newRow("wildcard-question") << "Lo?g" << "VeryLongCamelHump" QTest::newRow("wildcard-question") << "Lo?g" << "VeryLongCamelHump"
<< MatchStart{4} << MatchLength{4}; << MatchStart{4, 7} << MatchLength{2, 1};
QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp" QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp"
<< MatchStart{4} << MatchLength{6}; << MatchStart{4} << MatchLength{6};
} }