summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Reimer <manuel.reimer@gmx.de>2018-11-09 22:01:41 +0100
committerManuel Reimer <manuel.reimer@gmx.de>2018-11-09 22:01:41 +0100
commit4d0ba8108e90d056d9fb5566102bca3adf65ff59 (patch)
tree237b351253e339d43cdc642c62ec6c2eabd3a9ac
parent35d890d370e17a583835f501f2c418b16e7dfc13 (diff)
downloadgraphlcd-base-4d0ba8108e90d056d9fb5566102bca3adf65ff59.tar.gz
graphlcd-base-4d0ba8108e90d056d9fb5566102bca3adf65ff59.tar.bz2
Improved trim function
-rw-r--r--glcddrivers/common.c24
-rw-r--r--glcdgraphics/common.c24
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);
}