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 |