Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: compiled/filter/ElemHideBase.cpp

Issue 29580558: Issue 5174 - Allow '{' and '}' in selectors (Closed) Base URL: https://hg.adblockplus.org/adblockpluscore/
Left Patch Set: Preallocate the destination Created Oct. 17, 2017, 7:21 p.m.
Right Patch Set: Indent namespace content. Created Oct. 19, 2017, 12:48 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « compiled/filter/ElemHideBase.h ('k') | test/filterClasses.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
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
Wladimir Palant 2017/10/18 09:24:02 Please put the opening curly on the next line and
hub 2017/10/18 12:53:47 Done.
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 static constexpr size_t REPLACE_SIZE = 5; 129 OwnedString EscapeCurlies(String::size_type replacementCount,
Wladimir Palant 2017/10/18 09:24:02 This name doesn't quite make it clear what this co
hub 2017/10/18 12:53:48 Done.
130 const DependentString& str)
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.
124 133
125 OwnedString EscapeCurlies(String::size_type count, const DependentString& str) 134 String::value_type* current = result.data();
126 { 135 for (String::size_type i = 0; i < str.length(); i++)
127 OwnedString result(str.length() + (count * (REPLACE_SIZE - 1)));
Wladimir Palant 2017/10/18 09:24:02 Nit: the parentheses around `count * ...` are unne
hub 2017/10/18 12:53:47 Done.
128
129 String::size_type start = 0;
130 String::size_type i = 0;
131 String::value_type* current = result.data();
132 for (; i < str.length(); i++)
133 {
134 if (str[i] == '}' || str[i] == '{')
135 { 136 {
136 if (i != start)
137 {
138 std::memcpy(current, str.data() + start,
139 sizeof(String::value_type) * (i - start));
140 current += i - start;
Wladimir Palant 2017/10/18 09:24:02 The logic here got unnecessarily complicated. I'd
hub 2017/10/18 12:53:48 Done.
141 }
142 start = i + 1;
143 switch(str[i]) 137 switch(str[i])
144 { 138 {
145 case '}': 139 case u'}':
Wladimir Palant 2017/10/18 09:24:02 Nit: I'd probably say u'}' to indicate that we are
hub 2017/10/18 12:53:47 Done.
146 std::memcpy(current, u"\\x7D ", 140 std::memcpy(current, CLOSING_CURLY_REPLACEMENT,
147 sizeof(String::value_type) * REPLACE_SIZE); 141 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
Wladimir Palant 2017/10/18 09:24:02 You need to include <cstring> explicitly to use st
hub 2017/10/18 12:53:47 Done.
148 current += REPLACE_SIZE; 142 current += CURLY_REPLACEMENT_SIZE;
149 break; 143 break;
150 case '{': 144 case u'{':
151 std::memcpy(current, u"\\x7B ", 145 std::memcpy(current, OPENING_CURLY_REPLACEMENT,
152 sizeof(String::value_type) * REPLACE_SIZE); 146 sizeof(String::value_type) * CURLY_REPLACEMENT_SIZE);
153 current += REPLACE_SIZE; 147 current += CURLY_REPLACEMENT_SIZE;
154 break; 148 break;
155 default: 149 default:
150 *current = str[i];
151 current++;
156 break; 152 break;
157 } 153 }
158 } 154 }
155
156 return result;
159 } 157 }
160 if (start < i)
161 std::memcpy(current, str.data() + start,
162 sizeof(String::value_type) * (i - start));
163 return result;
164 }
165
166 } 158 }
167 159
168 OwnedString ElemHideBase::GetSelector() const 160 OwnedString ElemHideBase::GetSelector() const
169 { 161 {
170 DependentString selector = mData.GetSelector(mText); 162 DependentString selector = mData.GetSelector(mText);
171 String::size_type count = 0; 163 String::size_type replacementCount = 0;
Wladimir Palant 2017/10/18 09:24:02 Nit: Somewhat less generic name such as replacemen
hub 2017/10/18 12:53:47 Done.
172 for (String::size_type i = 0; i < selector.length(); i++) 164 for (String::size_type i = 0; i < selector.length(); i++)
173 if (selector[i] == '}' || selector[i] == '{') 165 if (selector[i] == '}' || selector[i] == '{')
174 count++; 166 replacementCount++;
175 if (count) 167 if (replacementCount)
176 return EscapeCurlies(count, selector); 168 return EscapeCurlies(replacementCount, selector);
177 169
178 return OwnedString(selector); 170 return OwnedString(selector);
179 } 171 }
180 172
181 OwnedString ElemHideBase::GetSelectorDomain() const 173 OwnedString ElemHideBase::GetSelectorDomain() const
182 { 174 {
183 /* TODO this is inefficient */ 175 /* TODO this is inefficient */
184 OwnedString result; 176 OwnedString result;
185 if (mDomains) 177 if (mDomains)
186 { 178 {
187 for (const auto& item : *mDomains) 179 for (const auto& item : *mDomains)
188 { 180 {
189 if (item.second && !item.first.empty()) 181 if (item.second && !item.first.empty())
190 { 182 {
191 if (!result.empty()) 183 if (!result.empty())
192 result.append(u','); 184 result.append(u',');
193 result.append(item.first); 185 result.append(item.first);
194 } 186 }
195 } 187 }
196 } 188 }
197 return result; 189 return result;
198 } 190 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld