summaryrefslogtreecommitdiffstats
path: root/src/corelib/io/qfilesystementry.cpp
diff options
context:
space:
mode:
authorKarsten Heimrich <karsten.heimrich@qt.io>2021-05-11 14:09:11 +0200
committerKarsten Heimrich <karsten.heimrich@qt.io>2021-06-05 01:16:06 +0200
commit0564ebdb3641d7325f73dbbf2cbb04e6dca92d83 (patch)
treea766802e3bd675e8d84d655c3b93a0ea62d98d92 /src/corelib/io/qfilesystementry.cpp
parenta1d967357f2715899d0236ea4910e615d178024f (diff)
Unify behavior for long path or UNC prefix removal
Split the code out of QDir::fromNativeSeparator into a separate reusable function to remove the above-mentioned prefixes. Fixes and unifies behavior if the prefix was given with slashes instead of backslashes. Add a couple more test cases. Fixes: QTBUG-93868 Pick-to: 5.15 6.0 6.1 Change-Id: Ibd94ae283e2fb113f9c2db97475fbc7d89522bbf Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
Diffstat (limited to 'src/corelib/io/qfilesystementry.cpp')
-rw-r--r--src/corelib/io/qfilesystementry.cpp28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/corelib/io/qfilesystementry.cpp b/src/corelib/io/qfilesystementry.cpp
index c061f58e87..bb39a46642 100644
--- a/src/corelib/io/qfilesystementry.cpp
+++ b/src/corelib/io/qfilesystementry.cpp
@@ -288,6 +288,34 @@ bool QFileSystemEntry::isDriveRootPath(const QString &path)
&& path.at(0).isLetter() && path.at(1) == QLatin1Char(':')
&& path.at(2) == QLatin1Char('/'));
}
+
+QString QFileSystemEntry::removeUncOrLongPathPrefix(QString path)
+{
+ constexpr qsizetype minPrefixSize = 4;
+ if (path.size() < minPrefixSize)
+ return path;
+
+ auto data = path.data();
+ const auto slash = path[0];
+ if (slash != u'\\' && slash != u'/')
+ return path;
+
+ // check for "//?/" or "/??/"
+ if (data[2] == u'?' && data[3] == slash && (data[1] == slash || data[1] == u'?')) {
+ path = path.sliced(minPrefixSize);
+
+ // check for a possible "UNC/" prefix left-over
+ if (path.size() >= 4) {
+ data = path.data();
+ if (data[0] == u'U' && data[1] == u'N' && data[2] == u'C' && data[3] == slash) {
+ data[2] = slash;
+ return path.sliced(2);
+ }
+ }
+ }
+
+ return path;
+}
#endif // Q_OS_WIN
bool QFileSystemEntry::isRootPath(const QString &path)