From b3e6f21953d5d4b55ab9ffde78e1a1ff2a59dad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Fri, 31 Jul 2009 19:14:23 +0200 Subject: Add String::split(). 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::split(char c) const { + assert((c & 0x80) == 0); + std::vector 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 getSplittedAtFirst(char c) const; + std::vector 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 result = String("abc def ghi").split(' '); + + CPPUNIT_ASSERT_EQUAL(3, static_cast(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); -- cgit v0.10.2-6-g49f6