add a StringUtils-alike remove (can replace EraseAllChars)

This commit is contained in:
Caolán McNamara
2011-11-16 22:19:58 +00:00
parent c4927a1b76
commit dca04e2361
7 changed files with 116 additions and 35 deletions

View File

@@ -200,6 +200,26 @@ COMPHELPER_DLLPUBLIC rtl::OString replace(const rtl::OString &rIn,
COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn, COMPHELPER_DLLPUBLIC rtl::OUString replace(const rtl::OUString &rIn,
const rtl::OUString &rSearch, const rtl::OUString &rReplace); 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 /** Returns a token in the OString
@param token the number of the token to return @param token the number of the token to return

View File

@@ -48,6 +48,7 @@ public:
void testSearchAndReplaceAsciiL(); void testSearchAndReplaceAsciiL();
void testNatural(); void testNatural();
void testReplace(); void testReplace();
void testRemove();
void testToken(); void testToken();
void testDecimalStringToNumber(); void testDecimalStringToNumber();
void testIsdigitAsciiString(); void testIsdigitAsciiString();
@@ -59,6 +60,7 @@ public:
CPPUNIT_TEST(testSearchAndReplaceAsciiL); CPPUNIT_TEST(testSearchAndReplaceAsciiL);
CPPUNIT_TEST(testNatural); CPPUNIT_TEST(testNatural);
CPPUNIT_TEST(testReplace); CPPUNIT_TEST(testReplace);
CPPUNIT_TEST(testRemove);
CPPUNIT_TEST(testToken); CPPUNIT_TEST(testToken);
CPPUNIT_TEST(testDecimalStringToNumber); CPPUNIT_TEST(testDecimalStringToNumber);
CPPUNIT_TEST(testIsdigitAsciiString); CPPUNIT_TEST(testIsdigitAsciiString);
@@ -395,6 +397,20 @@ void TestString::testReplace()
RTL_CONSTASCII_STRINGPARAM("aaafooaaafoobbb"))); 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() void TestString::testToken()
{ {
::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12")); ::rtl::OString aIn(RTL_CONSTASCII_STRINGPARAM("10.11.12"));

View File

@@ -138,6 +138,37 @@ rtl::OUString replace(const rtl::OUString &rIn, const rtl::OUString &rSearch,
rReplace); 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( sal_uInt32 decimalStringToNumber(
::rtl::OUString const & str ) ::rtl::OUString const & str )
{ {

View File

@@ -329,8 +329,8 @@ public:
static std::vector<ByteString> GetForcedLanguages(); static std::vector<ByteString> GetForcedLanguages();
static void SetLanguages( std::vector<ByteString> val ); static void SetLanguages( std::vector<ByteString> val );
static void RemoveUTF8ByteOrderMarker( ByteString &rString ); static void RemoveUTF8ByteOrderMarker( rtl::OString &rString );
static bool hasUTF8ByteOrderMarker( const ByteString &rString ); static bool hasUTF8ByteOrderMarker( const rtl::OString &rString );
static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ); static void RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename );
static bool fileHasUTF8ByteOrderMarker( const ByteString &rString ); static bool fileHasUTF8ByteOrderMarker( const ByteString &rString );
static void QuotHTML( ByteString &rString ); static void QuotHTML( ByteString &rString );

View File

@@ -192,35 +192,43 @@ void Export::QuotHTML( ByteString &rString )
rString = sReturn.makeStringAndClear(); rString = sReturn.makeStringAndClear();
} }
void Export::RemoveUTF8ByteOrderMarker( ByteString &rString ){ void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString )
{
if( hasUTF8ByteOrderMarker( rString ) ) if( hasUTF8ByteOrderMarker( rString ) )
rString.Erase( 0 , 3 ); rString = rString.copy(3);
} }
bool Export::hasUTF8ByteOrderMarker( const ByteString &rString ){ bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString )
return rString.Len() >= 3 && {
rString.GetChar( 0 ) == '\xEF' && return rString.getLength() >= 3 && rString[0] == '\xEF' &&
rString.GetChar( 1 ) == '\xBB' && rString[1] == '\xBB' && rString[2] == '\xBF' ;
rString.GetChar( 2 ) == '\xBF' ;
} }
bool Export::fileHasUTF8ByteOrderMarker( const ByteString &rString ){
bool Export::fileHasUTF8ByteOrderMarker( const ByteString &rString )
{
SvFileStream aFileIn( String( rString , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ ); SvFileStream aFileIn( String( rString , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ );
ByteString sLine; rtl::OString sLine;
if( !aFileIn.IsEof() ) { if( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine ); aFileIn.ReadLine( sLine );
if( aFileIn.IsOpen() ) aFileIn.Close(); if( aFileIn.IsOpen() )
aFileIn.Close();
return hasUTF8ByteOrderMarker( sLine ); return hasUTF8ByteOrderMarker( sLine );
} }
if( aFileIn.IsOpen() ) aFileIn.Close(); if( aFileIn.IsOpen() ) aFileIn.Close();
return false; return false;
} }
void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){
void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename )
{
SvFileStream aFileIn( String( rFilename , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ ); SvFileStream aFileIn( String( rFilename , RTL_TEXTENCODING_ASCII_US ) , STREAM_READ );
ByteString sLine; rtl::OString sLine;
if( !aFileIn.IsEof() ) { if( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine ); aFileIn.ReadLine( sLine );
// Test header // Test header
if( hasUTF8ByteOrderMarker( sLine ) ){ if( hasUTF8ByteOrderMarker( sLine ) )
{
DirEntry aTempFile = Export::GetTempFile(); DirEntry aTempFile = Export::GetTempFile();
ByteString sTempFile = ByteString( aTempFile.GetFull() , RTL_TEXTENCODING_ASCII_US ); ByteString sTempFile = ByteString( aTempFile.GetFull() , RTL_TEXTENCODING_ASCII_US );
SvFileStream aNewFile( String( sTempFile , RTL_TEXTENCODING_ASCII_US ) , STREAM_WRITE ); SvFileStream aNewFile( String( sTempFile , RTL_TEXTENCODING_ASCII_US ) , STREAM_WRITE );
@@ -228,7 +236,8 @@ void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){
RemoveUTF8ByteOrderMarker( sLine ); RemoveUTF8ByteOrderMarker( sLine );
aNewFile.WriteLine( sLine ); aNewFile.WriteLine( sLine );
// Copy the rest // Copy the rest
while( !aFileIn.IsEof() ){ while( !aFileIn.IsEof() )
{
aFileIn.ReadLine( sLine ); aFileIn.ReadLine( sLine );
aNewFile.WriteLine( sLine ); aNewFile.WriteLine( sLine );
} }
@@ -239,7 +248,8 @@ void Export::RemoveUTF8ByteOrderMarkerFromFile( const ByteString &rFilename ){
DirEntry( sTempFile ).MoveTo( DirEntry( rFilename.GetBuffer() ) ); 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 ) bool Export::CopyFile( const ByteString& source , const ByteString& dest )
@@ -398,12 +408,14 @@ sal_Bool Export::ConvertLineEnds(
return sal_False; return sal_False;
} }
ByteString sLine; rtl::OString sLine;
while ( !aSource.IsEof()) { while ( !aSource.IsEof())
{
aSource.ReadLine( sLine ); aSource.ReadLine( sLine );
if ( !aSource.IsEof()) { if ( !aSource.IsEof())
sLine.EraseAllChars( '\r' ); {
sLine = comphelper::string::remove(sLine, '\r');
aDestination.WriteLine( sLine ); aDestination.WriteLine( sLine );
} }
else else

View File

@@ -52,15 +52,20 @@ LngParser::LngParser( const ByteString &rLngFile, sal_Bool bUTF8, sal_Bool bULFF
{ {
pLines = new LngLineList(); pLines = new LngLineList();
DirEntry aEntry( String( sSource, RTL_TEXTENCODING_ASCII_US )); 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 ); SvFileStream aStream( String( sSource, RTL_TEXTENCODING_ASCII_US ), STREAM_STD_READ );
if ( aStream.IsOpen()) { if ( aStream.IsOpen())
ByteString sLine; {
rtl::OString sLine;
bool bFirstLine = true; bool bFirstLine = true;
while ( !aStream.IsEof()) { while ( !aStream.IsEof())
{
aStream.ReadLine( sLine ); 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 ); Export::RemoveUTF8ByteOrderMarker( sLine );
bFirstLine = false; bFirstLine = false;
} }

View File

@@ -28,6 +28,7 @@
// MARKER(update_precomp.py): autogen include statement, do not remove // MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_svl.hxx" #include "precompiled_svl.hxx"
#include <comphelper/string.hxx>
#include <svl/lngmisc.hxx> #include <svl/lngmisc.hxx>
#include <tools/solar.h> #include <tools/solar.h>
#include <tools/string.hxx> #include <tools/string.hxx>
@@ -55,22 +56,18 @@ sal_Int32 GetNumControlChars( const OUString &rTxt )
return nCnt; return nCnt;
} }
sal_Bool RemoveHyphens( OUString &rTxt ) sal_Bool RemoveHyphens( OUString &rTxt )
{ {
sal_Bool bModified = sal_False; sal_Bool bModified = sal_False;
if (HasHyphens( rTxt )) if (HasHyphens(rTxt))
{ {
String aTmp( rTxt ); rTxt = comphelper::string::remove(rTxt, SVT_SOFT_HYPHEN);
aTmp.EraseAllChars( SVT_SOFT_HYPHEN ); rTxt = comphelper::string::remove(rTxt, SVT_HARD_HYPHEN);
aTmp.EraseAllChars( SVT_HARD_HYPHEN );
rTxt = aTmp;
bModified = sal_True; bModified = sal_True;
} }
return bModified; return bModified;
} }
sal_Bool RemoveControlChars( OUString &rTxt ) sal_Bool RemoveControlChars( OUString &rTxt )
{ {
sal_Bool bModified = sal_False; sal_Bool bModified = sal_False;