Index: compiled/RegExpFilter.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/compiled/RegExpFilter.cpp |
@@ -0,0 +1,72 @@ |
+#include <emscripten.h> |
+ |
+#include "RegExpFilter.h" |
+#include "WhiteListFilter.h" |
+#include "InvalidFilter.h" |
+ |
+RegExpFilter::RegExpFilter(const std::u16string& text, |
+ const std::u16string& pattern, const std::u16string& options) |
+ : ActiveFilter(text), regexpId(0) |
+{ |
+ size_t len = pattern.length(); |
+ if (len >= 2 && pattern[0] == u'/' && pattern[len - 1] == u'/') |
+ { |
+ std::u16string param = pattern.substr(1, len - 2); |
+ regexpId = EM_ASM_INT(return regexps.create($0, $1), ¶m, false); |
+ |
+ std::u16string* error = reinterpret_cast<std::u16string*>(EM_ASM_INT(return regexps.getError($0), regexpId)); |
+ if (error) |
+ { |
+ EM_ASM_ARGS(regexps.delete($0), regexpId); |
+ throw std::u16string(*error); |
+ } |
+ } |
+ else |
+ regexpSource = pattern; |
+} |
+ |
+RegExpFilter::~RegExpFilter() |
+{ |
+ if (regexpId) |
+ EM_ASM_ARGS(regexps.delete($0), regexpId); |
+} |
+ |
+Filter* RegExpFilter::Create(const std::u16string& text) |
+{ |
+ bool blocking = true; |
+ size_t patternStart = 0; |
+ if (!text.compare(0, 2, u"@@")) |
+ { |
+ blocking = false; |
+ patternStart = 2; |
+ } |
+ |
+ size_t patternEnd = text.find(u'$', patternStart); |
+ size_t patternLength = (patternEnd != std::u16string::npos ? |
+ patternEnd - patternStart : patternEnd); |
+ std::u16string pattern(text.substr(patternStart, patternLength)); |
+ std::u16string options(patternEnd != std::u16string::npos ? |
+ text.substr(patternEnd) : u""); |
+ |
+ try |
+ { |
+ if (blocking) |
+ return new RegExpFilter(text, pattern, options); |
+ else |
+ return new WhiteListFilter(text, pattern, options); |
+ } |
+ catch (const std::u16string& reason) |
+ { |
+ return new InvalidFilter(text, reason); |
+ } |
+} |
+ |
+Filter::Type RegExpFilter::GetType() const |
+{ |
+ return Type::BLOCKING; |
+} |
+ |
+bool RegExpFilter::Matches(const std::u16string& location) |
+{ |
+ return EM_ASM_INT(return regexps.test($0, $1), regexpId, &location); |
+} |