Lack of "fast strings"

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
idle
Always Here
Always Here
Posts: 5042
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Lack of "fast strings"

Post by idle »

skywalk wrote: Wed Mar 22, 2023 10:26 pm
SMaag wrote: Wed Mar 22, 2023 4:36 pmThere is one greate trick to speed up pasing strings to a procedure.
Use a Prototype to call your Procedure what receives a String. In this case the PB do not copy the string.
Nice trick, but why can't you pass your strings byref, using a pointer?
If a sting isn't modified it should be passed in by reference but so often strings are needlessly passed by value just to maintain syntactic consistency. So using the prototype trick is actually very handy. It makes it easy for users.

Code: Select all

Prototype StrCmp(a.p-unicode,b.p-unicode)  ;convert a string input to a pointer 

Structure aUnicode 
  c.u[0]
EndStructure 
  
Procedure _StrCmp(*a.aUnicode,*b.aUnicode)   
  Protected a,b 
  
  While *a\c[ct] = *b\c[ct]
    ct+1
    a = *a\c[ct] 
    b = *b\c[ct]
    If (a = 0 And b = 0) 
      ProcedureReturn 1 
    ElseIf (a = 0 Or b = 0) 
      ProcedureReturn 0 
    EndIf   
  Wend   
      
EndProcedure  

Global StrCmp.StrCmp = @_StrCmp()

Global sa.s = "helloworld" 
Global sb.s = sa 
Global sc.s = "helloPB"

Debug StrCmp(sa,sb) 
Debug StrCmp(sa,sc) 


Post Reply