udfStrRemoveTabSpace (sString)
;------------------------------------------------------------------------------------------------------------------------------------------
; How to remove tabs and spaces from a string.
;------------------------------------------------------------------------------------------------------------------------------------------
; This is a collection of six different algorithms
; including a performancetest.
;------------------------------------------------------------------------------------------------------------------------------------------
; Detlev Dalitz.20020201
;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_1 (str)
len = StrLen(str)
OutStr = ""
i = 1
While (i <= len)
   While (i < len) && (StrClean(StrSub(str,i,2),StrCat(@TAB," "),"",@FALSE,1) == "")
      i = i + 1
   EndWhile
   OutStr = StrCat(OutStr,StrSub(str,i,1))
   i = i + 1
EndWhile
Return (OutStr)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_2 (str)
str = StrReplace(str,@TAB," ")
While @TRUE
   lenpre = StrLen(str)
   str = StrReplace(str,"  "," ")
   lenpost = StrLen(str)
   If (lenpre==lenpost) Then Break
EndWhile
Return (str)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_3 (str)
str = StrReplace(str,@TAB," ")
lenpost = StrLen(str)
lenpre  = lenpost + 1
While (lenpre>lenpost)
   lenpre = lenpost
   str = StrReplace(str,"  "," ")
   lenpost = StrLen(str)
EndWhile
Return (str)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_4 (str)
str = StrClean(str,@TAB," ",@FALSE,1)
While StrIndex(str,"  ",1,@FWDSCAN)
   str = StrReplace(str,"  "," ")
EndWhile
Return (str)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_5 (str) ; This udf seems to be the fastest.
str = StrReplace(str,@TAB," ")
While StrIndex(str,"  ",1,@FWDSCAN)
   str = StrReplace(str,"  "," ")
EndWhile
Return (str)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

#DefineFunction udfStrRemoveTabSpace_6 (str)
bb = BinaryAlloc(StrLen(str))
BinaryPokeStr(bb,0,str)
BinaryReplace(bb,@TAB," ",@FALSE)
While (BinaryReplace(bb,"  "," ",@FALSE) > 1)
EndWhile
str = BinaryPeekStr(bb,0,BinaryEodGet(bb))
BinaryFree(bb)
Return (str)
#EndFunction

;------------------------------------------------------------------------------------------------------------------------------------------

;--- test ---

msgtitle = "Demo udfStrRemoveTabSpace Performance Test"

TestStr = StrCat(@TAB,@TAB,@TAB,@TAB,"  Hi       there.  ")
TestLoop = 100

MaxTests = 6
For t=1 To MaxTests
   Display(1,msgtitle,"Running Test %t%, please wait ...")
   Exclusive(@ON)
   start = GetTickCount()
   For i=1 To TestLoop
      str = udfStrRemoveTabSpace_%t% (TestStr)
   Next
   stop = GetTickCount()
   Exclusive(@ON)
   Ticks%t% = stop-start
Next

MaxTicks = 0
For t=1 To MaxTests
   MaxTicks = Max(MaxTicks,Ticks%t%)
Next

For t=1 To MaxTests
   Pct%t% = 100*Ticks%t%/MaxTicks
Next

msgtext = ""
For t=1 To MaxTests
   msgtext = StrCat(msgtext,"Test ",t,@TAB,"Ticks = ",@TAB,Ticks%t%,@TAB,Pct%t%," %%",@CRLF)
Next
ClipPut(msgtext)
Message(msgtitle,msgtext)

Exit
;------------------------------------------------------------------------------------------------------------------------------------------
;   WinBatch Studio Debug
;   Test 1 Ticks =  73098 100 %
;   Test 2 Ticks =  16307 22 %
;   Test 3 Ticks =  17598 24 %
;   Test 4 Ticks =  9515 13 %
;   Test 5 Ticks =  9358 12 %  <== The Winner.
;   Test 6 Ticks =  9928 13 %
;------------------------------------------------------------------------------------------------------------------------------------------
;   WinBatch Studio Run
;   Test 1 Ticks =  10586 100 %
;   Test 2 Ticks =  1955 18 %
;   Test 3 Ticks =  1764 16 %
;   Test 4 Ticks =  1324 12 %
;   Test 5 Ticks =  1284 12 %  <== The Winner.
;   Test 6 Ticks =  1684 15 %
;------------------------------------------------------------------------------------------------------------------------------------------
;*EOF*