From ee97db4dfc10e87f637d8387fb3f4d3590d95697 Mon Sep 17 00:00:00 2001 From: Ismael Asensio Date: Sat, 4 Jul 2020 16:23:24 +0200 Subject: [search] Fix corner cases when using quotes in filenames The `filename` term in a search query is enclosed into quotes. As the user can have additional quotes in the search term, there were several corner cases where the parsing would fail to correctly split the terms New test cases have been added to cover this possibility Previous tests still passes to avoid regressions BEFORE: ``` (filename/quoted) Compared values are not the same Actual (query.text()): "xyz\"\"" Expected (expectedText): "\"abc xyz\"" (filename/mixed) Compared values are not the same Actual (query.text()): "xyz\" tuv\"" Expected (expectedText): "\"abc xyz\" tuv" (content+filename/quoted) Compared values are not the same Actual (query.text()): "abc xyz xyz\"\"" Expected (expectedText): "abc xyz filename:\"\"abc xyz\"\"" ``` --- src/search/dolphinquery.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/search/dolphinquery.cpp') diff --git a/src/search/dolphinquery.cpp b/src/search/dolphinquery.cpp index 0581a02ec..663ed9909 100644 --- a/src/search/dolphinquery.cpp +++ b/src/search/dolphinquery.cpp @@ -59,7 +59,11 @@ namespace { QStringList splitOutsideQuotes(const QString& text) { - const QRegularExpression subTermsRegExp("(\\S*?\"[^\"]*?\"|(?<=\\s|^)\\S+(?=\\s|$))"); + // Match groups on 3 possible conditions: + // - Groups with two leading quotes must close both on them (filename:""abc xyz" tuv") + // - Groups enclosed in quotes + // - Words separated by spaces + const QRegularExpression subTermsRegExp("(\\S*?\"\"[^\"]+\"[^\"]+\"+|\\S*?\"[^\"]+\"+|(?<=\\s|^)\\S+(?=\\s|$))"); auto subTermsMatchIterator = subTermsRegExp.globalMatch(text); QStringList textParts; -- cgit v1.3