summaryrefslogtreecommitdiffstats
path: root/src/corelib/mimetypes/qmimeglobpattern.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/mimetypes/qmimeglobpattern.cpp')
-rw-r--r--src/corelib/mimetypes/qmimeglobpattern.cpp57
1 files changed, 29 insertions, 28 deletions
diff --git a/src/corelib/mimetypes/qmimeglobpattern.cpp b/src/corelib/mimetypes/qmimeglobpattern.cpp
index f991d15b6c..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;
@@ -34,9 +35,9 @@ void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const Q
bool replace = weight > m_weight;
if (!replace) {
// Compare the length of the match
- if (pattern.length() < m_matchingPatternLength)
+ if (pattern.size() < m_matchingPatternLength)
return; // too short, ignore
- else if (pattern.length() > m_matchingPatternLength) {
+ else if (pattern.size() > m_matchingPatternLength) {
// longer: clear any previous match (like *.bz2, when pattern is *.tar.bz2)
replace = true;
}
@@ -44,7 +45,7 @@ void QMimeGlobMatchResult::addMatch(const QString &mimeType, int weight, const Q
if (replace) {
m_matchingMimeTypes.clear();
// remember the new "longer" length
- m_matchingPatternLength = pattern.length();
+ m_matchingPatternLength = pattern.size();
m_weight = weight;
}
if (!m_matchingMimeTypes.contains(mimeType)) {
@@ -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.length();
+ 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.length();
+ const qsizetype patternLength = m_pattern.size();
if (!patternLength)
return false;
- const int fileNameLength = fileName.length();
+ const qsizetype fileNameLength = fileName.size();
switch (m_patternType) {
case SuffixPattern: {
@@ -162,11 +163,11 @@ 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
- && pattern.length() > 1
+ && pattern.size() > 1
&& pattern.at(1) == u'.' // (other dots are OK, like *.tar.bz2)
// and contains no other special character
&& !pattern.contains(u'?')
@@ -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.length() - 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.length() - 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()