From 2fb54abd03d71e1e205a1dc6f94769688d1bc2e0 Mon Sep 17 00:00:00 2001 From: Orgad Shaneh Date: Sun, 10 Sep 2017 22:05:11 +0300 Subject: [PATCH] CamelHump: Do not match first char in the middle of a word MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When matching NLH against LongCamelHump do _not_ consider the N of Long as part of the match. The first character needs to meet the same requirements as the others. Change-Id: I4e7b2fe5a28296afe6f06f90e1986336fe7f3179 Reviewed-by: André Hartmann Reviewed-by: Eike Ziller --- src/libs/utils/camelhumpmatcher.cpp | 8 ++++---- .../auto/utils/camelhumpmatcher/tst_camelhumpmatcher.cpp | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/libs/utils/camelhumpmatcher.cpp b/src/libs/utils/camelhumpmatcher.cpp index c4f0d2be2fa..7c8af9b1292 100644 --- a/src/libs/utils/camelhumpmatcher.cpp +++ b/src/libs/utils/camelhumpmatcher.cpp @@ -68,6 +68,8 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp( bool first = true; const QChar asterisk = '*'; const QChar question = '?'; + const QLatin1String uppercaseWordFirst("(?<=\\b|[a-z0-9_])"); + const QLatin1String lowercaseWordFirst("(?<=\\b|[A-Z0-9_])"); const QLatin1String uppercaseWordContinuation("[a-z0-9_]*"); const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?"); for (const QChar &c : pattern) { @@ -82,12 +84,10 @@ QRegularExpression CamelHumpMatcher::createCamelHumpRegExp( (caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) { keyRegExp += "(?:"; - if (!first) - keyRegExp += uppercaseWordContinuation; + keyRegExp += first ? uppercaseWordFirst : uppercaseWordContinuation; keyRegExp += QRegularExpression::escape(c.toUpper()); keyRegExp += '|'; - if (!first) - keyRegExp += lowercaseWordContinuation; + keyRegExp += first ? lowercaseWordFirst : lowercaseWordContinuation; keyRegExp += QRegularExpression::escape(c.toLower()); keyRegExp += ')'; } else { diff --git a/tests/auto/utils/camelhumpmatcher/tst_camelhumpmatcher.cpp b/tests/auto/utils/camelhumpmatcher/tst_camelhumpmatcher.cpp index 8bd922e0a11..b42a46cba71 100644 --- a/tests/auto/utils/camelhumpmatcher/tst_camelhumpmatcher.cpp +++ b/tests/auto/utils/camelhumpmatcher/tst_camelhumpmatcher.cpp @@ -69,6 +69,10 @@ void tst_CamelHumpMatcher::camelHumpMatcher_data() QTest::newRow("unmatched-question-wildcard") << "Long?Ca" << "VeryLongCamelHump" << -1; QTest::newRow("asterix-wildcard") << "Long*Ca" << "VeryLongCamelHump" << 4; QTest::newRow("empty-asterix-wildcard") << "Lo*Ca" << "VeryLongCamelHump" << 4; + QTest::newRow("no-partial") << "NCH" << "LongCamelHump" << -1; + QTest::newRow("middle-after-number") << "CH" << "Long1CamelHump" << 5; + QTest::newRow("middle-after-underscore") << "CH" << "long_camel_hump" << 5; + QTest::newRow("middle-continued") << "cahu" << "LongCamelHump" << 4; } QTEST_APPLESS_MAIN(tst_CamelHumpMatcher)