@@ -8,6 +8,7 @@ Author: Daniel Poetzl
8
8
9
9
#include < cassert>
10
10
#include < cctype>
11
+ #include < algorithm>
11
12
12
13
#include " string_utils.h"
13
14
@@ -25,22 +26,18 @@ Author: Daniel Poetzl
25
26
26
27
std::string strip_string (const std::string &s)
27
28
{
28
- std::string::size_type n=s. length () ;
29
+ auto pred=[]( char c){ return std::isspace (c); } ;
29
30
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 ())
38
34
return " " ;
39
35
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);
42
37
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 ;
44
41
45
42
return s.substr (i, (j-i+1 ));
46
43
}
@@ -76,12 +73,12 @@ void split_string(
76
73
std::string::size_type n=s.length ();
77
74
assert (n>0 );
78
75
79
- unsigned start=0 ;
80
- unsigned i;
76
+ std::string::size_type start=0 ;
77
+ std::string::size_type i;
81
78
82
- for (i=0 ; i<n; i++)
79
+ for (i=0 ; i<n; i++)
83
80
{
84
- if (s[i]==delim)
81
+ if (s[i]==delim)
85
82
{
86
83
std::string new_s=s.substr (start, i-start);
87
84
@@ -90,6 +87,7 @@ void split_string(
90
87
91
88
if (!remove_empty || !new_s.empty ())
92
89
result.push_back (new_s);
90
+
93
91
start=i+1 ;
94
92
}
95
93
}
@@ -102,7 +100,8 @@ void split_string(
102
100
if (!remove_empty || !new_s.empty ())
103
101
result.push_back (new_s);
104
102
105
- assert (!result.empty ());
103
+ if (result.empty ())
104
+ result.push_back (" " );
106
105
}
107
106
108
107
/* ******************************************************************\
0 commit comments