@@ -7,11 +7,16 @@ Author: Daniel Poetzl
77\*******************************************************************/
88
99#include " string_utils.h"
10+ #include " invariant.h"
1011
1112#include < cassert>
1213#include < cctype>
1314#include < algorithm>
1415
16+ // / Remove all whitespace characters from either end of a string. Whitespace
17+ // / in the middle of the string is left unchanged
18+ // / \param s: the string to strip
19+ // / \return The stripped string
1520std::string strip_string (const std::string &s)
1621{
1722 auto pred=[](char c){ return std::isspace (c); };
@@ -30,15 +35,26 @@ std::string strip_string(const std::string &s)
3035 return s.substr (i, (j-i+1 ));
3136}
3237
38+ // / Given a string s, split into a sequence of substrings when separated by
39+ // / specified delimiter.
40+ // / \param s: The string to split up
41+ // / \param delim: The character to use as the delimiter
42+ // / \param [out] result: The sub strings. Must be empty.
43+ // / \param strip: If true, strip_string will be used on each element, removing
44+ // / whitespace from the beginning and end of each element
45+ // / \param remove_empty: If true, all empty-string elements will be removed.
46+ // / This is applied after strip so whitespace only elements will be removed if
47+ // / both are set to true
3348void split_string (
3449 const std::string &s,
3550 char delim,
3651 std::vector<std::string> &result,
3752 bool strip,
3853 bool remove_empty)
3954{
40- assert (result.empty ());
41- assert (!std::isspace (delim));
55+ PRECONDITION (result.empty ());
56+ // delim can't be a space character if using strip
57+ PRECONDITION (!std::isspace (delim) || !strip);
4258
4359 if (s.empty ())
4460 {
@@ -47,7 +63,7 @@ void split_string(
4763 }
4864
4965 std::string::size_type n=s.length ();
50- assert (n> 0 );
66+ INVARIANT (n > 0 , " Empty string case should already be handled " );
5167
5268 std::string::size_type start=0 ;
5369 std::string::size_type i;
@@ -87,7 +103,8 @@ void split_string(
87103 std::string &right,
88104 bool strip)
89105{
90- assert (!std::isspace (delim));
106+ // delim can't be a space character if using strip
107+ PRECONDITION (!std::isspace (delim) || !strip);
91108
92109 std::vector<std::string> result;
93110
0 commit comments