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: Replaced shared_ptr by boost-like intrusive_ptr Created Jan. 28, 2016, 7:50 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,130 @@
+#include "Filter.h"
+#include "CommentFilter.h"
+#include "RegExpFilter.h"
+#include "ElemHideFilter.h"
+#include "ElemHideException.h"
+#include "StringMap.h"
+
+namespace
+{
+ StringMap<Filter*> knownFilters(8192);
+
+ void trim_spaces(String& str)
+ {
+ String::size_type pos;
+
+ // Remove leading whitespace
+ for (pos = 0; pos < str.length(); ++pos)
+ if (str[pos] != u' ')
+ break;
+ str.reset(str, pos);
+
+ // Remove trailing whitespace
+ for (pos = str.length(); pos > 0; --pos)
+ if (str[pos - 1] != u' ')
+ break;
+ str.reset(str, 0, pos);
+ }
+
+ void remove_spaces(String& str)
+ {
+ String::size_type pos;
+
+ for (String::size_type i = 0; i < str.length(); ++i)
+ if (str[i] != u' ')
+ str[pos++] = str[i];
+
+ str.reset(str, 0, pos);
+ }
+}
+
+Filter::Filter(const String& text)
+ : mText(text)
+{
+ annotate_address(this, "Filter");
+ mText.ensure_own_buffer();
+}
+
+Filter::~Filter()
+{
+ // TODO: This should be removing from knownFilters
+}
+
+String Filter::Serialize() const
+{
+ String result(u"[Filter]\ntext="_str);
+ result.append(mText);
+ result.append(u'\n');
+ return std::move(result);
+}
+
+Filter* Filter::FromText(const String& text)
+{
+ auto it = knownFilters.find(text);
+ if (it != knownFilters.end())
+ return it->second;
+
+ FilterPtr filter(CommentFilter::Create(text));
+ if (!filter)
+ filter.reset(ElemHideBase::Create(text));
+ if (!filter)
+ filter.reset(RegExpFilter::Create(text));
+
+ enter_context("Adding to known filters");
+ knownFilters[filter->mText] = filter.get();
+ exit_context();
+
+ // TODO: We intentionally leak the filter here - currently it won't be used
+ // for anything and would be deleted immediately.
+ filter->AddRef();
+
+ return filter;
+}
+
+String Filter::Normalize(String& text)
+{
+ // Removing special characters like line breaks
+ String::size_type delta = 0;
+ for (String::size_type i = 0; i < text.length(); ++i)
+ {
+ if (text[i] >= u' ')
+ text[i - delta] = text[i];
+ else
+ ++delta;
+ }
+ text.reset(text, 0, text.length() - delta);
+
+ trim_spaces(text);
+
+ {
+ String::size_type domainsEnd;
+ String::size_type selectorStart;
+ Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart);
+ if (type != Filter::Type::UNKNOWN)
+ {
+ String domains(text, 0, domainsEnd);
+ String selector(text, selectorStart);
+ remove_spaces(domains);
+ trim_spaces(selector);
+
+ String::size_type domainsDelta = domainsEnd - domains.length();
+ String::size_type selectorDelta = text.length() - selectorStart -
+ selector.length();
+
+ if (domainsDelta)
+ for (String::size_type i = domainsEnd; i < selectorStart; ++i)
+ text[i - domainsDelta] = text[i];
+
+ if (domainsDelta + selectorDelta)
+ for (String::size_type i = 0; i < selector.length(); ++i)
+ text[selectorStart - domainsDelta + i] = selector[i];
+
+ text.reset(text, 0, text.length() - domainsDelta - selectorDelta);
+ return text;
+ }
+ }
+
+ if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN)
+ remove_spaces(text);
+ return text;
+}
« 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