Tasp
Full Member
Posts: 215
|
Post by Tasp on Sept 17, 2023 12:36:16 GMT -5
Warning! LB will hang and need to be forced closed if you run this.
I needed to replace the spaces in my text with double spaces. the below code makes LB hang, with the mouse pointer flickering between an arrow and hourglass. it causes 40% CPU usage and never completes. Needs to be force closed.
Both code examples create the same issue.
a$ = "If you're ever being chased by a Police dog, try not to run thru a tunnel, over a see-saw and jump thru a fiery ring, they're trained for that stuff."
a$ = replstr$(a$, " ", " ")
PRINT a$
a$ = "If you're ever being chased by a Police dog, try not to run thru a tunnel, over a see-saw and jump thru a fiery ring, they're trained for that stuff."
b$ = replstr$(a$, " ", SPACE$(2))
PRINT b$
Change that double space to any other char and it's fine. Change the first a$ to anything else and it still fails.
Did a search, didn't see this reported. Hope this helps.
|
|
|
Post by PaulDZ on Sept 17, 2023 14:04:36 GMT -5
The reason your program is hanging is because you are changing the " " to a double " ". When the REPSTR$() command finishes with the first change, it repeats and finds the first space of the double space you just created. One easy fix is to turn it into a two step process. First change to a different character (that wouldn't ever be part of the original string) and then change that to a double space.
Here's an example of what I mean:
a$ = "If you're ever being chased by a Police dog, try not to run thru a tunnel, over a see-saw and jump thru a fiery ring, they're trained for that stuff."
print a$
a$ = replstr$(a$, " ", "QQ") a$ = replstr$(a$, "QQ", " ")
PRINT a$
Using this method will eliminate the problem.
Note: I used the character sequence "QQ" since it is NEVER used in the American English Language. In normal text, you could as easily use a CHR$(7) (ASCII BEEP) or other unprintable character if you wanted.
I hope this helps.
|
|
Tasp
Full Member
Posts: 215
|
Post by Tasp on Sept 17, 2023 14:12:35 GMT -5
Yep, I understand what's happening. But I reporting this as a bug, because I'm not convinced this is intended behaviour.
I would expect it to replace every " " with " ".
I assume that in Smalltalk, Carl has written this as a function that starts from the beginning of the string each time it replaces a found match, rather than continuing on from where it left off.
|
|
|
Post by Carl Gundel on Sept 17, 2023 16:34:40 GMT -5
Yep, I understand what's happening. But I reporting this as a bug, because I'm not convinced this is intended behaviour. I would expect it to replace every " " with " ". I assume that in Smalltalk, Carl has written this as a function that starts from the beginning of the string each time it replaces a found match, rather than continuing on from where it left off. I think it's fair to call that a bug.
|
|
|
Post by Walt Decker on Sept 19, 2023 11:09:06 GMT -5
Why use 2-step process?
' Mstr$ = "PETER PIPER PICKED A PECK OF" Repl$ = " " With$ = " " A$ = ""
A$ = FN.ReplaceString$(Mstr$, Repl$, With$) PRINT A$ Mstr$ = A$ Repl$ = " " With$ = " " A$ = "" A$ = FN.ReplaceString$(Mstr$, Repl$, With$) PRINT A$ WAIT END
FUNCTION FN.ReplaceString$(MainStr$, StrToRepl$, ReplWith$)
ReplLen = 0 WithLen = 0 Found = 0 StrtPos = 0
TmpStr$ = "" Lft$ = "" Rgt$ = ""
TmpStr$ = MainStr$ ReplLen = LEN(StrToRepl$) WithLen = LEN(ReplWith$)
StrtPos = 1 [STRT.REPLACE] Found = INSTR(TmpStr$, StrToRepl$, StrtPos)
IF Found THEN Lft$ = LEFT$(TmpStr$, Found - 1) Rgt$ = MID$(TmpStr$, Found + ReplLen) TmpStr$ = Lft$ + ReplWith$ + Rgt$ StrtPos = Found + WithLen GOTO [STRT.REPLACE] END IF
FN.ReplaceString$ = TmpStr$ END FUNCTION '
WARNING: INSTR() is case sensitive!!!
|
|
|
Post by Brandon Parker on Sept 28, 2023 21:44:07 GMT -5
Yep, I understand what's happening. But I reporting this as a bug, because I'm not convinced this is intended behaviour. I would expect it to replace every " " with " ". I assume that in Smalltalk, Carl has written this as a function that starts from the beginning of the string each time it replaces a found match, rather than continuing on from where it left off. I think it's fair to call that a bug. This appears to be the same bug presented three years ago... libertybasiccom.proboards.com/thread/827/replstr-single-quote{:0) Brandon Parker
|
|