Fix QUrl::fromLocalFile with long path prefix
After commit 3966b571 the function was kinda broken already, though this got unnoticed since it was not covered by an the auto-test. This commit adds another test case with Windows native separators and removes the use of QDir::fromNativeSeparators. Instead use the original code from QDir::fromNativeSeparators to replace the backslashes. Change-Id: I190560d0e75cb8c177d63b142aa4be5b01498da2 Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io> (cherry picked from commit 06689a2d7a18882535819ed13ac7248c81330529) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
parent
115016c172
commit
ad6870a855
@ -413,7 +413,6 @@
|
|||||||
#include "qstringlist.h"
|
#include "qstringlist.h"
|
||||||
#include "qdebug.h"
|
#include "qdebug.h"
|
||||||
#include "qhash.h"
|
#include "qhash.h"
|
||||||
#include "qdir.h" // for QDir::fromNativeSeparators
|
|
||||||
#include "qdatastream.h"
|
#include "qdatastream.h"
|
||||||
#include "private/qipaddress_p.h"
|
#include "private/qipaddress_p.h"
|
||||||
#include "qurlquery.h"
|
#include "qurlquery.h"
|
||||||
@ -3291,6 +3290,25 @@ bool QUrl::isDetached() const
|
|||||||
return !d || d->ref.loadRelaxed() == 1;
|
return !d || d->ref.loadRelaxed() == 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static QString fromNativeSeparators(const QString &pathName)
|
||||||
|
{
|
||||||
|
#if defined(Q_OS_WIN)
|
||||||
|
QString result(pathName);
|
||||||
|
const QChar nativeSeparator = u'\\';
|
||||||
|
auto i = result.indexOf(nativeSeparator);
|
||||||
|
if (i != -1) {
|
||||||
|
QChar * const data = result.data();
|
||||||
|
const auto length = result.length();
|
||||||
|
for (; i < length; ++i) {
|
||||||
|
if (data[i] == nativeSeparator)
|
||||||
|
data[i] = u'/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
#else
|
||||||
|
return pathName;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
Returns a QUrl representation of \a localFile, interpreted as a local
|
Returns a QUrl representation of \a localFile, interpreted as a local
|
||||||
@ -3329,7 +3347,7 @@ QUrl QUrl::fromLocalFile(const QString &localFile)
|
|||||||
if (localFile.isEmpty())
|
if (localFile.isEmpty())
|
||||||
return url;
|
return url;
|
||||||
QString scheme = fileScheme();
|
QString scheme = fileScheme();
|
||||||
QString deslashified = QDir::fromNativeSeparators(localFile);
|
QString deslashified = fromNativeSeparators(localFile);
|
||||||
|
|
||||||
// magic for drives on windows
|
// magic for drives on windows
|
||||||
if (deslashified.length() > 1 && deslashified.at(1) == QLatin1Char(':') && deslashified.at(0) != QLatin1Char('/')) {
|
if (deslashified.length() > 1 && deslashified.at(1) == QLatin1Char(':') && deslashified.at(0) != QLatin1Char('/')) {
|
||||||
|
@ -1333,6 +1333,10 @@ void tst_QUrl::fromLocalFile_data()
|
|||||||
<< QString(QString("//somehost") + suffix).replace('/', '\\')
|
<< QString(QString("//somehost") + suffix).replace('/', '\\')
|
||||||
<< QString("file://somehost") + suffix
|
<< QString("file://somehost") + suffix
|
||||||
<< QString(suffix);
|
<< QString(suffix);
|
||||||
|
QTest::addRow("windows-backslash-extlen-%s", pathDescription)
|
||||||
|
<< QString(QString("//?") + suffix).replace('/', '\\')
|
||||||
|
<< QString("file:////%3F") + suffix
|
||||||
|
<< QString("//?") + suffix;
|
||||||
#endif
|
#endif
|
||||||
QTest::addRow("windows-extlen-%s", pathDescription)
|
QTest::addRow("windows-extlen-%s", pathDescription)
|
||||||
<< QString("//?") + suffix
|
<< QString("//?") + suffix
|
||||||
|
Loading…
x
Reference in New Issue
Block a user