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

Unified Diff: src/shared/Utils.h

Issue 29331055: Issue #1234 - Remove 'CString' from PluginFilter.* (Closed)
Patch Set: address comments Created Nov. 30, 2015, 3:51 p.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
@@ -23,6 +23,7 @@
#include <functional>
#include <string>
#include <vector>
+#include <wtypes.h>
#define WM_ALREADY_UP_TO_DATE WM_APP+1
#define WM_UPDATE_CHECK_ERROR WM_APP+2
@@ -51,6 +52,7 @@
bool IsWindowsVistaOrLater();
bool IsWindows8OrLater();
+std::wstring ToWstring(const BSTR b);
std::string ToUtf8String(const std::wstring& str);
std::wstring ToUtf16String(const std::string& str);
std::vector<std::wstring> ToUtf16Strings(const std::vector<std::string>& value);
@@ -67,15 +69,55 @@
std::wstring GetQueryString(const std::wstring& url);
+/*
+ * Th method used below for trimming strings is taken from here:
Oleksandr 2015/12/04 01:03:16 Nit: "The" method?
Eric 2015/12/06 17:39:57 Done.
+ * http://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
+ */
+template<class C>
+bool isNotWhitespace(C x)
+{
+ return !std::isspace(x, std::locale::classic());
+};
+
+template<class T>
+void TrimStringInPlaceLeft(T& text)
+{
+ text.erase(text.begin(), std::find_if(text.begin(), text.end(), isNotWhitespace<T::value_type>));
+}
+
+template<class T>
+void TrimStringInPlaceRight(T& text)
+{
+ text.erase(std::find_if(text.rbegin(), text.rend(), isNotWhitespace<T::value_type>).base(), text.end());
+}
+
+template<class T>
+void TrimStringInPlace(T& text)
+{
+ TrimStringInPlaceRight(text);
+ TrimStringInPlaceLeft(text);
+}
+
+template<class T>
+T TrimStringLeft(T text)
+{
+ TrimStringInPlaceLeft(text);
+ return text;
+}
+
+template<class T>
+T TrimStringRight(T text)
+{
+ TrimStringInPlaceRight(text);
+ return text;
+}
+
template<class T>
T TrimString(T text)
{
- // 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());
- return trimmed;
+ TrimStringInPlaceRight(text);
+ TrimStringInPlaceLeft(text);
+ return text;
}
#endif // UTILS_H

Powered by Google App Engine
This is Rietveld