| 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-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 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 | 76 |
| 77 int utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, N ULL, 0); | 77 int utf16StringLength = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, N ULL, 0); |
| 78 if (utf16StringLength == 0) | 78 if (utf16StringLength == 0) |
| 79 throw std::runtime_error("ToUTF16String failed. Can't determine the length o f the buffer needed."); | 79 throw std::runtime_error("ToUTF16String failed. Can't determine the length o f the buffer needed."); |
| 80 | 80 |
| 81 std::wstring utf16String(utf16StringLength, L'\0'); | 81 std::wstring utf16String(utf16StringLength, L'\0'); |
| 82 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf16String[0], utf16Str ingLength); | 82 MultiByteToWideChar(CP_UTF8, 0, str.c_str(), length, &utf16String[0], utf16Str ingLength); |
| 83 return utf16String; | 83 return utf16String; |
| 84 } | 84 } |
| 85 | 85 |
| 86 std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& values) | |
| 87 { | |
| 88 std::vector<std::wstring> result; | |
| 89 result.reserve(values.size()); | |
| 90 transform(values.begin(), values.end(), back_inserter(result), ToUtf16String); | |
| 91 return result; | |
| 92 } | |
| 93 | |
| 86 std::wstring GetDllDir() | 94 std::wstring GetDllDir() |
| 87 { | 95 { |
| 88 std::vector<WCHAR> path(MAX_PATH); | 96 std::vector<WCHAR> path(MAX_PATH); |
| 89 int length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast <DWORD>(path.size())); | 97 int length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast <DWORD>(path.size())); |
| 90 | 98 |
| 91 while (length == path.size()) | 99 while (length == path.size()) |
| 92 { | 100 { |
| 93 // Buffer too small, double buffer size | 101 // Buffer too small, double buffer size |
| 94 path.resize(path.size() * 2); | 102 path.resize(path.size() * 2); |
| 95 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast<D WORD>(path.size())); | 103 length = GetModuleFileNameW((HINSTANCE)&__ImageBase, &path[0], static_cast<D WORD>(path.size())); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 | 149 |
| 142 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement) | 150 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement) |
| 143 { | 151 { |
| 144 size_t replaceStart = input.find(placeholder); | 152 size_t replaceStart = input.find(placeholder); |
| 145 if (replaceStart != std::string::npos) | 153 if (replaceStart != std::string::npos) |
| 146 { | 154 { |
| 147 input.replace(replaceStart, placeholder.length(), replacement); | 155 input.replace(replaceStart, placeholder.length(), replacement); |
| 148 } | 156 } |
| 149 } | 157 } |
| 150 | 158 |
| 159 std::wstring GetSchemeAndHierarchicalPart(const std::wstring& url) | |
| 160 { | |
| 161 auto schemeAndHierarchicalPartEndsAt = url.find(L'?'); | |
| 162 if (schemeAndHierarchicalPartEndsAt == std::wstring::npos) | |
| 163 { | |
| 164 schemeAndHierarchicalPartEndsAt = url.find(L'#'); | |
| 165 } | |
| 166 return url.substr(0, schemeAndHierarchicalPartEndsAt); | |
| 167 } | |
| 168 | |
| 151 std::wstring GetQueryString(const std::wstring& url) | 169 std::wstring GetQueryString(const std::wstring& url) |
| 152 { | 170 { |
| 153 auto questionSignPos = url.find(L'?'); | 171 auto questionSignPos = url.find(L'?'); |
| 154 if (questionSignPos == std::wstring::npos) | 172 if (questionSignPos == std::wstring::npos) |
| 155 { | 173 { |
| 156 return L""; | 174 return L""; |
| 157 } | 175 } |
|
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); | 176 auto queryStringBeginsAt = questionSignPos + 1; |
| 177 auto endQueryStringPos = url.find(L'#', queryStringBeginsAt); | |
| 159 if (endQueryStringPos == std::wstring::npos) | 178 if (endQueryStringPos == std::wstring::npos) |
| 160 { | 179 { |
| 161 endQueryStringPos = url.length(); | 180 endQueryStringPos = url.length(); |
| 162 } | 181 } |
| 163 return url.substr(questionSignPos + 1, endQueryStringPos - questionSignPos - 1 ); | 182 return url.substr(queryStringBeginsAt, endQueryStringPos - queryStringBeginsAt ); |
| 164 } | 183 } |
| 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 = [¶meterHandler](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 } | |
| LEFT | RIGHT |