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 from Patch Set 24 Created Dec. 6, 2016, 10:43 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #include <cstdio>
2
3 #include "ActiveFilter.h"
4 #include "StringScanner.h"
5
6 namespace
7 {
8 OwnedString to_string(unsigned int i)
9 {
10 char buffer[11];
11 int len = sprintf(buffer, "%u", i);
12
13 OwnedString result(len);
14 for (String::size_type i = 0; i < len; i++)
15 result[i] = buffer[i];
16 return result;
17 }
18 }
19
20 ActiveFilter::ActiveFilter(Type type, const String& text, bool ignoreTrailingDot )
21 : Filter(type, text), mDisabled(false), mHitCount(0), mLastHit(0),
22 mIgnoreTrailingDot(ignoreTrailingDot)
23 {
24 }
25
26 ActiveFilter::DomainMap* ActiveFilter::GetDomains() const
27 {
28 return mDomains.get();
29 }
30
31 ActiveFilter::SitekeySet* ActiveFilter::GetSitekeys() const
32 {
33 return mSitekeys.get();
34 }
35
36 void ActiveFilter::ParseDomains(const String& domains,
37 String::value_type separator) const
38 {
39 DomainMap::size_type count = 2;
40 for (String::size_type i = 0; i < domains.length(); i++)
41 if (domains[i] == separator)
42 count++;
43
44 mDomains.reset(new DomainMap(count));
45 annotate_address(mDomains.get(), "DomainMap");
46
47 StringScanner scanner(domains, 0, separator);
48 String::size_type start = 0;
49 bool reverse = false;
50 bool hasIncludes = false;
51 bool done = false;
sergei 2017/01/10 15:57:29 BTW, it can be initialized to scanner.done() becau
Wladimir Palant 2017/03/13 17:41:51 Done.
52 while (!done)
53 {
54 done = scanner.done();
55 String::value_type currChar = scanner.next();
56 if (currChar == u'~' && scanner.position() == start)
57 {
58 start++;
59 reverse = true;
60 }
61 else if (currChar == separator)
62 {
63 String::size_type len = scanner.position() - start;
64 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.')
65 len--;
66 if (len > 0)
67 {
68 enter_context("Adding to ActiveFilter.mDomains");
69 (*mDomains)[DependentString(domains, start, len)] = !reverse;
70 exit_context();
71
72 if (!reverse)
73 hasIncludes = true;
74 }
75 start = scanner.position() + 1;
76 reverse = false;
77 }
78 }
79 enter_context("Adding to ActiveFilter.mDomains");
80 (*mDomains)[u""_str] = !hasIncludes;
sergei 2017/01/10 15:57:31 It would simplify the reading of the code if we ha
Wladimir Palant 2017/03/13 17:41:54 Yes, it's a magic value and we shouldn't have it l
81 exit_context();
82 }
83
84 void ActiveFilter::AddSitekey(const String& sitekey) const
85 {
86 if (!mSitekeys)
87 {
88 mSitekeys.reset(new SitekeySet());
89 annotate_address(mSitekeys.get(), "SitekeySet");
90 }
91
92 enter_context("Adding to ActiveFilter.mSitekeys");
93 mSitekeys->insert(sitekey);
94 exit_context();
95 }
96
97 bool ActiveFilter::IsActiveOnDomain(DependentString& docDomain, const String& si tekey) const
98 {
99 auto sitekeys = GetSitekeys();
100 if (sitekeys && !sitekeys->find(sitekey))
101 return false;
102
103 // If no domains are set the rule matches everywhere
104 auto domains = GetDomains();
105 if (!domains)
106 return true;
107
108 // If the document has no host name, match only if the filter isn't restricted
109 // to specific domains
110 if (docDomain.empty())
111 return (*domains)[u""_str];
112
113 docDomain.tolower();
114
115 String::size_type len = docDomain.length();
116 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.')
117 docDomain.reset(docDomain, 0, len - 1);
118 while (true)
119 {
120 auto it = domains->find(docDomain);
121 if (it)
122 return it->second;
123
124 String::size_type nextDot = docDomain.find(u'.');
125 if (nextDot == docDomain.npos)
126 break;
127 docDomain.reset(docDomain, nextDot + 1);
128 }
129 return (*domains)[u""_str];
130 }
131
132 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const
133 {
134 auto domains = GetDomains();
135 if (!domains || docDomain.empty() || (*domains)[u""_str])
136 return false;
137
138 docDomain.tolower();
139
140 String::size_type len = docDomain.length();
141 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.')
142 docDomain.reset(docDomain, 0, len - 1);
143 for (auto it = domains->begin(); it != domains->end(); ++it)
144 {
145 if (!it->second || it->first.equals(docDomain))
146 continue;
147
148 size_t len1 = it->first.length();
149 size_t len2 = docDomain.length();
150 if (len1 > len2 &&
151 DependentString(it->first, len1 - len2).equals(docDomain) &&
152 it->first[len1 - len2 - 1] == u'.')
153 {
154 continue;
155 }
156
157 return false;
158 }
159 return true;
160 }
161
162 bool ActiveFilter::IsGeneric() const
163 {
164 auto sitekeys = GetSitekeys();
165 auto domains = GetDomains();
166 return !sitekeys && (!domains || (*domains)[u""_str]);
167 }
168
169 OwnedString ActiveFilter::Serialize() const
170 {
171 /* TODO this is very inefficient */
172 OwnedString result(Filter::Serialize());
173 if (mDisabled)
174 result.append(u"disabled=true\n"_str);
175 if (mHitCount)
176 {
177 result.append(u"hitCount="_str);
178 result.append(to_string(mHitCount));
179 result.append(u'\n');
180 }
181 if (mLastHit)
182 {
183 result.append(u"lastHit="_str);
184 result.append(to_string(mLastHit));
185 result.append(u'\n');
186 }
187 return result;
188 }
OLDNEW

Powered by Google App Engine
This is Rietveld