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

Unified Diff: compiled/RegExpFilter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Back to manual approach for API Created Jan. 18, 2016, 12:41 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/RegExpFilter.h ('k') | compiled/WhiteListFilter.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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), &param, 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);
+}
« no previous file with comments | « compiled/RegExpFilter.h ('k') | compiled/WhiteListFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld