diff options
author | Caolán McNamara <caolanm@redhat.com> | 2011-11-16 22:19:58 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2011-11-17 23:04:10 +0000 |
commit | dca04e236193db7de908aad746fd4539e78eb428 (patch) | |
tree | 9808f843924944a740ca8037094d4d5c4553bbc8 | |
parent | c4927a1b76b728b2208c29d09dbf54e70bb26e13 (diff) |
add a StringUtils-alike remove (can replace EraseAllChars)
-rw-r--r-- | comphelper/inc/comphelper/string.hxx | 20 | ||||
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 16 | ||||
-rw-r--r-- | comphelper/source/misc/string.cxx | 31 | ||||
-rw-r--r-- | l10ntools/inc/export.hxx | 4 | ||||
-rw-r--r-- | l10ntools/source/export2.cxx | 54 | ||||
-rw-r--r-- | l10ntools/source/lngmerge.cxx | 15 | ||||
-rw-r--r-- | svl/source/misc/lngmisc.cxx | 11 |
7 files changed, 116 insertions, 35 deletions
diff --git a/comphelper/inc/comphelper/string.hxx b/comphelper/inc/comphelper/string.hxx index 620db1b8bf34..31414e5519ff 100644 --- a/comphelper/inc/comphelper/string.hxx +++ b/comphelper/inc/comphelper/string.hxx @@ -200,6 +200,26 @@ COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn, 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 + + @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); + +/** Removes all occurrences of a character from within the source string + + @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); + /** 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 a290cb7c5e93..110638164506 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -48,6 +48,7 @@ public: void testSearchAndReplaceAsciiL(); void testNatural(); void testReplace(); + void testRemove(); void testToken(); void testDecimalStringToNumber(); void testIsdigitAsciiString(); @@ -59,6 +60,7 @@ public: CPPUNIT_TEST(testSearchAndReplaceAsciiL); CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testReplace); + CPPUNIT_TEST(testRemove); CPPUNIT_TEST(testToken); CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testIsdigitAsciiString); @@ -395,6 +397,20 @@ void TestString::testReplace() RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb"))); } +void TestString::testRemove() +{ + ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("abc")); + ::rtl::OString aOut; + + aOut = ::comphelper::string::remove(aIn, 'b'); + CPPUNIT_ASSERT(aOut.equalsL(RTL_CONSTASCII_STRINGPARAM("ac"))); + + aIn = rtl::OString(RTL_CONSTASCII_STRINGPARAM("aaa")); + + aOut = ::comphelper::string::remove(aIn, 'a'); + CPPUNIT_ASSERT(aOut.isEmpty()); +} + 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 6f59564814b9..8525e79bd4fc 100644 --- a/comphelper/source/misc/string.cxx +++ b/comphelper/source/misc/string.cxx @@ -138,6 +138,37 @@ rtl::OUString replace(const rtl::OUString &rIn, const rtl::OUString &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); +} + sal_uInt32 decimalStringToNumber( ::rtl::OUString const & str ) { diff --git a/l10ntools/inc/export.hxx b/l10ntools/inc/export.hxx index 0058cf143f9c..94d59256f127 100644 --- a/l10ntools/inc/export.hxx +++ b/l10ntools/inc/export.hxx @@ -329,8 +329,8 @@ public: static std::vector<ByteString> GetForcedLanguages(); static void SetLanguages( std::vector<ByteString> val ); - static void RemoveUTF8ByteOrderMarker( ByteString &rString ); - static bool hasUTF8ByteOrderMarker( const ByteString &rString ); + static void RemoveUTF8ByteOrderMarker( rtl::OString &rString ); + static bool hasUTF8ByteOrderMarker( const rtl::OString &rString ); static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ); static bool fileHasUTF8ByteOrderMarker( const ByteString &rString ); static void QuotHTML( ByteString &rString ); diff --git a/l10ntools/source/export2.cxx b/l10ntools/source/export2.cxx index 26f99b59e7c8..72ffde06f777 100644 --- a/l10ntools/source/export2.cxx +++ b/l10ntools/source/export2.cxx @@ -192,35 +192,43 @@ void Export::QuotHTML( ByteString &rString ) rString = sReturn.makeStringAndClear(); } -void Export::RemoveUTF8ByteOrderMarker( ByteString &rString ){ +void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString ) +{ if( hasUTF8ByteOrderMarker( rString ) ) - rString.Erase( 0 , 3 ); + rString = rString.copy(3); } -bool Export::hasUTF8ByteOrderMarker( const ByteString &rString ){ - return rString.Len() >= 3 && - rString.GetChar( 0 ) == '\xEF' && - rString.GetChar( 1 ) == '\xBB' && - rString.GetChar( 2 ) == '\xBF' ; +bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString ) +{ + return rString.getLength() >= 3 && rString[0] == '\xEF' && + rString[1] == '\xBB' && rString[2] == '\xBF' ; } -bool Export::fileHasUTF8ByteOrderMarker( const ByteString &rString ){ + +bool Export::fileHasUTF8ByteOrderMarker( const ByteString &rString ) +{ SvFileStream aFileIn( String( rString , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ ); - ByteString sLine; - if( !aFileIn.IsEof() ) { + rtl::OString sLine; + if( !aFileIn.IsEof() ) + { aFileIn.ReadLine( sLine ); - if( aFileIn.IsOpen() ) aFileIn.Close(); + if( aFileIn.IsOpen() ) + aFileIn.Close(); return hasUTF8ByteOrderMarker( sLine ); } if( aFileIn.IsOpen() ) aFileIn.Close(); return false; } -void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){ + +void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ) +{ SvFileStream aFileIn( String( rFilename , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ ); - ByteString sLine; - if( !aFileIn.IsEof() ) { + rtl::OString sLine; + if( !aFileIn.IsEof() ) + { aFileIn.ReadLine( sLine ); // Test header - if( hasUTF8ByteOrderMarker( sLine ) ){ + if( hasUTF8ByteOrderMarker( sLine ) ) + { DirEntry aTempFile = Export::GetTempFile(); ByteString sTempFile = ByteString( aTempFile.GetFull() , RTL_TEXTENCODING_ASCII_US ); SvFileStream aNewFile( String( sTempFile , RTL_TEXTENCODING_ASCII_US ) , STREAM_WRITE ); @@ -228,7 +236,8 @@ void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){ RemoveUTF8ByteOrderMarker( sLine ); aNewFile.WriteLine( sLine ); // Copy the rest - while( !aFileIn.IsEof() ){ + while( !aFileIn.IsEof() ) + { aFileIn.ReadLine( sLine ); aNewFile.WriteLine( sLine ); } @@ -239,7 +248,8 @@ void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){ DirEntry( sTempFile ).MoveTo( DirEntry( rFilename.GetBuffer() ) ); } } - if( aFileIn.IsOpen() ) aFileIn.Close(); + if( aFileIn.IsOpen() ) + aFileIn.Close(); } bool Export::CopyFile( const ByteString& source , const ByteString& dest ) @@ -398,12 +408,14 @@ sal_Bool Export::ConvertLineEnds( return sal_False; } - ByteString sLine; + rtl::OString sLine; - while ( !aSource.IsEof()) { + while ( !aSource.IsEof()) + { aSource.ReadLine( sLine ); - if ( !aSource.IsEof()) { - sLine.EraseAllChars( '\r' ); + if ( !aSource.IsEof()) + { + sLine = comphelper::string::remove(sLine, '\r'); aDestination.WriteLine( sLine ); } else diff --git a/l10ntools/source/lngmerge.cxx b/l10ntools/source/lngmerge.cxx index f166595c09bf..7592d4979160 100644 --- a/l10ntools/source/lngmerge.cxx +++ b/l10ntools/source/lngmerge.cxx @@ -52,15 +52,20 @@ LngParser::LngParser( const ByteString &rLngFile, sal_Bool bUTF8, sal_Bool bULFF { pLines = new LngLineList(); DirEntry aEntry( String( sSource, RTL_TEXTENCODING_ASCII_US )); - if ( aEntry.Exists()) { + if ( aEntry.Exists()) + { SvFileStream aStream( String( sSource, RTL_TEXTENCODING_ASCII_US ), STREAM_STD_READ ); - if ( aStream.IsOpen()) { - ByteString sLine; + if ( aStream.IsOpen()) + { + rtl::OString sLine; bool bFirstLine = true; - while ( !aStream.IsEof()) { + while ( !aStream.IsEof()) + { aStream.ReadLine( sLine ); - if( bFirstLine ){ // Always remove UTF8 BOM from the first line + if( bFirstLine ) + { + // Always remove UTF8 BOM from the first line Export::RemoveUTF8ByteOrderMarker( sLine ); bFirstLine = false; } diff --git a/svl/source/misc/lngmisc.cxx b/svl/source/misc/lngmisc.cxx index 2eba00efd565..768dc4c8913c 100644 --- a/svl/source/misc/lngmisc.cxx +++ b/svl/source/misc/lngmisc.cxx @@ -28,6 +28,7 @@ // MARKER(update_precomp.py): autogen include statement, do not remove #include "precompiled_svl.hxx" +#include <comphelper/string.hxx> #include <svl/lngmisc.hxx> #include <tools/solar.h> #include <tools/string.hxx> @@ -55,22 +56,18 @@ sal_Int32 GetNumControlChars( const OUString &rTxt ) return nCnt; } - sal_Bool RemoveHyphens( OUString &rTxt ) { sal_Bool bModified = sal_False; - if (HasHyphens( rTxt )) + if (HasHyphens(rTxt)) { - String aTmp( rTxt ); - aTmp.EraseAllChars( SVT_SOFT_HYPHEN ); - aTmp.EraseAllChars( SVT_HARD_HYPHEN ); - rTxt = aTmp; + rTxt = comphelper::string::remove(rTxt, SVT_SOFT_HYPHEN); + rTxt = comphelper::string::remove(rTxt, SVT_HARD_HYPHEN); bModified = sal_True; } return bModified; } - sal_Bool RemoveControlChars( OUString &rTxt ) { sal_Bool bModified = sal_False; |