diff --git a/autotests/kfilewidgettest.cpp b/autotests/kfilewidgettest.cpp --- a/autotests/kfilewidgettest.cpp +++ b/autotests/kfilewidgettest.cpp @@ -362,6 +362,13 @@ << filter << QStringLiteral("some.txt") << QStringLiteral("some.txt"); + + // \" in filename + QTest::newRow("\"some\".txt") + << "\"some\".txt" + << filter + << QStringLiteral("\"some\".txt") + << QStringLiteral("\"some\".txt"); } void testSetFilterForSave() diff --git a/src/filewidgets/kfilewidget.cpp b/src/filewidgets/kfilewidget.cpp --- a/src/filewidgets/kfilewidget.cpp +++ b/src/filewidgets/kfilewidget.cpp @@ -1735,7 +1735,7 @@ return urlList; } -// FIXME: current implementation drawback: a filename can't contain quotes +// FIXME: current implementation drawback: a filename can't contain '" "' QList KFileWidgetPrivate::tokenize(const QString &line) const { // qDebug(); @@ -1747,8 +1747,10 @@ } QString name; - const int count = line.count(QLatin1Char('"')); - if (count == 0) { // no " " -> assume one single file + const QRegularExpression re(QStringLiteral("\" \"")); + const auto match = re.match(line); + + if (!match.hasMatch()) { if (!QDir::isAbsolutePath(line)) { u = u.adjusted(QUrl::RemoveFilename); u.setPath(u.path() + line); @@ -1762,15 +1764,20 @@ return urls; } - int start = 0; - int index1 = -1, index2 = -1; + int index1 = 0, index2 = -1; while (true) { - index1 = line.indexOf(QLatin1Char('"'), start); - index2 = line.indexOf(QLatin1Char('"'), index1 + 1); + index1 = line.indexOf(QStringLiteral("\""), index1); + // find next word separator + index2 = line.indexOf(QStringLiteral("\" \""), index1 + 1); - if (index1 < 0 || index2 < 0) { + if (index1 < 0) { break; } + if (index2 == -1) { + // when no word separator is found + // grab everything left until last character + index2 = line.length() -1; + } // get everything between the " " name = line.mid(index1 + 1, index2 - index1 - 1); @@ -1792,7 +1799,7 @@ urls.append(_u); } - start = index2 + 1; + index1 = index2 + 1; } return urls;