Left: | ||
Right: |
LEFT | RIGHT |
---|---|
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 const DependentString DEFAULT_DOMAIN(u""_str); | |
9 | |
8 OwnedString to_string(unsigned int i) | 10 OwnedString to_string(unsigned int i) |
9 { | 11 { |
10 char buffer[11]; | 12 char buffer[11]; |
11 int len = sprintf(buffer, "%u", i); | 13 int len = sprintf(buffer, "%u", i); |
12 | 14 |
13 OwnedString result(len); | 15 OwnedString result(len); |
14 for (String::size_type i = 0; i < len; i++) | 16 for (String::size_type i = 0; i < len; i++) |
15 result[i] = buffer[i]; | 17 result[i] = buffer[i]; |
16 return result; | 18 return result; |
17 } | 19 } |
18 } | 20 } |
19 | 21 |
20 ActiveFilter::ActiveFilter(const String& text, bool ignoreTrailingDot) | 22 ActiveFilter::ActiveFilter(Type type, const String& text, bool ignoreTrailingDot ) |
21 : Filter(text), mDisabled(false), mHitCount(0), mLastHit(0), | 23 : Filter(type, text), mDisabled(false), mHitCount(0), mLastHit(0), |
22 mIgnoreTrailingDot(ignoreTrailingDot) | 24 mIgnoreTrailingDot(ignoreTrailingDot) |
23 { | 25 { |
24 } | |
25 | |
26 void ActiveFilter::ToLower(DependentString&& str) | |
27 { | |
28 for (String::size_type i = 0; i < str.length(); ++i) | |
29 { | |
30 String::value_type currChar = str[i]; | |
31 | |
32 // This should be more efficient with a lookup table but I couldn't measure | |
33 // any performance difference. | |
34 if (currChar >= u'A' && currChar <= u'Z') | |
35 str[i] = currChar + u'a' - u'A'; | |
36 else if (currChar >= 128) | |
37 { | |
38 // It seems that calling JS it the easiest solution for lowercasing | |
39 // Unicode characters. | |
40 str[i] = EM_ASM_INT({ | |
41 return String.fromCharCode($0).toLowerCase().charCodeAt(0); | |
42 }, currChar); | |
43 } | |
44 } | |
45 } | 26 } |
46 | 27 |
47 ActiveFilter::DomainMap* ActiveFilter::GetDomains() const | 28 ActiveFilter::DomainMap* ActiveFilter::GetDomains() const |
48 { | 29 { |
49 return mDomains.get(); | 30 return mDomains.get(); |
50 } | 31 } |
51 | 32 |
52 ActiveFilter::SitekeySet* ActiveFilter::GetSitekeys() const | 33 ActiveFilter::SitekeySet* ActiveFilter::GetSitekeys() const |
53 { | 34 { |
54 return mSitekeys.get(); | 35 return mSitekeys.get(); |
55 } | 36 } |
56 | 37 |
57 void ActiveFilter::ParseDomains(const String& domains, | 38 void ActiveFilter::ParseDomains(const String& domains, |
58 String::value_type separator) const | 39 String::value_type separator) const |
59 { | 40 { |
60 DomainMap::size_type count = 2; | 41 DomainMap::size_type count = 2; |
61 for (String::size_type i = 0; i < domains.length(); i++) | 42 for (String::size_type i = 0; i < domains.length(); i++) |
62 if (domains[i] == separator) | 43 if (domains[i] == separator) |
63 count++; | 44 count++; |
64 | 45 |
65 mDomains.reset(new DomainMap(count)); | 46 mDomains.reset(new DomainMap(count)); |
66 annotate_address(mDomains.get(), "DomainMap"); | 47 annotate_address(mDomains.get(), "DomainMap"); |
67 | 48 |
68 StringScanner scanner(domains, 0, separator); | 49 StringScanner scanner(domains, 0, separator); |
69 String::size_type start = 0; | 50 String::size_type start = 0; |
70 bool reverse = false; | 51 bool reverse = false; |
71 bool hasIncludes = false; | 52 bool hasIncludes = false; |
72 bool done = false; | 53 bool done = scanner.done(); |
73 while (!done) | 54 while (!done) |
74 { | 55 { |
75 done = scanner.done(); | 56 done = scanner.done(); |
sergei
2016/06/16 21:16:19
Should it be in the condition?
while (!(done = sca
Wladimir Palant
2016/12/06 10:47:05
No, this will change the logic - currently we are
sergei
2017/01/10 15:57:16
Acknowledged.
| |
76 String::value_type currChar = scanner.next(); | 57 String::value_type currChar = scanner.next(); |
77 if (currChar == u'~' && scanner.position() == start) | 58 if (currChar == u'~' && scanner.position() == start) |
78 { | 59 { |
79 start++; | 60 start++; |
80 reverse = true; | 61 reverse = true; |
81 } | 62 } |
82 else if (currChar == separator) | 63 else if (currChar == separator) |
83 { | 64 { |
84 String::size_type len = scanner.position() - start; | 65 String::size_type len = scanner.position() - start; |
sergei
2016/06/16 21:16:17
If domains is empty string then len will be maximu
Wladimir Palant
2016/12/06 10:47:06
How so? Both scanner.position() and start are 0 he
sergei
2017/01/10 15:57:16
Acknowledged. The confusing part here was that it'
| |
85 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.') | 66 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.') |
86 len--; | 67 len--; |
87 if (len > 0) | 68 if (len > 0) |
88 { | 69 { |
89 enter_context("Adding to ActiveFilter.mDomains"); | 70 enter_context("Adding to ActiveFilter.mDomains"); |
90 (*mDomains)[DependentString(domains, start, len)] = !reverse; | 71 (*mDomains)[DependentString(domains, start, len)] = !reverse; |
91 exit_context(); | 72 exit_context(); |
92 | 73 |
93 if (!reverse) | 74 if (!reverse) |
94 hasIncludes = true; | 75 hasIncludes = true; |
95 } | 76 } |
96 start = scanner.position() + 1; | 77 start = scanner.position() + 1; |
97 reverse = false; | 78 reverse = false; |
98 } | 79 } |
99 } | 80 } |
100 enter_context("Adding to ActiveFilter.mDomains"); | 81 enter_context("Adding to ActiveFilter.mDomains"); |
101 (*mDomains)[u""_str] = !hasIncludes; | 82 (*mDomains)[DEFAULT_DOMAIN] = !hasIncludes; |
102 exit_context(); | 83 exit_context(); |
103 } | 84 } |
104 | 85 |
105 void ActiveFilter::AddSitekey(const String& sitekey) const | 86 void ActiveFilter::AddSitekey(const String& sitekey) const |
106 { | 87 { |
107 if (!mSitekeys) | 88 if (!mSitekeys) |
108 { | 89 { |
109 mSitekeys.reset(new SitekeySet()); | 90 mSitekeys.reset(new SitekeySet()); |
110 annotate_address(mSitekeys.get(), "SitekeySet"); | 91 annotate_address(mSitekeys.get(), "SitekeySet"); |
111 } | 92 } |
(...skipping 10 matching lines...) Expand all Loading... | |
122 return false; | 103 return false; |
123 | 104 |
124 // If no domains are set the rule matches everywhere | 105 // If no domains are set the rule matches everywhere |
125 auto domains = GetDomains(); | 106 auto domains = GetDomains(); |
126 if (!domains) | 107 if (!domains) |
127 return true; | 108 return true; |
128 | 109 |
129 // 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 |
130 // to specific domains | 111 // to specific domains |
131 if (docDomain.empty()) | 112 if (docDomain.empty()) |
132 return (*domains)[u""_str]; | 113 return (*domains)[DEFAULT_DOMAIN]; |
133 | 114 |
134 ToLower(DependentString(docDomain)); | 115 docDomain.toLower(); |
135 | 116 |
136 String::size_type len = docDomain.length(); | 117 String::size_type len = docDomain.length(); |
137 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | 118 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') |
138 docDomain.reset(docDomain, 0, len - 1); | 119 docDomain.reset(docDomain, 0, len - 1); |
139 while (true) | 120 while (true) |
140 { | 121 { |
141 auto it = domains->find(docDomain); | 122 auto it = domains->find(docDomain); |
142 if (it) | 123 if (it) |
143 return it->second; | 124 return it->second; |
144 | 125 |
145 String::size_type nextDot = docDomain.find(u'.'); | 126 String::size_type nextDot = docDomain.find(u'.'); |
146 if (nextDot == docDomain.npos) | 127 if (nextDot == docDomain.npos) |
147 break; | 128 break; |
148 docDomain.reset(docDomain, nextDot + 1); | 129 docDomain.reset(docDomain, nextDot + 1); |
149 } | 130 } |
150 return (*domains)[u""_str]; | 131 return (*domains)[DEFAULT_DOMAIN]; |
151 } | 132 } |
152 | 133 |
153 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const | 134 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const |
154 { | 135 { |
155 auto domains = GetDomains(); | 136 auto domains = GetDomains(); |
156 if (!domains || docDomain.empty() || (*domains)[u""_str]) | 137 if (!domains || docDomain.empty() || (*domains)[DEFAULT_DOMAIN]) |
157 return false; | 138 return false; |
158 | 139 |
159 ToLower(DependentString(docDomain)); | 140 docDomain.toLower(); |
160 | 141 |
161 String::size_type len = docDomain.length(); | 142 String::size_type len = docDomain.length(); |
162 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | 143 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') |
163 docDomain.reset(docDomain, 0, len - 1); | 144 docDomain.reset(docDomain, 0, len - 1); |
164 for (auto it = domains->begin(); it != domains->end(); ++it) | 145 for (auto it = domains->begin(); it != domains->end(); ++it) |
165 { | 146 { |
166 if (!it->second || it->first.equals(docDomain)) | 147 if (!it->second || it->first.equals(docDomain)) |
167 continue; | 148 continue; |
168 | 149 |
169 size_t len1 = it->first.length(); | 150 size_t len1 = it->first.length(); |
170 size_t len2 = docDomain.length(); | 151 size_t len2 = docDomain.length(); |
171 if (len1 > len2 && | 152 if (len1 > len2 && |
172 DependentString(it->first, len1 - len2).equals(docDomain) && | 153 DependentString(it->first, len1 - len2).equals(docDomain) && |
173 it->first[len1 - len2 - 1] == u'.') | 154 it->first[len1 - len2 - 1] == u'.') |
174 { | 155 { |
175 continue; | 156 continue; |
176 } | 157 } |
177 | 158 |
178 return false; | 159 return false; |
179 } | 160 } |
180 return true; | 161 return true; |
181 } | 162 } |
182 | 163 |
183 bool ActiveFilter::IsGeneric() const | 164 bool ActiveFilter::IsGeneric() const |
184 { | 165 { |
185 auto sitekeys = GetSitekeys(); | 166 auto sitekeys = GetSitekeys(); |
186 auto domains = GetDomains(); | 167 auto domains = GetDomains(); |
187 return !sitekeys && (!domains || (*domains)[u""_str]); | 168 return !sitekeys && (!domains || (*domains)[DEFAULT_DOMAIN]); |
sergei
2016/06/16 21:16:20
JIC, pay attention that we can modify domains if t
Wladimir Palant
2016/12/06 10:47:08
Given that ParseDomains() will always set the entr
| |
188 } | 169 } |
189 | 170 |
190 OwnedString ActiveFilter::Serialize() const | 171 OwnedString ActiveFilter::Serialize() const |
191 { | 172 { |
192 /* TODO this is very inefficient */ | 173 /* TODO this is very inefficient */ |
193 OwnedString result(Filter::Serialize()); | 174 OwnedString result(Filter::Serialize()); |
194 if (mDisabled) | 175 if (mDisabled) |
195 result.append(u"disabled=true\n"_str); | 176 result.append(u"disabled=true\n"_str); |
196 if (mHitCount) | 177 if (mHitCount) |
197 { | 178 { |
198 result.append(u"hitCount="_str); | 179 result.append(u"hitCount="_str); |
199 result.append(to_string(mHitCount)); | 180 result.append(to_string(mHitCount)); |
200 result.append(u'\n'); | 181 result.append(u'\n'); |
201 } | 182 } |
202 if (mLastHit) | 183 if (mLastHit) |
203 { | 184 { |
204 result.append(u"lastHit="_str); | 185 result.append(u"lastHit="_str); |
205 result.append(to_string(mLastHit)); | 186 result.append(to_string(mLastHit)); |
206 result.append(u'\n'); | 187 result.append(u'\n'); |
207 } | 188 } |
208 return result; | 189 return result; |
209 } | 190 } |
LEFT | RIGHT |