Page 2 of 2
Re: [Done] PB 6.40 Alpha 1 - Issue with referenced strings which are modified
Posted: Wed Jan 28, 2026 5:04 pm
by Matheos
Fred wrote: Wed Jan 28, 2026 4:38 pm
Small loop should be very fast, can you post your code ?
Yes, one example that concerns me especially, is where we have a console session open to provide an editor. The data is in a very proprietary form, which often contains control codes. We have to convert the control codes to a displayable symbol (not in the actual data but only when it's displayed).
Therefore, as the user moves around the data with PgUp/PgDn/Home/End and so on, we have to convert the control characters into a displayable character, normally 149 which is a dot, so the user can identify the characters that must not be changed or overwritten :
Code: Select all
ReplaceString(Params.s, Chr(60251), Chr(149), #PB_String_InPlace)
Re: [Done] PB 6.40 Alpha 1 - Issue with referenced strings which are modified
Posted: Wed Jan 28, 2026 5:29 pm
by Fred
How big is param ? To me, using a PB function for this should be very close in term of speed. Something like (use it with caution, I didn't test it much):
Code: Select all
Procedure ReplaceChar(*String.Character, CharToSearch.c, CharToReplace.c)
If *String = 0
ProcedureReturn
EndIf
While *String\c
If *String\c = CharToSearch
*String\c = CharToReplace
EndIf
*String+1
Wend
EndProcedure
Test$ = "Hello World !"
ReplaceChar(@Test$, 'o', 'O')
Debug Test$
With this proc, you can also replace several char in one pass.
Re: [Done] PB 6.40 Alpha 1 - Issue with referenced strings which are modified
Posted: Wed Jan 28, 2026 6:00 pm
by User_Russian
Fred wrote: Wed Jan 28, 2026 5:29 pm
PB uses Unicode strings, and each character requires 2 bytes. I think it's better to do it this way.
Re: [Done] PB 6.40 Alpha 1 - Issue with referenced strings which are modified
Posted: Wed Jan 28, 2026 7:28 pm
by ChrisR
If used with ReplaceChar(@Test$, 'o', ''), the length needs to be recalculated with PeekS
By changing the ReplaceChar procedure like this, you don't have to call PeekS
but Timo won't agree with this hack
Code: Select all
Procedure ReplaceChar(*String.Character, CharToSearch.c, CharToReplace.c)
If *String = 0 : ProcedureReturn : EndIf
Protected *Len.Integer = *String -SizeOf(Integer)
*Len\i = 0
While *String\c
If *String\c = CharToSearch
*String\c = CharToReplace
If CharToReplace = 0
Break ; Comment it for testing the lenght
EndIf
EndIf
*Len\i + 1
*String + SizeOf(Character)
Wend
EndProcedure
Test$ = "Hello World !"
ReplaceChar(@Test$, 'o', 'O')
Debug Test$
ReplaceChar(@Test$, ' ', '')
Debug Test$
Debug Len(Test$)
;Test$ = PeekS(@Test$)
;Debug Len(Test$)
Re: [Done] PB 6.40 Alpha 1 - Issue with referenced strings which are modified
Posted: Wed Jan 28, 2026 7:55 pm
by STARGĂ…TE
Interestingly, a self-written loop is faster than ReplaceString(, #PB_String_InPlace).
However, this self-written function only works for single character replacement.
Code: Select all
CompilerIf #PB_Compiler_Debugger
CompilerError "No Debugger!!!!!!!!!!"
CompilerEndIf
Procedure ReplaceChar(*String.Character, CharToSearch.c, CharToReplace.c)
If *String = 0
ProcedureReturn
EndIf
While *String\c
If *String\c = CharToSearch
*String\c = CharToReplace
EndIf
*String + SizeOf(Character)
Wend
EndProcedure
OpenConsole()
Define Time.q, Time1.q, Time2.q
Define String.s = ReplaceString(Space(10000), " ", "Abc")
Time = ElapsedMilliseconds()
For I = 1 To 1000
ReplaceString(String, "A", "a", #PB_String_InPlace)
ReplaceString(String, "a", "A", #PB_String_InPlace)
Next
Time1 = ElapsedMilliseconds() - Time
Time = ElapsedMilliseconds()
For I = 1 To 1000
ReplaceChar(@String, Asc("A"), Asc("a"))
ReplaceChar(@String, Asc("a"), Asc("A"))
Next
Debug String
Time2 = ElapsedMilliseconds() - Time
PrintN("ReplaceString: " + Time1 + " ms")
PrintN("ReplaceChar: " + Time2 + " ms")
Input()
Matheos wrote: Wed Jan 28, 2026 5:04 pm
Therefore, as the user moves around the data with PgUp/PgDn/Home/End and so on, we have to convert the control characters into a displayable character, normally 149 which is a dot, so the user can identify the characters that must not be changed or overwritten :
Code: Select all
ReplaceString(Params.s, Chr(60251), Chr(149), #PB_String_InPlace)
You can replace this function and you will gain speed, instead of loosing time.
ChrisR wrote: Wed Jan 28, 2026 7:28 pm
If used with ReplaceChar(@Test$, 'o', ''), the length needs to be recalculated with PeekS