Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: compiled/Filter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Fixed some bugs and exposed/tested more properties Created Jan. 15, 2016, 3:40 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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;
+ }
+ }
+ }
+}
+
+// API
+extern "C"
+{
+ Filter* EMSCRIPTEN_KEEPALIVE Filter_FromText(char16_t* textPtr, int textLen)
Felix Dahlke 2016/01/15 17:00:36 If this stays (see my comments on the C wrappers a
+ {
+ 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);
+ }
+
+ char16_t* EMSCRIPTEN_KEEPALIVE Filter_Serialize(Filter* filter, size_t* resultLen)
+ {
+ return stringToBuffer(filter->Serialize(), resultLen);
+ }
+
+ EXPOSE_READONLY_STRPROPERTY(Filter, text)
+ EXPOSE_READONLY_PROPERTY(Filter, Filter::Type, type)
+}
« compiled/ElemHideBase.cpp ('K') | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld