+ j = i;
+
+ while (i < len && !isspace((unsigned char)str[i]))
+ {
+ ++i;
+ }
+
+ if (j < i)
+ {
+ result.push_back( str.substr(j, i-j) );
+ ++countsplit;
+ while (i < len && isspace((unsigned char)str[i]))
+ {
+ ++i;
+ }
+
+ if (maxsplit && (countsplit >= maxsplit) && i < len)
+ {
+ result.push_back( str.substr( i, len-i ) );
+ i = len;
+ }
+ }
+ }
+
+ return result;
+ }
+
+ /**
+ *
+ */
+ vector<string>
+ split( const string& str, const char* sep, int maxsplit )
+ {
+ if (sep == 0)
+ return split_whitespace( str, maxsplit );
+
+ vector<string> result;
+ int n = std::strlen( sep );
+ if (n == 0)
+ {
+ // Error: empty separator string
+ return result;
+ }
+ const char* s = str.c_str();
+ string::size_type len = str.length();
+ string::size_type i = 0;
+ string::size_type j = 0;
+ int splitcount = 0;
+
+ while (i+n <= len)
+ {
+ if (s[i] == sep[0] && (n == 1 || std::memcmp(s+i, sep, n) == 0))
+ {
+ result.push_back( str.substr(j,i-j) );
+ i = j = i + n;
+ ++splitcount;
+ if (maxsplit && (splitcount >= maxsplit))
+ break;
+ }
+ else
+ {
+ ++i;
+ }
+ }
+
+ result.push_back( str.substr(j,len-j) );
+ return result;
+ }
+
+ /**
+ * The lstrip(), rstrip() and strip() functions are implemented
+ * in do_strip() which uses an additional parameter to indicate what
+ * type of strip should occur.
+ */
+ const int LEFTSTRIP = 0;
+ const int RIGHTSTRIP = 1;
+ const int BOTHSTRIP = 2;
+
+ static string
+ do_strip( const string& s, int striptype )
+ {
+ // if (s.empty())
+ // return s;
+
+ string::size_type len = s.length();
+ string::size_type i = 0;
+ if (striptype != RIGHTSTRIP)
+ {
+ while (i < len && isspace(s[i]))
+ {
+ ++i;
+ }
+ }
+
+ string::size_type j = len;
+ if (striptype != LEFTSTRIP)
+ {
+ do
+ {
+ --j;
+ }
+ while (j >= 1 && isspace(s[j]));
+ ++j;
+ }
+
+ if (i == 0 && j == len)
+ {
+ return s;
+ }
+ else
+ {
+ return s.substr( i, j - i );
+ }
+ }
+
+ string
+ lstrip( const string& s )
+ {
+ return do_strip( s, LEFTSTRIP );
+ }
+
+ string
+ rstrip( const string& s )
+ {
+ return do_strip( s, RIGHTSTRIP );
+ }
+
+ string
+ strip( const string& s )
+ {
+ return do_strip( s, BOTHSTRIP );
+ }