diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-22 23:40:24 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-23 10:10:09 +0000 |
commit | b7ea36101497c275cb08b0e37facbde656197d9b (patch) | |
tree | a05e25d4f01c94c69712d17a1ab4cdbc925ef355 /comphelper | |
parent | 62d880f3228c7f5c3f8a1d30f5a5c9ed390a1eb5 (diff) |
add stripStart, can replace EraseTrailingChars
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 23 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 22 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 31 |
3 files changed, 74 insertions, 2 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index e6b2d721099d..c1f56497da4c 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -223,7 +223,7 @@ COMPHELPER_DLLPUBLIC rtl::OUString remove(const rtl::OUString &rIn, /** Strips occurrences of a character from the start of the source string @param rIn The input OString - @param c The character to be stripped from the beginning + @param c The character to be stripped from the start @return The resulting OString */ @@ -233,13 +233,32 @@ COMPHELPER_DLLPUBLIC rtl::OString stripStart(const rtl::OString &rIn, /** Strips occurrences of a character from the start of the source string @param rIn The input OUString - @param c The character to be stripped from the beginning + @param c The character to be stripped from the start @return The resulting OUString */ COMPHELPER_DLLPUBLIC rtl::OUString stripStart(const rtl::OUString &rIn, sal_Unicode c); +/** Strips occurrences of a character from the end of the source string + + @param rIn The input OString + @param c The character to be stripped from the end + + @return The resulting OString + */ +COMPHELPER_DLLPUBLIC rtl::OString stripEnd(const rtl::OString &rIn, + sal_Char c); + +/** Strips occurrences of a character from the end of the source string + + @param rIn The input OUString + @param c The character to be stripped from the end + + @return The resulting OUString + */ +COMPHELPER_DLLPUBLIC rtl::OUString stripEnd(const rtl::OUString &rIn, + sal_Unicode c); /** Returns a token in the OString diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index 790519d07839..1e89408930bf 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -50,6 +50,7 @@ public: void testReplace(); void testRemove(); void testStripStart(); + void testStripEnd(); void testToken(); void testDecimalStringToNumber(); void testIsdigitAsciiString(); @@ -64,6 +65,7 @@ public: CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testStripStart); + CPPUNIT_TEST(testStripEnd); CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); @@ -452,6 +454,26 @@ void TestString::testStripStart() CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ba"))); } +void TestString::testStripEnd() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::stripEnd(aIn, 'b'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("abc"))); + + aOut = ::comphelper::string::stripEnd(aIn, 'c'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ab"))); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")); + aOut = ::comphelper::string::stripEnd(aIn, 'a'); + CPPUNIT_ASSERT(aOut.isEmpty()); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aba")); + aOut = ::comphelper::string::stripEnd(aIn, 'a'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ab"))); +} + 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 09fb2684e7ce..02d1cd4d104c 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -200,6 +200,37 @@ rtl::OUString stripStart(const rtl::OUString &rIn, sal_Unicode c) return tmpl_stripStart<rtl::OUString, sal_Unicode>(rIn, c); } +namespace +{ + template <typename T, typename C> T tmpl_stripEnd(const T &rIn, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + sal_Int32 i = rIn.getLength(); + + while (i > 0) + { + if (rIn[i-1] != cRemove) + break; + --i; + } + + return rIn.copy(0, i); + } +} + +rtl::OString stripEnd(const rtl::OString &rIn, sal_Char c) +{ + return tmpl_stripEnd<rtl::OString, sal_Char>(rIn, c); +} + +rtl::OUString stripEnd(const rtl::OUString &rIn, sal_Unicode c) +{ + return tmpl_stripEnd<rtl::OUString, sal_Unicode>(rIn, c); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { |