diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-22 17:17:53 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-23 10:10:08 +0000 |
commit | 20153742d2dee2df022275a07cc958b1759b9b72 (patch) | |
tree | a91d3d42faa559783d407bb1fe08f4070d945762 /comphelper | |
parent | a22ce3e4483f6fe462eaba8826a91355957e3676 (diff) |
add a stripStart, can replace EraseLeadingChars
Diffstat (limited to 'comphelper')
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 21 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 22 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 31 |
3 files changed, 74 insertions, 0 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index 00a1664016cf..e6b2d721099d 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -220,6 +220,27 @@ COMPHELPER_DLLPUBLIC rtl::OString remove(const rtl::OString &rIn, COMPHELPER_DLLPUBLIC rtl::OUString remove(const rtl::OUString &rIn, sal_Unicode c); +/** 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 + + @return The resulting OString + */ +COMPHELPER_DLLPUBLIC rtl::OString stripStart(const rtl::OString &rIn, + sal_Char c); + +/** 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 + + @return The resulting OUString + */ +COMPHELPER_DLLPUBLIC rtl::OUString stripStart(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 2ce6f2cff1bb..790519d07839 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -49,6 +49,7 @@ public: void testNatural(); void testReplace(); void testRemove(); + void testStripStart(); void testToken(); void testDecimalStringToNumber(); void testIsdigitAsciiString(); @@ -62,6 +63,7 @@ public: CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testRemove); + CPPUNIT_TEST(testStripStart); CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); @@ -430,6 +432,26 @@ void TestString::testRemove() CPPUNIT_ASSERT(aOut.isEmpty()); } +void TestString::testStripStart() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::stripStart(aIn, 'b'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("abc"))); + + aOut = ::comphelper::string::stripStart(aIn, 'a'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("bc"))); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")); + aOut = ::comphelper::string::stripStart(aIn, 'a'); + CPPUNIT_ASSERT(aOut.isEmpty()); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aba")); + aOut = ::comphelper::string::stripStart(aIn, 'a'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ba"))); +} + 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 8525e79bd4fc..09fb2684e7ce 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -169,6 +169,37 @@ 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, + const C cRemove) + { + if (rIn.isEmpty()) + return rIn; + + sal_Int32 i = 0; + + while (i < rIn.getLength()) + { + if (rIn[i] != cRemove) + break; + ++i; + } + + return rIn.copy(i); + } +} + +rtl::OString stripStart(const rtl::OString &rIn, sal_Char c) +{ + return tmpl_stripStart<rtl::OString, sal_Char>(rIn, c); +} + +rtl::OUString stripStart(const rtl::OUString &rIn, sal_Unicode c) +{ + return tmpl_stripStart<rtl::OUString, sal_Unicode>(rIn, c); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { |