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