| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include <cstdio> | |
| 2 | |
| 3 #include "ActiveFilter.h" | |
| 4 #include "StringScanner.h" | |
| 5 | |
| 6 namespace | |
| 7 { | |
| 8 OwnedString to_string(unsigned int i) | |
| 9 { | |
| 10 char buffer[11]; | |
| 11 int len = sprintf(buffer, "%u", i); | |
| 12 return std::move(OwnedString(buffer, len)); | |
|
sergei
2016/02/17 12:54:28
It's not necessary to use `std::move` for the retu
Wladimir Palant
2016/02/18 16:06:29
It got me confused that C++ generates different co
sergei
2016/02/22 12:45:32
Yes, the behavior of C++ compiler regarding the re
| |
| 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 | |
| 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); | |
|
sergei
2016/02/17 12:54:27
Wouldn't it be better to call JS function on the r
Wladimir Palant
2016/02/18 16:06:30
Not sure about that. "Rest of the string" isn't a
sergei
2016/02/22 12:45:32
I thought rather about surrogate pairs, e.g. http:
Wladimir Palant
2016/02/23 12:37:20
Interesting. I was under the impression that JavaS
sergei
2016/02/23 15:07:23
Agree.
| |
| 40 } | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 ActiveFilter::DomainMap* ActiveFilter::GetDomains() const | |
| 45 { | |
| 46 return mDomains.get(); | |
| 47 } | |
| 48 | |
| 49 ActiveFilter::SitekeySet* ActiveFilter::GetSitekeys() const | |
| 50 { | |
| 51 return mSitekeys.get(); | |
| 52 } | |
| 53 | |
| 54 void ActiveFilter::ParseDomains(const String& domains, | |
| 55 String::value_type separator) const | |
| 56 { | |
| 57 DomainMap::size_type count = 2; | |
| 58 for (String::size_type i = 0; i < domains.length(); i++) | |
| 59 if (domains[i] == separator) | |
| 60 count++; | |
| 61 | |
| 62 mDomains.reset(new DomainMap(count)); | |
| 63 annotate_address(mDomains.get(), "DomainMap"); | |
| 64 | |
| 65 StringScanner scanner(domains, 0, separator); | |
| 66 String::size_type start = 0; | |
| 67 bool reverse = false; | |
| 68 bool hasIncludes = false; | |
| 69 bool done = false; | |
| 70 while (!done) | |
| 71 { | |
| 72 done = scanner.done(); | |
| 73 String::value_type currChar = scanner.next(); | |
| 74 if (currChar == u'~' && scanner.position() == start) | |
| 75 { | |
| 76 start++; | |
| 77 reverse = true; | |
| 78 } | |
| 79 else if (currChar == separator) | |
| 80 { | |
| 81 String::size_type len = scanner.position() - start; | |
| 82 if (len > 0 && mIgnoreTrailingDot && domains[start + len - 1] == '.') | |
| 83 len--; | |
| 84 if (len > 0) | |
| 85 { | |
| 86 enter_context("Adding to ActiveFilter.mDomains"); | |
| 87 (*mDomains)[DependentString(domains, start, len)] = !reverse; | |
| 88 exit_context(); | |
| 89 | |
| 90 if (!reverse) | |
| 91 hasIncludes = true; | |
| 92 } | |
| 93 start = scanner.position() + 1; | |
| 94 reverse = false; | |
| 95 } | |
| 96 } | |
| 97 enter_context("Adding to ActiveFilter.mDomains"); | |
| 98 (*mDomains)[u""_str] = !hasIncludes; | |
| 99 exit_context(); | |
| 100 } | |
| 101 | |
| 102 void ActiveFilter::AddSitekey(const String& sitekey) const | |
| 103 { | |
| 104 if (!mSitekeys) | |
| 105 { | |
| 106 mSitekeys.reset(new SitekeySet()); | |
| 107 annotate_address(mSitekeys.get(), "SitekeySet"); | |
| 108 } | |
| 109 | |
| 110 enter_context("Adding to ActiveFilter.mSitekeys"); | |
| 111 mSitekeys->insert(sitekey); | |
| 112 exit_context(); | |
| 113 } | |
| 114 | |
| 115 bool ActiveFilter::IsActiveOnDomain(DependentString& docDomain, const String& si tekey) const | |
| 116 { | |
| 117 auto sitekeys = GetSitekeys(); | |
| 118 if (sitekeys && sitekeys->find(sitekey) == sitekeys->end()) | |
| 119 return false; | |
| 120 | |
| 121 // If no domains are set the rule matches everywhere | |
| 122 auto domains = GetDomains(); | |
| 123 if (!domains) | |
| 124 return true; | |
| 125 | |
| 126 // If the document has no host name, match only if the filter isn't restricted | |
| 127 // to specific domains | |
| 128 if (docDomain.empty()) | |
| 129 return (*domains)[u""_str]; | |
| 130 | |
| 131 String::size_type len = docDomain.length(); | |
| 132 ToLower(docDomain, 0, len); | |
| 133 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | |
| 134 docDomain.reset(docDomain, 0, len - 1); | |
| 135 while (true) | |
| 136 { | |
| 137 auto it = domains->find(docDomain); | |
| 138 if (it != domains->end()) | |
| 139 return it->second; | |
| 140 | |
| 141 String::size_type nextDot = docDomain.find(u'.'); | |
| 142 if (nextDot == docDomain.npos) | |
| 143 break; | |
| 144 docDomain.reset(docDomain, nextDot + 1); | |
| 145 } | |
| 146 return (*domains)[u""_str]; | |
| 147 } | |
| 148 | |
| 149 bool ActiveFilter::IsActiveOnlyOnDomain(DependentString& docDomain) const | |
| 150 { | |
| 151 auto domains = GetDomains(); | |
| 152 if (!domains || docDomain.empty() || (*domains)[u""_str]) | |
| 153 return false; | |
| 154 | |
| 155 String::size_type len = docDomain.length(); | |
| 156 ToLower(docDomain, 0, len); | |
| 157 if (len > 0 && mIgnoreTrailingDot && docDomain[len - 1] == '.') | |
| 158 docDomain.reset(docDomain, 0, len - 1); | |
| 159 for (auto it = domains->begin(); it != domains->end(); ++it) | |
| 160 { | |
| 161 if (!it->second || it->first.equals(docDomain)) | |
| 162 continue; | |
| 163 | |
| 164 size_t len1 = it->first.length(); | |
| 165 size_t len2 = docDomain.length(); | |
| 166 if (len1 > len2 && | |
| 167 DependentString(it->first, len1 - len2).equals(docDomain) && | |
| 168 it->first[len1 - len2 - 1] == u'.') | |
| 169 { | |
| 170 continue; | |
| 171 } | |
| 172 | |
| 173 return false; | |
| 174 } | |
| 175 return true; | |
| 176 } | |
| 177 | |
| 178 bool ActiveFilter::IsGeneric() const | |
| 179 { | |
| 180 auto sitekeys = GetSitekeys(); | |
| 181 auto domains = GetDomains(); | |
| 182 return !sitekeys && (!domains || (*domains)[u""_str]); | |
| 183 } | |
| 184 | |
| 185 OwnedString ActiveFilter::Serialize() const | |
| 186 { | |
| 187 /* TODO this is very inefficient */ | |
| 188 OwnedString result(Filter::Serialize()); | |
| 189 if (mDisabled) | |
| 190 result.append(u"disabled=true\n"_str); | |
| 191 if (mHitCount) | |
| 192 { | |
| 193 result.append(u"hitCount="_str); | |
| 194 result.append(to_string(mHitCount)); | |
| 195 result.append(u'\n'); | |
| 196 } | |
| 197 if (mLastHit) | |
| 198 { | |
| 199 result.append(u"lastHit="_str); | |
| 200 result.append(to_string(mLastHit)); | |
| 201 result.append(u'\n'); | |
| 202 } | |
| 203 return std::move(result); | |
| 204 } | |
| OLD | NEW |