diff options
author | Manuel Reimer <manuel.reimer@gmx.de> | 2018-11-09 22:04:33 +0100 |
---|---|---|
committer | Manuel Reimer <manuel.reimer@gmx.de> | 2018-11-09 22:04:33 +0100 |
commit | 445b57588da52b3e10a1b759e7723afe6687eb7a (patch) | |
tree | 8fb8d8939d762c804a6f6661fff73e7aabde83ce | |
parent | a6c8e3d4383ae164e26149888bbdc91cbd448ba8 (diff) | |
download | vdr-plugin-graphlcd-445b57588da52b3e10a1b759e7723afe6687eb7a.tar.gz vdr-plugin-graphlcd-445b57588da52b3e10a1b759e7723afe6687eb7a.tar.bz2 |
Improved trim function
-rw-r--r-- | strfct.c | 24 |
1 files changed, 7 insertions, 17 deletions
@@ -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) |