Faster Version of Replace

Category: Strings

Date: 02-16-2022

Return to Index


 
'Compilable Example:  (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
 
Enum Equates Singular
   IDC_ButtonA
   IDC_ButtonB
   IDC_ButtonC
End Enum
 
Global hDlg As Dword, qFreq,qStart,qStop As Quad, Rep() As Byte, BadChar$
 
Function PBMain() As Long
   Dialog New Pixels, 0, "PowerBASIC",300,300,200,200, %WS_OverlappedWindow To hDlg
   Control Add Button, hDlg, %IDC_ButtonA,"Replace$", 50,10,100,20
   Control Add Button, hDlg, %IDC_ButtonB,"Alternative15", 50,40,100,20
   Control Add Button, hDlg, %IDC_ButtonC,"Pierre17", 50,70,100,20
   Dialog Show Modal hDlg Call DlgProc
End Function
 
CallBack Function DlgProc() As Long
   Local temp$, i As Long, pSrc As Byte Ptr
   Select Case Cb.Msg
      Case %WM_InitDialog
         QueryPerformanceFrequency qFreq
         BadChar$ = Chr$(0 To 47,54 To 56, 58 To 64, 91 To 96, 123 To 255)  'GoodChar: 48-57 65-90 97-122
         ReDim Rep(255)
         For i = 0 To 255
            If InStr(BadChar$,Chr$(i)) Then Rep(i) = Asc(".") Else Rep(i) = i
         Next i
      Case %WM_Command
         Select Case Cb.Ctl
            Case %IDC_ButtonA
               temp$ = Repeat$(1000000,"0123456789")
               QueryPerformanceCounter   qStart
               Replace Any BadChar$ With Repeat$(Len(BadChar$),".") In temp$
               QueryPerformanceCounter   qStop
               ? Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
'               ? temp$
            Case %IDC_ButtonB
               temp$ = Repeat$(1000000,"0123456789")
               pSrc = StrPtr(temp$)
               QueryPerformanceCounter   qStart
               For i = 1 To Len(temp$)
                   @pSrc = Rep(@pSrc)
                   Incr pSrc
               Next i
               QueryPerformanceCounter   qStop
               ? Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
'               ? temp$
            Case %IDC_ButtonC
               Local sBadChar2 As String
               Local pBadChar2 As Dword
               Local LenTemp   As Dword
               Local Looper    As Long
 
               Temp$ = Repeat$(1000000, "0123456789")
 
               sBadChar2 = Chr$(0 To 255)
               For Looper = 1 To 256
                Select Case Looper
                  Case 1 To 48, 55 To 57, 59 To 65, 92 To 97, 124 To 256
                    Mid$(sBadChar2, Looper, 1) = "."
                End Select
               Next
 
               QueryPerformanceCounter(qStart)
               pSrc = StrPtr(Temp$)
               LenTemp = Len(Temp$)
               pBadChar2 = StrPtr(sBadChar2) 'Thank to B�rje Hagsten
               ! mov esi, pSrc               'Pointer to string data
               ! mov ecx, LenTemp            'Move length of string into ecx
               ! mov ebx, pBadChar2          'Move pointer to translation table into ebx
                 BadCharLoop:                'Loop label
               ! mov al, [esi]               'Move current character into al
               ! xlatb                       'Translate character
               ! mov [esi], al               'Move translated character back into string
               ! inc esi                     'Next character
               ! dec ecx                     'Decr counter
               ! jnz BadCharLoop             'Exit when ecx = 0
               QueryPerformanceCounter(qStop)
               ? Format$((qStop-qStart)/qFreq,"###.000") & " seconds"
'               ? temp$
         End Select
   End Select
End Function
   
'gbs_01361
'Date: 05-11-2013


created by gbSnippets
http://www.garybeene.com/sw/gbsnippets.htm