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