Page 1 of 1

[14 Feb 2008] Interesting Bug in ReplaceStr()

Posted: Sat Jun 13, 2020 5:31 pm
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.

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

Posted: Sat Jun 13, 2020 5:31 pm
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").

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

Posted: Sat Jun 13, 2020 5:31 pm
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.

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

Posted: Sat Jun 13, 2020 5:31 pm
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

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

Posted: Sat Jun 13, 2020 5:31 pm
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