Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 14 * You should have received a copy of the GNU General Public License |
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | |
18 #include <cstring> | |
17 | 19 |
18 #include "ElemHideBase.h" | 20 #include "ElemHideBase.h" |
19 #include "../StringScanner.h" | 21 #include "../StringScanner.h" |
20 | 22 |
21 namespace | 23 namespace |
22 { | 24 { |
23 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, | 25 void NormalizeWhitespace(DependentString& text, String::size_type& domainsEnd, |
24 String::size_type& selectorStart) | 26 String::size_type& selectorStart) |
25 { | 27 { |
26 // For element hiding filters we only want to remove spaces preceding the | 28 // For element hiding filters we only want to remove spaces preceding the |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 | 113 |
112 if (exception) | 114 if (exception) |
113 return Type::ELEMHIDEEXCEPTION; | 115 return Type::ELEMHIDEEXCEPTION; |
114 | 116 |
115 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) | 117 if (text.find(u"[-abp-properties="_str, data.mSelectorStart) != text.npos) |
116 return Type::ELEMHIDEEMULATION; | 118 return Type::ELEMHIDEEMULATION; |
117 | 119 |
118 return Type::ELEMHIDE; | 120 return Type::ELEMHIDE; |
119 } | 121 } |
120 | 122 |
121 namespace { | 123 namespace |
124 { | |
125 static constexpr String::value_type OPENING_CURLY_REPLACEMENT[] = u"\\7B "; | |
126 static constexpr String::value_type CLOSING_CURLY_REPLACEMENT[] = u"\\7D "; | |
127 static constexpr String::size_type CURLY_REPLACEMENT_SIZE = sizeof(OPENING_CUR LY_REPLACEMENT) / sizeof(OPENING_CURLY_REPLACEMENT[0]) - 1; | |
122 | 128 |
123 OwnedString EscapeCurlies(String::size_type first, const DependentString& str) | 129 OwnedString EscapeCurlies(String::size_type replacementCount, |
124 { | 130 const DependentString& str) |
125 OwnedString result; | 131 { |
132 OwnedString result(str.length() + replacementCount * (CURLY_REPLACEMENT_SIZE - 1)); | |
sergei
2017/11/21 11:42:10
I think we should not subtract 1 here because it's
hub
2017/11/21 15:58:21
the -1 is because we are replacing. str.length() a
sergei
2017/11/21 16:18:00
Acknowledged.
| |
126 | 133 |
127 String::size_type start = 0; | 134 String::value_type* current = result.data(); |
128 String::size_type i = first; | 135 for (String::size_type i = 0; i < str.length(); i++) |
129 for (; i < str.length(); i++) | |
130 { | |
131 if (str[i] == '}' || str[i] == '{') | |
132 { | 136 { |
133 if (i != start) | |
134 result.append(str.data() + start, i - start); | |
135 start = i + 1; | |
136 switch(str[i]) | 137 switch(str[i]) |
137 { | 138 { |
138 case '}': | 139 case u'}': |
139 result.append("\\x7D ", 5); | 140 std::memcpy(current, CLOSING_CURLY_REPLACEMENT, |
141 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | |
142 current += CURLY_REPLACEMENT_SIZE; | |
140 break; | 143 break; |
141 case '{': | 144 case u'{': |
142 result.append("\\x7B ", 5); | 145 std::memcpy(current, OPENING_CURLY_REPLACEMENT, |
146 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE); | |
147 current += CURLY_REPLACEMENT_SIZE; | |
143 break; | 148 break; |
144 default: | 149 default: |
150 *current = str[i]; | |
151 current++; | |
145 break; | 152 break; |
146 } | 153 } |
147 } | 154 } |
155 | |
156 return result; | |
148 } | 157 } |
149 if (start < i) | |
150 result.append(str.data() + start, i - start); | |
Wladimir Palant
2017/10/17 10:24:33
This is performing lots of reallocations. Please c
hub
2017/10/17 19:22:18
Done.
| |
151 return result; | |
152 } | |
153 | |
154 } | 158 } |
155 | 159 |
156 OwnedString ElemHideBase::GetSelector() const | 160 OwnedString ElemHideBase::GetSelector() const |
157 { | 161 { |
158 DependentString selector = mData.GetSelector(mText); | 162 DependentString selector = mData.GetSelector(mText); |
159 | 163 String::size_type replacementCount = 0; |
160 for (String::size_type i = 0; i < selector.length(); i++) | 164 for (String::size_type i = 0; i < selector.length(); i++) |
161 if (selector[i] == '}' || selector[i] == '{') | 165 if (selector[i] == '}' || selector[i] == '{') |
162 return EscapeCurlies(i, selector); | 166 replacementCount++; |
167 if (replacementCount) | |
168 return EscapeCurlies(replacementCount, selector); | |
163 | 169 |
164 return OwnedString(selector); | 170 return OwnedString(selector); |
165 } | 171 } |
166 | 172 |
167 OwnedString ElemHideBase::GetSelectorDomain() const | 173 OwnedString ElemHideBase::GetSelectorDomain() const |
168 { | 174 { |
169 /* TODO this is inefficient */ | 175 /* TODO this is inefficient */ |
170 OwnedString result; | 176 OwnedString result; |
171 if (mDomains) | 177 if (mDomains) |
172 { | 178 { |
173 for (const auto& item : *mDomains) | 179 for (const auto& item : *mDomains) |
174 { | 180 { |
175 if (item.second && !item.first.empty()) | 181 if (item.second && !item.first.empty()) |
176 { | 182 { |
177 if (!result.empty()) | 183 if (!result.empty()) |
178 result.append(u','); | 184 result.append(u','); |
179 result.append(item.first); | 185 result.append(item.first); |
180 } | 186 } |
181 } | 187 } |
182 } | 188 } |
183 return result; | 189 return result; |
184 } | 190 } |
LEFT | RIGHT |