Kaydet (Commit) 90fe8dad authored tarafından Caolán McNamara's avatar Caolán McNamara

add a jdk 1.5-alike string replace to comphelper::string

üst 3324eca4
......@@ -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
......
......@@ -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);
}
......
......@@ -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 )
{
......
......@@ -77,6 +77,7 @@
#include "cppuhelper/exc_hlp.hxx"
#include "cppuhelper/implbase3.hxx"
#include "comphelper/anytostring.hxx"
#include "comphelper/string.hxx"
#include "vcl/msgbox.hxx"
#include "toolkit/helper/vclunohelper.hxx"
#include "comphelper/processfactory.hxx"
......@@ -250,9 +251,6 @@ public:
void stop();
bool isBusy();
static OUString searchAndReplaceAll( const OUString &rSource,
const OUString &rWhat,
const OUString &rWith );
private:
Thread( Thread & ); // not defined
void operator =( Thread & ); // not defined
......@@ -902,7 +900,7 @@ void ExtensionCmdQueue::Thread::_addExtension( ::rtl::Reference< ProgressCmdEnv
rCmdEnv->setWarnUser( bWarnUser );
uno::Reference< deployment::XExtensionManager > xExtMgr = m_pManager->getExtensionManager();
uno::Reference< task::XAbortChannel > xAbortChannel( xExtMgr->createAbortChannel() );
OUString sTitle = searchAndReplaceAll( m_sAddingPackages, OUSTR("%EXTENSION_NAME"), sName );
OUString sTitle = comphelper::string::replace(m_sAddingPackages, OUSTR("%EXTENSION_NAME"), sName);
rCmdEnv->progressSection( sTitle, xAbortChannel );
try
......@@ -929,7 +927,7 @@ void ExtensionCmdQueue::Thread::_removeExtension( ::rtl::Reference< ProgressCmdE
{
uno::Reference< deployment::XExtensionManager > xExtMgr = m_pManager->getExtensionManager();
uno::Reference< task::XAbortChannel > xAbortChannel( xExtMgr->createAbortChannel() );
OUString sTitle = searchAndReplaceAll( m_sRemovingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName() );
OUString sTitle = comphelper::string::replace(m_sRemovingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName());
rCmdEnv->progressSection( sTitle, xAbortChannel );
OUString id( dp_misc::getIdentifier( xPackage ) );
......@@ -1012,7 +1010,7 @@ void ExtensionCmdQueue::Thread::_enableExtension( ::rtl::Reference< ProgressCmdE
uno::Reference< deployment::XExtensionManager > xExtMgr = m_pManager->getExtensionManager();
uno::Reference< task::XAbortChannel > xAbortChannel( xExtMgr->createAbortChannel() );
OUString sTitle = searchAndReplaceAll( m_sEnablingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName() );
OUString sTitle = comphelper::string::replace(m_sEnablingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName());
rCmdEnv->progressSection( sTitle, xAbortChannel );
try
......@@ -1034,7 +1032,7 @@ void ExtensionCmdQueue::Thread::_disableExtension( ::rtl::Reference< ProgressCmd
uno::Reference< deployment::XExtensionManager > xExtMgr = m_pManager->getExtensionManager();
uno::Reference< task::XAbortChannel > xAbortChannel( xExtMgr->createAbortChannel() );
OUString sTitle = searchAndReplaceAll( m_sDisablingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName() );
OUString sTitle = comphelper::string::replace(m_sDisablingPackages, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName());
rCmdEnv->progressSection( sTitle, xAbortChannel );
try
......@@ -1056,7 +1054,7 @@ void ExtensionCmdQueue::Thread::_acceptLicense( ::rtl::Reference< ProgressCmdEnv
uno::Reference< deployment::XExtensionManager > xExtMgr = m_pManager->getExtensionManager();
uno::Reference< task::XAbortChannel > xAbortChannel( xExtMgr->createAbortChannel() );
OUString sTitle = searchAndReplaceAll( m_sAcceptLicense, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName() );
OUString sTitle = comphelper::string::replace(m_sAcceptLicense, OUSTR("%EXTENSION_NAME"), xPackage->getDisplayName());
rCmdEnv->progressSection( sTitle, xAbortChannel );
try
......@@ -1089,27 +1087,6 @@ void ExtensionCmdQueue::Thread::_insert(const TExtensionCmd& rExtCmd)
m_wakeup.set();
}
//------------------------------------------------------------------------------
OUString ExtensionCmdQueue::Thread::searchAndReplaceAll( const OUString &rSource,
const OUString &rWhat,
const OUString &rWith )
{
OUString aRet( rSource );
sal_Int32 nLen = rWhat.getLength();
if ( !nLen )
return aRet;
sal_Int32 nIndex = rSource.indexOf( rWhat );
while ( nIndex != -1 )
{
aRet = aRet.replaceAt( nIndex, nLen, rWith );
nIndex = aRet.indexOf( rWhat, nIndex + rWith.getLength() );
}
return aRet;
}
//------------------------------------------------------------------------------
ExtensionCmdQueue::ExtensionCmdQueue( DialogHelper * pDialogHelper,
TheExtensionManager *pManager,
......
......@@ -169,10 +169,10 @@ private:
// of ByteString(sal_Char);
ByteString( const UniString& rUniStr, xub_StrLen nPos, xub_StrLen nLen,
rtl_TextEncoding eTextEncoding,
sal_uInt32 nCvtFlags = UNISTRING_TO_BYTESTRING_CVTFLAGS );
sal_uInt32 nCvtFlags = UNISTRING_TO_BYTESTRING_CVTFLAGS ); //not implemented, to detect use of removed methods without compiler making something to fit
ByteString( const sal_Unicode* pUniStr, xub_StrLen nLen,
rtl_TextEncoding eTextEncoding,
sal_uInt32 nCvtFlags = UNISTRING_TO_BYTESTRING_CVTFLAGS );
sal_uInt32 nCvtFlags = UNISTRING_TO_BYTESTRING_CVTFLAGS ); //not implemented, to detect use of removed methods without compiler making somethiing to fit
void Assign(int); // not implemented; to detect misuses of
// Assign(sal_Char)
void operator =(int); // not implemented; to detect misuses
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment