File indexing completed on 2024-04-14 14:23:10

0001 /* This file is part of the KDE project
0002 
0003    Copyright (C) 2005 Ivor Hewitt     <ivor@kde.org>
0004    Copyright (C) 2008 Maksim Orlovich <maksim@kde.org>
0005    Copyright (C) 2008 Vyacheslav Tokarev <tsjoker@gmail.com>
0006 
0007    This library is free software; you can redistribute it and/or
0008    modify it under the terms of the GNU Library General Public
0009    License as published by the Free Software Foundation; either
0010    version 2 of the License, or (at your option) any later version.
0011 
0012    This library is distributed in the hope that it will be useful,
0013    but WITHOUT ANY WARRANTY; without even the implied warranty of
0014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015    Library General Public License for more details.
0016 
0017    You should have received a copy of the GNU Library General Public License
0018    along with this library; see the file COPYING.LIB.  If not, write to
0019    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020    Boston, MA 02110-1301, USA.
0021 */
0022 
0023 #include <QString>
0024 #include <QRegExp>
0025 #include <QVector>
0026 #include <wtf/HashMap.h>
0027 #include <QHash>
0028 #include <QBitArray>
0029 
0030 namespace khtml
0031 {
0032 
0033 // Updateable Multi-String Matcher based on Rabin-Karp's algorithm
0034 class StringsMatcher
0035 {
0036 public:
0037     // add filter to matching set
0038     void addString(const QString &pattern);
0039 
0040     // check if string matches at least one string from matching set,
0041     // optionally return the matching string or filter
0042     bool isMatched(const QString &str, QString *by = nullptr) const;
0043 
0044     // add filter to matching set with wildcards (*,?) in it
0045     void addWildedString(const QString &prefix, const QRegExp &rx);
0046 
0047     void clear();
0048 
0049 private:
0050     QVector<QString> stringFilters;
0051     QVector<QString> shortStringFilters;
0052     QVector<QRegExp> reFilters;
0053     QVector<QString> rePrefixes;
0054     QBitArray fastLookUp;
0055 
0056     WTF::HashMap<int, QVector<int> > stringFiltersHash;
0057 };
0058 
0059 // This represents a set of filters that may match URLs.
0060 // Currently it supports a subset of AddBlock Plus functionality.
0061 class FilterSet
0062 {
0063 public:
0064     // Parses and registers a filter. This will also strip @@ for exclusion rules, skip comments, etc.
0065     // The user does have to split black and white lists into separate sets, however
0066     void addFilter(const QString &filter);
0067 
0068     bool isUrlMatched(const QString &url);
0069     QString urlMatchedBy(const QString &url);
0070 
0071     void clear();
0072 
0073 private:
0074     QVector<QRegExp> reFilters;
0075     StringsMatcher stringFiltersMatcher;
0076 };
0077 
0078 }
0079