diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-16 22:19:58 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-17 23:04:10 +0000 |
commit | dca04e236193db7de908aad746fd4539e78eb428 (patch) | |
tree | 9808f843924944a740ca8037094d4d5c4553bbc8 /comphelper | |
parent | c4927a1b76b728b2208c29d09dbf54e70bb26e13 (diff) |
add a StringUtils-alike remove (can replace EraseAllChars)
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 20 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 16 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 31 |
3 files changed, 67 insertions, 0 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index 620db1b8bf34..31414e5519ff 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -200,6 +200,26 @@ 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); +/** Removes all occurrences of a character from within the source string + + @param rIn The input OString + @param c The character to be removed + + @return The resulting OString + */ +COMPHELPER_DLLPUBLIC rtl::OString remove(const rtl::OString &rIn, + sal_Char c); + +/** Removes all occurrences of a character from within the source string + + @param rIn The input OUString + @param c The character to be removed + + @return The resulting OUString + */ +COMPHELPER_DLLPUBLIC rtl::OUString remove(const rtl::OUString &rIn, + sal_Unicode c); + /** Returns a token in the OString @param token the number of the token to return diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index a290cb7c5e93..110638164506 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -48,6 +48,7 @@ public: void testSearchAndReplaceAsciiL(); void testNatural(); void testReplace(); + void testRemove(); void testToken(); void testDecimalStringToNumber(); void testIsdigitAsciiString(); @@ -59,6 +60,7 @@ public: CPPUNIT_TEST(testSearchAndReplaceAsciiL); CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testReplace); + CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); @@ -395,6 +397,20 @@ void TestString::testReplace() RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb"))); } +void TestString::testRemove() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::remove(aIn, 'b'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ac"))); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")); + + aOut = ::comphelper::string::remove(aIn, 'a'); + CPPUNIT_ASSERT(aOut.isEmpty()); +} + void TestString::testToken() { ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12")); diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index 6f59564814b9..8525e79bd4fc 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -138,6 +138,37 @@ rtl::OUString replace(const rtl::OUString &rIn, const rtl::OUString &rSearch, rReplace); } +namespace +{ + template <typename T, typename C, typename O> T tmpl_remove(const T &rIn, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + O aRet; + + for (sal_Int32 i = 0; i < rIn.getLength(); ++i) + { + C cChar = rIn[i]; + if (cChar != cRemove) + aRet.append(cChar); + } + + return aRet.makeStringAndClear(); + } +} + +rtl::OString remove(const rtl::OString &rIn, sal_Char c) +{ + return tmpl_remove<rtl::OString, sal_Char, rtl::OStringBuffer>(rIn, c); +} + +rtl::OUString remove(const rtl::OUString &rIn, sal_Unicode c) +{ + return tmpl_remove<rtl::OUString, sal_Unicode, rtl::OUStringBuffer>(rIn, c); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { |