diff options
author | Manuel Reimer <manuel.reimer@gmx.de> | 2018-11-09 22:01:41 +0100 |
---|---|---|
committer | Manuel Reimer <manuel.reimer@gmx.de> | 2018-11-09 22:01:41 +0100 |
commit | 4d0ba8108e90d056d9fb5566102bca3adf65ff59 (patch) | |
tree | 237b351253e339d43cdc642c62ec6c2eabd3a9ac /glcdgraphics | |
parent | 35d890d370e17a583835f501f2c418b16e7dfc13 (diff) | |
download | graphlcd-base-4d0ba8108e90d056d9fb5566102bca3adf65ff59.tar.gz graphlcd-base-4d0ba8108e90d056d9fb5566102bca3adf65ff59.tar.bz2 |
Improved trim function
Diffstat (limited to 'glcdgraphics')
-rw-r--r-- | glcdgraphics/common.c | 24 |
1 files changed, 7 insertions, 17 deletions
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); } |