From 061ebc34468c37d27524352de56faaf9df742556 Mon Sep 17 00:00:00 2001 From: Caolán McNamara Date: Mon, 15 Aug 2011 00:22:23 +0100 Subject: add a getToken wrapper for extracting a single token painlessly --- comphelper/inc/comphelper/string.hxx | 28 ++++++++++++++++++++++++++++ comphelper/qa/string/test_string.cxx | 30 ++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'comphelper') diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index 886074dc5682..c576a24ae6f5 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -134,6 +134,34 @@ COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn, COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn, const rtl::OUString &rSearch, const rtl::OUString &rReplace); +/** Returns a token in the OString + + @param token the number of the token to return + @param cTok the character which seperate the tokens. + @return the token if token is negative or doesn't exist an empty token + is returned +*/ +COMPHELPER_DLLPUBLIC inline rtl::OString getToken(const rtl::OString &rIn, + sal_Int32 nToken, sal_Char cTok) SAL_THROW(()) +{ + sal_Int32 nIndex = 0; + return rIn.getToken(nToken, cTok, nIndex); +} + +/** Returns a token in the OUString + + @param token the number of the token to return + @param cTok the character which seperate the tokens. + @return the token if token is negative or doesn't exist an empty token + is returned +*/ +COMPHELPER_DLLPUBLIC inline rtl::OUString getToken(const rtl::OUString &rIn, + sal_Int32 nToken, sal_Unicode cTok) SAL_THROW(()) +{ + sal_Int32 nIndex = 0; + return rIn.getToken(nToken, cTok, nIndex); +} + /** Convert a sequence of strings to a single comma separated string. Note that no escaping of commas or anything fancy is done. diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index 8dbd056589c4..4d3d50d8697b 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -44,12 +44,14 @@ public: void test(); void testNatural(); void testReplace(); + void testToken(); void testDecimalStringToNumber(); CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST(test); CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testReplace); + CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST_SUITE_END(); }; @@ -311,11 +313,39 @@ void TestString::testReplace() CPPUNIT_ASSERT(aOut.isEmpty()); aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa foo aaa foo bbb")); + aOut = ::comphelper::string::replace(aIn, rtl::OString(RTL_CONSTASCII_STRINGPARAM("foo")), rtl::OString(RTL_CONSTASCII_STRINGPARAM("bar"))); CPPUNIT_ASSERT(aOut.equalsL( RTL_CONSTASCII_STRINGPARAM("aaa bar aaa bar bbb"))); + + aOut = ::comphelper::string::replace(aIn, + rtl::OString(' '), + rtl::OString()); + CPPUNIT_ASSERT(aOut.equalsL( + RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb"))); +} + +void TestString::testToken() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::getToken(aIn, -1, '.'); + CPPUNIT_ASSERT(aOut.isEmpty()); + + aOut = ::comphelper::string::getToken(aIn, 0, '.'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("10"))); + + aOut = ::comphelper::string::getToken(aIn, 1, '.'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("11"))); + + aOut = ::comphelper::string::getToken(aIn, 2, '.'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("12"))); + + aOut = ::comphelper::string::getToken(aIn, 3, '.'); + CPPUNIT_ASSERT(aOut.isEmpty()); } CPPUNIT_TEST_SUITE_REGISTRATION(TestString); -- cgit