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

Unified Diff: compiled/ElemHideBase.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Using embind Created Jan. 15, 2016, 8:44 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
« no previous file with comments | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/ElemHideBase.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/ElemHideBase.cpp
@@ -0,0 +1,117 @@
+#include <emscripten/bind.h>
+
+#include "ElemHideBase.h"
+#include "ElemHideFilter.h"
+#include "ElemHideException.h"
+
+namespace
+{
+ class Scanner
+ {
+ private:
+ const std::wstring& str;
+ size_t pos;
+ size_t end;
+ public:
+ Scanner(const std::wstring& str) : str(str), pos(0), end(str.length()) {}
+
+ bool done()
+ {
+ return pos >= end;
+ }
+
+ size_t position()
+ {
+ return pos - 1;
+ }
+
+ wchar_t next()
+ {
+ return done() ? L'\0' : str[pos++];
+ }
+ };
+}
+
+ElemHideBase::ElemHideBase(const std::wstring& text, size_t domainsEnd,
+ size_t selectorStart)
+ : ActiveFilter(text)
+{
+}
+
+Filter::Type ElemHideBase::Parse(const std::wstring& text, size_t* domainsEnd,
+ size_t* selectorStart)
+{
+ Scanner scanner(text);
+
+ // Domains part
+ loop:
+ while (!scanner.done())
+ {
+ wchar_t next = scanner.next();
+ if (next == L'#')
+ {
+ *domainsEnd = scanner.position();
+ break;
+ }
+
+ switch (next)
+ {
+ case L'/':
+ case L'*':
+ case L'|':
+ case L'@':
+ case L'"':
+ case L'!':
+ return Type::UNKNOWN;
+ }
+ }
+
+ bool exception = false;
+ wchar_t next = scanner.next();
+ if (next == L'@')
+ {
+ exception = true;
+ next = scanner.next();
+ }
+
+ if (next != L'#')
+ return Type::UNKNOWN;
+
+ // Selector part
+
+ // Selector shouldn't be empty
+ if (scanner.done())
+ return Type::UNKNOWN;
+
+ *selectorStart = scanner.position() + 1;
+ while (!scanner.done())
+ {
+ switch (scanner.next())
+ {
+ case L'{':
+ case L'}':
+ return Type::UNKNOWN;
+ }
+ }
+
+ return exception ? Type::ELEMHIDEEXCEPTION : Type::ELEMHIDE;
+}
+
+ElemHideBase* ElemHideBase::Create(const std::wstring& text)
+{
+ size_t domainsEnd;
+ size_t selectorStart;
+ Type type = Parse(text, &domainsEnd, &selectorStart);
+ if (type == Type::UNKNOWN)
+ return nullptr;
+ else if (type == Type::ELEMHIDEEXCEPTION)
+ return new ElemHideException(text, domainsEnd, selectorStart);
+ else
+ return new ElemHideFilter(text, domainsEnd, selectorStart);
+}
+
+EMSCRIPTEN_BINDINGS(elemhidebase)
+{
+ using namespace emscripten;
+ class_<ElemHideBase,base<ActiveFilter>>("ElemHideBase");
+}
« no previous file with comments | « compiled/ElemHideBase.h ('k') | compiled/ElemHideException.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld