summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorManuel Reimer <manuel.reimer@gmx.de>2018-11-09 22:04:33 +0100
committerManuel Reimer <manuel.reimer@gmx.de>2018-11-09 22:04:33 +0100
commit445b57588da52b3e10a1b759e7723afe6687eb7a (patch)
tree8fb8d8939d762c804a6f6661fff73e7aabde83ce
parenta6c8e3d4383ae164e26149888bbdc91cbd448ba8 (diff)
downloadvdr-plugin-graphlcd-445b57588da52b3e10a1b759e7723afe6687eb7a.tar.gz
vdr-plugin-graphlcd-445b57588da52b3e10a1b759e7723afe6687eb7a.tar.bz2
Improved trim function
-rw-r--r--strfct.c24
1 files changed, 7 insertions, 17 deletions
diff --git a/strfct.c b/strfct.c
index adafae1..445353e 100644
--- a/strfct.c
+++ b/strfct.c
@@ -28,6 +28,7 @@
#include <string.h>
#include <ctype.h>
+#include <algorithm>
#include "strfct.h"
@@ -45,23 +46,12 @@ char * strncopy(char * dest , const char * src , size_t n)
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 >= 0)
- {
- 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);
}
std::string compactspace(const std::string & s)