delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
498 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Bug in StringReplace (Handling Null characters) (fixed)Format this article printer-friendly!Bookmark function is only available for registered users!
Workaround for a bug in Delphi StringReplace Function
Product:
Delphi all versions
Category:
Object Pascal
Skill Level:
Scoring:
Last Update:
03/08/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet bug stringreplace null-character null
Times Scored:
3
Visits:
2607
Uploader: Stewart Moss
Company: New Heights Software Developme
Reference: N/A
 
Question/Problem/Abstract:
I've noticed a problem when you try to use StringReplace on a string which contains NULL (#0) characters (not null terminated).
Answer:



There is an undocumented bug in the StringReplace function.

It appears that it does not handle strings will NULL (#0) characters in them.

Here is a better routine which handles NULL correctly.



function customStringReplace(OriginalString, Pattern, Replace: string): string;
{-----------------------------------------------------------------------------
  Procedure: customStringReplace
  Date:      07-Feb-2002
  Arguments: OriginalString, Pattern, Replace: string
  Result:    string

  Description:
    Replaces Pattern with Replace in string OriginalString.
    Taking into account NULL (#0) characters.

    I cheated. This is ripped almost directly from Borland's
    StringReplace Function. The bug creeps in with the ANSIPos
    function. (Which does not detect #0 characters)
-----------------------------------------------------------------------------}
var
  
SearchStr, Patt, NewStr: string;
  
Offset: Integer;
begin
  
Result := '';
  
SearchStr := OriginalString;
  
Patt := Pattern;
  
NewStr := OriginalString;

  while
SearchStr <> '' do
  begin
    
Offset := Pos(Patt, SearchStr); // Was AnsiPos
    
if Offset = 0 then
    begin
      
Result := Result + NewStr;
      
Break;
    end;
    
Result := Result + Copy(NewStr, 1, Offset - 1) + Replace;
    
NewStr := Copy(NewStr, Offset + Length(Pattern), MaxInt);
    
SearchStr := Copy(SearchStr, Offset + Length(Patt), MaxInt);
  end;

end;










Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Seeking position
    Jerry Hayes (Mar 7 2002 8:30PM)

Haven't running it, but it looks like possible trouble from the reseek of the position.  Seems like a perpetual loop on replacing one set of characters with same, ('ab' with 'ab').  You'll continue to find it, delete it, insert it, etc.  Or something like 'aa' to 'aaa' -- it'll create a long string and be a very long day.

Also, looks like you could get an unexpected replacement because you're reseeking position from the front.  So you might create a character combination on replace that wouldn't have originally been found.

Really need to build a new string from scratch or start your next pos from the last position+length of found string.
Respond

RE: Seeking position
Stewart Moss (Mar 8 2002 8:45AM)

Thanks I have fixed it ;)

(And tested it )
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
R. Lefter
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)