Commit f56b2bfe authored by Orgad Shaneh's avatar Orgad Shaneh Committed by André Hartmann

CamelHump: Fix case insensitive matching after the same case

For example: window -> MAINWINDOW.

This was broken by the constraint that the first char must follow a non-
matching case in 2fb54abd. It was fixed for pure lowercase in f7924757,
but this fix was evidentially partial.

Change-Id: I0c29b54768c40d0501f006182f2bd1ae1e94f3bf
Reviewed-by: default avatarAndré Hartmann <aha_1980@gmx.de>
parent 0fb6a147
......@@ -65,6 +65,7 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp(
*/
QString keyRegExp;
QString plainRegExp;
bool first = true;
const QChar asterisk = '*';
const QChar question = '?';
......@@ -76,12 +77,17 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp(
keyRegExp += "(?:";
for (const QChar &c : pattern) {
if (!c.isLetter()) {
if (c == question)
if (c == question) {
keyRegExp += '.';
else if (c == asterisk)
plainRegExp += '.';
} else if (c == asterisk) {
keyRegExp += ".*";
else
keyRegExp += '(' + QRegularExpression::escape(c) + ')';
plainRegExp += ".*";
} else {
const QString escaped = QRegularExpression::escape(c);
keyRegExp += '(' + escaped + ')';
plainRegExp += escaped;
}
} else if (caseSensitivity == CaseSensitivity::CaseInsensitive ||
(caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) {
......@@ -97,6 +103,7 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp(
keyRegExp += '|' + upperSnakeWordContinuation + '(' + upper + ')';
}
keyRegExp += ')';
plainRegExp += '[' + upper + lower + ']';
} else {
if (!first) {
if (c.isUpper())
......@@ -104,12 +111,14 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp(
else
keyRegExp += lowercaseWordContinuation;
}
keyRegExp += QRegularExpression::escape(c);
const QString escaped = QRegularExpression::escape(c);
keyRegExp += escaped;
plainRegExp += escaped;
}
first = false;
}
keyRegExp += ")|(" + QRegularExpression::escape(pattern) + ')';
keyRegExp += ")|(" + plainRegExp + ')';
return QRegularExpression(keyRegExp);
}
......
......@@ -79,6 +79,8 @@ void tst_CamelHumpMatcher::camelHumpMatcher_data()
QTest::newRow("middle-after-underscore-uppercase") << "CH" << "LONG_CAMEL_HUMP" << 5;
QTest::newRow("middle-continued") << "cahu" << "LongCamelHump" << 4;
QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp" << 4;
QTest::newRow("case-insensitive") << "window" << "MAINWINDOW.cpp" << 4;
QTest::newRow("case-insensitive-2") << "wINDow" << "MainwiNdow.cpp" << 4;
}
typedef QVector<int> MatchStart;
......
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