mimetypes/: port to qsizetype

Fixes compiler warnings about narrowing conversions.

Found by compiling with clang and -Wshorten-64-to-32.

Drive-by changes:
- use range-for instead of an iterator based loop
- use strlen("*.") instead of magic number 2

Pick-to: 6.5
Task-number: QTBUG-102461
Change-Id: I0bf2299049c0411ed496468238ca30b69946ffc2
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Ahmad Samir 2023-04-10 23:02:22 +02:00
parent 8ca2d6ba09
commit c1e1d133c4
8 changed files with 32 additions and 27 deletions

View File

@ -668,7 +668,7 @@ QList<QMimeType> QMimeDatabase::mimeTypesForFileName(const QString &fileName) co
QString QMimeDatabase::suffixForFileName(const QString &fileName) const
{
QMutexLocker locker(&d->mutex);
const int suffixLength = d->findByFileName(fileName).m_knownSuffixLength;
const qsizetype suffixLength = d->findByFileName(fileName).m_knownSuffixLength;
return fileName.right(suffixLength);
}

View File

@ -22,7 +22,8 @@ using namespace Qt::StringLiterals;
Handles glob weights, and preferring longer matches over shorter matches.
*/
void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const QString &pattern, int knownSuffixLength)
void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const QString &pattern,
qsizetype knownSuffixLength)
{
if (m_allMatchingMimeTypes.contains(mimeType))
return;
@ -59,11 +60,11 @@ void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const Q
QMimeGlobPattern::PatternType QMimeGlobPattern::detectPatternType(const QString &pattern) const
{
const int patternLength = pattern.size();
const qsizetype patternLength = pattern.size();
if (!patternLength)
return OtherPattern;
const int starCount = pattern.count(u'*');
const qsizetype starCount = pattern.count(u'*');
const bool hasSquareBracket = pattern.indexOf(u'[') != -1;
const bool hasQuestionMark = pattern.indexOf(u'?') != -1;
@ -108,10 +109,10 @@ bool QMimeGlobPattern::matchFileName(const QString &inputFileName) const
const QString fileName = m_caseSensitivity == Qt::CaseInsensitive
? inputFileName.toLower() : inputFileName;
const int patternLength = m_pattern.size();
const qsizetype patternLength = m_pattern.size();
if (!patternLength)
return false;
const int fileNameLength = fileName.size();
const qsizetype fileNameLength = fileName.size();
switch (m_patternType) {
case SuffixPattern: {
@ -222,14 +223,10 @@ void QMimeAllGlobPatterns::removeMimeType(const QString &mimeType)
void QMimeGlobPatternList::match(QMimeGlobMatchResult &result,
const QString &fileName) const
{
QMimeGlobPatternList::const_iterator it = this->constBegin();
const QMimeGlobPatternList::const_iterator endIt = this->constEnd();
for (; it != endIt; ++it) {
const QMimeGlobPattern &glob = *it;
for (const QMimeGlobPattern &glob : *this) {
if (glob.matchFileName(fileName)) {
const QString pattern = glob.pattern();
const int suffixLen = isSimplePattern(pattern) ? pattern.size() - 2 : 0;
const qsizetype suffixLen = isSimplePattern(pattern) ? pattern.size() - strlen("*.") : 0;
result.addMatch(glob.mimeType(), glob.weight(), pattern, suffixLen);
}
}
@ -242,9 +239,9 @@ void QMimeAllGlobPatterns::matchingGlobs(const QString &fileName, QMimeGlobMatch
// Now use the "fast patterns" dict, for simple *.foo patterns with weight 50
// (which is most of them, so this optimization is definitely worth it)
const int lastDot = fileName.lastIndexOf(u'.');
const qsizetype lastDot = fileName.lastIndexOf(u'.');
if (lastDot != -1) { // if no '.', skip the extension lookup
const int ext_len = fileName.size() - lastDot - 1;
const qsizetype ext_len = fileName.size() - lastDot - 1;
const QString simpleExtension = fileName.right(ext_len).toLower();
// (toLower because fast patterns are always case-insensitive and saved as lowercase)

View File

@ -26,13 +26,14 @@ QT_BEGIN_NAMESPACE
struct QMimeGlobMatchResult
{
void addMatch(const QString &mimeType, int weight, const QString &pattern, int knownSuffixLength = 0);
void addMatch(const QString &mimeType, int weight, const QString &pattern,
qsizetype knownSuffixLength = 0);
QStringList m_matchingMimeTypes; // only those with highest weight
QStringList m_allMatchingMimeTypes;
int m_weight = 0;
int m_matchingPatternLength = 0;
int m_knownSuffixLength = 0;
qsizetype m_matchingPatternLength = 0;
qsizetype m_knownSuffixLength = 0;
};
class QMimeGlobPattern

View File

@ -61,12 +61,12 @@ bool QMimeMagicRule::operator==(const QMimeMagicRule &other) const
}
// Used by both providers
bool QMimeMagicRule::matchSubstring(const char *dataPtr, int dataSize, int rangeStart, int rangeLength,
int valueLength, const char *valueData, const char *mask)
bool QMimeMagicRule::matchSubstring(const char *dataPtr, qsizetype dataSize, int rangeStart, int rangeLength,
qsizetype valueLength, const char *valueData, const char *mask)
{
// Size of searched data.
// Example: value="ABC", rangeLength=3 -> we need 3+3-1=5 bytes (ABCxx,xABCx,xxABC would match)
const int dataNeeded = qMin(rangeLength + valueLength - 1, dataSize - rangeStart);
const qsizetype dataNeeded = qMin(rangeLength + valueLength - 1, dataSize - rangeStart);
if (!mask) {
// callgrind says QByteArray::indexOf is much slower, since our strings are typically too
@ -90,7 +90,7 @@ bool QMimeMagicRule::matchSubstring(const char *dataPtr, int dataSize, int range
// deviceSize is 4, so dataNeeded was max'ed to 4.
// maxStartPos = 4 - 3 + 1 = 2, and indeed
// we need to check for a match a positions 0 and 1 (ABCx and xABC).
const int maxStartPos = dataNeeded - valueLength + 1;
const qsizetype maxStartPos = dataNeeded - valueLength + 1;
for (int i = 0; i < maxStartPos; ++i) {
const char *d = readDataBase + i;
bool valid = true;
@ -201,7 +201,7 @@ QMimeMagicRule::QMimeMagicRule(const QString &type,
}
// Parse for offset as "1" or "1:10"
const int colonIndex = offsets.indexOf(u':');
const qsizetype colonIndex = offsets.indexOf(u':');
const QStringView startPosStr = QStringView{offsets}.mid(0, colonIndex); // \ These decay to returning 'offsets'
const QStringView endPosStr = QStringView{offsets}.mid(colonIndex + 1);// / unchanged when colonIndex == -1
if (Q_UNLIKELY(!QMimeTypeParserBase::parseNumber(startPosStr, &m_startPos, errorString)) ||

View File

@ -63,7 +63,9 @@ public:
static Type type(const QByteArray &type);
static QByteArray typeName(Type type);
static bool matchSubstring(const char *dataPtr, int dataSize, int rangeStart, int rangeLength, int valueLength, const char *valueData, const char *mask);
static bool matchSubstring(const char *dataPtr, qsizetype dataSize, int rangeStart,
int rangeLength, qsizetype valueLength, const char *valueData,
const char *mask);
private:
Type m_type;

View File

@ -250,7 +250,10 @@ void QMimeBinaryProvider::matchGlobList(QMimeGlobMatchResult &result, CacheFile
}
}
bool QMimeBinaryProvider::matchSuffixTree(QMimeGlobMatchResult &result, QMimeBinaryProvider::CacheFile *cacheFile, int numEntries, int firstOffset, const QString &fileName, int charPos, bool caseSensitiveCheck)
bool QMimeBinaryProvider::matchSuffixTree(QMimeGlobMatchResult &result,
QMimeBinaryProvider::CacheFile *cacheFile, int numEntries,
int firstOffset, const QString &fileName,
qsizetype charPos, bool caseSensitiveCheck)
{
QChar fileChar = fileName[charPos];
int min = 0;
@ -298,7 +301,7 @@ bool QMimeBinaryProvider::matchSuffixTree(QMimeGlobMatchResult &result, QMimeBin
bool QMimeBinaryProvider::matchMagicRule(QMimeBinaryProvider::CacheFile *cacheFile, int numMatchlets, int firstOffset, const QByteArray &data)
{
const char *dataPtr = data.constData();
const int dataSize = data.size();
const qsizetype dataSize = data.size();
for (int matchlet = 0; matchlet < numMatchlets; ++matchlet) {
const int off = firstOffset + matchlet * 32;
const int rangeStart = cacheFile->getUint32(off);

View File

@ -82,7 +82,9 @@ private:
struct CacheFile;
void matchGlobList(QMimeGlobMatchResult &result, CacheFile *cacheFile, int offset, const QString &fileName);
bool matchSuffixTree(QMimeGlobMatchResult &result, CacheFile *cacheFile, int numEntries, int firstOffset, const QString &fileName, int charPos, bool caseSensitiveCheck);
bool matchSuffixTree(QMimeGlobMatchResult &result, CacheFile *cacheFile, int numEntries,
int firstOffset, const QString &fileName, qsizetype charPos,
bool caseSensitiveCheck);
bool matchMagicRule(CacheFile *cacheFile, int numMatchlets, int firstOffset, const QByteArray &data);
QLatin1StringView iconForMime(CacheFile *cacheFile, int posListOffset, const QByteArray &inputMime);
void loadMimeTypeList();

View File

@ -810,7 +810,7 @@ void tst_QMimeDatabase::findByFileName_data()
QByteArray line(1024, Qt::Uninitialized);
while (!f.atEnd()) {
int len = f.readLine(line.data(), 1023);
const qint64 len = f.readLine(line.data(), 1023);
if (len <= 2 || line.at(0) == '#')
continue;