diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-23 17:24:38 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-23 23:16:44 +0000 |
commit | 0dbc4fa3efdea90ba23e17e12c2bfe15d763acbf (patch) | |
tree | 857875b2edaee72abb836c8235c4bb87e6e6a18b /comphelper | |
parent | 2dba28faae2266e72c05d8f3d55bfbc3e5771adb (diff) |
add string::strip, can replace EraseLeadingAndTrailingChars
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 20 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 22 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 10 |
3 files changed, 52 insertions, 0 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index c1f56497da4c..878b557db862 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -260,6 +260,26 @@ COMPHELPER_DLLPUBLIC rtl::OString stripEnd(const rtl::OString &rIn, COMPHELPER_DLLPUBLIC rtl::OUString stripEnd(const rtl::OUString &rIn, sal_Unicode c); +/** Strips occurrences of a character from the start and end of the source string + + @param rIn The input OString + @param c The character to be stripped from the start and end + + @return The resulting OString + */ +COMPHELPER_DLLPUBLIC rtl::OString strip(const rtl::OString &rIn, + sal_Char c); + +/** Strips occurrences of a character from the start and end of the source string + + @param rIn The input OUString + @param c The character to be stripped from the start and end + + @return The resulting OUString + */ +COMPHELPER_DLLPUBLIC rtl::OUString strip(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 1e89408930bf..5c161a21d4ee 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -51,6 +51,7 @@ public: void testRemove(); void testStripStart(); void testStripEnd(); + void testStrip(); void testToken(); void testDecimalStringToNumber(); void testIsdigitAsciiString(); @@ -66,6 +67,7 @@ public: CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testStripStart); CPPUNIT_TEST(testStripEnd); + CPPUNIT_TEST(testStrip); CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); @@ -474,6 +476,26 @@ void TestString::testStripEnd() CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ab"))); } +void TestString::testStrip() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::strip(aIn, 'b'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("abc"))); + + aOut = ::comphelper::string::strip(aIn, 'c'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ab"))); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")); + aOut = ::comphelper::string::strip(aIn, 'a'); + CPPUNIT_ASSERT(aOut.isEmpty()); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aba")); + aOut = ::comphelper::string::strip(aIn, 'a'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("b"))); +} + 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 02d1cd4d104c..2e8d631edf37 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -231,6 +231,16 @@ rtl::OUString stripEnd(const rtl::OUString &rIn, sal_Unicode c) return tmpl_stripEnd<rtl::OUString, sal_Unicode>(rIn, c); } +rtl::OString strip(const rtl::OString &rIn, sal_Char c) +{ + return stripEnd(stripStart(rIn, c), c); +} + +rtl::OUString strip(const rtl::OUString &rIn, sal_Unicode c) +{ + return stripEnd(stripStart(rIn, c), c); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { |