1/* This file is part of the KDE project
2
3 Copyright (C) 2005 Ivor Hewitt <ivor@kde.org>
4 Copyright (C) 2008 Maksim Orlovich <maksim@kde.org>
5 Copyright (C) 2008 Vyacheslav Tokarev <tsjoker@gmail.com>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21*/
22
23#include <QString>
24#include <QRegExp>
25#include <QVector>
26#include <wtf/HashMap.h>
27#include <QHash>
28#include <QBitArray>
29
30namespace khtml {
31
32// Updateable Multi-String Matcher based on Rabin-Karp's algorithm
33class StringsMatcher {
34public:
35 // add filter to matching set
36 void addString(const QString& pattern);
37
38 // check if string matches at least one string from matching set,
39 // optionally return the matching string or filter
40 bool isMatched(const QString& str, QString *by = 0) const;
41
42 // add filter to matching set with wildcards (*,?) in it
43 void addWildedString(const QString& prefix, const QRegExp& rx);
44
45 void clear();
46
47private:
48 QVector<QString> stringFilters;
49 QVector<QString> shortStringFilters;
50 QVector<QRegExp> reFilters;
51 QVector<QString> rePrefixes;
52 QBitArray fastLookUp;
53
54 WTF::HashMap<int, QVector<int> > stringFiltersHash;
55};
56
57// This represents a set of filters that may match URLs.
58// Currently it supports a subset of AddBlock Plus functionality.
59class FilterSet {
60public:
61 // Parses and registers a filter. This will also strip @@ for exclusion rules, skip comments, etc.
62 // The user does have to split black and white lists into separate sets, however
63 void addFilter(const QString& filter);
64
65 bool isUrlMatched(const QString& url);
66 QString urlMatchedBy(const QString& url);
67
68 void clear();
69
70private:
71 QVector<QRegExp> reFilters;
72 StringsMatcher stringFiltersMatcher;
73};
74
75}
76
77// kate: indent-width 4; replace-tabs on; tab-width 4; space-indent on;
78