diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-08-15 00:10:42 +0100 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-08-15 09:21:49 +0100 |
commit | 90fe8dadaaad07aee2ec513eab1ad75bbf306cb3 (patch) | |
tree | a84008d4e036d9b4476c96fdfa100a2d132cb336 /comphelper | |
parent | 3324eca4eddb906c0a0d8efbc86b097f96a2c3a7 (diff) |
add a jdk 1.5-alike string replace to comphelper::string
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 25 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 30 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 44 |
3 files changed, 98 insertions, 1 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index dd3261bef085..886074dc5682 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -110,6 +110,30 @@ COMPHELPER_DLLPUBLIC ::rtl::OUString& ::rtl::OUString const & replace, sal_Int32 beginAt = 0, sal_Int32 * replacedAt = NULL ); +/** Replaces each substring of this OString that matches the search OString + with the specified replacement OString + + @param rIn The input OString + @param rSearch The substring to be replaced + @param rReplace The replacement substring + + @return The resulting OString + */ +COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn, + const rtl::OString &rSearch, const rtl::OString &rReplace); + +/** Replaces each substring of this OUString that matches the search OUString + with the specified replacement OUString + + @param rIn The input OUString + @param rSearch The substring to be replaced + @param rReplace The replacement substring + + @return The resulting OUString + */ +COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn, + const rtl::OUString &rSearch, const rtl::OUString &rReplace); + /** Convert a sequence of strings to a single comma separated string. Note that no escaping of commas or anything fancy is done. @@ -213,7 +237,6 @@ COMPHELPER_DLLPUBLIC bool isAsciiDecimalString(const rtl::OString &rString); */ COMPHELPER_DLLPUBLIC bool isAsciiDecimalString(const rtl::OUString &rString); - } } #endif diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index eefe7a96f2c4..8dbd056589c4 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -43,11 +43,13 @@ class TestString: public CppUnit::TestFixture public: void test(); void testNatural(); + void testReplace(); void testDecimalStringToNumber(); CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST(test); CPPUNIT_TEST(testNatural); + CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST_SUITE_END(); }; @@ -288,6 +290,34 @@ void TestString::testNatural() ); } +void TestString::testReplace() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("aaa")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::replace(aIn, + rtl::OString(RTL_CONSTASCII_STRINGPARAM("aa")), + rtl::OString(RTL_CONSTASCII_STRINGPARAM("b"))); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ba"))); + + aOut = ::comphelper::string::replace(aIn, + rtl::OString(), + rtl::OString(RTL_CONSTASCII_STRINGPARAM("whatever"))); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("aaa"))); + + aOut = ::comphelper::string::replace(aIn, + rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")), + rtl::OString()); + 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"))); +} + CPPUNIT_TEST_SUITE_REGISTRATION(TestString); } diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index 249ef2e87800..b31d11549815 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -36,6 +36,8 @@ #include <rtl/ustring.hxx> #include <rtl/ustrbuf.hxx> +#include <rtl/string.hxx> +#include <rtl/strbuf.hxx> #include <sal/types.h> #include <comphelper/string.hxx> @@ -94,6 +96,48 @@ rtl::OUString searchAndReplaceAsciiL( return _source; } +namespace +{ + template <typename T, typename O> T tmpl_replace(const T &rIn, + const T &rSearch, const T &rReplace) + { + if (rIn.isEmpty() || rSearch.isEmpty()) + return rIn; + + O aRet; + + sal_Int32 nFromIndex = 0; + while (nFromIndex < rIn.getLength()) + { + sal_Int32 nIndex = rIn.indexOf(rSearch, nFromIndex); + if (nIndex == -1) + { + aRet.append(rIn.copy(nFromIndex)); + break; + } + aRet.append(rIn.copy(nFromIndex, nIndex-nFromIndex)); + aRet.append(rReplace); + nFromIndex = nIndex+rSearch.getLength(); + } + + return aRet.makeStringAndClear(); + } +} + +rtl::OString replace(const rtl::OString &rIn, const rtl::OString &rSearch, + const rtl::OString &rReplace) +{ + return tmpl_replace<rtl::OString, rtl::OStringBuffer>(rIn, rSearch, + rReplace); +} + +rtl::OUString replace(const rtl::OUString &rIn, const rtl::OUString &rSearch, + const rtl::OUString &rReplace) +{ + return tmpl_replace<rtl::OUString, rtl::OUStringBuffer>(rIn, rSearch, + rReplace); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { |