From c6d60a1b5778477d431cb0b57df92db8479ac307 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 18 Aug 2022 11:10:09 +0200 Subject: Port QDir to qsizetype [2/3]: implementation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Port ints that should be qsizetype. None of these are likely to cause bugs, because both path lengths and number of directory entries are usually limited by the OS, not by INT_MAX. As a drive-by, replace length() with size() and port some functions from QString to QStringView. Manual conflict resolutions: - u'x' vs. QLatin1Char('x') - kept the u'x' version on affected lines as a drive-by Task-number: QTBUG-103525 Change-Id: I79eb6d580631346a1e77eb1c7a73a3880943794e Reviewed-by: Sona Kurazyan Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira (cherry picked from commit 3f32b01b98f71df32281745fb54b62ab123fe424) --- src/corelib/io/qdir.cpp | 46 ++++++++++++++++++++--------------------- src/corelib/io/qdiriterator.cpp | 2 +- 2 files changed, 24 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/corelib/io/qdir.cpp b/src/corelib/io/qdir.cpp index bae47bfb2b..eb9cb99910 100644 --- a/src/corelib/io/qdir.cpp +++ b/src/corelib/io/qdir.cpp @@ -91,13 +91,13 @@ enum { }; // Return the length of the root part of an absolute path, for use by cleanPath(), cd(). -static int rootLength(const QString &name, bool allowUncPaths) +static qsizetype rootLength(QStringView name, bool allowUncPaths) { - const int len = name.length(); + const qsizetype len = name.size(); // starts with double slash if (allowUncPaths && name.startsWith(QLatin1String("//"))) { // Server name '//server/path' is part of the prefix. - const int nextSlash = name.indexOf(QLatin1Char('/'), 2); + const qsizetype nextSlash = name.indexOf(u'/', 2); return nextSlash >= 0 ? nextSlash + 1 : len; } #if defined(Q_OS_WIN) @@ -166,7 +166,7 @@ bool QDirPrivate::exists() const inline QChar QDirPrivate::getFilterSepChar(const QString &nameFilter) { QChar sep(QLatin1Char(';')); - int i = nameFilter.indexOf(sep, 0); + qsizetype i = nameFilter.indexOf(sep, 0); if (i == -1 && nameFilter.indexOf(QLatin1Char(' '), 0) != -1) sep = QChar(QLatin1Char(' ')); return sep; @@ -322,7 +322,7 @@ inline void QDirPrivate::sortFileList(QDir::SortFlags sort, QFileInfoList &l, QStringList *names, QFileInfoList *infos) { // names and infos are always empty lists or 0 here - int n = l.size(); + qsizetype n = l.size(); if (n > 0) { if (n == 1 || (sort & QDir::SortByMask) == QDir::Unsorted) { if (infos) @@ -333,16 +333,16 @@ inline void QDirPrivate::sortFileList(QDir::SortFlags sort, QFileInfoList &l, } } else { QScopedArrayPointer si(new QDirSortItem[n]); - for (int i = 0; i < n; ++i) + for (qsizetype i = 0; i < n; ++i) si[i].item = l.at(i); std::sort(si.data(), si.data() + n, QDirSortItemComparator(sort)); // put them back in the list(s) if (infos) { - for (int i = 0; i < n; ++i) + for (qsizetype i = 0; i < n; ++i) infos->append(si[i].item); } if (names) { - for (int i = 0; i < n; ++i) + for (qsizetype i = 0; i < n; ++i) names->append(si[i].item.fileName()); } } @@ -709,11 +709,11 @@ QString QDir::dirName() const #ifdef Q_OS_WIN -static int drivePrefixLength(const QString &path) +static qsizetype drivePrefixLength(QStringView path) { // Used to extract path's drive for use as prefix for an "absolute except for drive" path - const int size = path.length(); - int drive = 2; // length of drive prefix + const qsizetype size = path.size(); + qsizetype drive = 2; // length of drive prefix if (size > 1 && path.at(1).unicode() == ':') { if (Q_UNLIKELY(!path.at(0).isLetter())) return 0; @@ -725,7 +725,7 @@ static int drivePrefixLength(const QString &path) drive++; if (drive >= size) { qWarning("Base directory starts with neither a drive nor a UNC share: %s", - qUtf8Printable(QDir::toNativeSeparators(path))); + qUtf8Printable(QDir::toNativeSeparators(path.toString()))); return 0; } while (drive < size && path.at(drive).unicode() != '/') @@ -775,7 +775,7 @@ QString QDir::filePath(const QString &fileName) const #ifdef Q_OS_WIN if (fileName.startsWith(QLatin1Char('/')) || fileName.startsWith(QLatin1Char('\\'))) { // Handle the "absolute except for drive" case (i.e. \blah not c:\blah): - const int drive = drivePrefixLength(ret); + const qsizetype drive = drivePrefixLength(ret); return drive > 0 ? QStringView{ret}.left(drive) % fileName : fileName; } #endif // Q_OS_WIN @@ -807,7 +807,7 @@ QString QDir::absoluteFilePath(const QString &fileName) const // Handle the "absolute except for drive" case (i.e. \blah not c:\blah): if (fileName.startsWith(QLatin1Char('/')) || fileName.startsWith(QLatin1Char('\\'))) { // Combine absoluteDirPath's drive with fileName - const int drive = drivePrefixLength(absoluteDirPath); + const qsizetype drive = drivePrefixLength(absoluteDirPath); if (Q_LIKELY(drive)) return QStringView{absoluteDirPath}.left(drive) % fileName; @@ -916,7 +916,7 @@ QString QDir::relativeFilePath(const QString &fileName) const QString QDir::toNativeSeparators(const QString &pathName) { #if defined(Q_OS_WIN) - int i = pathName.indexOf(QLatin1Char('/')); + qsizetype i = pathName.indexOf(u'/'); if (i != -1) { QString n(pathName); @@ -2160,7 +2160,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza { const bool allowUncPaths = flags.testAnyFlag(QDirPrivate::AllowUncPaths); const bool isRemote = flags.testAnyFlag(QDirPrivate::RemotePath); - const int len = name.length(); + const qsizetype len = name.size(); if (ok) *ok = false; @@ -2168,15 +2168,15 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza if (len == 0) return name; - int i = len - 1; + qsizetype i = len - 1; QVarLengthArray outVector(len); - int used = len; + qsizetype used = len; char16_t *out = outVector.data(); const ushort *p = name.utf16(); const ushort *prefix = p; - int up = 0; + qsizetype up = 0; - const int prefixLength = rootLength(name, allowUncPaths); + const qsizetype prefixLength = rootLength(name, allowUncPaths); p += prefixLength; i -= prefixLength; @@ -2187,10 +2187,10 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza --i; } - auto isDot = [](const ushort *p, int i) { + auto isDot = [](const ushort *p, qsizetype i) { return i > 1 && p[i - 1] == '.' && p[i - 2] == '/'; }; - auto isDotDot = [](const ushort *p, int i) { + auto isDotDot = [](const ushort *p, qsizetype i) { return i > 2 && p[i - 1] == '.' && p[i - 2] == '.' && p[i - 3] == '/'; }; @@ -2293,7 +2293,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza // string only consists of a prefix followed by one or more slashes. Just skip the slash. ++used; } - for (int i = prefixLength - 1; i >= 0; --i) + for (qsizetype i = prefixLength - 1; i >= 0; --i) out[--used] = prefix[i]; } else { if (isEmpty) { diff --git a/src/corelib/io/qdiriterator.cpp b/src/corelib/io/qdiriterator.cpp index 7e86aacdb1..ed570f5d48 100644 --- a/src/corelib/io/qdiriterator.cpp +++ b/src/corelib/io/qdiriterator.cpp @@ -328,7 +328,7 @@ bool QDirIteratorPrivate::matchesFilters(const QString &fileName, const QFileInf return false; // filter . and ..? - const int fileNameSize = fileName.size(); + const qsizetype fileNameSize = fileName.size(); const bool dotOrDotDot = fileName[0] == QLatin1Char('.') && ((fileNameSize == 1) ||(fileNameSize == 2 && fileName[1] == QLatin1Char('.'))); -- cgit v1.2.3