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

Unified Diff: src/shared/Utils.h

Issue 4974480757620736: Issue #1356 - Improve detection of the issuer of the request (Closed)
Patch Set: Refactoring lambda and ExtractHttpHeaders functions. Coding style cleanup. Created Nov. 3, 2014, 11:36 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/shared/Utils.h
===================================================================
--- a/src/shared/Utils.h
+++ b/src/shared/Utils.h
@@ -42,12 +42,42 @@
template<class T>
T TrimString(T text)
{
+ std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_type>, std::placeholders::_1, std::locale::classic());
+ return TrimString(text, isspace);
+}
+
+template<class T>
+T TrimString(T text, std::function<bool(typename T::value_type)> differentiator)
sergei 2014/11/03 14:17:46 const references are missed.
+{
// Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
T trimmed(text);
- std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_type>, std::placeholders::_1, std::locale::classic());
- trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), std::not1(isspace)));
- trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace)).base(), trimmed.end());
+ trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), std::not1(differentiator)));
+ trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(differentiator)).base(), trimmed.end());
return trimmed;
}
+
+template <class T>
+T CreateConstString(std::string val);
sergei 2014/11/03 14:17:46 `cosnt std::string&`
+
+template <class T>
+T ExtractHttpHeader(const T& allHeaders, T& targetHeaderName, const T& delimiter = CreateConstString<T>("\n"))
+{
+ std::function<bool(T::value_type)> ispunct = std::bind(&std::ispunct<T::value_type>, std::placeholders::_1, std::locale::classic());
+ targetHeaderName = TrimString(targetHeaderName, ispunct);
+ const T colon = CreateConstString<T>(":");
+ targetHeaderName.append(colon);
+ auto targetHeaderBeginsAt = allHeaders.find(targetHeaderName);
+ if (targetHeaderBeginsAt == T::npos)
+ {
+ return T();
+ }
+ targetHeaderBeginsAt += targetHeaderName.length();
+ auto targetHeaderEndsAt = allHeaders.find(delimiter, targetHeaderBeginsAt);
+ if (targetHeaderEndsAt == T::npos)
+ {
+ return T();
+ }
+ return allHeaders.substr(targetHeaderBeginsAt, targetHeaderEndsAt - targetHeaderBeginsAt);
+}
#endif // UTILS_H

Powered by Google App Engine
This is Rietveld