diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Base/String.cpp | 17 | ||||
-rw-r--r-- | Swiften/Base/String.h | 2 | ||||
-rw-r--r-- | Swiften/Base/UnitTest/StringTest.cpp | 10 |
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); |