[14 Feb 2008] Interesting Bug in ReplaceStr()

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
PEB
Posts: 576
Joined: Sun Feb 21, 2010 1:28 am

[14 Feb 2008] Interesting Bug in ReplaceStr()

Post by PEB »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 14 Feb 2008 21:11:30 -0000

When using ReplaceStr() in a string with only one occurance of the string to be replaced, the resulting string gets truncated.

If you do this:

Code: Select all

SampleString$="This is a test string to demonstrate an annoying bug."
SampleString$=ReplaceStr(SampleString$," a "," the ")
...then SampleString$ gets shortened to "This is the test strin"

However, if you do this:

Code: Select all

SampleString$="This is a test string to demonstrate a annoying bug."
SampleString$=ReplaceStr(SampleString$," a "," the ")
...then the returned string is correct.
User avatar
Clyde
Posts: 351
Joined: Sun Feb 14, 2010 12:38 pm
Location: Dresden / Germany

[15 Feb 2008] Re: Interesting Bug in ReplaceStr()

Post by Clyde »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 15 Feb 2008 00:12:04 +0100

Thanks for that. It isn't really explainable ...

BTW, it took me nearly ages to see the difference in your code lines! :-) For all those who also look for it: in the first string it is "an annoying bug", in second "a annoying bug" ("an" vs "a").
Currently using: Hollywood 9 with Windows IDE and Hollywood 9 with Visual Studio Code and hw4vsc
User avatar
airsoftsoftwair
Posts: 5833
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[15 Feb 2008] Re: Interesting Bug in ReplaceStr()

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 15 Feb 2008 21:01:51 +0100
When using ReplaceStr() in a string with only one occurance of the string to be replaced, the resulting string gets truncated.

If you do this:

Code: Select all

SampleString$="This is a test string to demonstrate an annoying bug."
SampleString$=ReplaceStr(SampleString$," a "," the ")
...then SampleString$ gets shortened to "This is the test strin"
Hmm, yeah, sucks. I'll fix this. Thanks for the report.
User avatar
Allanon
Posts: 742
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

[21 Feb 2008] Re: Interesting Bug in ReplaceStr()

Post by Allanon »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 21 Feb 2008 09:56:51 -0000

Hello, until the function is patched you can use this workaround:

Code: Select all

Function MyReplace(Source, Sub, Rep)
     ; This function replace the bugged ReplaceStr()
     n = FindStr(Source, Sub)
     If n >= 0
         t = LeftStr(Source, n)..Rep..RightStr(Source, StrLen(Source) - StrLen(Sub) - n)
     Else
         t = Source
     EndIf

     Return(t)
EndFunction
Hope it helps :)

Regards, Fabio
----------------------------
[Allanon] Fabio Falcucci | GitHub (leaving) | Gitea (my new house) | My Patreon page | All my links
PEB
Posts: 576
Joined: Sun Feb 21, 2010 1:28 am

[21 Feb 2008] Re: Re: Interesting Bug in ReplaceStr()

Post by PEB »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Thu, 21 Feb 2008 15:05:36 -0800 (PST)

Thanks for the new replace function; it works well! :-)

And if you want this function to be able to handle repeat string replacements, this can be easily achieved by adding a While-Wend statement:

Code: Select all

Function MyReplace(Source, Sub, Rep)
 ;This function replace the bugged ReplaceStr()
 While n<>-1
  n = FindStr(Source, Sub)
  If n >= 0
   t = LeftStr(Source, n)..Rep..RightStr( Source,
StrLen(Source) - StrLen(Sub) - n)
  Else
   t = Source
  EndIf
  Source=t
 WEnd
 Return(t)
EndFunction
Locked