--- basic/source/runtime/methods.cxx +++ basic/source/runtime/methods.cxx @@ -1403,26 +1403,33 @@ RTLFUNC(Replace) if ( nArgCount == 6 ) bTextMode = rPar.Get(6)->GetInteger(); - USHORT nStrLen = aExpStr.Len(); + USHORT nExpStrLen = aExpStr.Len(); + USHORT nFindStrLen = aFindStr.Len(); + USHORT nReplaceStrLen = aReplaceStr.Len(); - if( lStartPos <= nStrLen ) + if( lStartPos <= nExpStrLen ) { - String aSrcStr( aExpStr ); - if( bTextMode ) + USHORT nPos = static_cast( lStartPos - 1 ); + USHORT nCounts = 0; + while( lCount == -1 || lCount > nCounts ) { + String aSrcStr( aExpStr ); + if( bTextMode ) + { aSrcStr.ToUpperAscii(); aFindStr.ToUpperAscii(); - } - - USHORT nPos = aSrcStr.Search( aFindStr, static_cast( lStartPos - 1 ) ); - USHORT nCounts = 0; - USHORT nReplaceLength = aReplaceStr.Len() ? aReplaceStr.Len():1; - while( nPos != STRING_NOTFOUND && (lCount == -1 || lCount > nCounts) ) - { - aExpStr.Replace( nPos, aFindStr.Len(), aReplaceStr ); - nPos = nPos + nReplaceLength; + } nPos = aSrcStr.Search( aFindStr, nPos ); - nCounts++; + if( nPos != STRING_NOTFOUND ) + { + aExpStr.Replace( nPos, nFindStrLen, aReplaceStr ); + nPos = nPos - nFindStrLen + nReplaceStrLen + 1; + nCounts++; + } + else + { + break; + } } } rPar.Get(0)->PutString( aExpStr.Copy( static_cast(lStartPos - 1) ) );