From: Tom Serface on
You can also create a CString using the constructor:

CString(TCHARch,intnRepeat=1);

Where the repeat character is the number of characters you want ultimately.

You might also want to try a regex engine like boost to see if that is
faster:

http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html

Tom

"dududuil" <dududuil(a)discussions.microsoft.com> wrote in message
news:69E810BD-F736-4B2E-BE05-CB17DBFCDDF3(a)microsoft.com...
> I have failed to find how to preallocate a CString to a certin size - does
> anyone know how can I set the size of a CString ?
>
>
> "Ulrich Eckhardt" wrote:
>
>> dududuil wrote:
>> > I have a huge application that does a lot of strings manipulations.
>> > Analyzing its performance, I have noticed that a lot of time is spend
>> > in
>> > CString::Replace.
>> >
>> > calls like :
>> > str.Replace("\n","\n\t"); or
>> > str.Replace("\r", "\0x00");
>> > take lots of time. (25% of overall performance !!)
>> >
>> > How can I improve the performance?
>>
>> The call to Replace() has to allocate a second buffer, transform the
>> content
>> accordingly into the new buffer and then release the old one. It's the
>> allocating and releasing that probably takes the performance, or at least
>> it's the part that can be avoided mostly.
>>
>> If you actually have more than one call to Replace() on the same string,
>> consider rolling your own function that does the replacements. If you
>> have
>> the two above, I'd do it like this:
>>
>> CString tmp;
>> tmp.Reserve(str.GetLength()); // preallocate, not sure if MFC allow this
>> for(int i=0, len=str.GetLength(); i!=len; ++i)
>> {
>> switch(str[i]) {
>> case '\n': tmp += "\n\t"; break;
>> case '\r': tmp += "\0x00"; break;
>> default: tmp += str[i]; break;
>> }
>> }
>> str = tmp;
>>
>> The preallocation makes the target string overallocate its internal
>> buffer,
>> so that not every call that adds something causes an allocation.
>>
>> Uli
>>
>> --
>> C++ FAQ: http://parashift.com/c++-faq-lite
>>
>> Sator Laser GmbH
>> Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
>> .
>>