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

Side by Side Diff: src/shared/Utils.cpp

Issue 5921969115496448: Issue 1115 - Some yahoo page not correctly shown on IE8 when ABP enabled (Closed)
Patch Set: x Created Jan. 29, 2015, 3:23 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
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-2015 Eyeo GmbH 3 * Copyright (C) 2006-2015 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
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
140 } 140 }
141 141
142 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement) 142 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement)
143 { 143 {
144 size_t replaceStart = input.find(placeholder); 144 size_t replaceStart = input.find(placeholder);
145 if (replaceStart != std::string::npos) 145 if (replaceStart != std::string::npos)
146 { 146 {
147 input.replace(replaceStart, placeholder.length(), replacement); 147 input.replace(replaceStart, placeholder.length(), replacement);
148 } 148 }
149 } 149 }
150
151 std::wstring GetQueryString(const std::wstring& url)
152 {
153 auto questionSignPos = url.find(L'?');
154 if (questionSignPos == std::wstring::npos)
155 {
156 return L"";
157 }
Eric 2015/02/02 18:41:58 If you increment questionSignPos here, you get a (
sergei 2015/02/12 14:44:06 done
158 auto endQueryStringPos = url.find(L'#', questionSignPos);
159 if (endQueryStringPos == std::wstring::npos)
160 {
161 endQueryStringPos = url.length();
162 }
163 return url.substr(questionSignPos + 1, endQueryStringPos - questionSignPos - 1 );
164 }
165
166 void SplitString(const std::wstring& value, wchar_t delimiter,
Eric 2015/02/02 18:41:58 A better name would be ForEachToken(), by analogy
sergei 2015/02/12 14:44:06 done.
167 const std::function<bool(std::wstring::const_iterator begin, st d::wstring::const_iterator end)>& tokenHandler)
168 {
169 if (value.empty() || !tokenHandler)
Eric 2015/02/02 18:41:58 As above, 'tokenHandler' is a reference.
170 {
171 return;
172 }
Eric 2015/02/02 18:41:58 I would write the following loop as a 'for' loop.
sergei 2015/02/12 14:44:06 Firstly, the proposed statements in the loop are n
Eric 2015/02/12 17:24:41 Look more closely.
sergei 2015/02/13 15:21:56 It results in an infinite cycle. Look, at the end
Eric 2015/02/13 16:38:22 While I think the loop could be improved, it does
173 std::wstring::size_type delimiterPos = value.find(delimiter);
174 std::wstring::size_type prevDelimiterPos = 0;
175 while(delimiterPos != std::wstring::npos)
176 {
177 if (!tokenHandler(value.begin() + prevDelimiterPos, value.begin() + delimite rPos))
178 {
179 return;
180 }
181 prevDelimiterPos = delimiterPos;
182 ++prevDelimiterPos; // skip the delimiter
183 delimiterPos = value.find(delimiter, prevDelimiterPos);
184 }
185 tokenHandler(value.begin() + prevDelimiterPos, value.end());
186 }
187
188 void ProcessQueryStringParameters(const std::wstring& queryString,
Eric 2015/02/02 18:41:58 Similiary, 'ForEachQueryParameter()' would be a be
sergei 2015/02/12 14:44:06 done.
189 const std::function<bool(const std::wstring& n ame, const std::wstring& value)>& parameterHandler)
190 {
191 if (queryString.empty() || !parameterHandler)
Eric 2015/02/02 18:41:58 Why bother with the term "!parameterHandler" when
sergei 2015/02/12 14:44:06 std::function<...> can be an empty object, so befo
Eric 2015/02/12 17:24:41 I had forgotten that you can have a non-empty refe
sergei 2015/02/13 15:21:56 I've added the comments.
192 {
193 return;
194 }
195 auto unparsedParameterHandler = [&parameterHandler](std::wstring::const_iterat or begin, std::wstring::const_iterator end)->bool
Eric 2015/02/02 18:41:58 No need for a local variable here; this lambda can
sergei 2015/02/12 14:44:06 done
196 {
197 auto assignSignIt = std::find(begin, end, L'=');
198 auto valueBeginsAt = assignSignIt != end ? assignSignIt + 1 : assignSignIt;
199 return parameterHandler(std::wstring(begin, assignSignIt), std::wstring(valu eBeginsAt, end));
200 };
201 SplitString(queryString, L'&', unparsedParameterHandler);
202 }
OLDNEW

Powered by Google App Engine
This is Rietveld