ok i have posted a version of his already. but this is modified, k heres its fetures. you can replace any string of any length with a any string of any length in the replace string expression u supply. it isnt a dinky replace one letter with a nother 1 letter. it will replace 1 letter with 6 or replace 7 letters with 2! it uses lots of memory access. so thats why its slow in replaceing. i want opinions about it, and any sugggestions on how to optimizie it.!


;---------------------------;---------------------------;---------------------------
;String replace 2
;---------------------------;---------------------------;---------------------------
.data
lpheap dd 0
.code

;=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|;for string replace
PStrMultiMove Proc,PTRDEST :DWORD ,PTRFIND :DWORD,PTRSRC :DWORD


Local pdest :DWORD
Local pfind :DWORD
Local psrc :DWORD
pushad
mov ecx,PTRDEST
Invoke StringLen,ecx
cmp eax,0
je PStrMultiMoveEND
mov pdest,eax

mov ebx,PTRSRC
Invoke StringLen,ebx
cmp eax,0
je PStrMultiMoveEND
mov psrc,eax

Invoke StringLen,PTRFIND
cmp eax,0
je PStrMultiMoveEND
mov pfind,eax
add ecx,eax
pushad
mov edx,pdest
add edx,1000
;13+1000 for 5l4xx0r
Invoke GetProcessHeap
Invoke HeapAlloc,eax, 0 ,edx
mov lpheap,eax
popad
Invoke PStrCpy,lpheap,ecx,0;copy restof string into memory w/o find string
sub ecx,pfind
Invoke PStrCpy,ecx,ebx,1; add the replace string
add ecx,psrc
Invoke PStrCpy,ecx,lpheap,0; append the rest of string onto it




PStrMultiMoveENDHEAP:
Invoke GetProcessHeap
Invoke HeapFree, eax, 0, lpheap
PStrMultiMoveEND:

popad
ret
PStrMultiMove EndP
;=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|
PStrReplace Proc stdcall public,PTRstringrep :DWORD,PTRstrrep :DWORD,PTRSTRING2REPW :DWORD,PTRbuffer :DWORD
Local PTRstringrepLEN :DWORD
Local PTRstrrepwLEN :DWORD
Local fstrun :DWORD;unused!
local dwReplacelen :DWORD
;int 3
pushad


Invoke PStrCpy,PTRbuffer,PTRstringrep,0; copy first string into buffer

mov ecx,PTRbuffer ; ecx is the pointer to the buffer were going to put our data in
mov ebx,PTRstrrep ; ebx is the pointer of the string werere going to replace

Invoke StringLen,ecx ; find the string lenght of ecx
mov PTRstringrepLEN,eax

Invoke StringLen,ebx
mov PTRstrrepwLEN,eax

cmp PTRstringrepLEN,eax
jb PStrReplace@endo
Invoke StringLen,PTRSTRING2REPW
mov dwReplacelen,eax

mov edx,PTRstringrepLEN
sub edx,PTRstrrepwLEN
mov edi,edx
xor eax,eax
xor edx,eax
mov esi,-1
jmp @neloop
PStrReplace@not:


dec ecx
Invoke PStrMultiMove,ecx,ebx,PTRSTRING2REPW

push eax
push edx
Invoke StringLen,PTRbuffer
mov PTRstringrepLEN,eax
mov edx,PTRstringrepLEN
sub edx,PTRstrrepwLEN
mov edi,edx
pop edx
pop eax
inc ecx
sub ecx,PTRstrrepwLEN; avaoid loops eg replace e with 1e1, would have been endless without this!
add ecx,dwReplacelen



@neloop:
cmp esi,edi
je PStrReplace@endo
Invoke PStrCmp,ecx,ebx
inc ecx
inc esi
cmp eax,1
jne @neloop
je PStrReplace@not
PStrReplace@endo:


popad
ret
PStrReplace EndP
;=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|=-|
PStrCmp Proc stdcall public, PTRstring:DWORD,PTRstring2find:DWORD
;Local strlenght1 :DWORD
push ecx
push ebx
push edx
push esi
mov ecx,PTRstring
;Invoke StringLen,ecx
;cmp eax,0
;je @ret0attrrcmp
mov ebx,PTRstring2find
Invoke StringLen,ebx
;cmp eax,0
;je @ret0attrrcmp
mov esi,eax
dec esi
xor eax,eax
xor edx,eax
mov dl, BYTE PTR [ebx+eax]
cmp dl, BYTE PTR [ecx+eax]
jne @ret0attrrcmp
;inc eax
@@:
cmp eax,esi
je @ret1attrrcmp
inc eax
mov dl, BYTE PTR [ebx+eax]
cmp dl, BYTE PTR [ecx+eax]
je @B
jne @ret0attrrcmp
@ret1attrrcmp:
xor eax,eax
inc eax
@popptrecmp:
pop esi
pop edx
pop ebx
pop ecx
ret
@ret0attrrcmp:
xor eax,eax
pop esi
pop edx
pop ebx
pop ecx
ret
PStrCmp EndP
PStrCpy Proc uses eax ebx ecx edx ,PTRDEST :DWORD ,PTRSRC :DWORD,DIFputnull :DWORD;
mov ecx,PTRDEST
mov ebx,PTRSRC
Invoke StringLen,ebx
mov edi,eax
cmp eax,0
je @PStrCpyEND
dec edi
xor eax,eax
xor edx,edx
mov dl, BYTE PTR [ebx+eax]
mov BYTE PTR [ecx+eax],dl
;inc eax
cmp eax,edi
je @PStrCpy@PUtnull
@@:
inc eax
mov dl, BYTE PTR [ebx+eax]
mov BYTE PTR [ecx+eax],dl
cmp eax,edi
jne @B
@PStrCpy@PUtnull:
cmp DIFputnull,1
je @PStrCpyEND
inc eax
mov BYTE PTR [ecx+eax],0
@PStrCpyEND:
ret
PStrCpy EndP
StringLen Proc stdcall public,ptrSTRING1:DWORD;duh
push esi
push ebx
mov esi,ptrSTRING1
xor ebx,ebx
xor eax,eax
cmp BYTE PTR [esi+eax],0
jne @F
ret
@@:
;mov dl,BYTE PTR [esi+eax]
inc eax
;cmp dl,0
cmp BYTE PTR [esi+eax],0
jne @B
;dec eax
pop ebx
pop esi
ret
StringLen EndP
Posted on 2002-08-11 22:39:42 by Qages
Nice code.

5 minutes fast little optimizations
(maybe useful, maybe useless)

cmp eax,0 -> test eax,eax
je label jz label

mov esi,-1 -> or esi,-1

xor eax,eax
cmp BYTE PTR ,0 -> cmp BYTE PTR ,0

inc eax -> inc esi
;cmp dl,0
cmp BYTE PTR ,0 -> cmp BYTE PTR ,0
jne @B

pushad/popad are not the faster ones around
are are certaintly useless where they are placed

also removing proc/endp might avoid prologues/epilogues generations
(in StringLen for example).

I don't think the pushs/pops are necessary here
since edx/eax are not really used:
push eax
push edx
Invoke StringLen,PTRbuffer
mov PTRstringrepLEN,eax
mov edx,PTRstringrepLEN
sub edx,PTRstrrepwLEN
mov edi,edx
pop edx
pop eax


h.
Posted on 2002-09-27 07:10:55 by hitchhikr
ooo this is a bit old, ive made quite number of optimizations to this after this post, its quite fast now, and verybig, i removed calls etc and is now way fast, but not the fastest. the vb string replace is the fastsst, but my only speed bump now is the allocate memory, i could just allocate one big block the max size needed and would be super fast.
Posted on 2002-09-27 13:47:17 by Qages