Baloo

regexpcache.cpp
1 /*
2  This file is part of the KDE Baloo project.
3  SPDX-FileCopyrightText: 2010 Sebastian Trueg <[email protected]>
4  SPDX-FileCopyrightText: 2014 Vishesh Handa <[email protected]>
5 
6  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
7 */
8 
9 #include "regexpcache.h"
10 #include "baloodebug.h"
11 
12 #include <QStringList>
13 
14 RegExpCache::RegExpCache()
15 {
16 }
17 
18 RegExpCache::~RegExpCache()
19 {
20 }
21 
22 bool RegExpCache::exactMatch(const QString& s) const
23 {
24  if (m_exactMatches.contains(s)) {
25  return true;
26  }
27  for (const QRegularExpression& filter : std::as_const(m_regexpCache)) {
28  if (filter.match(s).hasMatch()) {
29  return true;
30  }
31  }
32  return false;
33 }
34 
35 void RegExpCache::rebuildCacheFromFilterList(const QStringList& filters)
36 {
37  m_regexpCache.clear();
38  m_exactMatches.clear();
39 
40  // RE matching "*.foo" style patterns
41  QRegularExpression suffixOnlyRe(QStringLiteral("^\\*\\.([^.\\*\\?]+)$"));
42  QStringList suffixes;
43 
44  for (const QString& filter : filters) {
45  QString f = filter;
46  if (!f.contains(QLatin1Char('*')) && !f.contains(QLatin1Char('?'))) {
47  m_exactMatches += f;
48  continue;
49  }
50  auto m = suffixOnlyRe.match(f);
51  if (m.hasMatch()) {
52  // qCDebug(BALOO) << "filter is suffix match:" << m;
53  suffixes += m.captured(1);
54  continue;
55  }
56  f.replace(QLatin1Char('.'), QStringLiteral("\\."));
57  f.replace(QLatin1Char('?'), QLatin1Char('.'));
58  f.replace(QStringLiteral("*"), QStringLiteral(".*"));
59  f = QLatin1String("^") + f + QLatin1String("$");
60 
61  m_regexpCache.append(QRegularExpression(f));
62  }
63 
64  // Combine all suffixes into one large RE: "^.*(foo|bar|baz)$"
65  QString suffixMatch = QStringLiteral("^.*\\.(")
66  + suffixes.join(QLatin1Char('|'))
67  + QStringLiteral(")$");
68  // qCDebug(BALOO) << suffixMatch;
69  m_regexpCache.prepend(QRegularExpression(suffixMatch));
70 }
QFuture< void > filter(Sequence &sequence, KeepFunctor filterFunction)
QString join(const QString &separator) const const
QString & replace(int position, int n, QChar after)
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.