fb:porticula NoPaste
String Replace very fast in ASM
Uploader: | marpon |
Datum/Zeit: | 23.01.2013 10:20:50 |
Hinweis: Dieser Quelltext ist Bestandteil des Projekts CSED_FB multi-language Windows IDE for FreeBasic, zu dem es auf FreeBASIC-Portal.de eine Projektseite gibt.
'String Replace very fast in ASM
'
'Only 1 restriction with Asc("¿") ,
'it is used as temp replace character,
'if you don't put any character as replace string,
' it will be removed at the end
'"¿" has been choosen because of very few usage but you can change it by your own character.
'
'Usage:
' modified_string = Replace( initial_string, StringToReplace , ReplaceString)
'
'all variables are strings
'
'
Function Replace(TiStr As String ,TiFind As String ,TiRep As String ) As String
Dim As ZString Ptr iStr,iFind, iRep
Dim As Integer icor= 0
Dim As Integer i, code
If TiStr="" Or TiFind="" Then
Function = TiStr
Exit Function
EndIf
If TiRep="" Then
TiRep="¿"
icor=1
EndIf
iStr=StrPtr(TiStr)
iFind= StrPtr(TiFind)
iRep= StrPtr(TiRep)
Dim As Integer iSize=Len(TiStr)-Len(TiFind)
Dim As ZString Ptr dStr=CAllocate(Len(TiStr)*20)
Dim As String s
Asm
mov esi,[iStr]
add [iSize],esi
mov ebx,[iFind]
inc dword Ptr[iSize]
mov edi,[dStr]
End Asm
Asm Sub esi,1
Asm
jmp Start1
Start2: add esi,ecx
Start1: add esi,1
cmp [iSize],esi
jle Done
movzx eax,Byte Ptr[esi]
cmp al,[ebx]
je Match
mov [edi],al
add edi,1
jmp Start1
Match: mov ecx,-1
mov edx,ebx
B1: add ecx,1
movzx eax,Byte Ptr[edx]
Test eax,eax
jz Change
add edx,1
cmp [esi+ecx],al
je B1
movzx eax,Byte Ptr[esi]
mov [edi],al
add edi,1
jmp Start1
Change: mov edx,[iRep]
End Asm
saute: Asm Sub ecx,1
Asm
B2: movzx eax,Byte Ptr[edx]
Test eax,eax
jz Start2
add edx,1
mov [edi],al
add edi,1
jmp B2
Done: mov ecx,-1
B3: add ecx,1
movzx eax,Byte Ptr[esi+ecx]
mov [edi+ecx],al
Test eax,eax
jnz B3
' mov eax,[dStr]
' mov [toto],eax
End Asm
s=*dStr
If icor=0 Then
Function = s
Deallocate dStr
Exit Function
EndIf
code=Asc("¿") ' change it here if you want
asm
mov esi, [dStr]
mov edx, -1 '' "get" pointer
Xor ecx, ecx '' "put" pointer
Test esi, esi '' in case passed null string
jz 1f
0:
inc edx
movzx eax, Byte Ptr [esi+edx]
Test eax, eax
jz 1f
cmp eax,[code] '
je 0b
mov [esi+ecx], al
inc ecx
jmp 0b
1:
mov [i], ecx
End asm
s=Left$(*dStr,i)
Function = s
Deallocate dStr
End Function