summaryrefslogtreecommitdiff
path: root/comphelper
diff options
context:
space:
mode:
authorCaolán McNamara <caolanm@redhat.com>2012-01-02 10:55:27 +0000
committerCaolán McNamara <caolanm@redhat.com>2012-01-05 09:18:19 +0000
commitbacfd2dc4cea1a5d87658ed8592116acd931e000 (patch)
treed22172a33fdd13a440b6882a28c23ea2d639bbad /comphelper
parent6281eb0e0792da0194c07da18296e94dd944b8e5 (diff)
add a comphelper::string::getTokenCount
suitable for conversion from [Byte]String::GetTokenCount converted low-hanging variants to rtl::O[UString]::getToken loops added unit test
Diffstat (limited to 'comphelper')
-rw-r--r--comphelper/inc/comphelper/string.hxx22
-rw-r--r--comphelper/qa/string/test_string.cxx17
-rw-r--r--comphelper/source/misc/string.cxx29
3 files changed, 66 insertions, 2 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx
index 037c947d4953..1b8e18d9db74 100644
--- a/comphelper/inc/comphelper/string.hxx
+++ b/comphelper/inc/comphelper/string.hxx
@@ -280,8 +280,9 @@ COMPHELPER_DLLPUBLIC rtl::OString strip(const rtl::OString &rIn,
COMPHELPER_DLLPUBLIC rtl::OUString strip(const rtl::OUString &rIn,
sal_Unicode c);
-/** Returns a token in the OString
+/** Returns a token in an OString
+ @param rIn the input 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
@@ -294,8 +295,9 @@ COMPHELPER_DLLPUBLIC inline rtl::OString getToken(const rtl::OString &rIn,
return rIn.getToken(nToken, cTok, nIndex);
}
-/** Returns a token in the OUString
+/** Returns a token in an OUString
+ @param rIn the input 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
@@ -308,6 +310,22 @@ COMPHELPER_DLLPUBLIC inline rtl::OUString getToken(const rtl::OUString &rIn,
return rIn.getToken(nToken, cTok, nIndex);
}
+/** Returns number of tokens in an OUString
+
+ @param rIn the input OString
+ @param cTok the character which seperate the tokens.
+ @return the number of tokens
+*/
+COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OString &rIn, sal_Char cTok);
+
+/** Returns number of tokens in an OUString
+
+ @param rIn the input OUString
+ @param cTok the character which seperate the tokens.
+ @return the number of tokens
+*/
+COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OUString &rIn, sal_Unicode cTok);
+
/**
Match against a substring appearing in another string.
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx
index ae1fc58ef397..1fb892217740 100644
--- a/comphelper/qa/string/test_string.cxx
+++ b/comphelper/qa/string/test_string.cxx
@@ -52,6 +52,7 @@ public:
void testStripEnd();
void testStrip();
void testToken();
+ void testTokenCount();
void testDecimalStringToNumber();
void testIsdigitAsciiString();
void testIsalnumAsciiString();
@@ -68,6 +69,7 @@ public:
CPPUNIT_TEST(testStripEnd);
CPPUNIT_TEST(testStrip);
CPPUNIT_TEST(testToken);
+ CPPUNIT_TEST(testTokenCount);
CPPUNIT_TEST(testDecimalStringToNumber);
CPPUNIT_TEST(testIsdigitAsciiString);
CPPUNIT_TEST(testIsalnumAsciiString);
@@ -516,6 +518,21 @@ void TestString::testToken()
CPPUNIT_ASSERT(aOut.isEmpty());
}
+void TestString::testTokenCount()
+{
+ ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12"));
+ sal_Int32 nOut;
+
+ nOut = ::comphelper::string::getTokenCount(aIn, '.');
+ CPPUNIT_ASSERT(nOut == 3);
+
+ nOut = ::comphelper::string::getTokenCount(aIn, 'X');
+ CPPUNIT_ASSERT(nOut == 1);
+
+ nOut = ::comphelper::string::getTokenCount(rtl::OString(), 'X');
+ CPPUNIT_ASSERT(nOut == 0);
+}
+
CPPUNIT_TEST_SUITE_REGISTRATION(TestString);
}
diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx
index bc3f89048aaf..13a1236ea55e 100644
--- a/comphelper/source/misc/string.cxx
+++ b/comphelper/source/misc/string.cxx
@@ -240,6 +240,35 @@ rtl::OUString strip(const rtl::OUString &rIn, sal_Unicode c)
return stripEnd(stripStart(rIn, c), c);
}
+namespace
+{
+ template <typename T, typename C> sal_Int32 tmpl_getTokenCount(const T &rIn,
+ C cTok)
+ {
+ // Empty String: TokenCount by Definition is 0
+ if (rIn.isEmpty())
+ return 0;
+
+ sal_Int32 nTokCount = 1;
+ for (sal_Int32 i = 0; i < rIn.getLength(); ++i)
+ {
+ if (rIn[i] == cTok)
+ ++nTokCount;
+ }
+ return nTokCount;
+ }
+}
+
+sal_Int32 getTokenCount(const rtl::OString &rIn, sal_Char cTok)
+{
+ return tmpl_getTokenCount<rtl::OString, sal_Char>(rIn, cTok);
+}
+
+sal_Int32 getTokenCount(const rtl::OUString &rIn, sal_Unicode cTok)
+{
+ return tmpl_getTokenCount<rtl::OUString, sal_Unicode>(rIn, cTok);
+}
+
sal_uInt32 decimalStringToNumber(
::rtl::OUString const & str )
{