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

Delta Between Two Patch Sets: compiled/ActiveFilter.cpp

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

Powered by Google App Engine
This is Rietveld