From 4d0ba8108e90d056d9fb5566102bca3adf65ff59 Mon Sep 17 00:00:00 2001 From: Manuel Reimer Date: Fri, 9 Nov 2018 22:01:41 +0100 Subject: Improved trim function --- glcddrivers/common.c | 24 +++++++----------------- glcdgraphics/common.c | 24 +++++++----------------- 2 files changed, 14 insertions(+), 34 deletions(-) diff --git a/glcddrivers/common.c b/glcddrivers/common.c index 540bbdb..db5288b 100644 --- a/glcddrivers/common.c +++ b/glcddrivers/common.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include @@ -199,23 +200,12 @@ void clip(int & value, int min, int max) std::string trim(const std::string & s) { - std::string::size_type start, end; - - start = 0; - while (start < s.length()) - { - if (!isspace(s[start])) - break; - start++; - } - end = s.length() - 1; - while (end > start) - { - if (!isspace(s[end])) - break; - end--; - } - return s.substr(start, end - start + 1); + std::string::size_type left, right; + left = std::find_if_not(s.begin(), s.end(), isspace) - s.begin(); + if (left == s.length()) // String consists of space characters only + return ""; + right = std::find_if_not(s.rbegin(), s.rend(), isspace) - s.rbegin(); + return s.substr(left, s.length() - left - right); } } // end of namespace diff --git a/glcdgraphics/common.c b/glcdgraphics/common.c index 0fd78b8..d1d1944 100644 --- a/glcdgraphics/common.c +++ b/glcdgraphics/common.c @@ -12,6 +12,7 @@ #include #include +#include #include "common.h" @@ -40,23 +41,12 @@ void sort(int & value1, int & value2) std::string trim(const std::string & s) { - std::string::size_type start, end; - - start = 0; - while (start < s.length()) - { - if (!isspace(s[start])) - break; - start++; - } - end = s.length() - 1; - while (end > start) - { - if (!isspace(s[end])) - break; - end--; - } - return s.substr(start, end - start + 1); + std::string::size_type left, right; + left = std::find_if_not(s.begin(), s.end(), isspace) - s.begin(); + if (left == s.length()) // String consists of space characters only + return ""; + right = std::find_if_not(s.rbegin(), s.rend(), isspace) - s.rbegin(); + return s.substr(left, s.length() - left - right); } -- cgit v1.2.3