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: Minor improvements Created Jan. 20, 2016, 2: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/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: compiled/Filter.cpp
===================================================================
new file mode 100644
--- /dev/null
+++ b/compiled/Filter.cpp
@@ -0,0 +1,105 @@
+#include <unordered_map>
+
+#include "Filter.h"
+#include "CommentFilter.h"
+#include "RegExpFilter.h"
+#include "ElemHideFilter.h"
+#include "ElemHideException.h"
+
+namespace
+{
+ std::unordered_map<std::u16string,FilterPtr> 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;
+ }
+ }
+ }
+}
+
+Filter::Filter(const std::u16string& text)
+{
+ this->text = text;
+}
+
+const std::u16string Filter::Serialize()
+{
+ return (
+ u"[Filter]\n"
+ u"text=" + text + u"\n"
+ );
+}
+
+FilterPtr Filter::FromText(const std::u16string& text)
+{
+ auto it = knownFilters.find(text);
+ if (it != knownFilters.end())
+ return it->second;
+
+ FilterPtr filter = FilterPtr(CommentFilter::Create(text));
+ if (!filter)
+ filter.reset(ElemHideBase::Create(text));
+ if (!filter)
+ filter.reset(RegExpFilter::Create(text));
+ return knownFilters[text] = filter;
+}
+
+const std::u16string Filter::Normalize(const std::u16string& text)
+{
+ std::u16string result(text);
+
+ // Remove special characters like line breaks
+ for (size_t i = 0, l = result.length(); i < l; ++i)
+ {
+ if (result[i] < u' ')
+ {
+ result.erase(i, 1);
+ --i;
+ --l;
+ }
+ }
+
+ trim_spaces(result);
+
+ {
+ size_t domainsEnd;
+ size_t selectorStart;
+ Filter::Type type = ElemHideBase::Parse(result, &domainsEnd, &selectorStart);
+ if (type != Filter::Type::UNKNOWN)
+ {
+ std::u16string domains = result.substr(0, domainsEnd);
+ std::u16string selector = result.substr(selectorStart);
+ remove_spaces(domains);
+ trim_spaces(selector);
+ return domains + (
+ type == Filter::Type::ELEMHIDEEXCEPTION ? u"#@#" : u"##"
+ ) + selector;
+ }
+ }
+
+ if (CommentFilter::Parse(result) == Filter::Type::UNKNOWN)
+ remove_spaces(result);
+ return result;
+}
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld