QCString -> QByteArray

David Faure faure at kde.org
Wed Feb 14 12:55:01 CET 2007


On Tuesday 13 February 2007, Roger Larsson wrote:
> On Monday 12 February 2007 14:49, David Faure wrote:
> > Converting a QCString to a QByteArray:
> What if you like to do something more, like adding a character?
How would that change anything, if we do it after the conversion? It will be the same operation in all cases.

>     for ( uint i = 0; i < numIterations; ++i ) {
>         mBodyOld.duplicate( aStr.data(), aStr.length()+1 );
> 	mBodyOld += 'a';
>     }
QByteArray doesn't have operator+=(char) in Qt3, nor operator+=(char*).
It wasn't meant for string operations in Qt3. This is why I wrote KMail::Util::append() now.

inline void append( QByteArray& that, const char* str )
{
  if ( !str )
    return; // nothing to append
  that.detach();
  uint len1 = that.size();
  uint len2 = qstrlen(str);
  if ( that.resize( len1 + len2, QByteArray::SpeedOptim ) )
    memcpy( that.data() + len1, str, len2 );
}

Of course QByteArray in Qt4 solves all this, since it is meant for string operations
and takes care of the trailing nul.

> 	nBodyNew += 'a'; // Will this work without detach?
>         mBodyNew.truncate( aStr.size() -1+1 ); // Note: changed object for 
> size operation, correctly?
Nonsense. We need to truncate first, then do operations on the bytearray.
duplicate+truncate are two operations that go together, they are part of the cstring->bytearray
conversion.

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).


More information about the Kde-optimize mailing list