diff options
Diffstat (limited to 'src/corelib/mimetypes/qmimeglobpattern.cpp')
-rw-r--r-- | src/corelib/mimetypes/qmimeglobpattern.cpp | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/src/corelib/mimetypes/qmimeglobpattern.cpp b/src/corelib/mimetypes/qmimeglobpattern.cpp index 381b635b90..d50787a0be 100644 --- a/src/corelib/mimetypes/qmimeglobpattern.cpp +++ b/src/corelib/mimetypes/qmimeglobpattern.cpp @@ -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; @@ -57,13 +58,13 @@ void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const Q } } -QMimeGlobPattern::PatternType QMimeGlobPattern::detectPatternType(const QString &pattern) const +QMimeGlobPattern::PatternType QMimeGlobPattern::detectPatternType(QStringView 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: { @@ -162,7 +163,7 @@ bool QMimeGlobPattern::matchFileName(const QString &inputFileName) const return false; } -static bool isSimplePattern(const QString &pattern) +static bool isSimplePattern(QStringView pattern) { // starts with "*.", has no other '*' return pattern.lastIndexOf(u'*') == 0 @@ -174,7 +175,7 @@ static bool isSimplePattern(const QString &pattern) ; } -static bool isFastPattern(const QString &pattern) +static bool isFastPattern(QStringView pattern) { // starts with "*.", has no other '*' and no other '.' return pattern.lastIndexOf(u'*') == 0 @@ -219,45 +220,45 @@ void QMimeAllGlobPatterns::removeMimeType(const QString &mimeType) m_lowWeightGlobs.removeMimeType(mimeType); } -void QMimeGlobPatternList::match(QMimeGlobMatchResult &result, - const QString &fileName) const +void QMimeGlobPatternList::match(QMimeGlobMatchResult &result, const QString &fileName, + const AddMatchFilterFunc &filterFunc) const { - - QMimeGlobPatternList::const_iterator it = this->constBegin(); - const QMimeGlobPatternList::const_iterator endIt = this->constEnd(); - for (; it != endIt; ++it) { - const QMimeGlobPattern &glob = *it; - if (glob.matchFileName(fileName)) { + for (const QMimeGlobPattern &glob : *this) { + if (glob.matchFileName(fileName) && filterFunc(glob.mimeType())) { 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); } } } -void QMimeAllGlobPatterns::matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result) const +void QMimeAllGlobPatterns::matchingGlobs(const QString &fileName, QMimeGlobMatchResult &result, + const AddMatchFilterFunc &filterFunc) const { // First try the high weight matches (>50), if any. - m_highWeightGlobs.match(result, fileName); + m_highWeightGlobs.match(result, fileName, filterFunc); // 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) const QStringList matchingMimeTypes = m_fastPatterns.value(simpleExtension); const QString simplePattern = "*."_L1 + simpleExtension; - for (const QString &mime : matchingMimeTypes) - result.addMatch(mime, 50, simplePattern, simpleExtension.size()); + for (const QString &mime : matchingMimeTypes) { + if (filterFunc(mime)) { + result.addMatch(mime, 50, simplePattern, simpleExtension.size()); + } + } // Can't return yet; *.tar.bz2 has to win over *.bz2, so we need the low-weight mimetypes anyway, // at least those with weight 50. } // Finally, try the low weight matches (<=50) - m_lowWeightGlobs.match(result, fileName); + m_lowWeightGlobs.match(result, fileName, filterFunc); } void QMimeAllGlobPatterns::clear() |