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

Side by Side Diff: compiled/Filter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Improved performance Created Jan. 28, 2016, 2:31 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « compiled/Filter.h ('k') | compiled/InvalidFilter.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #include "Filter.h"
2 #include "CommentFilter.h"
3 #include "RegExpFilter.h"
4 #include "ElemHideFilter.h"
5 #include "ElemHideException.h"
6 #include "StringMap.h"
7
8 namespace
9 {
10 /* TODO: This is a memory leak. Store raw/weak pointers as soon as we are
11 actually using the filters somewhere. */
12 StringMap<FilterPtr> knownFilters(8192);
13
14 void trim_spaces(String& str)
15 {
16 String::size_type pos;
17
18 // Remove leading whitespace
19 for (pos = 0; pos < str.length(); ++pos)
20 if (str[pos] != u' ')
21 break;
22 str.reset(str, pos);
23
24 // Remove trailing whitespace
25 for (pos = str.length(); pos > 0; --pos)
26 if (str[pos - 1] != u' ')
27 break;
28 str.reset(str, 0, pos);
29 }
30
31 void remove_spaces(String& str)
32 {
33 String::size_type pos;
34
35 for (String::size_type i = 0; i < str.length(); ++i)
36 if (str[i] != u' ')
37 str[pos++] = str[i];
38
39 str.reset(str, 0, pos);
40 }
41 }
42
43 Filter::Filter(const String& text)
44 : mText(text)
45 {
46 annotate_address(this, "Filter");
47 mText.ensure_own_buffer();
48 }
49
50 String Filter::Serialize()
51 {
52 String result(u"[Filter]\ntext="_str);
53 result.append(mText);
54 result.append(u'\n');
55 return std::move(result);
56 }
57
58 FilterPtr Filter::FromText(const String& text)
59 {
60 auto it = knownFilters.find(text);
61 if (it != knownFilters.end())
62 return it->second;
63
64 FilterPtr filter = FilterPtr(CommentFilter::Create(text));
65 if (!filter)
66 filter.reset(ElemHideBase::Create(text));
67 if (!filter)
68 filter.reset(RegExpFilter::Create(text));
69
70 enter_context("Adding to known filters");
71 knownFilters[filter->mText] = filter;
72 exit_context();
73 return filter;
74 }
75
76 String Filter::Normalize(String& text)
77 {
78 // Removing special characters like line breaks
79 String::size_type delta = 0;
80 for (String::size_type i = 0; i < text.length(); ++i)
81 {
82 if (text[i] >= u' ')
83 text[i - delta] = text[i];
84 else
85 ++delta;
86 }
87 text.reset(text, 0, text.length() - delta);
88
89 trim_spaces(text);
90
91 {
92 String::size_type domainsEnd;
93 String::size_type selectorStart;
94 Filter::Type type = ElemHideBase::Parse(text, &domainsEnd, &selectorStart);
95 if (type != Filter::Type::UNKNOWN)
96 {
97 String domains(text, 0, domainsEnd);
98 String selector(text, selectorStart);
99 remove_spaces(domains);
100 trim_spaces(selector);
101
102 String::size_type domainsDelta = domainsEnd - domains.length();
103 String::size_type selectorDelta = text.length() - selectorStart -
104 selector.length();
105
106 if (domainsDelta)
107 for (String::size_type i = domainsEnd; i < selectorStart; ++i)
108 text[i - domainsDelta] = text[i];
109
110 if (domainsDelta + selectorDelta)
111 for (String::size_type i = 0; i < selector.length(); ++i)
112 text[selectorStart - domainsDelta + i] = selector[i];
113
114 text.reset(text, 0, text.length() - domainsDelta - selectorDelta);
115 return text;
116 }
117 }
118
119 if (CommentFilter::Parse(text) == Filter::Type::UNKNOWN)
120 remove_spaces(text);
121 return text;
122 }
OLDNEW
« 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