@@ -8,6 +8,7 @@ Author: Daniel Poetzl
88
99#include < cassert>
1010#include < cctype>
11+ #include < algorithm>
1112
1213#include " string_utils.h"
1314
@@ -25,22 +26,18 @@ Author: Daniel Poetzl
2526
2627std::string strip_string (const std::string &s)
2728{
28- std::string::size_type n=s. length () ;
29+ auto pred=[]( char c){ return std::isspace (c); } ;
2930
30- // find first non-space char
31- unsigned i;
32- for (i=0 ; i<n; i++)
33- {
34- if (!std::isspace (s[i]))
35- break ;
36- }
37- if (i==n)
31+ std::string::const_iterator left
32+ =std::find_if_not (s.begin (), s.end (), pred);
33+ if (left==s.end ())
3834 return " " ;
3935
40- std::string::const_reverse_iterator r_it;
41- for (r_it=s.rbegin (); std::isspace (*r_it); r_it++);
36+ std::string::size_type i=std::distance (s.begin (), left);
4237
43- unsigned j=std::distance (r_it, s.rend ())-1 ;
38+ std::string::const_reverse_iterator right
39+ =std::find_if_not (s.rbegin (), s.rend (), pred);
40+ std::string::size_type j=std::distance (right, s.rend ())-1 ;
4441
4542 return s.substr (i, (j-i+1 ));
4643}
@@ -76,12 +73,12 @@ void split_string(
7673 std::string::size_type n=s.length ();
7774 assert (n>0 );
7875
79- unsigned start=0 ;
80- unsigned i;
76+ std::string::size_type start=0 ;
77+ std::string::size_type i;
8178
82- for (i=0 ; i<n; i++)
79+ for (i=0 ; i<n; i++)
8380 {
84- if (s[i]==delim)
81+ if (s[i]==delim)
8582 {
8683 std::string new_s=s.substr (start, i-start);
8784
@@ -90,6 +87,7 @@ void split_string(
9087
9188 if (!remove_empty || !new_s.empty ())
9289 result.push_back (new_s);
90+
9391 start=i+1 ;
9492 }
9593 }
@@ -102,7 +100,8 @@ void split_string(
102100 if (!remove_empty || !new_s.empty ())
103101 result.push_back (new_s);
104102
105- assert (!result.empty ());
103+ if (result.empty ())
104+ result.push_back (" " );
106105}
107106
108107/* ******************************************************************\
0 commit comments