File indexing completed on 2024-04-28 07:40:09
0001 /* 0002 This file is part of the KDE Baloo project. 0003 SPDX-FileCopyrightText: 2010 Sebastian Trueg <trueg@kde.org> 0004 SPDX-FileCopyrightText: 2014 Vishesh Handa <me@vhanda.in> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "regexpcache.h" 0010 #include "baloodebug.h" 0011 0012 #include <QStringList> 0013 0014 RegExpCache::RegExpCache() 0015 { 0016 } 0017 0018 RegExpCache::~RegExpCache() 0019 { 0020 } 0021 0022 bool RegExpCache::exactMatch(const QString& s) const 0023 { 0024 if (m_exactMatches.contains(s)) { 0025 return true; 0026 } 0027 for (const QRegularExpression& filter : std::as_const(m_regexpCache)) { 0028 if (filter.match(s).hasMatch()) { 0029 return true; 0030 } 0031 } 0032 return false; 0033 } 0034 0035 void RegExpCache::rebuildCacheFromFilterList(const QStringList& filters) 0036 { 0037 m_regexpCache.clear(); 0038 m_exactMatches.clear(); 0039 0040 // RE matching "*.foo" style patterns 0041 QRegularExpression suffixOnlyRe(QStringLiteral("^\\*\\.([^.\\*\\?]+)$")); 0042 QStringList suffixes; 0043 0044 for (const QString& filter : filters) { 0045 QString f = filter; 0046 if (!f.contains(QLatin1Char('*')) && !f.contains(QLatin1Char('?'))) { 0047 m_exactMatches += f; 0048 continue; 0049 } 0050 auto m = suffixOnlyRe.match(f); 0051 if (m.hasMatch()) { 0052 // qCDebug(BALOO) << "filter is suffix match:" << m; 0053 suffixes += m.captured(1); 0054 continue; 0055 } 0056 f.replace(QLatin1Char('.'), QStringLiteral("\\.")); 0057 f.replace(QLatin1Char('?'), QLatin1Char('.')); 0058 f.replace(QStringLiteral("*"), QStringLiteral(".*")); 0059 f = QLatin1String("^") + f + QLatin1String("$"); 0060 0061 m_regexpCache.append(QRegularExpression(f)); 0062 } 0063 0064 // Combine all suffixes into one large RE: "^.*(foo|bar|baz)$" 0065 QString suffixMatch = QStringLiteral("^.*\\.(") 0066 + suffixes.join(QLatin1Char('|')) 0067 + QStringLiteral(")$"); 0068 // qCDebug(BALOO) << suffixMatch; 0069 m_regexpCache.prepend(QRegularExpression(suffixMatch)); 0070 }