diff options
author | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-05-03 12:59:05 +0300 |
---|---|---|
committer | Markus Mohrhard <markus.mohrhard@googlemail.com> | 2016-06-18 17:02:08 +0200 |
commit | 0d4dcf380033902fe4eb70425138e419fb711271 (patch) | |
tree | d1c7ddc3d62d7bc787e8b84d942ca5e0b333de39 | |
parent | fbd80e546c51875ff310462312090abb84076b35 (diff) |
add comphelper::string::split
Change-Id: Iccc989a786e8e7b8dca1996b635248d7bf7fc5d8
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 12 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 29 | ||||
-rw-r--r-- | include/comphelper/string.hxx | 3 |
3 files changed, 35 insertions, 9 deletions
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index 97814da5fe4c..70998759e04f 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -43,6 +43,7 @@ public: void testDecimalStringToNumber(); void testIsdigitAsciiString(); void testReverseString(); + void testSplit(); CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST(testNatural); @@ -55,6 +56,7 @@ public: CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); CPPUNIT_TEST(testReverseString); + CPPUNIT_TEST(testSplit); CPPUNIT_TEST_SUITE_END(); }; @@ -394,6 +396,16 @@ void TestString::testReverseString() CPPUNIT_ASSERT(aOut == "CBA"); } +void TestString::testSplit() +{ + OUString aIn("CTRL+ALT+F1"); + std::vector<OUString> aRet = ::comphelper::string::split(aIn, '+'); + CPPUNIT_ASSERT_EQUAL(size_t(3), aRet.size()); + CPPUNIT_ASSERT_EQUAL(OUString("CTRL"), aRet[0]); + CPPUNIT_ASSERT_EQUAL(OUString("ALT"), aRet[1]); + CPPUNIT_ASSERT_EQUAL(OUString("F1"), aRet[2]); +} + CPPUNIT_TEST_SUITE_REGISTRATION(TestString); } diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index 2dbbc0c90d11..db035eb22967 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -253,19 +253,30 @@ OUString convertCommaSeparated( return buf.makeStringAndClear(); } -uno::Sequence< OUString > - convertCommaSeparated( OUString const& i_rString ) +std::vector<OUString> + split(const OUString& rStr, sal_Unicode cSeparator) { std::vector< OUString > vec; sal_Int32 idx = 0; - do { - OUString kw = - i_rString.getToken(0, static_cast<sal_Unicode> (','), idx); - kw = kw.trim(); - if (!kw.isEmpty()) { - vec.push_back(kw); - } + do + { + OUString kw = + rStr.getToken(0, cSeparator, idx); + kw = kw.trim(); + if (!kw.isEmpty()) + { + vec.push_back(kw); + } + } while (idx >= 0); + + return vec; +} + +uno::Sequence< OUString > + convertCommaSeparated( OUString const& i_rString ) +{ + std::vector< OUString > vec = split(i_rString, ','); return comphelper::containerToSequence(vec); } diff --git a/include/comphelper/string.hxx b/include/comphelper/string.hxx index c0fee8fb558c..f1d253f30401 100644 --- a/include/comphelper/string.hxx +++ b/include/comphelper/string.hxx @@ -319,6 +319,9 @@ COMPHELPER_DLLPUBLIC OString join(const OString& rSeparator, const std::vector<O COMPHELPER_DLLPUBLIC sal_uInt32 decimalStringToNumber( OUString const & str ); +COMPHELPER_DLLPUBLIC std::vector<OUString> + split(const OUString& rString, const sal_Unicode cSeparator); + /** Convert a single comma separated string to a sequence of strings. Note that no escaping of commas or anything fancy is done. |