| 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 } |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 41 if (domains[i] == separator) | 43 if (domains[i] == separator) |
| 42 count++; | 44 count++; |
| 43 | 45 |
| 44 mDomains.reset(new DomainMap(count)); | 46 mDomains.reset(new DomainMap(count)); |
| 45 annotate_address(mDomains.get(), "DomainMap"); | 47 annotate_address(mDomains.get(), "DomainMap"); |
| 46 | 48 |
| 47 StringScanner scanner(domains, 0, separator); | 49 StringScanner scanner(domains, 0, separator); |
| 48 String::size_type start = 0; | 50 String::size_type start = 0; |
| 49 bool reverse = false; | 51 bool reverse = false; |
| 50 bool hasIncludes = false; | 52 bool hasIncludes = false; |
| 51 bool done = false; | 53 bool done = scanner.done(); |
|
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) | 54 while (!done) |
| 53 { | 55 { |
| 54 done = scanner.done(); | 56 done = scanner.done(); |
| 55 String::value_type currChar = scanner.next(); | 57 String::value_type currChar = scanner.next(); |
| 56 if (currChar == u'~' && scanner.position() == start) | 58 if (currChar == u'~' && scanner.position() == start) |
| 57 { | 59 { |
| 58 start++; | 60 start++; |
| 59 reverse = true; | 61 reverse = true; |
| 60 } | 62 } |
| 61 else if (currChar == separator) | 63 else if (currChar == separator) |
| 62 { | 64 { |
| 63 String::size_type len = scanner.position() - start; | 65 String::size_type len = scanner.position() - start; |
| 64 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.') | 66 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.') |
| 65 len--; | 67 len--; |
| 66 if (len > 0) | 68 if (len > 0) |
| 67 { | 69 { |
| 68 enter_context("Adding to ActiveFilter.mDomains"); | 70 enter_context("Adding to ActiveFilter.mDomains"); |
| 69 (*mDomains)[DependentString(domains, start, len)] = !reverse; | 71 (*mDomains)[DependentString(domains, start, len)] = !reverse; |
| 70 exit_context(); | 72 exit_context(); |
| 71 | 73 |
| 72 if (!reverse) | 74 if (!reverse) |
| 73 hasIncludes = true; | 75 hasIncludes = true; |
| 74 } | 76 } |
| 75 start = scanner.position() + 1; | 77 start = scanner.position() + 1; |
| 76 reverse = false; | 78 reverse = false; |
| 77 } | 79 } |
| 78 } | 80 } |
| 79 enter_context("Adding to ActiveFilter.mDomains"); | 81 enter_context("Adding to ActiveFilter.mDomains"); |
| 80 (*mDomains)[u""_str] = !hasIncludes; | 82 (*mDomains)[DEFAULT_DOMAIN] = !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(); | 83 exit_context(); |
| 82 } | 84 } |
| 83 | 85 |
| 84 void ActiveFilter::AddSitekey(const String& sitekey) const | 86 void ActiveFilter::AddSitekey(const String& sitekey) const |
| 85 { | 87 { |
| 86 if (!mSitekeys) | 88 if (!mSitekeys) |
| 87 { | 89 { |
| 88 mSitekeys.reset(new SitekeySet()); | 90 mSitekeys.reset(new SitekeySet()); |
| 89 annotate_address(mSitekeys.get(), "SitekeySet"); | 91 annotate_address(mSitekeys.get(), "SitekeySet"); |
| 90 } | 92 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 101 return false; | 103 return false; |
| 102 | 104 |
| 103 // If no domains are set the rule matches everywhere | 105 // If no domains are set the rule matches everywhere |
| 104 auto domains = GetDomains(); | 106 auto domains = GetDomains(); |
| 105 if (!domains) | 107 if (!domains) |
| 106 return true; | 108 return true; |
| 107 | 109 |
| 108 // 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 |
| 109 // to specific domains | 111 // to specific domains |
| 110 if (docDomain.empty()) | 112 if (docDomain.empty()) |
| 111 return (*domains)[u""_str]; | 113 return (*domains)[DEFAULT_DOMAIN]; |
| 112 | 114 |
| 113 docDomain.tolower(); | 115 docDomain.toLower(); |
| 114 | 116 |
| 115 String::size_type len = docDomain.length(); | 117 String::size_type len = docDomain.length(); |
| 116 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | 118 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') |
| 117 docDomain.reset(docDomain, 0, len - 1); | 119 docDomain.reset(docDomain, 0, len - 1); |
| 118 while (true) | 120 while (true) |
| 119 { | 121 { |
| 120 auto it = domains->find(docDomain); | 122 auto it = domains->find(docDomain); |
| 121 if (it) | 123 if (it) |
| 122 return it->second; | 124 return it->second; |
| 123 | 125 |
| 124 String::size_type nextDot = docDomain.find(u'.'); | 126 String::size_type nextDot = docDomain.find(u'.'); |
| 125 if (nextDot == docDomain.npos) | 127 if (nextDot == docDomain.npos) |
| 126 break; | 128 break; |
| 127 docDomain.reset(docDomain, nextDot + 1); | 129 docDomain.reset(docDomain, nextDot + 1); |
| 128 } | 130 } |
| 129 return (*domains)[u""_str]; | 131 return (*domains)[DEFAULT_DOMAIN]; |
| 130 } | 132 } |
| 131 | 133 |
| 132 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const | 134 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const |
| 133 { | 135 { |
| 134 auto domains = GetDomains(); | 136 auto domains = GetDomains(); |
| 135 if (!domains || docDomain.empty() || (*domains)[u""_str]) | 137 if (!domains || docDomain.empty() || (*domains)[DEFAULT_DOMAIN]) |
| 136 return false; | 138 return false; |
| 137 | 139 |
| 138 docDomain.tolower(); | 140 docDomain.toLower(); |
| 139 | 141 |
| 140 String::size_type len = docDomain.length(); | 142 String::size_type len = docDomain.length(); |
| 141 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | 143 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') |
| 142 docDomain.reset(docDomain, 0, len - 1); | 144 docDomain.reset(docDomain, 0, len - 1); |
| 143 for (auto it = domains->begin(); it != domains->end(); ++it) | 145 for (auto it = domains->begin(); it != domains->end(); ++it) |
| 144 { | 146 { |
| 145 if (!it->second || it->first.equals(docDomain)) | 147 if (!it->second || it->first.equals(docDomain)) |
| 146 continue; | 148 continue; |
| 147 | 149 |
| 148 size_t len1 = it->first.length(); | 150 size_t len1 = it->first.length(); |
| 149 size_t len2 = docDomain.length(); | 151 size_t len2 = docDomain.length(); |
| 150 if (len1 > len2 && | 152 if (len1 > len2 && |
| 151 DependentString(it->first, len1 - len2).equals(docDomain) && | 153 DependentString(it->first, len1 - len2).equals(docDomain) && |
| 152 it->first[len1 - len2 - 1] == u'.') | 154 it->first[len1 - len2 - 1] == u'.') |
| 153 { | 155 { |
| 154 continue; | 156 continue; |
| 155 } | 157 } |
| 156 | 158 |
| 157 return false; | 159 return false; |
| 158 } | 160 } |
| 159 return true; | 161 return true; |
| 160 } | 162 } |
| 161 | 163 |
| 162 bool ActiveFilter::IsGeneric() const | 164 bool ActiveFilter::IsGeneric() const |
| 163 { | 165 { |
| 164 auto sitekeys = GetSitekeys(); | 166 auto sitekeys = GetSitekeys(); |
| 165 auto domains = GetDomains(); | 167 auto domains = GetDomains(); |
| 166 return !sitekeys && (!domains || (*domains)[u""_str]); | 168 return !sitekeys && (!domains || (*domains)[DEFAULT_DOMAIN]); |
| 167 } | 169 } |
| 168 | 170 |
| 169 OwnedString ActiveFilter::Serialize() const | 171 OwnedString ActiveFilter::Serialize() const |
| 170 { | 172 { |
| 171 /* TODO this is very inefficient */ | 173 /* TODO this is very inefficient */ |
| 172 OwnedString result(Filter::Serialize()); | 174 OwnedString result(Filter::Serialize()); |
| 173 if (mDisabled) | 175 if (mDisabled) |
| 174 result.append(u"disabled=true\n"_str); | 176 result.append(u"disabled=true\n"_str); |
| 175 if (mHitCount) | 177 if (mHitCount) |
| 176 { | 178 { |
| 177 result.append(u"hitCount="_str); | 179 result.append(u"hitCount="_str); |
| 178 result.append(to_string(mHitCount)); | 180 result.append(to_string(mHitCount)); |
| 179 result.append(u'\n'); | 181 result.append(u'\n'); |
| 180 } | 182 } |
| 181 if (mLastHit) | 183 if (mLastHit) |
| 182 { | 184 { |
| 183 result.append(u"lastHit="_str); | 185 result.append(u"lastHit="_str); |
| 184 result.append(to_string(mLastHit)); | 186 result.append(to_string(mLastHit)); |
| 185 result.append(u'\n'); | 187 result.append(u'\n'); |
| 186 } | 188 } |
| 187 return result; | 189 return result; |
| 188 } | 190 } |
| LEFT | RIGHT |