summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2011-08-15 00:22:23 +0100
committerCaolán McNamara <caolanm@redhat.com>2011-08-15 09:21:49 +0100
commit061ebc34468c37d27524352de56faaf9df742556 (patch)
tree96d924d1f23690029ba13598290858881bdb2495 /comphelper
parent90fe8dadaaad07aee2ec513eab1ad75bbf306cb3 (diff)
add a getToken wrapper for extracting a single token painlessly
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/inc/comphelper/string.hxx28
-rw-r--r--comphelper/qa/string/test_string.cxx30
2 files changed, 58 insertions, 0 deletions
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);