Index: compiled/Filter.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/compiled/Filter.cpp |
@@ -0,0 +1,118 @@ |
+#include <emscripten.h> |
+#include <unordered_map> |
+ |
+#include "Filter.h" |
+#include "CommentFilter.h" |
+#include "RegExpFilter.h" |
+#include "ElemHideFilter.h" |
+#include "ElemHideException.h" |
+#include "tools.h" |
+ |
+Filter::Filter(const std::u16string& text) |
+{ |
+ this->text = text; |
+} |
+ |
+const std::u16string Filter::Serialize() |
+{ |
+ return ( |
+ u"[Filter]\n" |
+ u"text=" + text + u"\n" |
+ ); |
+} |
+ |
+namespace |
+{ |
+ std::unordered_map<std::u16string,Filter*> knownFilters; |
+ |
+ void trim_spaces(std::u16string& str) |
+ { |
+ size_t pos; |
+ |
+ // Remove leading whitespace |
+ pos = str.find_first_not_of(u' '); |
+ if (pos > 0) |
+ str.erase(0, pos); |
+ |
+ // Remove trailing whitespace |
+ pos = str.find_last_not_of(u' '); |
+ if (pos < str.length() - 1) |
+ str.erase(pos + 1); |
+ } |
+ |
+ void remove_spaces(std::u16string& str) |
+ { |
+ for (size_t i = 0, l = str.length(); i < l; ++i) |
+ { |
+ if (str[i] == u' ') |
+ { |
+ str.erase(i, 1); |
+ --i; |
+ --l; |
+ } |
+ } |
+ } |
+} |
+ |
+extern "C" |
+{ |
+ Filter* EMSCRIPTEN_KEEPALIVE Filter_FromText(char16_t* textPtr, int textLen) |
+ { |
+ std::u16string text(textPtr, textLen); |
+ auto it = knownFilters.find(text); |
+ if (it != knownFilters.end()) |
+ return it->second; |
+ |
+ Filter* filter = CommentFilter::Create(text); |
+ if (!filter) |
+ filter = ElemHideBase::Create(text); |
+ if (!filter) |
+ filter = RegExpFilter::Create(text); |
+ return knownFilters[text] = filter; |
+ } |
+ char16_t* EMSCRIPTEN_KEEPALIVE Filter_Normalize(char16_t* textPtr, size_t textLen, size_t* resultLen) |
+ { |
+ std::u16string text(textPtr, textLen); |
+ |
+ // Remove special characters like line breaks |
+ for (size_t i = 0, l = text.length(); i < l; ++i) |
+ { |
+ if (text[i] < u' ') |
+ { |
+ text.erase(i, 1); |
+ --i; |
+ --l; |
+ } |
+ } |
+ |
+ trim_spaces(text); |
+ |
+ { |
+ size_t domainsEnd; |
+ size_t selectorStart; |
+ Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart); |
+ if (type != Filter::Type::UNKNOWN) |
+ { |
+ std::u16string domains = text.substr(0, domainsEnd); |
+ std::u16string selector = text.substr(selectorStart); |
+ remove_spaces(domains); |
+ trim_spaces(selector); |
+ return stringToBuffer(domains + (type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##") + selector, resultLen); |
+ } |
+ } |
+ |
+ if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN) |
+ remove_spaces(text); |
+ return stringToBuffer(text, resultLen); |
+ } |
+ |
+ Filter::Type EMSCRIPTEN_KEEPALIVE Filter_get_type(Filter* filter) |
+ { |
+ return filter->GetType(); |
+ } |
+ |
+ char16_t* EMSCRIPTEN_KEEPALIVE Filter_Serialize(Filter* filter, size_t* resultLen) |
+ { |
+ return stringToBuffer(filter->Serialize(), resultLen); |
+ } |
+} |