lcdd/include
diff -u -r1.7 -r1.8
--- StringUtil.hh 9 Jul 2005 00:25:56 -0000 1.7
+++ StringUtil.hh 22 Sep 2005 00:55:36 -0000 1.8
@@ -1,4 +1,4 @@
-// $Header: /cvs/lcd/lcdd/include/StringUtil.hh,v 1.7 2005/07/09 00:25:56 jeremy Exp $
+// $Header: /cvs/lcd/lcdd/include/StringUtil.hh,v 1.8 2005/09/22 00:55:36 jeremy Exp $
#ifndef StringUtil_hh
#define StringUtil_hh 1
@@ -52,20 +52,12 @@
static bool toBool(std::string& s);
static bool toBool(const std::string& s);
- /*
- static std::string trimLeft(const std::string& s, const std::string& trimStr = WS_STR);
- static std::string trimRight (const std::string& s, const std::string& trimStr = WS_STR);
- */
- //static std::string trim(const std::string& s, const std::string& trimStr = WS_STR);
static void trim(std::string& str);
static std::string concatStrVec(const std::vector<std::string>& s_vec,
const std::string& sep = " ");
- /**
- * split a string from
- * http://www.codeproject.com/string/stringsplit.asp
- */
- static int split(const std::string& input, const std::string& delimiter, std::vector<std::string>& results);
+
+ static void split(const std::string& str, const std::string& delimiters, std::vector<std::string>& tokens);
};
#endif
lcdd/src
diff -u -r1.8 -r1.9
--- StringUtil.cc 16 Jul 2005 00:18:25 -0000 1.8
+++ StringUtil.cc 22 Sep 2005 00:55:36 -0000 1.9
@@ -1,4 +1,4 @@
-// $Header: /cvs/lcd/lcdd/src/StringUtil.cc,v 1.8 2005/07/16 00:18:25 jeremy Exp $
+// $Header: /cvs/lcd/lcdd/src/StringUtil.cc,v 1.9 2005/09/22 00:55:36 jeremy Exp $
#include "StringUtil.hh"
@@ -122,50 +122,24 @@
return r;
}
-int StringUtil::split(const string& input, const string& delimiter, vector<string>& results)
+void StringUtil::split(const string& str, const string& delimiters, vector<string>& tokens)
{
- int iPos = 0;
- int newPos = -1;
- int sizeS2 = delimiter.size();
- int isize = input.size();
+ // Skip delimiters at beginning.
+ string::size_type lastPos = str.find_first_not_of(delimiters, 0);
- vector<int> positions;
+ // Find first "non-delimiter".
+ string::size_type pos = str.find_first_of(delimiters, lastPos);
- newPos = input.find (delimiter, 0);
-
- if( newPos < 0 ) { return 0; }
+ while (string::npos != pos || string::npos != lastPos)
+ {
- int numFound = 0;
+ // Found a token, add it to the vector.
+ tokens.push_back(str.substr(lastPos, pos - lastPos));
- while( newPos > iPos )
- {
- numFound++;
- positions.push_back(newPos);
- iPos = newPos;
- newPos = input.find (delimiter, iPos+sizeS2+1);
- }
+ // Skip delimiters. Note the "not_of"
+ lastPos = str.find_first_not_of(delimiters, pos);
- for( int i=0; i <= (int)positions.size(); i++ )
- {
- string s;
- if( i == 0 ) { s = input.substr( i, positions[i] ); }
- int offset = positions[i-1] + sizeS2;
- if( offset < isize )
- {
- if( i == (int)positions.size() )
- {
- s = input.substr(offset);
- }
- else if( i > 0 )
- {
- s = input.substr( positions[i-1] + sizeS2,
- positions[i] - positions[i-1] - sizeS2 );
- }
+ // Find next "non-delimiter"
+ pos = str.find_first_of(delimiters, lastPos);
}
- if( s.size() > 0 )
- {
- results.push_back(s);
- }
- }
- return numFound;
}