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

Side by Side Diff: compiled/ActiveFilter.cpp

Issue 29333474: Issue 4125 - [emscripten] Convert filter classes to C++ (Closed)
Patch Set: Addressed comments, made String class slightly more sane, slightly cleaned up bindings.cpp Created Feb. 2, 2016, 5:48 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/ActiveFilter.h ('k') | compiled/CSSPropertyFilter.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 <cstdio>
2
3 #include "ActiveFilter.h"
4 #include "StringScanner.h"
5
6 namespace
7 {
8 void ProcessDomain(String& docDomain, bool ignoreTrailingDot)
9 {
10 String::size_type len = docDomain.length();
11 if (ignoreTrailingDot)
12 {
13 for (; len > 0; --len)
14 {
15 if (docDomain[len - 1] != u'.')
16 break;
17 }
18 }
19
20 for (String::size_type i = 0; i < len; ++i)
21 {
22 String::value_type currChar = docDomain[i];
23 // TODO: This needs to work for non-ASCII as well
24 if (currChar >= u'A' && currChar <= u'Z')
25 docDomain[i] = currChar + u'a' - u'A';
26 }
27
28 docDomain.reset(docDomain, 0, len);
29 }
30
31 String to_string(unsigned int i)
32 {
33 char buffer[11];
34 int len = sprintf(buffer, "%u", i);
35 return std::move(String(buffer, len).ensure_own_buffer());
36 }
37 }
38
39 ActiveFilter::ActiveFilter(const String& text, bool ignoreTrailingDot)
40 : Filter(text), mDisabled(false), mHitCount(0), mLastHit(0),
41 mIgnoreTrailingDot(ignoreTrailingDot)
42 {
43 }
44
45 void ActiveFilter::ParseDomains(String& str, String::size_type pos,
46 String::size_type len, String::value_type separator)
47 {
48 String domains(str, pos, len);
49 DomainMap::size_type count = 2;
50 for (String::size_type i = 0; i < domains.length(); i++)
51 if (domains[i] == separator)
52 count++;
53
54 mDomains.reset(new DomainMap(count));
55 annotate_address(mDomains.get(), "DomainMap");
56
57 StringScanner scanner(domains, separator);
58 String::size_type start = 0;
59 bool reverse = false;
60 bool hasIncludes = false;
61 bool done = false;
62 while (!done)
63 {
64 done = scanner.done();
65 String::value_type currChar = scanner.next();
66 if (currChar == u'~' && scanner.position() == start)
67 {
68 start++;
69 reverse = true;
70 }
71 else if (currChar == separator)
72 {
73 if (scanner.position() > start)
74 {
75 String domain(domains, start, scanner.position() - start);
76 ProcessDomain(domain, mIgnoreTrailingDot);
77
78 enter_context("Adding to ActiveFilter.mDomains");
79 (*mDomains)[domain] = !reverse;
80 exit_context();
81
82 if (!reverse)
83 hasIncludes = true;
84 }
85 start = scanner.position() + 1;
86 reverse = false;
87 }
88 }
89 enter_context("Adding to ActiveFilter.mDomains");
90 (*mDomains)[u""_str] = !hasIncludes;
91 exit_context();
92 }
93
94 void ActiveFilter::AddSitekey(const String& sitekey)
95 {
96 if (!mSitekeys)
97 {
98 mSitekeys.reset(new SitekeySet());
99 annotate_address(mSitekeys.get(), "SitekeySet");
100 }
101
102 enter_context("Adding to ActiveFilter.mSitekeys");
103 mSitekeys->insert(sitekey);
104 exit_context();
105 }
106
107 bool ActiveFilter::IsActiveOnDomain(String& docDomain, const String& sitekey) co nst
108 {
109 if (mSitekeys && mSitekeys->find(sitekey) == mSitekeys->end())
110 return false;
111
112 // If no domains are set the rule matches everywhere
113 if (!mDomains)
114 return true;
115
116 // If the document has no host name, match only if the filter isn't restricted
117 // to specific domains
118 if (docDomain.empty())
119 return (*mDomains)[u""_str];
120
121 ProcessDomain(docDomain, mIgnoreTrailingDot);
122 while (true)
123 {
124 auto it = mDomains->find(docDomain);
125 if (it != mDomains->end())
126 return it->second;
127
128 String::size_type nextDot = docDomain.find(u'.');
129 if (nextDot == docDomain.npos)
130 break;
131 docDomain.reset(docDomain, nextDot + 1);
132 }
133 return (*mDomains)[u""_str];
134 }
135
136 bool ActiveFilter::IsActiveOnlyOnDomain(String& docDomain) const
137 {
138 if (!mDomains || docDomain.empty() || (*mDomains)[u""_str])
139 return false;
140
141 ProcessDomain(docDomain, mIgnoreTrailingDot);
142 for (auto it = mDomains->begin(); it != mDomains->end(); ++it)
143 {
144 if (!it->second || it->first.equals(docDomain))
145 continue;
146
147 size_t len1 = it->first.length();
148 size_t len2 = docDomain.length();
149 if (len1 > len2 &&
150 String(it->first, len1 - len2).equals(docDomain) &&
151 it->first[len1 - len2 - 1] == u'.')
152 {
153 continue;
154 }
155
156 return false;
157 }
158 return true;
159 }
160
161 bool ActiveFilter::IsGeneric() const
162 {
163 return !mSitekeys && (!mDomains || (*mDomains)[u""_str]);
164 }
165
166 String ActiveFilter::Serialize() const
167 {
168 /* TODO this is very inefficient */
169 String result(Filter::Serialize());
170 if (mDisabled)
171 result.append(u"disabled=true\n"_str);
172 if (mHitCount)
173 {
174 result.append(u"hitCount="_str);
175 result.append(to_string(mHitCount));
176 result.append(u'\n');
177 }
178 if (mLastHit)
179 {
180 result.append(u"lastHit="_str);
181 result.append(to_string(mLastHit));
182 result.append(u'\n');
183 }
184 return std::move(result.ensure_own_buffer());
185 }
OLDNEW
« no previous file with comments | « compiled/ActiveFilter.h ('k') | compiled/CSSPropertyFilter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld