summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-31 17:14:23 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-31 18:38:18 (GMT)
commitb3e6f21953d5d4b55ab9ffde78e1a1ff2a59dad1 (patch)
tree5abcfac22759c56c53a4de9bdf6f7247bb079b07 /Swiften/Base
parentaa60aa80d2d170a536c246ef6c221f92de7dd8ed (diff)
downloadswift-contrib-b3e6f21953d5d4b55ab9ffde78e1a1ff2a59dad1.zip
swift-contrib-b3e6f21953d5d4b55ab9ffde78e1a1ff2a59dad1.tar.bz2
Add String::split().
Diffstat (limited to 'Swiften/Base')
-rw-r--r--Swiften/Base/String.cpp17
-rw-r--r--Swiften/Base/String.h2
-rw-r--r--Swiften/Base/UnitTest/StringTest.cpp10
3 files changed, 29 insertions, 0 deletions
diff --git a/Swiften/Base/String.cpp b/Swiften/Base/String.cpp
index 3495d9a..cc989f6 100644
--- a/Swiften/Base/String.cpp
+++ b/Swiften/Base/String.cpp
@@ -96,4 +96,21 @@ String String::getLowerCase() const {
return String(lower);
}
+std::vector<String> String::split(char c) const {
+ assert((c & 0x80) == 0);
+ std::vector<String> result;
+ String accumulator;
+ for (size_t i = 0; i < data_.size(); ++i) {
+ if (data_[i] == c) {
+ result.push_back(accumulator);
+ accumulator = "";
+ }
+ else {
+ accumulator += data_[i];
+ }
+ }
+ result.push_back(accumulator);
+ return result;
+}
+
}
diff --git a/Swiften/Base/String.h b/Swiften/Base/String.h
index 247a8a3..336a663 100644
--- a/Swiften/Base/String.h
+++ b/Swiften/Base/String.h
@@ -37,6 +37,8 @@ namespace Swift {
*/
std::pair<String,String> getSplittedAtFirst(char c) const;
+ std::vector<String> split(char c) const;
+
size_t getLength() const;
String getLowerCase() const;
diff --git a/Swiften/Base/UnitTest/StringTest.cpp b/Swiften/Base/UnitTest/StringTest.cpp
index 2fa9f54..1dd44fb 100644
--- a/Swiften/Base/UnitTest/StringTest.cpp
+++ b/Swiften/Base/UnitTest/StringTest.cpp
@@ -24,6 +24,7 @@ class StringTest : public CppUnit::TestFixture
CPPUNIT_TEST(testReplaceAll_ConsecutiveChars);
CPPUNIT_TEST(testReplaceAll_MatchingReplace);
CPPUNIT_TEST(testGetLowerCase);
+ CPPUNIT_TEST(testSplit);
CPPUNIT_TEST_SUITE_END();
public:
@@ -148,6 +149,15 @@ class StringTest : public CppUnit::TestFixture
CPPUNIT_ASSERT_EQUAL(String("abcd e"), testling.getLowerCase());
}
+
+ void testSplit() {
+ std::vector<String> result = String("abc def ghi").split(' ');
+
+ CPPUNIT_ASSERT_EQUAL(3, static_cast<int>(result.size()));
+ CPPUNIT_ASSERT_EQUAL(String("abc"), result[0]);
+ CPPUNIT_ASSERT_EQUAL(String("def"), result[1]);
+ CPPUNIT_ASSERT_EQUAL(String("ghi"), result[2]);
+ }
};
CPPUNIT_TEST_SUITE_REGISTRATION(StringTest);