[opengtl-commits] [233] introduce a split function |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/opengtl-commits Archives
]
Revision: 233
Author: cyrille
Date: 2008-06-25 21:45:10 +0200 (Wed, 25 Jun 2008)
Log Message:
-----------
introduce a split function
Modified Paths:
--------------
trunk/OpenGTL/OpenGTL/GTLCore/String.cpp
trunk/OpenGTL/OpenGTL/GTLCore/String.h
trunk/OpenGTL/OpenGTL/GTLCore/tests/TestString.h
Modified: trunk/OpenGTL/OpenGTL/GTLCore/String.cpp
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/String.cpp 2008-06-25 19:44:41 UTC (rev 232)
+++ trunk/OpenGTL/OpenGTL/GTLCore/String.cpp 2008-06-25 19:45:10 UTC (rev 233)
@@ -28,6 +28,11 @@
{
}
+String::String(char c)
+{
+ *((std::string*)this) = c;
+}
+
String::String(const char* c) : std::string(c)
{
}
@@ -122,3 +127,37 @@
return *this;
}
+std::list<String> String::split( const String& _separators , bool _allowEmpty) const
+{
+ std::list<String> separatorsList;
+ for( size_t i = 0; i < _separators.length(); ++i)
+ {
+ separatorsList.push_back( _separators[ i ] );
+ }
+ return split( separatorsList, _allowEmpty );
+}
+
+std::list<String> String::split( const std::list<String>& _separators, bool _allowEmpty) const
+{
+ std::list<String> result;
+ int lastPos = 0;
+ for( size_t i = 0; i < length(); ++i )
+ {
+ String::const_reference cc = (*this)[i];
+ for( std::list<String>::const_iterator it = _separators.begin();
+ it != _separators.end(); ++it)
+ {
+ if( (*it)[0] == cc )
+ {
+ if( i - lastPos > 0 )
+ {
+ result.push_back( substr( lastPos, i - lastPos ) );
+ }
+ lastPos = i + 1;
+ break;
+ }
+ }
+ }
+ result.push_back( substr( lastPos, length() - lastPos ) );
+ return result;
+}
Modified: trunk/OpenGTL/OpenGTL/GTLCore/String.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/String.h 2008-06-25 19:44:41 UTC (rev 232)
+++ trunk/OpenGTL/OpenGTL/GTLCore/String.h 2008-06-25 19:45:10 UTC (rev 233)
@@ -21,30 +21,98 @@
#define _GTLCORE_STRING_H_
#include <string>
+#include <list>
namespace GTLCore {
-
+ /**
+ * String class which provides convenient function on top of \ref std::string .
+ * @ingroup GTLCore
+ */
class String : public std::string {
public:
+ /**
+ * Construct an empty string.
+ */
String();
+ /**
+ * Construct a \ref String from a single character.
+ */
+ String(char );
+ /**
+ * Construct a \ref String from a C string.
+ */
String(const char* );
+ /**
+ * Construct a \ref String from a C++ string.
+ */
String(const std::string& );
+ /**
+ * Construct a \ref String from an other \ref String .
+ */
String(const String& );
public:
+ /**
+ * Append a C string to the current \ref String, and return it the
+ * result.
+ */
String& append(const char* );
+ /**
+ * Append a C++ string to the current \ref String, and return the
+ * result.
+ */
String& append(const std::string& );
+ /**
+ * Append a \ref String to the current \ref String, and return the
+ * result.
+ */
String& append(const String& );
public:
+ /**
+ * Attempt to convert the string to an integer.
+ */
int toInt() const;
+ /**
+ * Attempt to convert the string to a float.
+ */
float toFloat() const;
+ /**
+ * @return the n first characters
+ */
String head(int n) const;
+ /**
+ * @return the n last characters
+ */
String tail(int n) const;
+ /**
+ * @return a lower case version of the string.
+ */
String toLower() const;
+ /**
+ * @return a upper case version of the string.
+ */
String toUpper() const;
+ /**
+ * Split the \ref String along the given separators
+ * @param _separators list of separators given in a string (one character for one separator)
+ * @return a list of \ref String
+ */
+ std::list<String> split( const String& _separators, bool _allowEmpty = false) const;
+ /**
+ * Split the \ref String along the given separators
+ * @param _separators the list of separators
+ * @return a list of \ref String
+ */
+ std::list<String> split( const std::list<String>& _separators, bool _allowEmpty = false) const;
public:
String& operator=(char c);
public:
+ /**
+ * @return a \ref String containing the number
+ */
static String number(int );
+ /**
+ * @return a \ref String containing the number
+ */
static String number(float );
};
Modified: trunk/OpenGTL/OpenGTL/GTLCore/tests/TestString.h
===================================================================
--- trunk/OpenGTL/OpenGTL/GTLCore/tests/TestString.h 2008-06-25 19:44:41 UTC (rev 232)
+++ trunk/OpenGTL/OpenGTL/GTLCore/tests/TestString.h 2008-06-25 19:45:10 UTC (rev 233)
@@ -78,6 +78,26 @@
}
};
+class TestStringSplit : public GTLTest::Case {
+ public:
+ TestStringSplit() : GTLTest::Case("Split")
+ {
+ }
+ virtual void runTest()
+ {
+ GTLCore::String hello("a,b;cd,efg;hij;jkl;mnop");
+ std::list<GTLCore::String> res = hello.split(",;.");
+ std::list<GTLCore::String>::iterator it = res.begin();
+ GTLTEST_CHECK_EQUAL( *it, "a"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "b"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "cd"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "efg"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "hij"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "jkl"); ++it;
+ GTLTEST_CHECK_EQUAL( *it, "mnop");
+ }
+};
+
class TestString : public GTLTest::Suite {
public:
TestString() : GTLTest::Suite("String")
@@ -86,5 +106,6 @@
addCase( new TestStringNumber);
addCase( new TestStringLowerUpper );
addCase( new TestStringHeadTail );
+ addCase( new TestStringSplit );
}
};