Index: src/shared/Utils.cpp |
diff --git a/src/shared/Utils.cpp b/src/shared/Utils.cpp |
index 52b5a69f7920a6b9a49a46e16b250e2accaa33f8..b39ffbb18872d48f9bab7b2aef8669af2992fa0a 100644 |
--- a/src/shared/Utils.cpp |
+++ b/src/shared/Utils.cpp |
@@ -147,3 +147,56 @@ void ReplaceString(std::wstring& input, const std::wstring placeholder, const st |
input.replace(replaceStart, placeholder.length(), replacement); |
} |
} |
+ |
+std::wstring GetQueryString(const std::wstring& url) |
+{ |
+ auto questionSignPos = url.find(L'?'); |
+ if (questionSignPos == std::wstring::npos) |
+ { |
+ return L""; |
+ } |
Eric
2015/02/02 18:41:58
If you increment questionSignPos here, you get a (
sergei
2015/02/12 14:44:06
done
|
+ auto endQueryStringPos = url.find(L'#', questionSignPos); |
+ if (endQueryStringPos == std::wstring::npos) |
+ { |
+ endQueryStringPos = url.length(); |
+ } |
+ return url.substr(questionSignPos + 1, endQueryStringPos - questionSignPos - 1); |
+} |
+ |
+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.
|
+ const std::function<bool(std::wstring::const_iterator begin, std::wstring::const_iterator end)>& tokenHandler) |
+{ |
+ if (value.empty() || !tokenHandler) |
Eric
2015/02/02 18:41:58
As above, 'tokenHandler' is a reference.
|
+ { |
+ return; |
+ } |
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
|
+ std::wstring::size_type delimiterPos = value.find(delimiter); |
+ std::wstring::size_type prevDelimiterPos = 0; |
+ while(delimiterPos != std::wstring::npos) |
+ { |
+ if (!tokenHandler(value.begin() + prevDelimiterPos, value.begin() + delimiterPos)) |
+ { |
+ return; |
+ } |
+ prevDelimiterPos = delimiterPos; |
+ ++prevDelimiterPos; // skip the delimiter |
+ delimiterPos = value.find(delimiter, prevDelimiterPos); |
+ } |
+ tokenHandler(value.begin() + prevDelimiterPos, value.end()); |
+} |
+ |
+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.
|
+ const std::function<bool(const std::wstring& name, const std::wstring& value)>& parameterHandler) |
+{ |
+ 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.
|
+ { |
+ return; |
+ } |
+ auto unparsedParameterHandler = [¶meterHandler](std::wstring::const_iterator 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
|
+ { |
+ auto assignSignIt = std::find(begin, end, L'='); |
+ auto valueBeginsAt = assignSignIt != end ? assignSignIt + 1 : assignSignIt; |
+ return parameterHandler(std::wstring(begin, assignSignIt), std::wstring(valueBeginsAt, end)); |
+ }; |
+ SplitString(queryString, L'&', unparsedParameterHandler); |
+} |