= is a nasty thing

Originally Posted by
SDK Help, TPtr8
Copies data into this 8-bit modifiable pointer descriptor replacing any existing data.
so textPtr is not updated. What you need is TPtr8::Set.
However you can also rely on variable scope, and use the fact that Find works on read-only descriptors (like aText) too:
Code:
void StringReplaceL(HBufC8*& aText, const TDesC8& aSearchString, const TDesC8& aReplaceWith )
{
TInt searchLength = aSearchString.Length();
TInt replaceLength = aReplaceWith.Length();
TInt pos = 0;
while( pos != KErrNotFound )
{
pos = aText->Find( aSearchString );
if( pos != KErrNotFound )
{
//Expand the buffer to hold the new data
aText = aText->ReAllocL(aText->Length() - searchLength + replaceLength);
TPtr8 textPtr( aText->Des() );
textPtr.Replace(pos, searchLength, aReplaceWith);
}
}
}
In fact textPtr is used in a single statement, thus it does not have to be a separate variable any more
Code:
void StringReplaceL(HBufC8*& aText, const TDesC8& aSearchString, const TDesC8& aReplaceWith )
{
TInt searchLength = aSearchString.Length();
TInt replaceLength = aReplaceWith.Length();
for(TInt pos = aText->Find(aSearchString); pos != KErrNotFound; pos = aText->Find(aSearchString))
{
aText = aText->ReAllocL(aText->Length() - searchLength + replaceLength);
aText->Des().Replace(pos, searchLength, aReplaceWith);
}
}