From 313bb32364e106e111b8518b786c4364770aaaac Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Sun, 21 May 2023 17:33:17 +0200 Subject: [PATCH] QRegularExpression: match newlines when converting wildcards A * or a ? in a wildcard pattern is allowed to match any character, including newlines. When converting a wildcard pattern to a PCRE, * and ? were converted to ., which by default does _not_ match over newlines (/s is necessary). There isn't a metacharacter that matches everything, so either we modify the returned pattern to enable dot-matches-all (for instance, by wrapping the returned expression in (?s:...)), or use a character class that includes everything. Picking this last approach for simplicity. Change-Id: I86703f654e3414783427c4c8e0bb018885b42e54 Fixes: QTBUG-113676 Pick-to: 6.5 Reviewed-by: Samuel Gaist Reviewed-by: Thiago Macieira --- src/corelib/text/qregularexpression.cpp | 4 +++- .../text/qregularexpression/tst_qregularexpression.cpp | 4 ++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/corelib/text/qregularexpression.cpp b/src/corelib/text/qregularexpression.cpp index 2c83d122a3b..07e2f7965e4 100644 --- a/src/corelib/text/qregularexpression.cpp +++ b/src/corelib/text/qregularexpression.cpp @@ -1942,7 +1942,9 @@ QString QRegularExpression::wildcardToRegularExpression(QStringView pattern, Wil const GlobSettings settings = [options]() { if (options.testFlag(NonPathWildcardConversion)) { - return GlobSettings{ u'\0', u".*", u"." }; + // using [\d\D] to mean "match everything"; + // dot doesn't match newlines, unless in /s mode + return GlobSettings{ u'\0', u"[\\d\\D]*", u"[\\d\\D]" }; } else { #ifdef Q_OS_WIN return GlobSettings{ u'\\', u"[^/\\\\]*", u"[^/\\\\]" }; diff --git a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp index 5a8dc2b3736..02c2725ba4d 100644 --- a/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/text/qregularexpression/tst_qregularexpression.cpp @@ -2491,6 +2491,10 @@ void tst_QRegularExpression::wildcard_data() addRow("foo/(?)/bar", "foo/(Q)/bar", true, true); addRow("foo*bar", "foo/fie/baz/bar", false, true); + addRow("foo*bar", "foo bar", true, true); + addRow("foo*bar", "foo\tbar", true, true); + addRow("foo*bar", "foo\nbar", true, true); + addRow("foo*bar", "foo\r\nbar", true, true); // different anchor modes addRow("foo", "afoob", false, false, true);