diff options
author | Stephan Bergmann <sbergman@redhat.com> | 2012-02-15 15:26:43 +0100 |
---|---|---|
committer | Stephan Bergmann <sbergman@redhat.com> | 2012-02-15 15:41:09 +0100 |
commit | 9ab0b38e95133dab720408cc2c80093b8a201c10 (patch) | |
tree | 416dde227ed5c4ded99292feb94f36a64c327999 /comphelper | |
parent | 42422f2599220b678aa41c4aadeec28df113c3ec (diff) |
Various string function clean up
Added:
* rtl::OString::matchL
* rtl::OString::endsWith
* rtl::OString::endsWithL
* rtl::OString::indexOfL
* rtl::OString::replaceFirst
* rtl::OString::replaceAll
* rtl::OString::getToken
* rtl::OUString::endsWith
* rtl::OUString::replaceFirst
* rtl::OUString::replaceFirstAsciiL
* rtl::OUString::replaceFirstAsciiLAsciiL
* rtl::OUString::replaceAll
* rtl::OUString::replaceAllAsciiL
* rtl::OUString::replaceAllAsciiLAsciiL
* rtl::OUString::getToken
plus underlying C functions where necessary
Deprecated:
* comphelper::string::remove
* comphelper::string::getToken
Removed:
* comphelper::string::searchAndReplaceAsciiL
* comphelper::string::searchAndReplaceAllAsciiWithAscii
* comphelper::string::searchAndReplaceAsciiI
* comphelper::string::replace
* comphelper::string::matchL
* comphelper::string::matchIgnoreAsciiCaseL
* comphelper::string::indexOfL
Also fixed some apparent misuses of RTL_CONSTASCII_USTRINGPARAM ->
RTL_CONSTASCII_STRINGPARAM.
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 192 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 106 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 120 |
3 files changed, 20 insertions, 398 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index 2c63954a3535..f52021d9b68f 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -112,113 +112,33 @@ COMPHELPER_DLLPUBLIC inline rtl_String * SAL_CALL rtl_string_alloc(sal_Int32 nLe return detail::string_alloc<rtl_String, sal_Char>(nLen); } -/** - Replace the first occurrence of a substring with another string. - - @param source - The source string, in which the search will take place. - - @param from - The ASCII substring to search for. Must point to at least fromLength ASCII - characters. - - @param fromLength - The length of the from substring. Must not be negative. - - @param to - The string to use as replacement. - - @param beginAt - The index at which to begin the search. Must be between zero and the length - of source, inclusive. - - @param replacedAt - If non-null, receives the starting index at which the replacement took place - or -1 if from was not found. - - @return - The resulting string, in which the replacement has taken place. -*/ -COMPHELPER_DLLPUBLIC rtl::OUString searchAndReplaceAsciiL( - rtl::OUString const & source, char const * from, sal_Int32 fromLength, - rtl::OUString const & to, sal_Int32 beginAt = 0, - sal_Int32 * replacedAt = NULL); - -/** replaces, in the given source string, all occurrences of a given ASCII pattern - with another ASCII pattern -*/ -COMPHELPER_DLLPUBLIC ::rtl::OUString searchAndReplaceAllAsciiWithAscii( - const ::rtl::OUString& source, const sal_Char* from, const sal_Char* to, - const sal_Int32 beginAt = 0 ); - -/** does an in-place replacement of the first occurrence of a sub string with - another string - - @param source - the string to search and replace in. - @param asciiPattern - the ASCII sub string to search for. Must point to a 0-terminated string. - @param replace - The string to use as replacement. - @param beginAt - The index at which to begin the search. Must be between zero and the length - of source, inclusive. - - @param replacedAt - If non-null, receives the starting index at which the replacement took place - or -1 if from was not found. - - @return - a reference to <code>source</code> -*/ -COMPHELPER_DLLPUBLIC ::rtl::OUString& - searchAndReplaceAsciiI( ::rtl::OUString & source, sal_Char const * asciiPattern, - ::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); - /** Removes all occurrences of a character from within the source string + @deprecated Use rtl::OString::replaceAll(rtl::OString(c), rtl::OString()) + instead. + @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); +inline rtl::OString remove(const rtl::OString &rIn, + sal_Char c) +{ return rIn.replaceAll(rtl::OString(c), rtl::OString()); } /** Removes all occurrences of a character from within the source string + @deprecated Use + rtl::OUString::replaceAll(rtl::OUString(c), rtl::OUString()) instead. + @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); +inline rtl::OUString remove(const rtl::OUString &rIn, + sal_Unicode c) +{ return rIn.replaceAll(rtl::OUString(c), rtl::OUString()); } /** Strips occurrences of a character from the start of the source string @@ -282,32 +202,34 @@ COMPHELPER_DLLPUBLIC rtl::OUString strip(const rtl::OUString &rIn, /** Returns a token in an OString + @deprecated Use rtl::OString::getToken(nToken, cTok) instead. + @param rIn the input OString @param nToken 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, +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); + return rIn.getToken(nToken, cTok); } /** Returns a token in an OUString + @deprecated Use rtl::OUString::getToken(nToken, cTok) instead. + @param rIn the input OUString @param nToken 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, +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); + return rIn.getToken(nToken, cTok); } /** Returns number of tokens in an OUString @@ -326,80 +248,6 @@ COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OString &rIn, sal_Char c */ COMPHELPER_DLLPUBLIC sal_Int32 getTokenCount(const rtl::OUString &rIn, sal_Unicode cTok); -/** - Match against a substring appearing in another string. - - The result is true if and only if the second string appears as a substring - of the first string, at the given position. - This function can't be used for language specific comparison. - - @param rStr The string that pMatch will be compared to. - @param pMatch The substring rStr is to be compared against - @param nMatchLen The length of pMatch - @param fromIndex The index to start the comparion from. - The index must be greater or equal than 0 - and less or equal as the string length. - @return sal_True if pMatch match with the characters in the string - at the given position; - sal_False, otherwise. -*/ -COMPHELPER_DLLPUBLIC inline sal_Bool matchL(const rtl::OString& rStr, const char *pMatch, sal_Int32 nMatchLen, sal_Int32 fromIndex = 0) SAL_THROW(()) -{ - return rtl_str_shortenedCompare_WithLength( rStr.pData->buffer+fromIndex, - rStr.pData->length-fromIndex, pMatch, nMatchLen, nMatchLen ) == 0; -} - -/** - Match against a substring appearing in this string, ignoring the case of - ASCII letters. - - The result is true if and only if the second string appears as a substring - of this string, at the given position. - Character values between 65 and 90 (ASCII A-Z) are interpreted as - values between 97 and 122 (ASCII a-z). - This function can't be used for language specific comparison. - - @param rStr The string that pMatch will be compared to. - @param pMatch The substring rStr is to be compared against - @param nMatchLen The length of pMatch - @param fromIndex the index to start the comparion from. - The index must be greater or equal than 0 - and less or equal as the string length. - @return sal_True if str match with the characters in the string - at the given position; - sal_False, otherwise. -*/ -COMPHELPER_DLLPUBLIC inline sal_Bool matchIgnoreAsciiCaseL(const rtl::OString& rStr, const char *pMatch, sal_Int32 nMatchLen, sal_Int32 fromIndex = 0) SAL_THROW(()) -{ - return rtl_str_shortenedCompareIgnoreAsciiCase_WithLength( rStr.pData->buffer+fromIndex, rStr.pData->length-fromIndex, - pMatch, nMatchLen, - nMatchLen ) == 0; -} - -/** - Returns the index within this string of the first occurrence of the - specified substring, starting at the specified index. - - If str doesn't include any character, always -1 is - returned. This is also the case, if both strings are empty. - - @param rStr The string that pSearch will be searched within. - @param pSearch the substring to search for. - @param nSearchLen the length of pSearch - @param fromIndex the index to start the search from. - @return If the string argument occurs one or more times as a substring - within this string at the starting index, then the index - of the first character of the first such substring is - returned. If it does not occur as a substring starting - at fromIndex or beyond, -1 is returned. -*/ -COMPHELPER_DLLPUBLIC inline sal_Int32 indexOfL(const rtl::OString& rStr, const char *pSearch, sal_Int32 nSearchLen, sal_Int32 fromIndex = 0) SAL_THROW(()) -{ - sal_Int32 ret = rtl_str_indexOfStr_WithLength(rStr.pData->buffer+fromIndex, - rStr.pData->length-fromIndex, pSearch, nSearchLen); - return (ret < 0 ? ret : ret+fromIndex); -} - namespace detail { template<typename B> B& truncateToLength(B& rBuffer, sal_Int32 nLen) diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index e53ae9030fa4..b775e01804da 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -44,9 +44,7 @@ namespace { class TestString: public CppUnit::TestFixture { public: - void testSearchAndReplaceAsciiL(); void testNatural(); - void testReplace(); void testRemove(); void testStripStart(); void testStripEnd(); @@ -56,12 +54,9 @@ public: void testDecimalStringToNumber(); void testIsdigitAsciiString(); void testIndexOfL(); - void testMatchIgnoreAsciiCaseL(); CPPUNIT_TEST_SUITE(TestString); - CPPUNIT_TEST(testSearchAndReplaceAsciiL); CPPUNIT_TEST(testNatural); - CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testStripStart); CPPUNIT_TEST(testStripEnd); @@ -70,41 +65,9 @@ public: CPPUNIT_TEST(testTokenCount); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); - CPPUNIT_TEST(testIndexOfL); - CPPUNIT_TEST(testMatchIgnoreAsciiCaseL); CPPUNIT_TEST_SUITE_END(); }; -void TestString::testSearchAndReplaceAsciiL() -{ - rtl::OUString s1(RTL_CONSTASCII_USTRINGPARAM("foobarbar")); - sal_Int32 n1; - rtl::OUString s2( - comphelper::string::searchAndReplaceAsciiL( - s1, RTL_CONSTASCII_STRINGPARAM("bar"), - rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baaz")), 0, &n1)); - CPPUNIT_ASSERT( - s2 == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobaazbar"))); - CPPUNIT_ASSERT(n1 == 3); - sal_Int32 n2; - rtl::OUString s3( - comphelper::string::searchAndReplaceAsciiL( - s2, RTL_CONSTASCII_STRINGPARAM("bar"), - rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("bz")), - n1 + RTL_CONSTASCII_LENGTH("baaz"), &n2)); - CPPUNIT_ASSERT( - s3 == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("foobaazbz"))); - CPPUNIT_ASSERT(n2 == 7); - sal_Int32 n3; - rtl::OUString s4( - comphelper::string::searchAndReplaceAsciiL( - s3, RTL_CONSTASCII_STRINGPARAM("bar"), - rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("baz")), - n2 + RTL_CONSTASCII_LENGTH("bz"), &n3)); - CPPUNIT_ASSERT(s4 == s3); - CPPUNIT_ASSERT(n3 == -1); -} - void TestString::testDecimalStringToNumber() { rtl::OUString s1(RTL_CONSTASCII_USTRINGPARAM("1234")); @@ -129,40 +92,6 @@ void TestString::testIsdigitAsciiString() CPPUNIT_ASSERT_EQUAL(comphelper::string::isdigitAsciiString(s3), true); } -void TestString::testIndexOfL() -{ - rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("one two three")); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, - RTL_CONSTASCII_STRINGPARAM("one")), static_cast<sal_Int32>(0)); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, - RTL_CONSTASCII_STRINGPARAM("two")), static_cast<sal_Int32>(4)); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, - RTL_CONSTASCII_STRINGPARAM("four")), static_cast<sal_Int32>(-1)); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::indexOfL(s1, - RTL_CONSTASCII_STRINGPARAM("two"), 5), static_cast<sal_Int32>(-1)); -} - -void TestString::testMatchIgnoreAsciiCaseL() -{ - rtl::OString s1(RTL_CONSTASCII_STRINGPARAM("one two three")); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::matchIgnoreAsciiCaseL(s1, - RTL_CONSTASCII_STRINGPARAM("one")), sal_True); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::matchIgnoreAsciiCaseL(s1, - RTL_CONSTASCII_STRINGPARAM("ONE")), sal_True); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::matchIgnoreAsciiCaseL(s1, - RTL_CONSTASCII_STRINGPARAM("two")), sal_False); - - CPPUNIT_ASSERT_EQUAL(comphelper::string::matchIgnoreAsciiCaseL(s1, - RTL_CONSTASCII_STRINGPARAM("two"), 4), sal_True); -} - using namespace ::com::sun::star; class testCollator : public cppu::WeakImplHelper1< i18n::XCollator > @@ -357,41 +286,6 @@ 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"))); - - aOut = ::comphelper::string::replace(aIn, - rtl::OString(' '), - rtl::OString()); - CPPUNIT_ASSERT(aOut.equalsL( - RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb"))); -} - void TestString::testRemove() { ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); diff --git a/comphelper/source/misc/string.cxx b/comphelper/source/misc/string.cxx index 8514a83872c6..3547de626318 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -48,126 +48,6 @@ namespace comphelper { namespace string { -rtl::OUString searchAndReplaceAsciiL( - rtl::OUString const & source, char const * from, sal_Int32 fromLength, - rtl::OUString const & to, sal_Int32 beginAt, sal_Int32 * replacedAt) -{ - sal_Int32 n = source.indexOfAsciiL(from, fromLength, beginAt); - if (replacedAt != NULL) { - *replacedAt = n; - } - return n == -1 ? source : source.replaceAt(n, fromLength, to); -} - -::rtl::OUString searchAndReplaceAllAsciiWithAscii( - const ::rtl::OUString& _source, const sal_Char* _from, const sal_Char* _to, - const sal_Int32 _beginAt ) -{ - sal_Int32 fromLength = strlen( _from ); - sal_Int32 n = _source.indexOfAsciiL( _from, fromLength, _beginAt ); - if ( n == -1 ) - return _source; - - ::rtl::OUString dest( _source ); - ::rtl::OUString to( ::rtl::OUString::createFromAscii( _to ) ); - do - { - dest = dest.replaceAt( n, fromLength, to ); - n = dest.indexOfAsciiL( _from, fromLength, n + to.getLength() ); - } - while ( n != -1 ); - - return dest; -} - -::rtl::OUString& searchAndReplaceAsciiI( - ::rtl::OUString & _source, sal_Char const * _asciiPattern, ::rtl::OUString const & _replace, - sal_Int32 _beginAt, sal_Int32 * _replacedAt ) -{ - sal_Int32 fromLength = strlen( _asciiPattern ); - sal_Int32 n = _source.indexOfAsciiL( _asciiPattern, fromLength, _beginAt ); - if ( _replacedAt != NULL ) - *_replacedAt = n; - - if ( n != -1 ) - _source = _source.replaceAt( n, fromLength, _replace ); - - 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); -} - -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); -} - namespace { template <typename T, typename C> T tmpl_stripStart(const T &rIn, |