From 0f8bf9b6e20a60c94384a274c218ad74e9b67d2b Mon Sep 17 00:00:00 2001 From: Johan Wikman Date: Fri, 18 May 2018 16:11:41 +0300 Subject: [PATCH] Implement mxs::[l|r]trim(std::string&); Also remove the non-used implementation in httprequest.cc --- include/maxscale/utils.hh | 35 +++++++++++++++++++++++++++++++++++ server/core/httprequest.cc | 34 ---------------------------------- 2 files changed, 35 insertions(+), 34 deletions(-) diff --git a/include/maxscale/utils.hh b/include/maxscale/utils.hh index ad0fe2cac..15b2eb8b7 100644 --- a/include/maxscale/utils.hh +++ b/include/maxscale/utils.hh @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -25,10 +26,44 @@ #include #include +#include namespace maxscale { +/** + * @brief Left trim a string. + * + * @param s The string to be trimmed. + */ +inline void ltrim(std::string &s) +{ + s.erase(s.begin(), std::find_if(s.begin(), s.end(), + std::not1(std::ptr_fun(std::isspace)))); +} + +/** + * @brief Right trim a string. + * + * @param s The string to be trimmed. + */ +inline void rtrim(std::string &s) +{ + s.erase(std::find_if(s.rbegin(), s.rend(), + std::not1(std::ptr_fun(std::isspace))).base(), s.end()); +} + +/** + * @brief Trim a string. + * + * @param s The string to be trimmed. + */ +inline void trim(std::string &s) +{ + ltrim(s); + rtrim(s); +} + /** * @class CloserTraits utils.hh * diff --git a/server/core/httprequest.cc b/server/core/httprequest.cc index a6b50b67b..835c203b2 100644 --- a/server/core/httprequest.cc +++ b/server/core/httprequest.cc @@ -27,40 +27,6 @@ using std::deque; const std::string HttpRequest::HTTP_PREFIX = "http://"; const std::string HttpRequest::HTTPS_PREFIX = "https://"; -/** TODO: Move this to a C++ string utility header */ -namespace maxscale -{ -static inline string& trim(string& str) -{ - if (str.length()) - { - if (isspace(*str.begin())) - { - string::iterator it = str.begin(); - - while (it != str.end() && isspace(*it)) - { - it++; - } - str.erase(str.begin(), it); - } - - if (isspace(*str.rbegin())) - { - string::reverse_iterator it = str.rbegin(); - while (it != str.rend() && isspace(*it)) - { - it++; - } - - str.erase(it.base(), str.end()); - } - } - - return str; -} -} - static void process_uri(string& uri, std::deque& uri_parts) { /** Clean up trailing slashes in requested resource */