Commit d3c7220c authored by hjk's avatar hjk

Mime: Use string comparisons instead of regexps for simple globbing

Change-Id: Ia894f3663f43088cdb2d8cb685bcebecaded8146
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
parent b45e7b01
......@@ -481,24 +481,39 @@ IMagicMatcher::IMagicMatcherList MagicRuleMatcher::createMatchers(
\sa Core::Internal::BaseMimeTypeParser, Core::Internal::MimeTypeParser
*/
MimeGlobPattern::MimeGlobPattern(const QRegExp &regExp, unsigned weight) :
m_regExp(regExp), m_weight(weight)
MimeGlobPattern::MimeGlobPattern(const QString &pattern, unsigned weight) :
m_pattern(pattern), m_weight(weight)
{
bool hasQuestionMark = pattern.contains(QLatin1Char('?'));
bool hasStar = pattern.contains(QLatin1Char('*'));
if (!hasQuestionMark && hasStar && pattern.lastIndexOf(QLatin1Char('*')) == 0) {
m_type = Suffix;
} else if (!hasQuestionMark && !hasStar) {
m_type = Exact;
} else {
// This hopefully never happens as it is expensive.
m_type = Glob;
m_regexp.setPattern(pattern);
m_regexp.setPatternSyntax(QRegExp::Wildcard);
if (!m_regexp.isValid())
qWarning("%s: Invalid wildcard '%s'.", Q_FUNC_INFO, pattern.toUtf8().constData());
}
}
MimeGlobPattern::~MimeGlobPattern()
{
}
const QRegExp &MimeGlobPattern::regExp() const
bool MimeGlobPattern::matches(const QString &fileName) const
{
return m_regExp;
if (m_type == Exact)
return fileName == m_pattern;
if (m_type == Suffix)
return fileName.endsWith(m_pattern.midRef(1));
return m_regexp.exactMatch(fileName);
}
unsigned MimeGlobPattern::weight() const
{
return m_weight;
}
/*!
\class Core::MimeType
......@@ -607,7 +622,7 @@ void MimeTypeData::debug(QTextStream &str, int indent) const
if (!globPatterns.empty()) {
str << indentS << "Glob: ";
foreach (const MimeGlobPattern &gp, globPatterns)
str << gp.regExp().pattern() << '(' << gp.weight() << ')';
str << gp.pattern() << '(' << gp.weight() << ')';
str << '\n';
if (!suffixes.empty()) {
str << indentS << "Suffixes: " << suffixes.join(comma)
......@@ -777,7 +792,7 @@ QString MimeType::formatFilterString(const QString &description, const QList<Mim
for (int i = 0; i < size; i++) {
if (i)
str << ' ';
str << globs.at(i).regExp().pattern();
str << globs.at(i).pattern();
}
str << ')';
}
......@@ -809,8 +824,7 @@ unsigned MimeType::matchesFileBySuffix(Internal::FileMatchContext &c) const
{
// check globs
foreach (const MimeGlobPattern &gp, m_d->globPatterns) {
QRegExp regExp = gp.regExp();
if (regExp.exactMatch(c.fileName()))
if (gp.matches(c.fileName()))
return gp.weight();
}
return 0;
......@@ -950,17 +964,10 @@ void BaseMimeTypeParser::addGlobPattern(const QString &pattern, const QString &w
return;
// Collect patterns as a QRegExp list and filter out the plain
// suffix ones for our suffix list. Use first one as preferred
const QRegExp wildCard(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
if (!wildCard.isValid()) {
qWarning("%s: Invalid wildcard '%s'.",
Q_FUNC_INFO, pattern.toUtf8().constData());
return;
}
if (weight.isEmpty())
d->globPatterns.push_back(MimeGlobPattern(wildCard));
d->globPatterns.push_back(MimeGlobPattern(pattern));
else
d->globPatterns.push_back(MimeGlobPattern(wildCard, weight.toInt()));
d->globPatterns.push_back(MimeGlobPattern(pattern, weight.toInt()));
d->assignSuffix(pattern);
}
......@@ -1784,10 +1791,8 @@ void MimeDatabasePrivate::clearUserModifiedMimeTypes()
QList<MimeGlobPattern> MimeDatabasePrivate::toGlobPatterns(const QStringList &patterns, int weight)
{
QList<MimeGlobPattern> globPatterns;
foreach (const QString &pattern, patterns) {
QRegExp regExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
globPatterns.append(Core::MimeGlobPattern(regExp, weight));
}
foreach (const QString &pattern, patterns)
globPatterns.append(Core::MimeGlobPattern(pattern, weight));
return globPatterns;
}
......@@ -1795,7 +1800,7 @@ QStringList MimeDatabasePrivate::fromGlobPatterns(const QList<MimeGlobPattern> &
{
QStringList patterns;
foreach (const MimeGlobPattern &globPattern, globPatterns)
patterns.append(globPattern.regExp().pattern());
patterns.append(globPattern.pattern());
return patterns;
}
......
......@@ -163,17 +163,21 @@ public:
static const unsigned MaxWeight = 100;
static const unsigned MinWeight = 1;
explicit MimeGlobPattern(const QRegExp &regExp, unsigned weight = MaxWeight);
explicit MimeGlobPattern(const QString &pattern, unsigned weight = MaxWeight);
~MimeGlobPattern();
const QRegExp &regExp() const;
unsigned weight() const;
bool matches(const QString &fileName) const;
unsigned weight() const { return m_weight; }
QString pattern() const { return m_pattern; }
private:
QRegExp m_regExp;
int m_weight;
enum { Suffix, Exact, Glob } m_type;
QString m_pattern;
QRegExp m_regexp; // Will be used in \c Glob case only.
unsigned m_weight;
};
class CORE_EXPORT MimeType
{
public:
......
......@@ -416,7 +416,7 @@ void MimeTypeSettingsPrivate::syncData(const QModelIndex &current,
QStringList formatedPatterns;
foreach (const MimeGlobPattern &pattern, currentMimeType.globPatterns())
formatedPatterns.append(pattern.regExp().pattern());
formatedPatterns.append(pattern.pattern());
m_ui.patternsLineEdit->setText(formatedPatterns.join(kSemiColon));
// Consider only rule-based matchers.
......
......@@ -172,7 +172,7 @@ Core::GeneratedFiles GenericProjectWizard::generateFiles(const QWizard *w,
QStringList nameFilters;
foreach (const Core::MimeGlobPattern &gp, headerTy.globPatterns())
nameFilters.append(gp.regExp().pattern());
nameFilters.append(gp.pattern());
QStringList includePaths;
foreach (const QString &path, paths) {
......
......@@ -1488,7 +1488,7 @@ static inline QStringList projectFileGlobs()
if (const Core::MimeType mimeType = mimeDatabase->findByType(ipm->mimeType())) {
const QList<Core::MimeGlobPattern> patterns = mimeType.globPatterns();
if (!patterns.isEmpty())
result.push_back(patterns.front().regExp().pattern());
result.push_back(patterns.front().pattern());
}
}
return result;
......@@ -3016,7 +3016,7 @@ QStringList ProjectExplorerPlugin::projectFilePatterns()
foreach (const IProjectManager *pm, allProjectManagers())
if (const Core::MimeType mt = mdb->findByType(pm->mimeType()))
foreach (const Core::MimeGlobPattern &gp, mt.globPatterns())
patterns += gp.regExp().pattern();
patterns.append(gp.pattern());
return patterns;
}
......
......@@ -79,12 +79,16 @@ ModelManagerInterface::ProjectInfo QmlJSTools::defaultProjectInfoForProject(
if (mimeType.type() == QLatin1String(Constants::QML_MIMETYPE)
|| mimeType.subClassesOf().contains(QLatin1String(Constants::QML_MIMETYPE)))
globs << mimeType.globPatterns();
if (globs.isEmpty())
globs << Core::MimeGlobPattern(QRegExp(QLatin1String(".*\\.(?:qbs|qml|qmltypes|qmlproject)$")));
if (globs.isEmpty()) {
globs.append(Core::MimeGlobPattern(QLatin1String("*.qbs")));
globs.append(Core::MimeGlobPattern(QLatin1String("*.qml")));
globs.append(Core::MimeGlobPattern(QLatin1String("*.qmltypes")));
globs.append(Core::MimeGlobPattern(QLatin1String("*.qmlproject")));
}
foreach (const QString &filePath
, project->files(ProjectExplorer::Project::ExcludeGeneratedFiles))
foreach (const Core::MimeGlobPattern &glob, globs)
if (glob.regExp().exactMatch(filePath))
if (glob.matches(filePath))
projectInfo.sourceFiles << filePath;
activeTarget = project->activeTarget();
}
......@@ -216,9 +220,9 @@ QStringList QmlJSTools::qmlAndJsGlobPatterns()
QStringList pattern;
foreach (const Core::MimeGlobPattern &glob, jsSourceTy.globPatterns())
pattern << glob.regExp().pattern();
pattern << glob.pattern();
foreach (const Core::MimeGlobPattern &glob, qmlSourceTy.globPatterns())
pattern << glob.regExp().pattern();
pattern << glob.pattern();
} else {
pattern << QLatin1String("*.qml") << QLatin1String("*.js");
}
......
......@@ -95,7 +95,7 @@ bool QNXPlugin::initialize(const QStringList &arguments, QString *errorString)
addAutoReleasedObject(new QnxRunConfigurationFactory);
// bar-descriptor.xml editor
Core::MimeGlobPattern barDescriptorGlobPattern(QRegExp(QLatin1String("*.xml"), Qt::CaseInsensitive, QRegExp::Wildcard), Core::MimeGlobPattern::MinWeight + 1);
Core::MimeGlobPattern barDescriptorGlobPattern(QLatin1String("*.xml"), Core::MimeGlobPattern::MinWeight + 1);
Core::MimeType barDescriptorMimeType;
barDescriptorMimeType.setType(QLatin1String(Constants::QNX_BAR_DESCRIPTOR_MIME_TYPE));
barDescriptorMimeType.setComment(tr("Bar descriptor file (BlackBerry)"));
......
......@@ -278,8 +278,7 @@ void ManagerProcessor::process(QFutureInterface<QPair<Manager::RegisterData,
else
continue;
}
QRegExp regExp(pattern, Qt::CaseSensitive, QRegExp::Wildcard);
globPatterns.append(Core::MimeGlobPattern(regExp, 50));
globPatterns.append(Core::MimeGlobPattern(pattern, 50));
}
}
mimeType.setGlobPatterns(globPatterns);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment