diff options
-rw-r--r-- | glcddrivers/common.c | 24 | ||||
-rw-r--r-- | 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 <sched.h> #include <time.h> #include <unistd.h> +#include <algorithm> #include <sys/time.h> #include <sys/resource.h> @@ -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 <ctype.h> #include <syslog.h> +#include <algorithm> #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); } |