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

Side by Side 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.
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 #ifndef UTILS_H 1 #ifndef UTILS_H
2 #define UTILS_H 2 #define UTILS_H
3 3
4 #include <algorithm> 4 #include <algorithm>
5 #include <locale> 5 #include <locale>
6 #include <functional> 6 #include <functional>
7 #include <string> 7 #include <string>
8 8
9 #define WM_ALREADY_UP_TO_DATE WM_APP+1 9 #define WM_ALREADY_UP_TO_DATE WM_APP+1
10 #define WM_UPDATE_CHECK_ERROR WM_APP+2 10 #define WM_UPDATE_CHECK_ERROR WM_APP+2
(...skipping 24 matching lines...) Expand all
35 35
36 std::string ToUtf8String(const std::wstring& str); 36 std::string ToUtf8String(const std::wstring& str);
37 std::wstring ToUtf16String(const std::string& str); 37 std::wstring ToUtf16String(const std::string& str);
38 std::wstring GetDllDir(); 38 std::wstring GetDllDir();
39 std::wstring GetAppDataPath(); 39 std::wstring GetAppDataPath();
40 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement); 40 void ReplaceString(std::wstring& input, const std::wstring placeholder, const st d::wstring replacement);
41 41
42 template<class T> 42 template<class T>
43 T TrimString(T text) 43 T TrimString(T text)
44 { 44 {
45 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_ type>, std::placeholders::_1, std::locale::classic());
46 return TrimString(text, isspace);
47 }
48
49 template<class T>
50 T TrimString(T text, std::function<bool(typename T::value_type)> differentiator)
sergei 2014/11/03 14:17:46 const references are missed.
51 {
45 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring 52 // Via http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-st dstring
46 T trimmed(text); 53 T trimmed(text);
47 std::function<bool(T::value_type)> isspace = std::bind(&std::isspace<T::value_ type>, std::placeholders::_1, std::locale::classic()); 54 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(differentiator)));
48 trimmed.erase(trimmed.begin(), std::find_if(trimmed.begin(), trimmed.end(), st d::not1(isspace))); 55 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(differe ntiator)).base(), trimmed.end());
49 trimmed.erase(std::find_if(trimmed.rbegin(), trimmed.rend(), std::not1(isspace )).base(), trimmed.end());
50 return trimmed; 56 return trimmed;
51 } 57 }
52 58
59 template <class T>
60 T CreateConstString(std::string val);
sergei 2014/11/03 14:17:46 `cosnt std::string&`
61
62 template <class T>
63 T ExtractHttpHeader(const T& allHeaders, T& targetHeaderName, const T& delimiter = CreateConstString<T>("\n"))
64 {
65 std::function<bool(T::value_type)> ispunct = std::bind(&std::ispunct<T::value_ type>, std::placeholders::_1, std::locale::classic());
66 targetHeaderName = TrimString(targetHeaderName, ispunct);
67 const T colon = CreateConstString<T>(":");
68 targetHeaderName.append(colon);
69 auto targetHeaderBeginsAt = allHeaders.find(targetHeaderName);
70 if (targetHeaderBeginsAt == T::npos)
71 {
72 return T();
73 }
74 targetHeaderBeginsAt += targetHeaderName.length();
75 auto targetHeaderEndsAt = allHeaders.find(delimiter, targetHeaderBeginsAt);
76 if (targetHeaderEndsAt == T::npos)
77 {
78 return T();
79 }
80 return allHeaders.substr(targetHeaderBeginsAt, targetHeaderEndsAt - targetHead erBeginsAt);
81 }
82
53 #endif // UTILS_H 83 #endif // UTILS_H
OLDNEW

Powered by Google App Engine
This is Rietveld