From fc216c5bd5181191c867eb6b6ec45bf252c4a074 Mon Sep 17 00:00:00 2001 From: Benjamin Poulain Date: Wed, 18 Aug 2010 01:28:43 +0200 Subject: Cleaning of the constructor of AdBlockRule Basic cleaning and bug fixing of AdBlockRule. The options shoud be parsed before checking if the filter is a regexp, otherwhise a regexp with option will never match the rule. For example: -"/.*/$xmlhttprequest" is a valid regexp that would not be matched. -"/.*$xmlhttprequest/" is a valid regexp without options and the option should not have been parsed. The matching of the option should match the last index of "$", not the first, for the same reason as above. Use mid() to split the vector at once instead QString::mid() + QString::lef(). Clean the coding style to follow the conventions of KDE. --- src/adblock/adblockrule.cpp | 36 +++++++++++++++--------------------- 1 file changed, 15 insertions(+), 21 deletions(-) (limited to 'src/adblock') diff --git a/src/adblock/adblockrule.cpp b/src/adblock/adblockrule.cpp index 6ff98f03..b47f8bb8 100644 --- a/src/adblock/adblockrule.cpp +++ b/src/adblock/adblockrule.cpp @@ -59,38 +59,32 @@ #include #include +static inline bool isRegExpFilter(const QString &filter) +{ + return filter.startsWith(QL1C('/')) && filter.endsWith(QL1C('/')); +} AdBlockRule::AdBlockRule(const QString &filter) { - bool isRegExpRule = false; + m_regExp.setCaseSensitivity(Qt::CaseInsensitive); + m_regExp.setPatternSyntax(QRegExp::RegExp2); QString parsedLine = filter; - if (parsedLine.startsWith(QL1C('/')) && parsedLine.endsWith(QL1C('/'))) - { - parsedLine = parsedLine.mid(1); - parsedLine = parsedLine.left(parsedLine.size() - 1); - isRegExpRule = true; - } - - int optionsNumber = parsedLine.indexOf(QL1C('$'), 0); - QStringList options; - - if (optionsNumber >= 0) - { - options = parsedLine.mid(optionsNumber + 1).split(QL1C(',')); + const int optionsNumber = parsedLine.lastIndexOf(QL1C('$')); + if (optionsNumber >= 0 && !isRegExpFilter(parsedLine)) { + const QStringList options(parsedLine.mid(optionsNumber + 1).split(QL1C(','))); + if (options.contains(QL1S("match-case"))) + m_regExp.setCaseSensitivity(Qt::CaseSensitive); parsedLine = parsedLine.left(optionsNumber); } - if (!isRegExpRule) + if (isRegExpFilter(parsedLine)) + parsedLine = parsedLine.mid(1, parsedLine.length() - 2); + else parsedLine = convertPatternToRegExp(parsedLine); - m_regExp = QRegExp(parsedLine, Qt::CaseInsensitive, QRegExp::RegExp2); - - if (options.contains(QL1S("match-case"))) - { - m_regExp.setCaseSensitivity(Qt::CaseSensitive); - } + m_regExp.setPattern(parsedLine); } -- cgit v1.2.1